public void AddConnection(NetConnection connection) { var lobbyPlayer = new LobbyPlayer(); lobbyPlayer.Id = idToGive; connection.Tag = lobbyPlayer; clients.Add(connection); idToGive++; var memory = new MemoryStream(); var writer = new BinaryWriter(memory); writer.Write((byte) LobbyProtocol.SetID); writer.Write(lobbyPlayer.Id); myServer.SendLobbyData(memory.ToArray(), connection); memory = new MemoryStream(); writer = new BinaryWriter(memory); writer.Write((byte)LobbyProtocol.SendAllPlayers); writer.Write(AllPlayersData()); myServer.SendLobbyData(memory.ToArray()); SendData(new byte[1]{MaxSlots}, LobbyProtocol.SendMaxSlots ); memory = new MemoryStream(); writer = new BinaryWriter(memory); writer.Write(Name); writer.Write(Description); SendData(memory.ToArray(), LobbyProtocol.SendLobbyName); }
public void UpdatePlayer(LobbyPlayer player) { var memory = new MemoryStream(); var writer = new BinaryWriter(memory); writer.Write(player.Id); writer.Write(player.ToBytes()); SendData(memory.ToArray(), LobbyProtocol.UpdatePlayer); }
public void ParseData(MemoryStream memory) { var reader = new BinaryReader(memory); var lobbyProtocol = (LobbyProtocol) reader.ReadByte(); Console.WriteLine(lobbyProtocol); switch (lobbyProtocol) { case LobbyProtocol.UpdatePlayer: { var id = reader.ReadByte(); if (players.ContainsKey(id)) { players[id].Load(memory); } } break; case LobbyProtocol.SetTeam: break; case LobbyProtocol.IsReady: break; case LobbyProtocol.StartGame: break; case LobbyProtocol.SetHost: break; case LobbyProtocol.SetID: { myId = reader.ReadByte(); idSet = true; } break; case LobbyProtocol.SendAllPlayers: { var playerCount = reader.ReadByte(); for (var i = 0; i < playerCount; i++) { var playerAdd = new LobbyPlayer(); playerAdd.Load(memory); if (players.ContainsKey(playerAdd.Id) == false) players.Add(playerAdd.Id, playerAdd); } } break; case LobbyProtocol.ChangeName: break; case LobbyProtocol.SendMaxSlots: { MaxSlots = reader.ReadByte(); } break; case LobbyProtocol.SendLobbyName: { Name = reader.ReadString(); Description = reader.ReadString(); } break; case LobbyProtocol.SwitchingToGame: { FLAG_IsSwitchedToGame = true; } break; case LobbyProtocol.StartGameState: { FLAG_StartGameState = true; } break; default: break; } }