/// <summary>
        /// Keeps the game alive.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param>
        private async void KeepGameAlive(object sender, ElapsedEventArgs e)
        {
            if (!this.currentGameId.Equals(Guid.Empty))
            {
                bool isGameRunning;
                try
                {
                    isGameRunning = await this.gameConsoleServiceClient.IsGameRunningAsync(this.currentGameId);
                }
                catch (ServerUnavailableException ex)
                {
                    ApplicationHelpers.HandleServerException(ex);
                    return;
                }
                catch (Exception ex)
                {
                    this.LogError(ex.Message, ex);
                    ThreadContext.InvokeOnUiThread(() => this.RunningGameDisappeared(this, null));
                    this.keepAliveTimer.Stop();
                    return;
                }

                if (isGameRunning)
                {
                    await Tracer.Debug(string.Format("Sent keepalive for the game with the id {0}. Game is still running.", this.currentGameId));
                }
                else
                {
                    await Tracer.Debug(string.Format("Sent keepalive for the game with the id {0}. Game is no longer running.", this.currentGameId));

                    ThreadContext.InvokeOnUiThread(() => this.RunningGameDisappeared(this, null));
                    this.keepAliveTimer.Stop();
                }
            }
        }
        /// <summary>
        /// Places the ball on game field.
        /// </summary>
        /// <param name="windowId">The window identifier.</param>
        /// <param name="position">The position.</param>
        public async void PlaceBallOnGameField(long windowId, Point position)
        {
            try
            {
                GameManager.Current.LogMessage(
                    string.Format(
                        "Placed ball in window with id {0} on position {1}",
                        windowId,
                        position),
                    Tracer.Debug);

                await GameConsoleContext.Current.GameConsoleServiceClient.SetStartPointAsync(
                    GameManager.Current.CurrentGame.GameId,
                    windowId,
                    position.X,
                    position.Y);
            }
            catch (ServerUnavailableException ex)
            {
                ApplicationHelpers.HandleServerException(ex);
            }
            catch (Exception ex)
            {
                this.HandleException(ex);
            }
        }
        /// <summary>
        /// Ends the round.
        /// </summary>
        /// <param name="firstPlayer">if set to <c>true</c> the current round was played by the first player.</param>
        /// <param name="score">The score.</param>
        public async void EndRound(bool firstPlayer, int score)
        {
            this.currentRoundEnded = true;

            // We only want to send the end round command to the server if this was the current players turn
            if (GameManager.Current.CurrentGame.CurrentPlayer.IsLocalPlayer)
            {
                try
                {
                    GameManager.Current.LogMessage(
                        string.Format("Finished round with a score of {0}", score),
                        Tracer.Debug);

                    await GameConsoleContext.Current.GameConsoleServiceClient.EndRoundAsync(
                        GameManager.Current.CurrentGame.GameId,
                        firstPlayer,
                        score);
                }
                catch (ServerUnavailableException ex)
                {
                    ApplicationHelpers.HandleServerException(ex);
                }
                catch (Exception ex)
                {
                    this.LogError(ex);
                    this.ErrorOccurred(this, null);
                }
            }
            else if (this.currentRoundEndedEventRecieved && this.currentRoundEndedArguments != null)
            {
                // Fire the RoundEnded event if the server already sent the appropriate event
                this.OnRoundEnded(this.currentRoundEndedArguments);
            }
        }
        /// <summary>
        /// Cancels the game.
        /// </summary>
        public async void CancelGame()
        {
            try
            {
                GameManager.Current.LogMessage(
                    string.Format("Canceled the game"),
                    Tracer.Info);

                await GameConsoleContext.Current.GameConsoleServiceClient.CancelGameAsync(GameManager.Current.CurrentGame.GameId, GameManager.Current.CurrentGame.LocalPlayer.IsFirstPlayer, true);
            }
            catch (ServerUnavailableException ex)
            {
                ApplicationHelpers.HandleServerException(ex);
            }
            catch (Exception ex)
            {
                // We log the error and close the game, but we do not resend the CancelGame command because it failed already
                this.HandleException(ex, false);
            }
        }
        /// <summary>
        /// Starts the round.
        /// </summary>
        /// <param name="direction">The direction.</param>
        public async void StartRound(Vector direction)
        {
            try
            {
                GameManager.Current.LogMessage(
                    string.Format("Started round with ball direction {0}", direction),
                    Tracer.Debug);

                await
                GameConsoleContext.Current.GameConsoleServiceClient.StartRoundAsync(
                    GameManager.Current.CurrentGame.GameId,
                    direction.X,
                    direction.Y);
            }
            catch (ServerUnavailableException ex)
            {
                ApplicationHelpers.HandleServerException(ex);
            }
            catch (Exception ex)
            {
                this.HandleException(ex);
            }
        }
Exemple #6
0
 /// <summary>
 /// Handles server communication exceptions
 /// </summary>
 /// <param name="ex">The ex.</param>
 /// <param name="withoutShutdown">if set to <c>true</c> the application does not shut down.</param>
 protected virtual void HandleServerException(ServerUnavailableException ex, bool withoutShutdown = false)
 {
     ApplicationHelpers.HandleServerException(ex, withoutShutdown);
 }