public void SerializeAstronaut() { var astronaut = new Astronaut { Guid = Guid.NewGuid(), Position = new Vec2(1, 2), Velocity = new Vec2(3, 4) }; // Serialize to/from JSON var json = astronaut.ToJson(); var astronaut2 = Astronaut.FromJson(json); // Assert JSON representations are equal Assert.AreEqual(json, astronaut2.ToJson()); }
private void OnClientNotification(object sender, NetComms.NotificationEventArgs e) { Logger.Log("NetServerWorld.OnClientNotification"); 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.PlayerPacket: { var player = Player.FromJson(payloadJson); Logger.Log($"NetServerWorld.OnClientNotification - got player {player.Guid}"); // While not playing we only accept None or Observer players, everyone else is bounced back to None if (State.Mode == GameMode.Playing && player.Type != PlayerType.None && player.Type != PlayerType.Observer) { player.Type = PlayerType.None; } Players.Players.RemoveAll(p => p.Guid == player.Guid); Players.Players.Add(player); // Save the player guid e.Connection.AssociatedData = player.Guid; break; } case NetConstants.AstronautPacket: { var astronaut = Astronaut.FromJson(payloadJson); Logger.Log($"NetServerWorld.OnClientNotification - got astronaut update {astronaut.Guid}"); // Only accept astronauts when playing if (State.Mode == GameMode.Playing) { State.Astronauts.RemoveAll(a => a.Guid == astronaut.Guid); State.Astronauts.Add(astronaut); } break; } case NetConstants.AsteroidPacket: { // Only accept asteroids when playing if (State.Mode == GameMode.Playing) { var asteroid = Asteroid.FromJson(payloadJson); Logger.Log($"NetServerWorld.OnClientNotification - got fired asteroid {asteroid.Guid}"); State.Asteroids.RemoveAll(a => a.Guid == asteroid.Guid); State.Asteroids.Add(asteroid); } break; } } } } catch (Exception ex) { Logger.Log($"NetServerWorld.OnClientNotification - failed with {ex}", ex); } }