public override void Run()
        {
            using (var db = new DataContext())
            {
                var currentPointsIngestion = db.Ingestions.OrderByDescending(x => x.Id).ToList().ElementAt(1);
                var latestCurrentlyAnalysedGameTime = db.UniqueGmMatches.Where(m => m.IngestionId == currentPointsIngestion.Id).OrderByDescending(m => m.DateTime).First().DateTime;

                var newGamesTimeSpan = DateTime.UtcNow - latestCurrentlyAnalysedGameTime;

                var currentPointsJson = db.PostProcessingOutputs.OrderByDescending(x => x.ProcessingRunId).First(x => x.PostProcessingJobType == JobType).JsonResults;
                var currentPoints = Newtonsoft.Json.JsonConvert.DeserializeObject<BalanceHistoryPoint[]>(currentPointsJson);

                var newGames = GetUniqueGamesInTimeSpan(newGamesTimeSpan);
                var newPoints = new List<BalanceHistoryPoint>();
                if (newGames.Count != 0)
                {
                    newPoints = GetBalanceHistoryPointsFromUniqueGames(newGames).ToList();
                    newPoints = newPoints.SkipWhile(x => x.DateTime <= currentPointsIngestion.Time).ToList();
                }

                var newTotalPoints = currentPoints.Concat(newPoints);

                var json = JsonConvert.SerializeObject(newTotalPoints.OrderBy(x => x.DateTime));
                Save(json);
            }
        }
        protected BalanceHistoryPoint[] GetBalanceHistoryPointsFromUniqueGames(IList<UniqueGmMatch> games, string map = null)
        {
            using (var db = new DataContext())
            {
                var ingestions = db.Ingestions.ToList();

                var points = new List<BalanceHistoryPoint>();

                foreach (var ingestion in ingestions)
                {
                    var ingestionId = ingestion.Id;
                    var ingestionGames = games.Where(g => g.IngestionId == ingestionId).ToList();

                    points.Add(new BalanceHistoryPoint
                    {
                        DateTime = ingestion.Time,
                        WinRate = new WinRate
                        {
                            TerranWinRate = GetWinRateFromUniqueGames(ingestionGames, "TERRAN", map),
                            ProtossWinRate = GetWinRateFromUniqueGames(ingestionGames, "PROTOSS", map),
                            ZergWinRate = GetWinRateFromUniqueGames(ingestionGames, "ZERG", map),
                        }
                    });
                }

                return points.ToArray();
            }
        }
        protected IList<UniqueGmMatch> GetUniqueGamesInTimeSpan(TimeSpan span)
        {
            using (var db = new DataContext())
            {
                var currentTime = DateTime.UtcNow;
                var earliestTime = (currentTime.Ticks - span.Ticks < DateTime.MinValue.Ticks) ? DateTime.MinValue : (currentTime - span);

                var ingestionIds = db.Ingestions.Where(i => i.Time > earliestTime).Select(i => i.Id);
                var gamesInTimeSpan =
                    db.UniqueGmMatches.Include(x => x.LadderMember1)
                        .Include(x => x.LadderMember2)
                        .Include(x => x.Winner)
                        .Where(m => ingestionIds.Contains(m.IngestionId))
                        .ToList();
                var uniqueGamesInTimeSpan = new List<UniqueGmMatch>();
                //Make sure that we only count matches once, no matter if they are in several ingestions
                foreach (var match in gamesInTimeSpan)
                {
                    if (uniqueGamesInTimeSpan.Any(u => DateTime.Equals(u.DateTime, match.DateTime)))
                    {
                        continue;
                    }

                    uniqueGamesInTimeSpan.Add(match);
                }

                return uniqueGamesInTimeSpan;
            }
        }
示例#4
0
 public int GetHoursSinceUpdate()
 {
     using (var db = new DataContext())
     {
         var lastUpdated = db.ProcessingRuns.OrderByDescending(r => r.Id).First().DateTime;
         return (int)TimeSpan.FromTicks(DateTime.UtcNow.Ticks - lastUpdated.Ticks).TotalHours;
     }
 }
        public void RunUniqueGamesPass(int ingestionId = 0)
        {
            using (var db = new DataContext())
            {
                var processingRun = new ProcessingRun { DateTime = DateTime.UtcNow };
                ingestionId = ingestionId == 0 ? GetLatestIngestionId(db) : ingestionId;

                var latestMatches = db.Matches.Where(x => x.Ingestion.Id == ingestionId);
                var duplicateMatches = latestMatches.Where(x => latestMatches.Any(y => y.Date == x.Date && x.Id != y.Id));

                // Avoid access to modified closure
                var matches = duplicateMatches;
                var badMatches = duplicateMatches.Where(x => matches.Where(d => d.Date == x.Date).FirstOrDefault(d => d.Decision == "WIN") == null);
                duplicateMatches = duplicateMatches.Except(badMatches);

                var populatedMatches = duplicateMatches.Include(x => x.LadderMember).Select(x => new
                    {
                        Date = x.Date,
                        LadderMember1 = x.LadderMember,
                        LadderMember2 = duplicateMatches.Where(y => x.Date == y.Date).FirstOrDefault(y => x.Id != y.Id).LadderMember,
                        Map = x.Map,
                        Speed = x.Speed,
                        Type = x.Type
                    }
                );

                var ingestion = db.Ingestions.FirstOrDefault(i => i.Id == ingestionId);
                foreach (var populatedMatch in populatedMatches)
                {
                    //Make sure that we only store matches once from this ingestion
                    var date = UnixTimeStampToDateTime(populatedMatch.Date);
                    var uniqueMatches = new List<UniqueGmMatch>();
                    if (uniqueMatches.Any(u => DateTime.Equals(date, u.DateTime)))
                    {
                        continue;
                    }

                    var hydratedMatch = new UniqueGmMatch
                    {
                        DateTime = date,
                        Ingestion = ingestion,
                        LadderMember1 = populatedMatch.LadderMember1,
                        LadderMember2 = populatedMatch.LadderMember2,
                        Map = populatedMatch.Map,
                        Speed = populatedMatch.Speed,
                        Type = populatedMatch.Type,
                        Winner = populatedMatch.LadderMember1.Matches.First(x => x.Date == populatedMatch.Date).Decision == "WIN" ? populatedMatch.LadderMember1 : populatedMatch.LadderMember2,
                        ProcessingRun = processingRun
                    };
                    uniqueMatches.Add(hydratedMatch);
                    db.UniqueGmMatches.Add(hydratedMatch);
                }

                db.SaveChanges();
            }
        }
