Example #1
0
        public async Task <GameBoardState> ResetGameAsync(ResetGameRequest request)
        {
            await Task.Delay(500).ConfigureAwait(false);

            string    userId      = ExtractUserId();
            GameState gameState   = null;
            bool      gameExists  = serverState.Exists(request.GameId);
            bool      validUserId = false;

            if (gameExists)
            {
                gameState   = serverState.GetGameState(request.GameId);
                validUserId = gameState.Player1.Id == userId || gameState.Player2.Id == userId;
            }
            if (!gameExists || !validUserId)
            {
                throw new ArgumentException($"Game with id {request.GameId} not found");
            }

            if (!GameLogicUtils.GameCanBeReset(gameState))
            {
                throw new ArgumentException("Game cannot be reset, only games vs computer and finished games can be reset");
            }

            gameState.BoardState = GameLogicUtils.InitializeGameBoard(request.UserTurn);
            //return Task.FromResult(gameState.BoardState);
            return(gameState.BoardState);
        }
Example #2
0
        private GameState InitializeGameVsComputer(Player player, InitGameRequest request)
        {
            var gameState = new GameState
            {
                Id           = Guid.NewGuid().ToString("N"),
                Type         = GameType.VS_COMPUTER,
                StartTimeUtc = DateTime.UtcNow,
                Stage        = GameStage.PLAYING,
                Player1      = player,
                Player2      = serverState.ComputerPlayer,
                BoardState   = GameLogicUtils.InitializeGameBoard(request.UserTurn),
                Difficulty   = request.Difficulty ?? serverState.DefaultGameDifficulty
            };

            serverState.AddNewGame(gameState, player);
            return(gameState);
        }