示例#1
0
        public Response HandleGameMatchStatsRequest(Request request)
        {
            var endpoint  = GameMatchStatsPath.Match(request.Uri).Groups["endpoint"].ToString();
            var timestamp = GameMatchStatsPath.Match(request.Uri).Groups["timestamp"].ToString();

            if (!Cache.GameServersInformation.ContainsKey(endpoint))
            {
                return(new Response(Response.Status.BadRequest));
            }
            var matchInfo = new GameMatchResult(endpoint, Extensions.ParseTimestamp(timestamp));

            if (request.Method == HttpMethod.Put)
            {
                Console.WriteLine("PUT запрос GameMatchStats");
                var matchStats = JsonConvert.DeserializeObject <GameMatchStats>(request.Json, Serializable.Settings);
                matchInfo.Results = matchStats;
                PutGameMatchResult(matchInfo);
                return(new Response(Response.Status.OK));
            }
            if (request.Method == HttpMethod.Get)
            {
                Console.WriteLine("GET запрос GameMatchStats");
                if (!Cache.GameMatches.ContainsKey(matchInfo))
                {
                    return(new Response(Response.Status.NotFound));
                }
                var stats = database.GetGameMatchStats(Cache.GameMatches[matchInfo]);
                var json  = JsonConvert.SerializeObject(stats, Formatting.Indented, Serializable.Settings);
                return(new Response(Response.Status.OK, json));
            }
            return(new Response(Response.Status.MethodNotAllowed));
        }
示例#2
0
        private void AddOrUpdatePlayersStats(string name, GameMatchResult matchResult)
        {
            var stats = Cache.PlayersStats.ContainsKey(name)
                ? database.GetPlayerStats(Cache.PlayersStats[name])
                : new PlayerStats(name);

            if (!Cache.PlayersMatchesPerDay.ContainsKey(name))
            {
                Cache.PlayersMatchesPerDay[name] = new ConcurrentDictionary <DateTime, int>();
            }

            var date          = matchResult.Timestamp.Date;
            var matchesPerDay = Cache.PlayersMatchesPerDay[name];

            matchesPerDay[date] = matchesPerDay.ContainsKey(date) ? matchesPerDay[date] + 1 : 1;
            stats.UpdateStats(matchResult, matchesPerDay);

            if (Cache.PlayersStats.ContainsKey(name))
            {
                var id = Cache.PlayersStats[name];
                database.UpdatePlayerStats(id, stats);
            }
            else
            {
                var id = database.InsertPlayerStats(stats);
                Cache.PlayersStats[name] = id;
            }
            if (stats.TotalMatchesPlayed >= 10 && stats.TotalDeaths != 0)
            {
                Cache.Players[stats.Name] = stats.KillToDeathRatio;
            }
        }
示例#3
0
        public void UpdateStats(GameMatchResult match, ConcurrentDictionary <DateTime, int> matchesPerDay)
        {
            var scoreboardPercent = CalculateScoreboardPercent(match.Results.Scoreboard);

            if (match.Results.Scoreboard.First().Name == Name)
            {
                TotalMatchesWon++;
            }
            AverageScoreboardPercent = (AverageScoreboardPercent * TotalMatchesPlayed + scoreboardPercent) / ++TotalMatchesPlayed;
            var mode   = match.Results.GameMode;
            var server = match.Server;

            PlayedModes[mode]     = PlayedModes.ContainsKey(mode) ? PlayedModes[mode] + 1 : 1;
            PlayedServers[server] = PlayedServers.ContainsKey(server) ? PlayedServers[server] + 1 : 1;
            FavoriteServer        = CalculateFavoriteServer(PlayedServers);
            FavoriteGameMode      = CalculateFavoriteMode(PlayedModes);
            UniqueServers         = PlayedServers.Keys.Count;
            MaximumMatchesPerDay  = matchesPerDay.Values.DefaultIfEmpty().Max();
            var playerResult = match.Results.Scoreboard.First(info => info.Name == Name);

            TotalKills      += playerResult.Kills;
            TotalDeaths     += playerResult.Deaths;
            KillToDeathRatio = CalculateKillToDeathRatio(TotalKills, TotalDeaths);
            if (LastMatchPlayed < match.Timestamp)
            {
                LastMatchPlayed = match.Timestamp;
            }
        }