示例#6
0
        private HttpResponseMessage GetResultsForPostProcessingJob(PostProcessingJobType postProcessingJobType)
        {
            var json = String.Empty;
            using (var db = new DataContext())
            {
                json = db.PostProcessingOutputs.OrderByDescending(x => x.Id).First(x => x.PostProcessingJobType == postProcessingJobType.ToString()).JsonResults;
            }

            return BuildResponseFromJson(json);
        }
示例#7
0
 public int GetHoursSinceUpdate()
 {
     using (var db = new DataContext())
     {
         try
         {
             var lastUpdated = db.ProcessingRuns.OrderByDescending(r => r.Id).First().DateTime;
             return (int) TimeSpan.FromTicks(DateTime.UtcNow.Ticks - lastUpdated.Ticks).TotalHours;
         }
         catch (InvalidOperationException)
         {
             throw new ApplicationException("No ProcessingRuns currently loaded. Run the IngestAndProcessRunner, then the ProcessRunner to load the database before viewing the webpage.");
         }
     }
 }
示例#8
0
        public void RunNewIngestion()
        {
            var ingestion = new Ingestion
            {
                Time = DateTime.UtcNow
            };

            var ladderMembers = GetGmPlayersWithMatchesForRegion(LadderRegion.NorthAmerica);
            ladderMembers.AddRange(GetGmPlayersWithMatchesForRegion(LadderRegion.Europe));
            ladderMembers.AddRange(GetGmPlayersWithMatchesForRegion(LadderRegion.Korea));

            ingestion.LadderMembers = ladderMembers;

            using (var db = new DataContext())
            {
                db.Ingestions.Add(ingestion);
                db.SaveChanges();
            }
        }
        public override void Run()
        {
            var uniqueGames = GetUniqueGamesInTimeSpan(TimeSpan.MaxValue);
            MapBalanceHistoryPoints[] mapBalanceHistoryPoints = null;

            using (var db = new DataContext())
            {
                var maps = db.UniqueGmMatches.Where(g => g.Type == "SOLO").Select(x => x.Map).Distinct().ToList();

                mapBalanceHistoryPoints =  maps.Select(map => new MapBalanceHistoryPoints
                {
                    Map = map,
                    BalanceHistoryPoints = GetBalanceHistoryPointsFromUniqueGames(uniqueGames, map)
                }).ToArray();
            }

            var json = JsonConvert.SerializeObject(mapBalanceHistoryPoints);
            Save(json);
        }
        public override void Run()
        {
            MapRaceWinRate[] mapRaceWinRate = null;

            using (var db = new DataContext())
            {
                var maps = db.UniqueGmMatches.Where(g => g.Type == "SOLO").Select(x => x.Map).Distinct().ToList();
                var uniqueGamesInTimeSpan = GetUniqueGamesInTimeSpan(Week);

                mapRaceWinRate = maps.Select(map => new MapRaceWinRate
                {
                    Map = map,
                    RaceWinRate = new RaceWinRate
                    {
                        TVPWinRate = GetWinRateFromUniqueGamesForRaces(uniqueGamesInTimeSpan, "TERRAN", "PROTOSS", map),
                        TVZWinRate = GetWinRateFromUniqueGamesForRaces(uniqueGamesInTimeSpan, "TERRAN", "ZERG", map),
                        ZVPWinRate = GetWinRateFromUniqueGamesForRaces(uniqueGamesInTimeSpan, "ZERG", "PROTOSS", map)
                    }
                }).Where(x => !x.RaceWinRate.IsEmpty()).ToArray();
            }

            var json = JsonConvert.SerializeObject(mapRaceWinRate);
            Save(json);
        }
 protected void Save(string jsonResults)
 {
     using (var db = new DataContext())
     {
         var latestRun = db.ProcessingRuns.OrderByDescending(x => x.Id).First();
         var output = new PostProcessingOutput
         {
             PostProcessingJobType = JobType,
             ProcessingRun = latestRun,
             ProcessingRunId = latestRun.Id,
             JsonResults = jsonResults
         };
         db.PostProcessingOutputs.Add(output);
         db.SaveChanges();
     }
 }
示例#12
0
 private int GetLatestIngestionId(DataContext db)
 {
     return db.Ingestions.OrderByDescending(x => x.Id).First().Id;
 }