Exemplo n.º 1
0
        private static void LoadCache(ITheGameCacheProvider cacheProvider, TheGameSettings settings, ILogger <IHost> logger)
        {
            var factory    = DbProviderFactories.GetFactory(settings.DataProviderName);
            var connection = factory.CreateConnection();

            connection.ConnectionString = settings.DbConnectionString;
            string sql;

            if (!cacheProvider.GetPlayersListAsync(CancellationToken.None).GetAwaiter().GetResult()?.Any() ?? true)
            {
                logger.LogInformation("Loading cache with player data. Please wait...");
                sql = $"SELECT {nameof(Player.Id)} FROM dbo.{nameof(Player)}s";
                try
                {
                    connection.Open();

                    var players = connection.Query <long>(sql);

                    cacheProvider.StorePlayersListAsync(players, null, CancellationToken.None).GetAwaiter().GetResult();
                }
                catch (Exception exc)
                {
                    logger?.LogError(exc, "Unable to load cache with player data!");
                }
                finally
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }

            if (!cacheProvider.GetGamesListAsync(CancellationToken.None).GetAwaiter().GetResult()?.Any() ?? true)
            {
                logger.LogInformation("Loading cache with game data. Please wait...");
                sql = $"SELECT {nameof(Game.Id)} FROM dbo.{nameof(Game)}s";
                try
                {
                    connection.Open();

                    var games = connection.Query <long>(sql);

                    cacheProvider.StoreGamesListAsync(games, null, CancellationToken.None).GetAwaiter().GetResult();
                }
                catch (Exception exc)
                {
                    logger?.LogError(exc, "Unable to load cache with game data!");
                }
                finally
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
        }
        public async Task <SaveGameMatchDataResponse> Handle(SaveGameMatchDataRequest request, CancellationToken cancellationToken)
        {
            if (cancellationToken == CancellationToken.None)
            {
                return new SaveGameMatchDataResponse {
                           Result = OperationResult.Failure(new FailureDetail("CancellationToken", "CancellationToken argument cannot be null."))
                }
            }
            ;

            var players = await _cacheProvider.GetPlayersListAsync(cancellationToken);

            if ((players?.Count() ?? 0) == 0 || !players.Any(p => p == request.PlayerId))
            {
                return new SaveGameMatchDataResponse {
                           Result = OperationResult.Failure(PlayerNotFound)
                }
            }
            ;

            var games = await _cacheProvider.GetGamesListAsync(cancellationToken);

            if ((games?.Count() ?? 0) == 0 || !games.Any(p => p == request.GameId))
            {
                return new SaveGameMatchDataResponse {
                           Result = OperationResult.Failure(GameNotFound)
                }
            }
            ;

            var cachedGameMatches = await _cacheProvider.GetGameMatchesAsync(cancellationToken);

            var gameMatches = cachedGameMatches == null ? new List <CacheItem <GameMatchDataDto> >() : cachedGameMatches.ToList();

            gameMatches.Add(CacheItem <GameMatchDataDto> .Create(MapFrom(request)));

            await _cacheProvider.StoreGameMatchesAsync(gameMatches, null, cancellationToken);

            return(new SaveGameMatchDataResponse
            {
                Result = OperationResult.Successful()
            });
        }
Exemplo n.º 3
0
 public async Task <ActionResult <OperationResult <IEnumerable <long> > > > Players(CancellationToken cancellationToken)
 {
     return(Ok(OperationResult <IEnumerable <long> > .Successful(await _cacheProvider.GetPlayersListAsync(cancellationToken))));
 }