/// <summary> /// Ends the match by sending out a match result packet and burning all the leaves, starting a reset timer /// </summary> /// <param name="winningTeam">The team that won the match</param> private void EndMatch(Team winningTeam) { GameResultPacket donePacket = new GameResultPacket(winningTeam.name); network.SendAll(PacketUtil.Serialize(donePacket)); foreach (PlayerServer player in GameServer.instance.playerServerList) { network.SendAll(PacketUtil.Serialize(new StatResultPacket(player.playerStats, player.Id))); } game.GetLeafListAsObjects().ForEach(l => { l.Burning = true; }); match.StopMatch(); matchResetTimer.Start(); }
public void fireReceiveGameResult(GameResultPacket r) { onReceiveGameResult?.Invoke(r); }
private void ClientMethod(int index) { try { Packet receivedMessage; while ((receivedMessage = clients[index].Read()) != null) { switch (receivedMessage.packetType) { case PacketType.ChatMessage: ChatMessagePacket chatPacket = (ChatMessagePacket)receivedMessage; foreach (KeyValuePair <int, Client> cli in clients) { if (cli.Value != clients[index]) { cli.Value.Send(chatPacket); } } break; case PacketType.NewName: NewNamePacket namePacket = (NewNamePacket)receivedMessage; try { clients[index].name = namePacket.newName; } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); break; } SendClientList(); break; case PacketType.Connect: ConnectPacket connectPacket = (ConnectPacket)receivedMessage; clients[index].isGame = false; clients[index].name = connectPacket.userName; SendClientList(); break; case PacketType.Disconnect: break; case PacketType.PrivateMessage: PrivateMessagePacket msgPacket = (PrivateMessagePacket)receivedMessage; foreach (KeyValuePair <int, Client> cli in clients) { if (cli.Value.name == msgPacket.receiver) { cli.Value.Send(msgPacket); } } break; case PacketType.GameConnect: GameConnectPacket gameConnectPacket = (GameConnectPacket)receivedMessage; clients[index].isGame = true; clients[index].ID = gameConnectPacket.ID; clients[index].connectedPlayers = gameConnectPacket.connectedPlayers; if (clients[index].connectedPlayers.Count >= 2) { break; } foreach (KeyValuePair <int, Client> cli in clients) { if (cli.Value.isGame) { if (cli.Value.connectedPlayers.Count < 2) { if (cli.Value.ID != clients[index].ID) { cli.Value.connectedPlayers.Add(gameConnectPacket.ID); clients[index].connectedPlayers.Add(cli.Value.ID); cli.Value.Send(new GameConnectPacket(gameConnectPacket.ID, cli.Value.connectedPlayers, GameConnectPacket.PlayerType.Chooser)); clients[index].Send(new GameConnectPacket(gameConnectPacket.ID, clients[index].connectedPlayers, GameConnectPacket.PlayerType.Guesser)); break; //break out of foreach so only connect to one other player } } } } break; case PacketType.GameDisconnect: GameDisconnectPacket gameDisconnectPacket = (GameDisconnectPacket)receivedMessage; foreach (KeyValuePair <int, Client> cli in clients) { if (clients[index].connectedPlayers.Contains(cli.Value.ID)) { if (cli.Value.ID != clients[index].ID) { cli.Value.Send(gameDisconnectPacket); break; //send to the other player, not to the player that sent the packet } } } break; case PacketType.GameResult: GameResultPacket resultPacket = (GameResultPacket)receivedMessage; foreach (KeyValuePair <int, Client> cli in clients) { if (clients[index].connectedPlayers.Contains(cli.Value.ID)) { cli.Value.Send(resultPacket); } } break; case PacketType.GameUpdateDisplayedWord: GameUpdateWordPacket updateWordPacket = (GameUpdateWordPacket)receivedMessage; foreach (KeyValuePair <int, Client> cli in clients) { if (clients[index].connectedPlayers.Contains(cli.Value.ID)) { cli.Value.Send(updateWordPacket); } } break; case PacketType.GameUpdateHangmanState: GameUpdateHangmanPacket updateHangmanPacket = (GameUpdateHangmanPacket)receivedMessage; foreach (KeyValuePair <int, Client> cli in clients) { if (clients[index].connectedPlayers.Contains(cli.Value.ID)) { cli.Value.Send(updateHangmanPacket); } } break; case PacketType.GameSetWord: GameSetWordPacket setWordPacket = (GameSetWordPacket)receivedMessage; foreach (KeyValuePair <int, Client> cli in clients) { if (clients[index].connectedPlayers.Contains(cli.Value.ID)) { cli.Value.Send(setWordPacket); } } break; default: Console.WriteLine("Received packet of type " + receivedMessage.packetType); break; } } } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } finally { clients[index].Close(); clients.TryRemove(index, out Client c); SendClientList(); } }
public static void FReceiveGamePacket(IGamerEntity gamer, Dictionary <byte, object> packet, out SimWorld world) { foreach (byte key in packet.Keys) { if (!Enum.TryParse(key.ToString(), out EServerGameCode code)) { //LogProxy.WriteError($"Parse EServerGameCode fail, Value:{key}"); continue; } switch (code) { //遊戲正式開始 case EServerGameCode.GameStart: if (!gamer.input.IsStartGame) { gamer.input.IsStartGame = true; gamer.input.SetLevel(GamerInput.Level.Gaming); //LogProxy.WriteLine($"EServerGameCode.GameStart({gamer.account.Info.Name})"); } break; //遊戲訊息 case EServerGameCode.GamerInfo: break; //遊戲結果 case EServerGameCode.GameResult: Console.WriteLine("Receive game result."); //LogProxy.WriteLine($"EServerGameCode.GameResult({gamer.account.Info.Name})"); gamer.input.IsOverGame = true; break; } } EnvironmentPacket?PacketEnv = null; GameEvent[] PacketGameEvent = null; DropItemPacket PacketDropItem = null; int? PacketBonuspot = null; GameResultPacket PacketGameResult = null; GamersPacket PacketGamersInfo = null; object PacketBroadcast = null; string PacketPureData = null; object PacketGameStart = null; Dictionary <string, byte> PacketGamerSlots = null; byte[] PacketGMGamer = null; int? PacketCountDown = null; float? PacketGameTime = null; foreach (var key in packet.Keys) { if (!Enum.TryParse(key.ToString(), out EServerGameCode code)) { continue; } switch (code) { case EServerGameCode.Environment: PacketEnv = (EnvironmentPacket)packet[key]; break; case EServerGameCode.GameEvent: PacketGameEvent = (GameEvent[])packet[key]; break; case EServerGameCode.DropItem: PacketDropItem = (DropItemPacket)packet[key]; break; case EServerGameCode.Bonuspot: PacketBonuspot = (int)packet[key]; break; case EServerGameCode.GameResult: PacketGameResult = (GameResultPacket)packet[key]; break; case EServerGameCode.GamerInfo: PacketGamersInfo = (GamersPacket)packet[key]; break; case EServerGameCode.Broadcast: PacketBroadcast = packet[key]; break; case EServerGameCode.PureData: PacketPureData = (string)packet[key]; break; case EServerGameCode.GameStart: PacketGameStart = packet[key]; break; case EServerGameCode.GamerSlots: PacketGamerSlots = (Dictionary <string, byte>)packet[key]; break; case EServerGameCode.RMGamer: PacketGMGamer = (byte[])packet[key]; break; case EServerGameCode.CountDown: PacketCountDown = (int)packet[key]; break; case EServerGameCode.GameTime: PacketGameTime = (float)packet[key]; break; } } if (PacketEnv != null) { gamer.fireReceiver.fireReceiveEnvironment(PacketEnv.Value); } if (PacketGameEvent != null) { gamer.fireReceiver.fireReceiveGameEvent(PacketGameEvent); } if (PacketDropItem != null) { gamer.fireReceiver.fireReceiveDropItem(PacketDropItem); } if (PacketBonuspot != null) { gamer.fireReceiver.fireReceiveBonuspot(PacketBonuspot.Value); } if (PacketGameResult != null) { gamer.fireReceiver.fireReceiveGameResult(PacketGameResult); } if (PacketGamersInfo != null) { gamer.fireReceiver.fireReceiveGamerInfo(PacketGamersInfo); } if (PacketBroadcast != null) { gamer.fireReceiver.fireReceiveBroadcast(PacketBroadcast); } if (PacketPureData != null) { gamer.fireReceiver.fireReceivePureData(PacketPureData); } if (PacketGameStart != null) { gamer.fireReceiver.fireReceiveGameStart(PacketGameStart); } if (PacketGamerSlots != null) { gamer.fireReceiver.fireReceiveGamerSlots(PacketGamerSlots); } if (PacketGMGamer != null) { gamer.fireReceiver.fireReceiveGMGamer(PacketGMGamer); } if (PacketCountDown != null) { gamer.fireReceiver.fireReceiveCountDown(PacketCountDown.Value); } if (PacketGameTime != null) { gamer.fireReceiver.fireReceiveGameTime(PacketGameTime.Value); } world = new SimWorld(); if (PacketEnv != null) { world.Environment = PacketEnv.Value; } if (PacketGameEvent != null) { world.GameEvent = PacketGameEvent; } if (PacketDropItem != null) { world.DropItem = PacketDropItem; } if (PacketBonuspot != null) { world.Bonuspot = PacketBonuspot.Value; } if (PacketGameResult != null) { world.GameResult = PacketGameResult; } if (PacketGamersInfo != null) { world.GamerInfo = PacketGamersInfo; } if (PacketBroadcast != null) { world.Broadcast = PacketBroadcast; } if (PacketPureData != null) { world.PureData = PacketPureData; } if (PacketGameStart != null) { world.GameStart = PacketGameStart; } if (PacketGamerSlots != null) { world.GamerSlots = PacketGamerSlots; } if (PacketGMGamer != null) { world.RMGamer = PacketGMGamer; } if (PacketCountDown != null) { world.CountDown = PacketCountDown.Value; } if (PacketGameTime != null) { world.GameTime = PacketGameTime.Value; } }
public void ResultPacket(bool win) { GameResultPacket packet = new GameResultPacket(win); SendPacket(packet); }
void ProcessServerResponse() { int byteNum; try { while (connected) { if ((byteNum = reader.ReadInt32()) != 0) { byte[] buffer = reader.ReadBytes(byteNum); MemoryStream memstream = new MemoryStream(buffer); Packet packet = formatter.Deserialize(memstream) as Packet; switch (packet.packetType) { case PacketType.GameConnect: GameConnectPacket connectPacket = (GameConnectPacket)packet; connectedPlayers.Add(connectPacket.ID); currentPlayerType = connectPacket.playerType == GameConnectPacket.PlayerType.Chooser ? PlayerType.Chooser : PlayerType.Guesser; SwitchScreen(GameScreen.WaitForStart); break; case PacketType.GameDisconnect: stopGame = true; stopGameMessage = "The other player has disconnected"; break; case PacketType.GameSetWord: GameSetWordPacket wordPacket = (GameSetWordPacket)packet; correctWord = wordPacket.correctWord; displayedWord = ""; displayedWord += new string('_', correctWord.Length); SwitchScreen(GameScreen.Main); break; case PacketType.GameUpdateDisplayedWord: if (currentPlayerType == PlayerType.Chooser) { GameUpdateWordPacket updateWordPacket = (GameUpdateWordPacket)packet; displayedWord = updateWordPacket.displayedWord; } break; case PacketType.GameUpdateHangmanState: if (currentPlayerType == PlayerType.Chooser) { GameUpdateHangmanPacket updateHangmanPacket = (GameUpdateHangmanPacket)packet; hangmanState = updateHangmanPacket.hangmanState; } break; case PacketType.GameResult: GameResultPacket resultPacket = (GameResultPacket)packet; if (resultPacket.win) { stopGame = true; stopGameMessage = currentPlayerType == PlayerType.Guesser ? "You win!" : "The guesser won!"; } else { stopGame = true; stopGameMessage = currentPlayerType == PlayerType.Guesser ? "You lost!\nThe correct word was " + correctWord : "The guesser lost!\nThe correct word was " + correctWord; } break; } } } } catch (Exception ex) { connected = false; } }