예제 #1
0
        public async Task <ActionResult <ApiGame> > PlayerMove(string token, [FromBody] ApiMove body)
        {
            var game = await iRepository.GetGame(token);

            if (game is null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game"));
            }

            if (game.Moving != game.GetPlayerColor(body.Player))
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "It is not your move"));
            }

            if (game.Status != GameStatus.Running)
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "Game hasn't started yet"));
            }

            if (!game.Move(body.Row, body.Col))
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "Invalid move"));
            }

            await iRepository.UpdateGame(game);

            return(ApiGame.GameToApiGame(game));
        }
예제 #2
0
        public async Task <ActionResult <ApiGame> > GetGameWithToken(string token)
        {
            var game = await iRepository.GetGame(token);

            if (game is null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game"));
            }
            return(ApiGame.GameToApiGame(game));
        }
예제 #3
0
        public async Task <ApiGame> AddNewGame([FromBody] ApiGame game)
        {
            var result = await iRepository.AddGame(new Game()
            {
                Description  = game.Description,
                Player1Token = game.Player1Token,
                Status       = GameStatus.Waiting,
            });

            return(ApiGame.GameToApiGame(result));
        }
예제 #4
0
        public async Task <ActionResult <ApiGame> > JoinGame(string token, [FromBody] ApiMove body)
        {
            var game = await iRepository.GetGame(token);

            if (game is null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game"));
            }

            if (!game.Join(body.Player))
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "Not allowed to join"));
            }

            var result = await iRepository.UpdateGame(game);

            return(ApiGame.GameToApiGame(game));
        }
예제 #5
0
        public async Task <ActionResult <ApiGame> > Surrender(string token, [FromBody] ApiMove body)
        {
            var game = await iRepository.GetGame(token);

            if (game is null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game"));
            }

            if (body.Player is null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, "No player found"));
            }

            game.SurrenderGame(body.Player);
            await iRepository.UpdateGame(game);

            return(ApiGame.GameToApiGame(game));
        }
예제 #6
0
        public async Task <ActionResult <ApiGame> > StartGame(string token)
        {
            var game = await iRepository.GetGame(token);

            if (game is null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, "Could not find game"));
            }

            var startingGame = game.StartGame();

            if (startingGame == false)
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "Could not start game"));
            }
            await iRepository.UpdateGame(game);

            return(ApiGame.GameToApiGame(game));
        }