Пример #1
0
    public async ValueTask <IActionResult> AiMove(string id, int playerId)
    {
        if (string.IsNullOrEmpty(id))
        {
            return(BadRequest());
        }
        var previousMovesCacheKey = $"{id}-moves-{playerId}";

        var gameSessionJson = await _distributedCache.GetStringAsync(id);

        var previousMovesJson = await _distributedCache.GetStringAsync(previousMovesCacheKey);

        if (string.IsNullOrEmpty(gameSessionJson))
        {
            return(NotFound());
        }

        var gameState = JsonSerializer.Deserialize <GameState>(gameSessionJson, _jsonSerializerOptions) !;

        var(players, cells, _, gameStatus) = gameState;

        if (new[] { GameStatus.GameOver, GameStatus.AiWin, GameStatus.Player0Win, GameStatus.Player1Win, GameStatus.Draw }.Contains(
                gameStatus
                ))
        {
            return(Conflict(gameState));
        }

        var previousMoves = PreventRepeatedMoves(playerId, previousMovesJson, players);

        var game = new Domain.Hive(players.ToList(), cells.ToHashSet());

        var(status, move) = await game.AiMove(async (type, tile) => await BroadCast(id, type, tile));

        await StorePreviousMoves(previousMovesCacheKey, move, previousMoves);

        var newGameState = new GameState(game.Players, game.Cells, id, status);
        await _hubContext.Clients.Group(id).SendAsync("ReceiveGameState", newGameState);

        var gameJson = JsonSerializer.Serialize(newGameState, _jsonSerializerOptions);
        await _distributedCache.SetStringAsync(id, gameJson);

        return(Accepted($"/game/{id}", newGameState));
    }
Пример #2
0
    public async ValueTask <IActionResult> Post(string id, [FromBody] Move move)
    {
        if (string.IsNullOrEmpty(id))
        {
            return(BadRequest());
        }

        var gameSession = await _distributedCache.GetStringAsync(id);

        if (string.IsNullOrEmpty(gameSession))
        {
            return(NotFound());
        }

        var(players, cells, _, _) = JsonSerializer.Deserialize <GameState>(gameSession, _jsonSerializerOptions) !;

        var game = new Domain.Hive(players.ToList(), cells.ToHashSet());

        var(tileId, coords) = move;
        var tile = players.SelectMany(p => p.Tiles).Concat(cells.SelectMany(c => c.Tiles)).FirstOrDefault(t => t.Id == tileId);

        if (tile == null)
        {
            return(Forbid());
        }

        var gameStatus = game.Move(new Domain.Entities.Move(tile, coords));

        if (gameStatus == GameStatus.MoveInvalid)
        {
            return(Forbid());
        }

        var newGameState = new GameState(game.Players, game.Cells, id, gameStatus);
        await _hubContext.Clients.Group(id).SendAsync("ReceiveGameState", newGameState);

        var json = JsonSerializer.Serialize(newGameState, _jsonSerializerOptions);
        await _distributedCache.SetStringAsync(id, json);

        return(Accepted($"/game/{id}", newGameState));
    }