Example #1
0
        private async Task <ActionResult> UpdateUnitStatistics()
        {
            if (!await CheckIp())
            {
                return(Json(new NoPermissionFailure()));
            }

            try
            {
                var strategy = _db.Database.CreateExecutionStrategy();
                await strategy.ExecuteAsync(async() =>
                {
                    using var transaction = await _db.Database.BeginTransactionAsync();
                    await UpdateUnitStatistics_Internal();
                    await _db.SaveChangesAsync();
                    lock (_dbLock)
                    {
                        transaction.Commit();
                    }
                });

                LoggingUtil.Log("Unit statistics have been updated.");
                return(Json(new { success = true }));
            }
            catch (Exception e)
            {
                LoggingUtil.Error("Failed to compute unit statistics");
                LoggingUtil.Error(e.StackTrace);
                return(Json(new { success = false }));
            }
        }
Example #2
0
        public async Task <ActionResult> OnPostFixAsync(int id)
        {
            BugReport bug = await _db.BugReports.FindAsync(id);

            if (bug != null)
            {
                bug.Done = true;
                _db.BugReports.Update(bug);
                await _db.SaveChangesAsync();
            }
            return(RedirectToPage());
        }
        public async Task <ActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Bug.CreationDate = DateTimeOffset.UtcNow;
            _db.BugReports.Add(Bug);
            await _db.SaveChangesAsync();

            return(RedirectToPage(new { success = true }));
        }
Example #4
0
        private async Task <Dictionary <long, Player> > UpdatePlayerInformation_Internal(IEnumerable <long> playerIds, Func <IQueryable <Player>, IQueryable <Player> > query = null)
        {
            if (query == null)
            {
                query = q => q;
            }
            var data = await RequestPlayerInformation(playerIds.ToArray());

            var players = await _db.GetOrCreateAsync(playerIds, p => p.SteamId, id => new Player {
                SteamId = id
            }, query : query);

            players.ForEach(p => p.Update(data[p.SteamId]));
            await _db.SaveChangesAsync();

            return(players.ToDictionary(p => p.SteamId));
        }
Example #5
0
        private async Task <Dictionary <long, Player> > UpdatePlayerInformation_Internal(IEnumerable <long> playerIds, Func <IQueryable <Player>, IQueryable <Player> > query = null)
        {
            if (query == null)
            {
                query = q => q;
            }
            var ids  = playerIds.ToArray();
            var data = await RequestPlayerInformation(ids);

            var players = await query(_db.Players).Where(p => ids.Contains(p.SteamId)).ToListAsync();

            var result = new Dictionary <long, Player>(data.Count);

            foreach (var entry in data)
            {
                var player = players.FirstOrDefault(p => p.SteamId == entry.Key);
                if (player == null)
                {
                    _db.Players.Add(player = new Player {
                        SteamId = entry.Key, Matches = new List <PlayerMatchData>()
                    });
                }
                else
                {
                    _db.Entry(player).State = EntityState.Modified;
                }
                player.Avatar      = entry.Value.Avatar;
                player.PersonaName = entry.Value.PersonaName;
                player.RealName    = entry.Value.RealName;
                player.ProfileUrl  = entry.Value.ProfileUrl;
                result[entry.Key]  = player;
            }
            await _db.SaveChangesAsync();

            return(result);
        }
        private async Task UpdateUnitStatistic(string unitName)
        {
            var unit = await GetOrCreateUnit(unitName);

            var timeStamp = DateTimeOffset.UtcNow;
            var now       = DateTime.Now;
            var yesterday = now.AddDays(-1);
            var unitData  = _db.PlayerUnitRelations
                            .Include(r => r.PlayerMatch)
                            .ThenInclude(p => p.Match)
                            .Where(r => r.PlayerMatch.Match.Date >= yesterday && r.PlayerMatch.Match.Date <= now && r.UnitName == unitName && r.PlayerMatch.FractionName == unit.FractionName);
            var matchData = _db.PlayerMatchData
                            .Include(p => p.Match)
                            .Include(p => p.UnitDatas)
                            .Where(r => r.Match.Date >= yesterday && r.Match.Date <= now && r.FractionName == unit.FractionName);
            int killed = await unitData.SumAsync(d => d.Killed);

            int leaked = await unitData.SumAsync(d => d.Leaked);

            int send = await unitData.SumAsync(d => d.Send);

            int build = await unitData.SumAsync(d => d.Build);

            var gameCount = await matchData.CountAsync();

            var gamesBuild = await matchData.CountAsync(m => m.UnitDatas.Any(u => u.UnitName == unitName && u.Build > 0));

            var gamesWon = await matchData.CountAsync(m => m.Match.Winner == m.Team && m.UnitDatas.Any(u => u.UnitName == unitName && u.Build > 0));

            _db.UnitStatistics.Add(new UnitStatistic
            {
                TimeStamp      = timeStamp,
                UnitName       = unitName,
                Killed         = killed,
                Leaked         = leaked,
                Send           = send,
                Build          = build,
                GamesBuild     = gamesBuild,
                GamesEvaluated = gameCount,
                GamesWon       = gamesWon
            });
            _db.Entry(unit).State = EntityState.Modified;
            await _db.SaveChangesAsync();
        }