Skip to content

Commit

Permalink
Networking: Send previous clients to new client, add player destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Aug 3, 2022
1 parent bac5ff1 commit 38cfec0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
41 changes: 33 additions & 8 deletions server/mockclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ func spawnPlayer(player *Player) {
localPlayer.player = player
}

log.Info("Spawning player %d", player.guid)
log.Info("Is local player: %t", player.guid == localPlayer.guid)
log.Info("Is master client: %t", player.guid == localPlayer.guid && localPlayer.isMasterClient)
log.Info("Model: %d", player.model)
log.Info(" Spawning player %d", player.guid)
log.Info(" Is local player: %t", player.guid == localPlayer.guid)
log.Info(" Is master client: %t", player.guid == localPlayer.guid && localPlayer.isMasterClient)
log.Info(" Model: %d", player.model)

// Doesn't actually do anything, this is just mock code.
// Implement it in the actual game.
Expand Down Expand Up @@ -325,6 +325,7 @@ func main() {

case nier.PacketTypeID_CREATE_PLAYER:
log.Info("Player creation packet received")

playerCreationPacket := nier.GetRootAsCreatePlayer(data.DataBytes(), 0)

players[playerCreationPacket.Guid()] = new(Player)
Expand All @@ -336,22 +337,44 @@ func main() {
// implement it in the actual game.
spawnPlayer(players[playerCreationPacket.Guid()])

break
case nier.PacketTypeID_DESTROY_PLAYER:
log.Info("Player destruction packet received")

playerDestructionPacket := &nier.DestroyPlayer{}
flatbuffers.GetRootAs(data.DataBytes(), 0, playerDestructionPacket)

log.Info(" Destroying player %d", playerDestructionPacket.Guid())

players[playerDestructionPacket.Guid()] = nil // ezpz

break
// Bounced packets from server
case nier.PacketTypeID_ANIMATION_START:
log.Info("Animation start received from client %d", playerPacket.Guid())

if players[playerPacket.Guid()] == nil {
log.Error(" Player %d not found", playerPacket.Guid())
continue
}

animationData := &nier.AnimationStart{}
flatbuffers.GetRootAs(playerPacket.DataBytes(), 0, animationData)

log.Info("Animation: %d", animationData.Anim())
log.Info("Variant: %d", animationData.Variant())
log.Info("a3: %d", animationData.A3())
log.Info("a4: %d", animationData.A4())
log.Info(" Animation: %d", animationData.Anim())
log.Info(" Variant: %d", animationData.Variant())
log.Info(" a3: %d", animationData.A3())
log.Info(" a4: %d", animationData.A4())

break
case nier.PacketTypeID_PLAYER_DATA:
log.Info("Player data received from client %d", playerPacket.Guid())

if players[playerPacket.Guid()] == nil {
log.Error(" Player %d not found", playerPacket.Guid())
continue
}

playerData := &nier.PlayerData{}
flatbuffers.GetRootAs(playerPacket.DataBytes(), 0, playerData)

Expand All @@ -360,6 +383,8 @@ func main() {
log.Info("Facing: %f", playerData.Facing())
pos := playerData.Position(nil)
log.Info("Position: %f, %f, %f", pos.X(), pos.Y(), pos.Z())

break
default:
log.Error("Unknown packet type: %d", data.Id())
}
Expand Down
56 changes: 45 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type Client struct {
guid uint64
model uint32
name string
isMasterClient bool
lastPlayerData *nier.PlayerData
Expand Down Expand Up @@ -229,6 +230,15 @@ func main() {
case enet.EventDisconnect: // A connected peer has disconnected
log.Info("Peer disconnected: %s", ev.GetPeer().GetAddress())
if connections[ev.GetPeer()] != nil {
// Broadcast a destroy player packet to everyone except the disconnected peer
if connections[ev.GetPeer()].client != nil {
destroyPlayerBytes := builderSurround(func(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
return nier.CreateDestroyPlayer(builder, connections[ev.GetPeer()].client.guid)
})

broadcastPacketToAllExceptSender(ev.GetPeer(), nier.PacketTypeID_DESTROY_PLAYER, destroyPlayerBytes)
}

clients[connections[ev.GetPeer()]] = nil
}

Expand Down Expand Up @@ -314,15 +324,21 @@ func main() {

// Create a new client for the peer
client := &Client{
guid: connectionCount,
name: clientName,
isMasterClient: false,
guid: connectionCount,
name: clientName,
model: uint32(nier.ModelTypeMODEL_2B), // Placeholder

// Allows the first person that connects to be the master client
// In an ideal world, the server would run all of the simulation logic
// like movement, physics, enemy AI & movement, but this would be
// a monumental task because this is a mod, not a game where we have the source code.
// So we let the master client control the simulation.
isMasterClient: len(clients) == 0,
}

client.isMasterClient = len(clients) == 0

log.Info("Client name: %s", clientName)
log.Info("Client GUID: %d", client.guid)
log.Info("Client is master client: %t", client.isMasterClient)

// Add the client to the map
connection.client = client
Expand All @@ -345,29 +361,47 @@ func main() {
nier.CreatePlayerStart(builder)
nier.CreatePlayerAddGuid(builder, client.guid)
nier.CreatePlayerAddName(builder, playerName)
nier.CreatePlayerAddModel(builder, uint32(nier.ModelTypeMODEL_2B)) // Placeholder
nier.CreatePlayerAddModel(builder, client.model)
return nier.CreatePlayerEnd(builder)
})

log.Info("Sending create player packet to everyone")
broadcastPacketToAll(nier.PacketTypeID_CREATE_PLAYER, createPlayerBytes)

// Broadcast previously connected clients to the new client
for _, prevClient := range clients {
if prevClient == nil || prevClient == client { // Skip the new client
continue
}

createPlayerBytes := builderSurround(func(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
playerName := builder.CreateString(prevClient.name)
nier.CreatePlayerStart(builder)
nier.CreatePlayerAddGuid(builder, prevClient.guid)
nier.CreatePlayerAddName(builder, playerName)
nier.CreatePlayerAddModel(builder, prevClient.model)
return nier.CreatePlayerEnd(builder)
})

log.Info("Sending create player packet for previous client %d to client %d", prevClient.guid, client.guid)
ev.GetPeer().SendBytes(makePacketBytes(nier.PacketTypeID_CREATE_PLAYER, createPlayerBytes), 0, enet.PacketFlagReliable)
}
break
case nier.PacketTypeID_PING:
log.Info("Ping received from %s", connection.client.name)

ev.GetPeer().SendBytes(makeEmptyPacketBytes(nier.PacketTypeID_PONG), 0, enet.PacketFlagReliable)
break
case nier.PacketTypeID_PLAYER_DATA:
log.Info("Player data received %d", flatbuffers.GetSizePrefix(data.DataBytes(), 0))
log.Info("Player data received")
playerData := &nier.PlayerData{}
flatbuffers.GetRootAs(data.DataBytes(), 0, playerData)

log.Info("Flashlight: %d", playerData.Flashlight())
log.Info("Speed: %f", playerData.Speed())
log.Info("Facing: %f", playerData.Facing())
log.Info(" Flashlight: %d", playerData.Flashlight())
log.Info(" Speed: %f", playerData.Speed())
log.Info(" Facing: %f", playerData.Facing())
pos := playerData.Position(nil)
log.Info("Position: %f, %f, %f", pos.X(), pos.Y(), pos.Z())
log.Info(" Position: %f, %f, %f", pos.X(), pos.Y(), pos.Z())

connection.client.lastPlayerData = playerData

Expand Down

0 comments on commit 38cfec0

Please sign in to comment.