Example #1
0
        // Return if one of the players or none won the game
        private static async Task <ESidesInGame> WinningInGame(SingleMatch gameObject)
        {
            ESidesInGame returnResult = ESidesInGame.None;

            await Task.Run(() =>
            {
                try
                {
                    // If Player A reached to 40 points
                    if (gameObject.ScorePlayerA == 4)
                    {
                        returnResult = ESidesInGame.PlayerA;
                    }
                    // If Player B reached to 40 points
                    else if (gameObject.ScorePlayerB == 4)
                    {
                        returnResult = ESidesInGame.PlayerB;
                    }
                    // None of the players won the game
                    else
                    {
                        returnResult = ESidesInGame.None;
                    }
                }
                catch
                {
                    returnResult = ESidesInGame.None;
                }
            });

            return(returnResult);
        }
Example #2
0
        // Increase 1 point to the player sent, calculate the match new state, save and return it
        public static async Task <string> PlayerWinningPoint(int gameId, ESidesInGame playerWinningPoint)
        {
            string gameData = string.Empty;

            try
            {
                gameData = await GetGameByID(gameId);

                var gameObject = Newtonsoft.Json.JsonConvert.DeserializeObject <SingleMatch>(gameData);
                switch (playerWinningPoint)
                {
                case ESidesInGame.PlayerA:
                    // Didn't reach to 40 points yet
                    if (gameObject.ScorePlayerA < 3)
                    {
                        gameObject.ScorePlayerA++;
                        break;
                    }
                    // Has 40 points with advantage - increase a point (to win)
                    if (gameObject.AdvantageToPlayer == ESidesInGame.PlayerA)
                    {
                        gameObject.ScorePlayerA++;
                        break;
                    }
                    // Has 40 points with advantage to opponent - cancel advantage and back to 40:40
                    if (gameObject.AdvantageToPlayer == ESidesInGame.PlayerB)
                    {
                        gameObject.AdvantageToPlayer = ESidesInGame.None;
                        break;
                    }
                    // Has 40 points without advantage - gain advantage
                    if (gameObject.ScorePlayerB == 3)
                    {
                        gameObject.AdvantageToPlayer = ESidesInGame.PlayerA;
                        break;
                    }
                    // If none of the above it true - gain a point
                    gameObject.ScorePlayerA++;
                    break;

                case ESidesInGame.PlayerB:
                    // Didn't reach to 40 points yet
                    if (gameObject.ScorePlayerB < 3)
                    {
                        gameObject.ScorePlayerB++;
                        break;
                    }
                    // Has 40 points with advantage - increase a point (to win)
                    if (gameObject.AdvantageToPlayer == ESidesInGame.PlayerB)
                    {
                        gameObject.ScorePlayerB++;
                        break;
                    }
                    // Has 40 points with advantage to opponent - cancel advantage and back to 40:40
                    if (gameObject.AdvantageToPlayer == ESidesInGame.PlayerA)
                    {
                        gameObject.AdvantageToPlayer = ESidesInGame.None;
                        break;
                    }
                    // Has 40 points without advantage - gain advantage
                    if (gameObject.ScorePlayerA == 3)
                    {
                        gameObject.AdvantageToPlayer = ESidesInGame.PlayerB;
                        break;
                    }
                    // If none of the above it true - gain a point
                    gameObject.ScorePlayerB++;
                    break;
                }
                // Check if the current game was won by one of the players or none
                var playerWins = await WinningInGame(gameObject);

                if (playerWins != ESidesInGame.None) // The game was won
                {
                    switch (playerWins)
                    {
                    // Player A won
                    case ESidesInGame.PlayerA:
                        // If Set 1 is not over yet
                        if (gameObject.Set1.WinnerInSet == ESidesInGame.None)
                        {
                            gameObject.Set1.ScorePlayerA++;      // Give a win to Player A
                            await WinningInSet(gameObject.Set1); // Check if Set 1 is over and update

                            break;
                        }
                        // If Set 2 is not over yet
                        if (gameObject.Set2.WinnerInSet == ESidesInGame.None)
                        {
                            gameObject.Set2.ScorePlayerA++;      // Give a win to Player A
                            await WinningInSet(gameObject.Set2); // Check if Set 2 is over and update
                            await WinningInMatch(gameObject);    // Check if Match is over and update

                            break;
                        }
                        // If Set 3 is not over yet
                        if (gameObject.Set3.WinnerInSet == ESidesInGame.None)
                        {
                            gameObject.Set3.ScorePlayerA++;      // Give a win to Player A
                            await WinningInSet(gameObject.Set3); // Check if Set 3 is over and update
                            await WinningInMatch(gameObject);    // Check if Match is over and update

                            break;
                        }
                        break;

                    // Player B won
                    case ESidesInGame.PlayerB:
                        // If Set 1 is not over yet
                        if (gameObject.Set1.WinnerInSet == ESidesInGame.None)
                        {
                            gameObject.Set1.ScorePlayerB++;      // Give a win to Player B
                            await WinningInSet(gameObject.Set1); // Check if Set 1 is over and update

                            break;
                        }
                        // If Set 2 is not over yet
                        if (gameObject.Set2.WinnerInSet == ESidesInGame.None)
                        {
                            gameObject.Set2.ScorePlayerB++;      // Give a win to Player B
                            await WinningInSet(gameObject.Set2); // Check if Set 2 is over and update
                            await WinningInMatch(gameObject);    // Check if Match is over and update

                            break;
                        }
                        // If Set 3 is not over yet
                        if (gameObject.Set3.WinnerInSet == ESidesInGame.None)
                        {
                            gameObject.Set3.ScorePlayerB++;      // Give a win to Player B
                            await WinningInSet(gameObject.Set3); // Check if Set 3 is over and update
                            await WinningInMatch(gameObject);    // Check if Match is over and update

                            break;
                        }
                        break;
                    }

                    // The Set score updated - reset points and advantage for a new game
                    gameObject.ScorePlayerA      = 0;
                    gameObject.ScorePlayerB      = 0;
                    gameObject.AdvantageToPlayer = ESidesInGame.None;
                }

                gameData = Newtonsoft.Json.JsonConvert.SerializeObject(gameObject);
                File.WriteAllText($"{DbDirectory}\\{gameObject.MatchID}.txt", gameData); // Write the updated data of the game to the game file
            }
            catch
            {
                gameData = string.Empty;
            }

            return(gameData);
        }
Example #3
0
 public async Task <string> IncreaseOnePoint(int gameId, ESidesInGame playerWins)
 {
     return(await ScoreCalculations.PlayerWinningPoint(gameId, playerWins));
 }