/// <summary> /// Handles reduceTimeButton click event. /// </summary> /// <param name="sender">Button object</param> /// <param name="e">Button click event</param> private void ReduceTimeButton_Click(object sender, RoutedEventArgs e) { if (!didPressDostButton) { return; } player = game.Players.Find(playerInGame => playerInGame.Account.Id == Session.Account.Id); if (player == null) { return; } if (Session.Account.Coins < Session.ROUND_REDUCE_TIME_COST) { MessageBox.Show(Properties.Resources.YouDontHaveEnoughCoinsErrorText); return; } else if (timeRemaining <= Session.ROUND_REDUCE_TIME_SECONDS) { return; } EngineNetwork.DoNetworkOperation <CommunicationException>(onExecute: () => { inGameService.ReduceTime(game.ActiveGuidGame, player.ActivePlayerGuid); return(true); }, onSuccess: () => { Application.Current.Dispatcher.Invoke(delegate { reduceTimeButton.IsEnabled = false; Session.Account.Coins -= Session.ROUND_REDUCE_TIME_COST; }); }); }
/// <summary> /// Sends to server the player answers and retries operation if it fails. /// </summary> private void SendCategoryAnswers() { IsEnabled = false; DialogHost.Show(loadingStackPanel, "GameWindow_WindowDialogHost", (openSender, openEventArgs) => { List <CategoryPlayerAnswer> categoryPlayerAnswers = new List <CategoryPlayerAnswer>(); for (int index = 0; index < categoriesTextBox.Count; index++) { categoryPlayerAnswers.Add(new CategoryPlayerAnswer(0, player, game.Categories[index], categoriesTextBox[index].Text, game.Round)); } EngineNetwork.DoNetworkOperation(onExecute: () => { return(player.SendCategoryAnswers(categoryPlayerAnswers)); }, onSuccess: () => { Thread.Sleep(2500); game = Session.AllGamesAvailable.Find(thisGame => thisGame.ActiveGuidGame == game.ActiveGuidGame); DateTime waitingTimeout = DateTime.Now.AddSeconds(8); while (game.Categories[0].CategoryPlayerAnswer.Count < game.Players.Count) { if (DateTime.Now >= waitingTimeout) { break; } } Application.Current.Dispatcher.Invoke(delegate { openEventArgs.Session.Close(true); Session.GameWindow.Close(); Session.GameWindow = null; new GameWindow_EndRound(game).Show(); }); }, onFinish: null, onFail: null, true); }, null); }
/// <summary> /// Opens a new GameWindow. /// </summary> private void StartGame() { DialogHost.Show(loadingStackPanel, "GameWindow_LetterSelection_WindowDialogHost", (openSender, openEventArgs) => { EngineNetwork.DoNetworkOperation(onExecute: () => { Thread.Sleep(2000); // allows session thread to load latest game data return(true); }, onSuccess: () => { Application.Current.Dispatcher.Invoke(delegate { openEventArgs.Session.Close(true); Session.GameWindow = new GameWindow(game); Session.GameWindow.Show(); Close(); }); }, onFinish: null, null, false); }, null); }
/// <summary> /// Handles ChatMessageTextBox key enter down. Sends through network a chat message. /// </summary> /// <param name="sender">TextBox object</param> /// <param name="e">TextBox key event</param> private void ChatMessageTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { if (string.IsNullOrWhiteSpace(chatMessageTextBox.Text)) { return; } EngineNetwork.DoNetworkOperation <CommunicationException>(onExecute: () => { Application.Current.Dispatcher.Invoke(delegate { chatService.BroadcastMessage(game.ActiveGuidGame, player.Account.Username, chatMessageTextBox.Text); chatMessageTextBox.Clear(); }); return(true); }); } }
/// <summary> /// Handles ShowGameResultsButton click event. Sends data to all players to indicate that game results is about to show up. /// </summary> /// <param name="sender">Button object</param> /// <param name="e">Button click event</param> private void ShowGameResultsButton_Click(object sender, RoutedEventArgs e) { this.game = Session.AllGamesAvailable.First(gameList => gameList.ActiveGuidGame == game.ActiveGuidGame); if (game.Players.Find(playerInGame => playerInGame.IsReady == false && playerInGame.ActivePlayerGuid != player.ActivePlayerGuid) != null) { MessageBox.Show(Properties.Resources.PlayersNotReadyErrorText); return; } showGameResultsButton.IsEnabled = false; EngineNetwork.DoNetworkOperation(onExecute: () => { if (player.SetPlayerReady(true)) { inGameService.EndGame(game.ActiveGuidGame); return(true); } return(false); }, null, null, null, true); }
/// <summary> /// Handles SelectRandomLetterButton click event. Sends through network data to indicate that a random letter /// was set and game should start. /// </summary> /// <param name="sender">Button object</param> /// <param name="e">Button click event</param> private void SelectRandomLetterButton_Click(object sender, RoutedEventArgs e) { IsEnabled = false; EngineNetwork.DoNetworkOperation <CommunicationException>(onExecute: () => { if (game.SetLetter(true, Session.Account.Id)) { inGameService.StartGame(game.ActiveGuidGame); return(true); } else { Application.Current.Dispatcher.Invoke(delegate { MessageBox.Show(Properties.Resources.CouldntSelectLetterErrorText); IsEnabled = true; }); return(false); } }, null, null, null, true); }
/// <summary> /// Handles ReadyButton click event. Sends data to all players to indicate that this player is ready. /// </summary> /// <param name="sender">Button object</param> /// <param name="e">Button click event</param> private void ReadyButton_Click(object sender, RoutedEventArgs e) { if (player.IsHost) { return; } EngineNetwork.DoNetworkOperation <CommunicationException>(onExecute: () => { if (player.SetPlayerReady(true)) { inGameService.SetPlayerReady(game.ActiveGuidGame, player.ActivePlayerGuid, true); return(true); } return(false); }, onSuccess: () => { Application.Current.Dispatcher.Invoke(delegate { readyButton.IsEnabled = false; }); }, null, onFail: null, true); }
/// <summary> /// Handles LoginButton click event. Establishes a connection with account service to try login with /// entered credentials. /// </summary> /// <param name="sender">Button object</param> /// <param name="e">Button click event</param> private void LoginButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(usernameTextBox.Text) || string.IsNullOrWhiteSpace(passwordPasswordBox.Password)) { MessageBox.Show(Properties.Resources.UncompletedFieldsErrorText); return; } IsEnabled = false; Account account = new Account(usernameTextBox.Text, passwordPasswordBox.Password); DialogHost.Show(loadingStackPanel, "LoginWindow_WindowDialogHost", (openSender, openEventArgs) => { EngineNetwork.DoNetworkOperation(onExecute: () => { if (!account.Login()) { if (account.Id == 0) { MessageBox.Show(Properties.Resources.LoginErrorText); } else if (!account.IsVerified) { MessageBox.Show(Properties.Resources.AccountNotConfirmedErrorText); } return(false); } return(true); }, onSuccess: () => { Application.Current.Dispatcher.Invoke(delegate { passwordPasswordBox.Password = ""; Session.Account = account; Session.MainMenuWindow = new MainMenuWindow(); Session.LoginWindow = this; Session.MainMenuWindow.Show(); Hide(); }); }, onFinish: () => { Application.Current.Dispatcher.Invoke(delegate { openEventArgs.Session.Close(true); IsEnabled = true; }); }, null, false); }, null); }
/// <summary> /// Handles DialogHost loaded event. Tries to execute get games list method until it gets successful. /// </summary> /// <param name="sender">DialogHost object</param> /// <param name="e">DialogHost event</param> private void DialogHost_Loaded(object sender, RoutedEventArgs e) { IsEnabled = false; var dialog = DialogHost.Show(loadingStackPanel, "MainMenuWindow_WindowDialogHost", (openSender, openEventArgs) => { EngineNetwork.DoNetworkOperation(onExecute: () => { var getGamesListTask = Task.Run(() => { Session.GetGamesList(); }); Thread.Sleep(1000); if (getGamesListTask.Status != TaskStatus.Running) { return(false); } new Thread(JoinGameIfNeeded).Start(); return(true); }, onSuccess: () => { Application.Current.Dispatcher.Invoke(delegate { openEventArgs.Session.Close(true); IsEnabled = true; }); }, null, null, true); }, null); }