예제 #1
0
        /// <summary>
        /// Updates the players UI when a player joins or ready status changes
        /// When all players are ready (min 2) it will start the game
        /// </summary>
        private void UpdatePlayers()
        {
            int readyPlayers = 0;

            if (players.Count == 0)
            {
                return;
            }
            // update the ui
            for (int i = 0; i <= playerLabels.Count; i++)
            {
                if (players.Count <= i)
                {
                    break;
                }
                string ready = players[i].isReady ? "(Ready)" : "(Not Ready)";
                readyPlayers        += players[i].isReady ? 1 : 0;
                playerLabels[i].Text = $@"{players[i]} {ready}";
            }
            // when ready condition is met, start the game
            if (readyPlayers >= 2 && readyPlayers == players.Count)
            {
                GameStarted = true;
                wheel.StartGame();
                Hide();
                gamePanel ??= new GamePanel(wheel, players, user);
                gamePanel.Show();
                gamePanel.FormClosed += (_, _) => wheel.LeaveGame();
                gamePanel.FormClosed += (_, _) => Close();
                gamePanel.PlayersUpdated(players.ToArray());
            }
        }
예제 #2
0
 /// <summary>
 /// Callback method that updates the players UI and declares a winner if applicable
 /// </summary>
 /// <param name="messages">Contains updated player info</param>
 public void PlayersUpdated(Player[] messages)
 {
     if (thread.Thread == System.Threading.Thread.CurrentThread)
     {
         try
         {
             // If game is over
             if (wheel.GameOver())
             {
                 try
                 {
                     if (endDialog != null)
                     {
                         return;
                     }
                     int    playerCount = wheel.GetAllPlayers().Length;
                     Player winner;
                     if (playerCount == 0)
                     {
                         return;
                     }
                     else if (playerCount == 1)
                     {
                         winner = wheel.GetAllPlayers()[0];
                     }
                     else
                     {
                         winner = wheel.GetAllPlayers().OrderByDescending(p => p.Score).FirstOrDefault();
                     }
                     winnerSound.Play();
                     endDialog = new EndGameDialog(wheel);
                     endDialog.ShowDialog(this);
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show($@"Error getting winner: {ex.Message}");
                 }
                 wheel.LeaveGame();
                 Close();
                 return;
             }
             // if the wheel is spinning do not update the current player
             // otherwise the GUI will attempt to make additional threads and display errors
             if (!isSpinning)
             {
                 GetCurrentPlayer();
             }
             players = messages.ToList();
             UpdatePlayerScores();
             lbl_PuzzleDisplay.Text = wheel.GetCurrentState();
             btn_answer.Enabled     = isUsersTurn;
             UpdateLetters();
             Refresh();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }
     else
     {
         this.BeginInvoke(new GuiUpdateDelegate(this.PlayersUpdated), new object[] { messages });
     }
 }