Beispiel #1
0
        private static void AddWeeklyRanking(RankContext context, Guild guild, DateTime?rankingDate = null)
        {
            rankingDate = GetRankingUpdatedDate(rankingDate);

            List <Rank> currentWeekRanks = new List <Rank>();

            foreach (var characterId in guild.Characters.Where(C => C.IsActivated).Select(C => C.Id))
            {
                var character = context.Characters.Include("Guild").Include("Ranks")
                                .FirstOrDefault(C => C.Id == characterId);

                var currentWeekRank = character.Ranks.FirstOrDefault(R => R.Date.GetValueOrDefault() == rankingDate);
                if (currentWeekRank != null)
                {
                    currentWeekRanks.Add(currentWeekRank);
                }
            }

            currentWeekRanks = currentWeekRanks.OrderBy(R => R.Ranking).ToList();

            for (int i = 1; i <= currentWeekRanks.Count; i++)
            {
                var rank       = currentWeekRanks[i - 1];
                var weeklyRank = new WeeklyRank {
                    CharacterId = rank.CharacterId, GuildId = guild.Id, Ranking = i, Date = rankingDate
                };
                context.WeeklyRanks.Add(weeklyRank);
                context.SaveChanges();
            }
        }
Beispiel #2
0
        private static int UpdateGuildWeeklyRanking(RankContext context, DateTime?rankingDate = null)
        {
            rankingDate = GetRankingUpdatedDate(rankingDate);

            foreach (var guild in context.Characters.Include(C => C.Guild)
                     .Include(C => C.Ranks)
                     .Where(C => C.IsActivated && C.GuildId != null)
                     .GroupBy(C => C.GuildId))
            {
                var currentWeekRanks = guild.Select(C => new { C.Id, C.Name, Rank = C.Ranks?.FirstOrDefault(R => R.Date.GetValueOrDefault() == rankingDate) })
                                       .Where(CWR => CWR.Rank != null)
                                       .OrderBy(CWR => CWR.Rank.Ranking)
                                       .ToList();

                for (int i = 1; i <= currentWeekRanks.Count; i++)
                {
                    var rank = currentWeekRanks[i - 1];
                    if (context.WeeklyRanks.Any(WR => WR.CharacterId == rank.Id && WR.Date == rankingDate))
                    {
                        continue;
                    }

                    var weeklyRank = new WeeklyRank {
                        CharacterId = rank.Id, GuildId = guild.Key, Ranking = i, Date = rankingDate
                    };
                    context.WeeklyRanks.Add(weeklyRank);
                }
            }

            return(context.SaveChanges());
        }
Beispiel #3
0
        public async Task <IActionResult> UpdateAll()
        {
            var rankingDate = GetRankingUpdatedDate();
            var characters  = _context.Characters.Include(C => C.Guild).Include(C => C.Ranks);

            foreach (var character in characters)
            {
                var rankText = await _rankingCrawler.GetCharacterRankingAsync(character.Name);

                if (String.IsNullOrWhiteSpace(rankText))
                {
                    continue;
                }

                // Update Character Ranking
                var rank = character.Ranks.FirstOrDefault(R => R.Date.Value == rankingDate);
                if (rank == null)
                {
                    rank = new Rank {
                        CharacterId = character.Id, Ranking = Int32.Parse(rankText), Date = rankingDate
                    };
                    _context.Ranks.Add(rank);
                }
                //else // Warning!!
                //{
                //    rank.Ranking = Int32.Parse(rankText);
                //}
            }

            // Update Guild Weekly Ranking
            foreach (var guild in characters.Where(C => C.IsActivated && C.GuildId != null).GroupBy(C => C.GuildId))
            {
                var currentWeekRanks = guild.Select(C => new { C.Id, C.Name, Rank = C.Ranks?.FirstOrDefault(R => R.Date.GetValueOrDefault() == rankingDate) })
                                       .Where(CWR => CWR.Rank != null)
                                       .OrderBy(CWR => CWR.Rank.Ranking)
                                       .ToList();

                for (int i = 1; i <= currentWeekRanks.Count; i++)
                {
                    var character  = currentWeekRanks[i - 1];
                    var weeklyRank = _context.WeeklyRanks.FirstOrDefault(WR => WR.CharacterId == character.Id && WR.Date == rankingDate);
                    if (weeklyRank == null)
                    {
                        weeklyRank = new WeeklyRank {
                            CharacterId = character.Id, GuildId = guild.Key, Ranking = i, Date = rankingDate
                        };
                        _context.WeeklyRanks.Add(weeklyRank);
                    }
                    else // Warning!!
                    {
                        weeklyRank.Ranking = i;
                    }
                }
            }

            await _context.SaveChangesAsync();

            // Clear cache for each Guild
            foreach (var guildName in _context.Guilds.Select(G => G.Name))
            {
                _memoryCache.Remove($"{guildName}_{rankingDate.ToString("yyyy-MM-dd")}");
            }

            return(RedirectToAction(nameof(Index)));
        }