/// <summary> /// Main loop to recieve messages from the server /// </summary> public static void RecieveLoop() { NetIncomingMessage inc; while (!_shouldStop) { while ((inc = NetClient.ReadMessage()) != null) { switch (inc.MessageType) { case NetIncomingMessageType.Error: Console.WriteLine(inc.ToString()); break; case NetIncomingMessageType.StatusChanged: Console.WriteLine("Status changed: " + inc.SenderConnection.Status); if (inc.SenderConnection.Status == NetConnectionStatus.Disconnected) { NetClient.Shutdown("Closed"); //ends the receive loop _shouldStop = true; Conn = ""; } break; case NetIncomingMessageType.Data: var type = inc.ReadByte(); if (type == (byte)PacketTypes.Start) { var end = inc.ReadInt32(); MonsterPackets = new MonsterDataPacket[end]; for (var i = 0; i < end; i++) { var json = inc.ReadString(); MonsterPackets[i] = JsonConvert.DeserializeObject <MonsterDataPacket>(json); } LobbyController.StartGame(MonsterPackets); //Makes this thread a STAThread, not sure if necessary... GameLoop = new Thread(Program.Run); GameLoop.SetApartmentState(ApartmentState.STA); GameLoop.Start(); } else if (type == (byte)PacketTypes.Spectate) //The initial message to catch the new spectator up { var end = inc.ReadInt32(); MonsterPackets = new MonsterDataPacket[end]; for (var i = 0; i < end; i++) { var json = inc.ReadString(); MonsterPackets[i] = JsonConvert.DeserializeObject <MonsterDataPacket>(json); } LobbyController.StartGame(MonsterPackets); //Makes this thread a STAThread, not sure if necessary... GameLoop = new Thread(Program.Run); GameLoop.SetApartmentState(ApartmentState.STA); GameLoop.Start(); } else if (type == (byte)PacketTypes.Update) { var end = inc.ReadInt32(); MonsterPackets = new MonsterDataPacket[end]; for (var i = 0; i < end; i++) { var json = inc.ReadString(); MonsterPackets[i] = JsonConvert.DeserializeObject <MonsterDataPacket>(json); } MonsterController.AcceptDataPackets(MonsterPackets); if (inc.ReadByte() == (byte)PacketTypes.Dice) { var diceJson = inc.ReadString(); var dice = JsonConvert.DeserializeObject <DiceDataPacket>(diceJson); DiceController.AcceptDataPacket(dice); } else { Console.Error.WriteLine("No Dice! (╯°□°)╯︵ ┻━┻"); } if (inc.ReadByte() == (byte)PacketTypes.Cards) { var cardJson = inc.ReadString(); var cardsDataPackets = JsonConvert.DeserializeObject <CardDataPacket[]>(cardJson); CardController.SetCardsForSale(cardsDataPackets.ToList() .Select(CardController.AcceptDataPacket) .ToList()); } else { Console.Error.WriteLine("No Cards! (╯°□°)╯︵ ┻━┻"); } CanContinue = true; } else if (type == (byte)PacketTypes.Closed) { NetClient.Shutdown("Closed"); isSpectator = false; Conn = ""; break; } else if (type == (byte)PacketTypes.GameOver) { Console.WriteLine("Game Over!"); MainGameScreen.gameOver = true; var winnerName = inc.ReadString(); if (winnerName == NetworkClasses.GetUserValue("_Character").ToString()) { NetworkClasses.AddWin(User.PlayerId); } MainGameScreen.EndGame(winnerName); } else if (type == (byte)PacketTypes.Message) { var message = inc.ReadString(); MessageHistory.Add(message); } else if (type == (byte)PacketTypes.Chat) { ChatHistory.Add(inc.ReadString()); } break; case NetIncomingMessageType.WarningMessage: Console.WriteLine("WARNING"); break; default: //TODO catch attempts to connect non-existing servers Console.WriteLine("Unhandled message of type: " + inc.MessageType); //throw new ArgumentOutOfRangeException(); break; } NetClient.Recycle(inc); } Thread.Sleep(50); } }