/// <summary> /// Scrapes current lobby events. /// </summary> /// <param name="args">Unused</param> public void ScrapeCurrentMatches(object args) { // Disable outside tournament to save API calls var timeSetting = WebConfigurationManager.AppSettings["TournamentStart"]; var tournamentStart = DateTime.ParseExact(timeSetting, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture); if (DateTime.Now < tournamentStart || DateTime.Now > tournamentStart + TimeSpan.FromHours(12)) // assuming tournament lasts for a maximum of 12 hours { return; } // For each match that is next up to be played (except ingame matches) var matches = Mongo.Teams.Find(Query <Models.Team> .Where(x => !x.Cancelled)) .Select(team => team.GetNextMatch()) .Where(x => x != null && x.Status != Status.InGame) .DistinctBy(x => x.Id).ToList(); foreach (var m in matches) { List <TournamentLobbyEvent> lobbyEvents; // Get lobby events try { lobbyEvents = _api.GetTournamentLobbyEvents(m.TournamentCode); if (lobbyEvents.Count == 0) { throw new Exception(); } } catch (Exception) { try { lobbyEvents = _api.GetTournamentLobbyEvents(m.TournamentCodeBlind); } catch (Exception) { continue; } } if (lobbyEvents == null || lobbyEvents.Count == 0) { m.Status = Status.Pending; continue; } var latest = lobbyEvents.OrderByDescending(x => x.Timestamp).FirstOrDefault(); if (latest == null) { m.Status = Status.Pending; continue; } switch (latest.EventType) { case TournamentEventType.PracticeGameCreatedEvent: case TournamentEventType.PlayerJoinedGameEvent: case TournamentEventType.PlayerQuitGameEvent: case TournamentEventType.PlayerSwitchedTeamEvent: case TournamentEventType.PlayerBannedFromGameEvent: case TournamentEventType.OwnerChangedEvent: m.Status = Status.Lobby; break; case TournamentEventType.ChampSelectStartedEvent: m.Status = Status.ChampionSelect; break; case TournamentEventType.GameAllocationStartedEvent: m.Status = Status.Loading; break; case TournamentEventType.GameAllocatedToLsmEvent: m.Status = Status.InGame; break; case TournamentEventType.GameDissolvedEvent: m.Status = Status.Pending; break; default: m.Status = Status.Pending; break; } Mongo.Matches.Save(m); } }