Esempio n. 1
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 });
     }
 }