示例#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> > 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));
        }
示例#3
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));
        }