示例#4
0
        public int InsertGameMatchStats(GameMatchResult info)
        {
            var indeces = new int[info.Results.Scoreboard.Length];

            for (var i = 0; i < info.Results.Scoreboard.Length; i++)
            {
                AddToTableGameMatchPlayersResults(info.Results.Scoreboard[i]);
                indeces[i] = tableRowsCount[Table.GameMatchPlayersResults];
            }
            InsertInto(Table.GameMatchStats, info.Results.Map, info.Results.GameMode, info.Results.FragLimit,
                       info.Results.TimeLimit, info.Results.TimeElapsed, string.Join(", ", indeces), info.Server, info.Timestamp);
            return(tableRowsCount[Table.GameMatchStats]);
        }
示例#5
0
        private void UpdateCachePlayerInformation(Cache cache, string player, GameMatchResult match)
        {
            var date = match.Timestamp.Date;

            if (!cache.PlayersFirstMatchDate.ContainsKey(player) || cache.PlayersFirstMatchDate[player] > date)
            {
                cache.PlayersFirstMatchDate[player] = date;
            }
            if (!cache.PlayersMatchesPerDay.ContainsKey(player))
            {
                cache.PlayersMatchesPerDay[player] = new ConcurrentDictionary <DateTime, int>();
            }
            var playerMatches = cache.PlayersMatchesPerDay[player];

            playerMatches[date.Date] = playerMatches.ContainsKey(date.Date) ? playerMatches[date.Date] + 1 : 1;
        }
示例#6
0
        public void Update(GameMatchResult match, ConcurrentDictionary <DateTime, int> matchesPerDay)
        {
            TotalMatchesPlayed++;
            MaximumMatchesPerDay = matchesPerDay.Values.DefaultIfEmpty().Max();
            var population = match.Results.Scoreboard.Length;

            MaximumPopulation = MaximumPopulation < population ? population : MaximumPopulation;
            var mode = match.Results.GameMode;
            var map  = match.Results.Map;

            PlayedGameModes[mode] = PlayedGameModes.ContainsKey(mode) ? PlayedGameModes[mode] + 1 : 1;
            PlayedMaps[map]       = PlayedMaps.ContainsKey(map) ? PlayedMaps[map] + 1 : 1;
            Top5GameModes         = GetTop5(PlayedGameModes);
            Top5Maps         = GetTop5(PlayedMaps);
            TotalPopulation += population;
        }
示例#7
0
        private void UpdateGameServerAndPlayerStats(GameMatchResult matchResult)
        {
            lock (database)
            {
                foreach (var player in GetMatchPlayers(matchResult.Results))
                {
                    AddOrUpdatePlayersStats(player, matchResult);
                }
            }

            var date          = matchResult.Timestamp.Date;
            var matchesPerDay = Cache.GameServersMatchesPerDay[matchResult.Server];

            matchesPerDay[date] = matchesPerDay.ContainsKey(date) ? matchesPerDay[date] + 1 : 1;
            Cache.GameServersStats[matchResult.Server].Update(matchResult, matchesPerDay);
            Cache.GameServersStats[matchResult.Server].CalculateAverageData(Cache.GameServersFirstMatchDate[matchResult.Server], Cache.LastMatchDate);
        }
示例#8
0
        private void UpdateCacheGameServerInformation(Cache cache, GameMatchResult match)
        {
            var date = match.Timestamp.Date;

            if (cache.LastMatchDate < match.Timestamp)
            {
                cache.LastMatchDate = match.Timestamp;
            }
            if (!cache.GameServersFirstMatchDate.ContainsKey(match.Server) || cache.GameServersFirstMatchDate[match.Server] > date)
            {
                cache.GameServersFirstMatchDate[match.Server] = date;
            }
            var serverMatchesPerDay = cache.GameServersMatchesPerDay[match.Server];

            serverMatchesPerDay[date] = serverMatchesPerDay.ContainsKey(date) ? serverMatchesPerDay[date] + 1 : 1;
            cache.GameServersStats[match.Server].Update(match, serverMatchesPerDay);
        }
示例#9
0
        public void PutGameMatchResult(GameMatchResult matchInfo)
        {
            var date = matchInfo.Timestamp;

            UpdateLastDate(date);
            var id      = database.InsertGameMatchStats(matchInfo);
            var players = GetMatchPlayers(matchInfo.Results);

            AddOrUpdateFirstMatchDate(date, matchInfo.Server, players);
            lock (Cache.RecentMatches)
            {
                Cache.RecentMatches.Add(matchInfo);
            }
            Cache.UpdateRecentMatches();
            Cache.GameMatches[matchInfo] = id;

            UpdateGameServerAndPlayerStats(matchInfo);
        }