Exemple #1
0
 public ServerStats(EFServer sv, EFServerStatistics st)
 {
     PlayerStats      = new ConcurrentDictionary <int, EFClientStatistics>();
     PlayerDetections = new ConcurrentDictionary <int, Detection>();
     ServerStatistics = st;
     Server           = sv;
 }
Exemple #2
0
 public ServerStats(EFServer sv, EFServerStatistics st, Server server)
 {
     HitCache         = new List <EFClientKill>();
     ServerStatistics = st;
     Server           = sv;
     _server          = server;
     OnSaving         = new SemaphoreSlim(1, 1);
 }
Exemple #3
0
        public async Task Test_StatsQueryHelper_Get()
        {
            var queryHelper = serviceProvider.GetRequiredService <StatsResourceQueryHelper>();

            await using var context = contextFactory.CreateContext();

            var server = new EFServer()
            {
                ServerId = 1
            };
            var stats = new EFClientStatistics()
            {
                Client = ClientGenerators.CreateBasicClient(null),
                SPM    = 100,
                Server = server
            };

            var ratingHistory = new EFClientRatingHistory()
            {
                Client  = stats.Client,
                Ratings = new[]
                {
                    new EFRating()
                    {
                        Ranking = 100,
                        Server  = server,
                        Newest  = true
                    }
                }
            };

            context.Set <EFClientStatistics>().Add(stats);
            context.Set <EFClientRatingHistory>().Add(ratingHistory);
            await context.SaveChangesAsync();

            var query = new StatsInfoRequest()
            {
                ClientId = stats.Client.ClientId
            };
            var result = await queryHelper.QueryResource(query);

            Assert.IsNotEmpty(result.Results);
            Assert.AreEqual(stats.SPM, result.Results.First().ScorePerMinute);
            Assert.AreEqual(ratingHistory.Ratings.First().Ranking, result.Results.First().Ranking);

            context.Set <EFClientStatistics>().Remove(stats);
            context.Set <EFClientRatingHistory>().Remove(ratingHistory);
            context.Set <EFServer>().Remove(server);
            await context.SaveChangesAsync();
        }
Exemple #4
0
        public async Task Test_ConcurrentCallsToUpdateStatHistoryDoesNotCauseException()
        {
            var server        = serviceProvider.GetRequiredService <IW4MServer>();
            var configHandler = serviceProvider.GetRequiredService <IConfigurationHandler <StatsConfiguration> >();
            var mgr           = serviceProvider.GetRequiredService <StatManager>();
            var target        = ClientGenerators.CreateDatabaseClient();

            target.CurrentServer = server;

            A.CallTo(() => configHandler.Configuration())
            .Returns(new StatsConfiguration()
            {
                TopPlayersMinPlayTime = 0
            });

            var dbFactory = serviceProvider.GetRequiredService <IDatabaseContextFactory>();
            var db        = dbFactory.CreateContext(true);
            var efServer  = new EFServer()
            {
                EndPoint = server.EndPoint.ToString()
            };

            db.Set <EFServer>().Add(efServer);

            db.Clients.Add(target);
            db.SaveChanges();

            mgr.AddServer(server);
            await mgr.AddPlayer(target);

            var stats = target.GetAdditionalProperty <EFClientStatistics>("ClientStats");

            await mgr.UpdateStatHistory(target, stats);

            db.Clients.Remove(target);
            db.Set <EFServer>().Remove(efServer);
            await db.SaveChangesAsync();
        }
Exemple #5
0
        /// <summary>
        /// Add a server to the StatManager server pool
        /// </summary>
        /// <param name="sv"></param>
        public void AddServer(Server sv)
        {
            try
            {
                int serverId = sv.GetHashCode();
                var statsSvc = new ThreadSafeStatsService();
                ContextThreads.TryAdd(serverId, statsSvc);
                StreakMessages.TryAdd(serverId, new StreakMessage(sv));

                // get the server from the database if it exists, otherwise create and insert a new one
                var server = statsSvc.ServerSvc.Find(c => c.ServerId == serverId).FirstOrDefault();
                if (server == null)
                {
                    server = new EFServer()
                    {
                        Port     = sv.GetPort(),
                        Active   = true,
                        ServerId = serverId
                    };

                    statsSvc.ServerSvc.Insert(server);
                }

                // this doesn't need to be async as it's during initialization
                statsSvc.ServerSvc.SaveChanges();
                // check to see if the stats have ever been initialized
                InitializeServerStats(sv);
                statsSvc.ServerStatsSvc.SaveChanges();

                var serverStats = statsSvc.ServerStatsSvc.Find(c => c.ServerId == serverId).FirstOrDefault();
                Servers.TryAdd(serverId, new ServerStats(server, serverStats));
            }

            catch (Exception e)
            {
                Log.WriteWarning($"Could not add server to ServerStats - {e.Message}");
            }
        }