/// <summary> /// Called when the time for this turn elapsed. /// Tells all clients and starts the next turn. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TurnTimeElapsed(object sender, EventArgs e) { var chatMessagePacket = new ChatMessagePacket(Properties.strings.serverManagerTimeOverMessage); BroadcastPacket(chatMessagePacket); turnTimer.Close(); StartNewTurn(); }
/// <summary> /// Converts a message into a ChatMessagePacket. /// </summary> /// <param name="message"></param> /// <returns></returns> private static Packet ToChatMessagePacket(NetIncomingMessage message) { var chatMessagePacket = new ChatMessagePacket(message.ReadString()) { Sender = message.SenderEndPoint }; return(chatMessagePacket); }
/// <summary> /// Called when all rounds have been played. /// Tells all client ths the game has ended and announces the winner(s). /// </summary> private void EndGame() { BroadcastPacket(new GameEndPacket()); gameRunning = false; turnTimer.Close(); var winningPlayers = playerPoints.Where(x => x.Value == playerPoints.Values.Max()); string chatMessage = Properties.strings.serverManagerWinMessage; chatMessage = winningPlayers.Aggregate(chatMessage, (current, player) => current + (player.Key + "\n")); var chatMessagePacket = new ChatMessagePacket(chatMessage); BroadcastPacket(chatMessagePacket); }
/// <summary> /// Starts a new turn. /// </summary> private void StartNewTurn() { //Gets a new random word for the players to guess guessWord = WordList.GetRandomWord(); if (lastCorrectPlayerIndex >= 0) { BroadcastPacket(new NewTurnPacket(lastCorrectPlayerIndex, currentPlayerIndex)); //Somebody guessed the word last round } else { BroadcastPacket(new NewTurnPacket(-1, -1)); //Nobody guessed the word last round } //set current player and reset last correct player lastCorrectPlayerIndex = -1; currentPlayerIndex++; //If every player has played the next round begins if (currentPlayerIndex < 0 || currentPlayerIndex >= server.ConnectionsCount) { currentPlayerIndex = 0; roundCount++; } if (server.ConnectionsCount > 0) { currentPlayer = server.Connections[currentPlayerIndex]; } //Check if all rounds have been played if (roundCount > Variables.RoundsMax) { EndGame(); } else { //Start the countdown for this turn turnTimer = new System.Timers.Timer { Interval = Variables.RoundTime * 1000 }; turnTimer.Elapsed += TurnTimeElapsed; turnTimer.Start(); //Tell every player the next turn begins var nextPlayerMessagePacket = new ChatMessagePacket(string.Format(Properties.strings.serverManagerNextPlayerMessage, currentPlayer.RemoteEndPoint)); BroadcastPacket(nextPlayerMessagePacket); //Tell the drawing player what the searched word is var guessWordMessagePacket = new ChatMessagePacket(string.Format(Properties.strings.serverManagerGuessWordMessage, guessWord)); SendPacket(guessWordMessagePacket, currentPlayer); } }
/// <summary> /// Handles a ChatNessagePacket. /// Checks if it's the searched word. If it is the players get points and the next turn starts /// </summary> /// <param name="packet"></param> private void HandleChatMessage(ChatMessagePacket packet) { BroadcastPacket(packet); //Check if it's the searched word if (packet.ChatMessage.ToLower().Equals(guessWord.ToLower()) && !packet.Sender.Equals(currentPlayer.RemoteEndPoint) && gameRunning) { //Saves the winning player to announce in the next turn. var lastCorrectPlayer = server.Connections.SingleOrDefault(i => i.RemoteEndPoint.Equals(packet.Sender)); //finds connection by ip playerPoints[lastCorrectPlayer.RemoteEndPoint] += Variables.PointsPlayerGuess; playerPoints[currentPlayer.RemoteEndPoint] += Variables.PointsPlayerDraw; var chatMessage = new ChatMessagePacket(string.Format(Properties.strings.serverManagerPlayerGuessedWordMessage, lastCorrectPlayer.RemoteEndPoint)); lastCorrectPlayerIndex = server.Connections.IndexOf(lastCorrectPlayer); turnTimer.Close(); BroadcastPacket(chatMessage); StartNewTurn(); } }
/// <summary> /// Handles a ChatMessagePacket. /// </summary> /// <param name="packet"></param> private void HandleChatMessage(ChatMessagePacket packet) { OnNewChatMessage?.Invoke(this, packet.ChatMessage); }
/// <summary> /// Sends a ChatMessagePacket to the server. /// </summary> /// <param name="chatMessage"></param> public void SendChatMessage(string chatMessage) { var packet = new ChatMessagePacket(chatMessage); SendPacket(packet); }