Beispiel #1
0
 public static void SaveGameWeek(GameWeek GW, IMongoDatabase db)
 {
     db.GetCollection <BsonDocument>("GameWeeks").InsertOne(GW.ToBsonDocument());
 }
Beispiel #2
0
        public static void Update(IMongoDatabase db)
        {
            //            db.DropCollection("Games");
            //            db.DropCollection("W1Games");
            //            db.DropCollection("RunningGames");
            db.DropCollection("GameWeeks");
            Ladder.Clear();
            CurrentPlayers.Clear();
            OldPlayers.Clear();

// Have to get them piecemeal because we've hit the limit of how many games snellman will return
            List <Game> AllGames = GetGamesFromSnellman("FireIceLadderW0%")
                                   .Concat(GetGamesFromSnellman("FireIceLadderW1%"))
                                   .Concat(GetGamesFromSnellman("FireIceLadderW2%"))
                                   .Concat(GetGamesFromSnellman("FireIceLadderW3%"))
                                   .Concat(GetGamesFromSnellman("FireIceLadderW4%"))
                                   .Concat(GetGamesFromSnellman("FireIceLadderW5%"))
                                   .Concat(GetGamesFromSnellman("FireIceLadderW6%"))
                                   .Concat(GetGamesFromSnellman("FireIceLadderW7%"))
                                   .Concat(GetGamesFromSnellman("FireIceLadderW8%"))
                                   .Concat(GetGamesFromSnellman("FireIceLadderW9%"))
                                   .ToList();


            // First create GameWeek 1

            GameWeek GW1 = new GameWeek();

            GW1.Name            = "Week 1";
            GW1.ProcessingOrder = 1;
            GW1.Games           = new List <Game>();

            List <Game> FinishedW1Games = AllGames.Where(x => (x.WeekNumber == 1 && x.finished == 1 && x.aborted == 0))
                                          .OrderByDescending(x => x.seconds_since_update).ToList();


            foreach (Game GameData in FinishedW1Games)
            {
                ProcessGame(GameData);
                GameData.index = GW1.Games.Count();
                GW1.Games.Add(GameData);
            }

            for (int rank = 1; rank < 5; rank++)
            {
                foreach (Game GameData in FinishedW1Games)
                {
                    foreach (GamePlayer gameplayer in GameData.GamePlayers.Where(x => x.rank == rank && x.dropped != 1).OrderBy(x => x.playername))
                    {
                        Ladder.Add(new LadderPlayer(gameplayer.playername, Ladder.Count() + 1));
                    }
                }
            }


            GW1.Ladder = Ladder;
            SaveGameWeek(GW1, db);
            foreach (LadderPlayer LadderPlayer in Ladder)
            {
                LadderPlayer.OldPosition = LadderPlayer.Position;
            }


            // Now gameweeks 2 - 7

            int      i         = 2;
            GameWeek CurrentGW = new GameWeek();

            CurrentGW.ProcessingOrder = i;
            CurrentGW.Name            = "Week ~" + i.ToString();
            CurrentGW.Games           = new List <Game>();
            foreach (Game GameData in AllGames.Where(x => (x.aborted == 0 && x.WeekNumber > 1 && x.WeekNumber < 8))
                     .OrderByDescending(x => x.seconds_since_update))
            {
                ProcessGame(GameData);

                if (CurrentGW.Games.Count() > 15)
                {
                    CurrentGW.Ladder = Ladder;
                    SaveGameWeek(CurrentGW, db);
                    foreach (LadderPlayer LadderPlayer in Ladder)
                    {
                        LadderPlayer.OldPosition = LadderPlayer.Position;
                    }
                    i = Math.Min(7, i + 1);
                    CurrentGW.Name = "Week ~" + i.ToString();
                    CurrentGW.Games.Clear();
                    CurrentGW.ProcessingOrder = i;
                }
                GameData.index = CurrentGW.Games.Count();
                CurrentGW.Games.Add(GameData);


                if (GameData.finished == 1)
                {
                    AddGameToLadder(GameData);
                }
            }
            CurrentGW.Ladder = Ladder;
            SaveGameWeek(CurrentGW, db);
            foreach (LadderPlayer LadderPlayer in Ladder)
            {
                LadderPlayer.OldPosition = LadderPlayer.Position;
            }


            // Now gameweeks 8+
            int CurrentWeekNumber = 8;

            CurrentPlayers            = AllGames.Where(x => (x.aborted == 0 && x.WeekNumber == CurrentWeekNumber)).SelectMany(x => x.usernames).ToList();
            CurrentGW.ProcessingOrder = CurrentWeekNumber;
            CurrentGW.Name            = "Week " + CurrentWeekNumber.ToString();
            CurrentGW.Games.Clear();
            foreach (Game GameData in AllGames.Where(x => (x.aborted == 0 && x.WeekNumber >= 8 && x.WeekNumber < 38))
                     .OrderBy(x => x.WeekNumber).ThenByDescending(x => x.GameNumber))
            {
                ProcessGame(GameData);

                if (GameData.WeekNumber != CurrentWeekNumber)
                {
                    CurrentGW.Ladder = PurgeNonPlayers(Ladder, AllGames, CurrentWeekNumber);
                    SaveGameWeek(CurrentGW, db);
                    foreach (LadderPlayer LadderPlayer in Ladder)
                    {
                        LadderPlayer.OldPosition = LadderPlayer.Position;
                    }
                    CurrentWeekNumber = GameData.WeekNumber;
                    CurrentPlayers    = AllGames.Where(x => (x.aborted == 0 && x.WeekNumber == CurrentWeekNumber)).SelectMany(x => x.usernames).ToList();
                    CurrentGW.Name    = "Week " + CurrentWeekNumber.ToString();
                    CurrentGW.Games.Clear();
                    CurrentGW.ProcessingOrder = CurrentWeekNumber;
                }

                GameData.index = CurrentGW.Games.Count();
                CurrentGW.Games.Add(GameData);


                if (GameData.finished == 1)
                {
                    AddGameToLadder(GameData);
                }
            }

            CurrentGW.Ladder = PurgeNonPlayers(Ladder, AllGames, CurrentWeekNumber);
            SaveGameWeek(CurrentGW, db);
            foreach (LadderPlayer LadderPlayer in Ladder)
            {
                LadderPlayer.OldPosition = LadderPlayer.Position;
            }


            // Now gameweeks 38 onwards
            // Process all games in a gameweek at once and then order everyone properly at the end
            // No longer purge or drop down non players - just hide them somehow (probably in the front end)
            while (true)
            {
                CurrentWeekNumber += 1;
                CurrentGW.Games    = AllGames.Where(x => (x.aborted == 0 && x.WeekNumber == CurrentWeekNumber)).OrderBy(x => x.GameNumber).ToList();
                if (CurrentGW.Games.Count == 0)
                {
                    break;
                }
                CurrentGW.Name            = "Week " + CurrentWeekNumber.ToString();
                CurrentGW.ProcessingOrder = CurrentWeekNumber;
                int InitialLadderSize = Ladder.Max(x => x.Playing ? x.Position : 0);
                if (InitialLadderSize == 0)
                {
                    InitialLadderSize = Ladder.Max(x => x.Position);
                }
                foreach (LadderPlayer LP in Ladder)
                {
                    LP.Playing     = false;
                    LP.OldPosition = LP.Position;
                    // see note below
                    if (CurrentWeekNumber == 38)
                    {
                        LP.TemporaryPositionDouble = LP.Position;
                    }
                }

                foreach (Game GameData in CurrentGW.Games.OrderByDescending(x => x.GameNumber))
                {
                    ProcessGame(GameData);
                    GameData.index = 0 - GameData.GameNumber;
                    UpdateLadderBasedOnGame(GameData, InitialLadderSize + 1);
                }

                // then put everyone in the right place on the ladder
                // NOTE that for GW 38 we have to account for the fact everyone's moving a long way up the ladder. This means moving non-players up too, hence the "|| CurrentWeekNumber = 38". This works because we've set their temporary position above too.
                int newpos = 1;
                foreach (LadderPlayer LadderPlayer in Ladder.Where(x => x.Playing || CurrentWeekNumber == 38).OrderBy(x => x.TemporaryPositionDouble).ThenBy(x => x.OldPosition).ThenBy(x => x.Games.Last().Value.GameNumber).ThenBy(x => x.Games.Count()))
                {
                    LadderPlayer.Position = newpos;
                    if (LadderPlayer.Playing)
                    {
                        newpos++;
                    }
                }

                CurrentGW.Ladder = Ladder;
                SaveGameWeek(CurrentGW, db);
            }
        }