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();
        }
Exemplo n.º 2
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);
        }