private void OnServerNotification(object sender, NetComms.NotificationEventArgs e) { Logger.Log("NetClientWorld.OnServerNotification"); try { // Skip if no type if (e.Notification.Length == 0) { return; } // Get the packet type var type = e.Notification[0]; // Get the payload JSON var payloadJson = Encoding.ASCII.GetString(e.Notification, 1, e.Notification.Length - 1); lock (WorldLock) { switch (type) { case NetConstants.PlayersPacket: { var newPlayers = GamePlayers.FromJson(payloadJson); Logger.Log("NetClientWorld.OnServerNotification - got players"); // If we have a local player then synchronize with new data if (LocalPlayer != null) { // Find if the server has any new information var serverPlayer = newPlayers.Players.FirstOrDefault(p => p.Guid == LocalPlayer.Guid); if (serverPlayer != null) { LocalPlayer.Type = serverPlayer.Type; } } // Write the new players UpdatePlayers(newPlayers); break; } case NetConstants.StatePacket: { var newState = GameState.FromJson(payloadJson); Logger.Log("NetClientWorld.OnServerNotification - got state"); // Write the new game state UpdateState(newState); break; } } } } catch (Exception ex) { Logger.Log($"NetClientWorld.OnServerNotification - failed with {ex}", ex); } }
public void SerializeGamePlayers() { var players = new GamePlayers { Players = new List <Player> { new Player { Guid = Guid.NewGuid(), Name = "Player 1", Type = PlayerType.Astronaut }, new Player { Guid = Guid.NewGuid(), Name = "Player 2", Type = PlayerType.Astronaut }, new Player { Guid = Guid.NewGuid(), Name = "Player 3", Type = PlayerType.Alien } } }; // Serialize to/from JSON var json = players.ToJson(); var players2 = GamePlayers.FromJson(json); // Assert JSON representations are equal Assert.AreEqual(json, players2.ToJson()); // Assert players count is equal Assert.AreEqual(players.Players.Count, players2.Players.Count); }