private void ProcessMessage(string message) { StringReader sr = new StringReader(message); var toDo = sr.ReadLine(); switch (toDo) { case "DM": DM = JsonConvert.DeserializeObject <DungeonMaster>(sr.ReadToEnd()); DM.SetupEvents(); DM.data = GameData.CreateFromJson(TileJson.text, CharacterJson.text, ActionJson.text); Debug.Log("Loaded level from server"); LoadCombatScene(); break; case "Take action": var outcome = JsonConvert.DeserializeObject <Outcome>(sr.ReadToEnd()); DM.ApplyOutcome(outcome); break; default: Debug.LogError("Unknown action: " + toDo); break; } }
public static void HostGame() { Console.WriteLine("Hosting a new game"); DM = LoadEncounter(); DM.OnTurnPassed += TurnPassed; Console.WriteLine("Loaded encounter"); SideControlledBy.Add(DM.Sides.First(s => s.Name == "Heros").ID, PlayerType.Player); SideControlledBy.Add(DM.Sides.First(s => s.Name == "The Foe").ID, PlayerType.Player); SetupAI(); var config = new NetPeerConfiguration("Shrinelands") { Port = Program.Port }; Server = new NetServer(config); Server.Start(); while (true) { NetIncomingMessage message; while ((message = Server.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: var originalMessage = message.ReadString(); StringReader data = new StringReader(originalMessage); var toDo = data.ReadLine(); if (toDo == "Send DM") { Console.WriteLine("Sending DM to client"); string json = JsonConvert.SerializeObject(DM); var response = Server.CreateMessage("DM\n" + json); Server.SendMessage(response, message.SenderConnection, NetDeliveryMethod.ReliableOrdered); } else if (toDo == "Take action") { var outcome = JsonConvert.DeserializeObject <Outcome>(data.ReadToEnd()); DM.ApplyOutcome(outcome); //TODO: make function foreach (var connection in Connections) { var response = Server.CreateMessage(originalMessage); if (connection != message.SenderConnection) { Server.SendMessage(response, connection, NetDeliveryMethod.ReliableOrdered); } } } break; case NetIncomingMessageType.StatusChanged: // handle connection status messages switch (message.SenderConnection.Status) { case NetConnectionStatus.Connected: Connections.Add(message.SenderConnection); break; } break; case NetIncomingMessageType.DebugMessage: // handle debug messages // (only received when compiled in DEBUG mode) Console.WriteLine(message.ReadString()); break; /* .. */ default: Console.WriteLine("unhandled message with type: " + message.MessageType); break; } } } }