public async Task <DateTimeOffset> AddPlayerToQueue(string playerId) { var currentEntry = await _dbContext.MatchmakingEntries.FirstOrDefaultAsync(x => x.PlayerId == playerId); if (currentEntry == null) { currentEntry = new MatchmakingEntry(playerId, DateTime.UtcNow); _dbContext.MatchmakingEntries.Add(currentEntry); _logger.LogInformation("Player joined queue, {@details}", new { PlayerId = playerId, }); } else { currentEntry.LatestJoinDate = DateTime.UtcNow; _logger.LogInformation("Player rejoined queue, {@details}", new { PlayerId = playerId, }); } await _dbContext.SaveChangesAsync(); return(currentEntry.CreatedDate); }
public async Task ResetScores(string userId) { if (userId == null) { throw new ArgumentNullException(ErrorMessagesConstants.USERID_IS_NULL); } var userScores = this.context.Scores.Where(s => s.UserId == userId); foreach (var score in userScores) { context.Scores.RemoveRange(score); } await context.SaveChangesAsync(); }
public async Task <ReasonResult <GameModel> > TakeTurnAsync(int id, int turnX, int turnY, ClaimsUser user) { var game = await _dbContext.Games .FirstOrDefaultAsync(x => x.Id == id); if (game == null) { return(ReasonResult <GameModel> .NotFound()); } BoardPlayer player; if (game.PlayerOne == user.Id) { player = BoardPlayer.PlayerOne; } else if (game.PlayerTwo == user.Id) { player = BoardPlayer.PlayerTwo; } else { return(ReasonResult <GameModel> .NotFound()); } if (game.GameState != GameState.InProgress) { _logger.LogInformation("Player tried to take turn on game with incorrect state, {@details}", new { GameId = game.Id, GameState = game.GameState, UserId = user.Id, }); return(ReasonResult <GameModel> .BadRequest(new[] { GameInWrongState })); } var oldDecompressedBoardState = new DecompressedBoardState(game.BoardHistory); var takeTurnResult = oldDecompressedBoardState.TryTakeTurn(turnX, turnY, player, out var newDecompressedBoardState); switch (takeTurnResult) { case TakeTurnResult.Success: { _logger.LogInformation("Player took turn, {@details}", new { GameId = game.Id, UserId = user.Id, GameState = game.GameState, X = turnX, Y = turnY, OldBoardHistory = oldDecompressedBoardState.BoardHistory, NewBoardHistory = newDecompressedBoardState.BoardHistory, Snapshot = newDecompressedBoardState.ToString(), }); UpdateGameUsingDecompressedBoardState(game, newDecompressedBoardState); await _dbContext.SaveChangesAsync(); return(ReasonResult <GameModel> .Success(new GameModel(game))); } case TakeTurnResult.NotInProgress: return(ReasonResult <GameModel> .BadRequest(new[] { GameInWrongState })); case TakeTurnResult.TileAlreadySet: return(ReasonResult <GameModel> .BadRequest(new[] { TileAlreadySet })); case TakeTurnResult.WrongPlayer: return(ReasonResult <GameModel> .BadRequest(new[] { ActingOnWrongTurn })); default: throw new Exception($"Unknown failure state reached during {nameof(DecompressedBoardState)}.{nameof(DecompressedBoardState.TryTakeTurn)}"); } }