Beispiel #1
0
        public void Run(HockeyDataContext dbContext)
        {
            var url     = Feeds.SeasonsFeed.GetFeedUrl();
            var rawJson = this.JsonUtility.GetRawJsonFromUrl(url);
            var feed    = Feeds.SeasonsFeed.FromJson(rawJson);

            int nhlLeagueId = dbContext.Leagues.Single(x => x.LeagueAbbr == "NHL").LeagueId;
            var seasonsDict = dbContext.Seasons.ToDictionary(x => x.NhlSeasonKey, y => y);
            var apiSeasons  = feed.Seasons.OrderBy(x => x.SeasonId).ToList();

            foreach (var apiSeason in apiSeasons)
            {
                if (!seasonsDict.ContainsKey(apiSeason.SeasonId))
                {
                    var dbSeason = new Season
                    {
                        LeagueId       = nhlLeagueId,
                        NhlSeasonKey   = apiSeason.SeasonId,
                        HasConferences = apiSeason.ConferencesInUse,
                        HasDivisions   = apiSeason.DivisionsInUse,
                        HasOlympics    = apiSeason.OlympicsParticipation,
                        HasTies        = apiSeason.TiesInUse,
                        HasWildCard    = apiSeason.WildCardInUse
                    };
                    seasonsDict.Add(dbSeason.NhlSeasonKey, dbSeason);
                    dbContext.Seasons.Add(dbSeason);
                    dbContext.SaveChanges();
                }
            }
        }
Beispiel #2
0
        public void Run(HockeyDataContext dbContext)
        {
            var url     = Feeds.TeamsFeed.GetFeedUrl(this.NhlSeasonKey);
            var rawJson = this.JsonUtility.GetRawJsonFromUrl(url);
            var feed    = Feeds.TeamsFeed.FromJson(rawJson);

            var teamsDict = dbContext.Teams.ToDictionary(x => x.NhlTeamId, y => y);
            var apiTeams  = feed.Teams.OrderBy(x => x.Name).ToList();

            foreach (var apiTeam in apiTeams)
            {
                if (!teamsDict.TryGetValue(apiTeam.Id, out Team dbTeam))
                {
                    dbTeam = new Team
                    {
                        NhlTeamId       = apiTeam.Id,
                        FirstYearOfPlay = apiTeam.FirstYearOfPlay,
                        TeamAbbr        = apiTeam.Abbreviation,
                        TeamFullName    = apiTeam.Name,
                        TeamLocation    = apiTeam.LocationName,
                        TeamName        = apiTeam.TeamName,
                        TeamShortName   = apiTeam.ShortName,
                        WebSiteUrl      = apiTeam.OfficialSiteUrl,
                        TimeZoneAbbr    = apiTeam.Venue?.TimeZone?.TimeZoneAbbr,
                        TimeZoneName    = apiTeam.Venue?.TimeZone?.TimeZoneName,
                        TimeZoneOffset  = apiTeam.Venue?.TimeZone?.Offset,
                    };
                    teamsDict.Add(apiTeam.Id, dbTeam);
                    dbContext.Teams.Add(dbTeam);
                    dbContext.SaveChanges();
                }
                else if (HasUpdates(dbTeam, apiTeam))
                {
                    dbTeam.TeamFullName  = apiTeam.Name;
                    dbTeam.TeamLocation  = apiTeam.LocationName;
                    dbTeam.TeamName      = apiTeam.TeamName;
                    dbTeam.TeamShortName = apiTeam.ShortName;
                    dbTeam.WebSiteUrl    = apiTeam.OfficialSiteUrl;
                    dbTeam.TeamAbbr      = apiTeam.Abbreviation;
                    dbContext.SaveChanges();
                }
            }
        }
Beispiel #3
0
        public override void Run()
        {
            Console.WriteLine("Hello World!");
            HockeyDataContext context = null;

            try
            {
                var config = GetConfig();

                context = new HockeyDataContext(config);

                //context.Database.EnsureDeleted();
                //context.Database.EnsureCreated();
                //context.SaveChanges();

                //context.RefGameTypes.AddRange(new List<RefGameType>
                //{
                //	new RefGameType{ GameTypeId = GameType.Unknown, NhlGameTypeKey = string.Empty, GameTypeDescription = "Unknown"},
                //	new RefGameType{ GameTypeId = GameType.RegularSeason, NhlGameTypeKey = "R", GameTypeDescription = "Regular Season"},
                //	new RefGameType{ GameTypeId = GameType.Postseason, NhlGameTypeKey = "P", GameTypeDescription = "Postseason"},
                //	new RefGameType{ GameTypeId = GameType.Preseason, NhlGameTypeKey = "PR", GameTypeDescription = "Preseason"},
                //	new RefGameType{ GameTypeId = GameType.AllStarGame, NhlGameTypeKey = "A", GameTypeDescription = "All-Star Game"},
                //	new RefGameType{ GameTypeId = GameType.AllStarGameWomen, NhlGameTypeKey = "WA", GameTypeDescription = "All-Star Women Game"},
                //	new RefGameType{ GameTypeId = GameType.Olympics, NhlGameTypeKey = "O", GameTypeDescription = "Olympics Game"},
                //	new RefGameType{ GameTypeId = GameType.Exhibition, NhlGameTypeKey = "E", GameTypeDescription = "Exhibition"},
                //	new RefGameType{ GameTypeId = GameType.WorldCupExhibition, NhlGameTypeKey = "WCOH_EXH", GameTypeDescription = "World Cup of Hockey exhibition/preseason games "},
                //	new RefGameType{ GameTypeId = GameType.WorldCupPrelim, NhlGameTypeKey = "WCOH_PRELIM", GameTypeDescription = "World Cup of Hockey preliminary games"},
                //	new RefGameType{ GameTypeId = GameType.WorldCupFinal, NhlGameTypeKey = "WCOH_FINAL", GameTypeDescription = "World Cup of Hockey semifinals and finals"},
                //});
                //context.SaveChanges();

                //context.RefGameStatuses.AddRange(new List<RefGameStatus>
                //{
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.Scheduled, GameStatusId = GameStatus.Pregame, NhlStatusCode = 01, GameStatusName = "Pregame", DetailedGameStatusName = "Scheduled"},
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.Pregame, GameStatusId = GameStatus.Pregame, NhlStatusCode = 02, GameStatusName = "Pregame", DetailedGameStatusName = "Pregame"},
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.InProgress, GameStatusId = GameStatus.Live, NhlStatusCode = 03, GameStatusName = "Live", DetailedGameStatusName = "In-Progress"},
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.InProgressCritical, GameStatusId = GameStatus.Live, NhlStatusCode = 04, GameStatusName = "Live", DetailedGameStatusName = "In-Progress - Critical"},
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.GameOver, GameStatusId = GameStatus.Final, NhlStatusCode = 05, GameStatusName = "Final", DetailedGameStatusName = "Game Over"},
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.Final, GameStatusId = GameStatus.Final, NhlStatusCode = 06, GameStatusName = "Final", DetailedGameStatusName = "Final"},
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.Final2, GameStatusId = GameStatus.Final, NhlStatusCode = 07, GameStatusName = "Final", DetailedGameStatusName = "Final"},
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.ScheduledTbd, GameStatusId = GameStatus.Pregame, NhlStatusCode = 08, GameStatusName = "Pregame", DetailedGameStatusName = "Scheduled (Time TBD)"},
                //	new RefGameStatus{ DetailedGameStatusId = DetailedGameStatus.Postponed, GameStatusId = GameStatus.Postponed, NhlStatusCode = 09, GameStatusName = "Postponed", DetailedGameStatusName = "Postponed"},
                //});
                //context.SaveChanges();

                //context.RefPlayTypes.AddRange(new List<RefPlayType>
                //{
                //	new RefPlayType{ PlayTypeId = PlayType.BlockedShot, NhlCode = "BLOCKED_SHOT", PlayTypeName = "Blocked Shot" },
                //	new RefPlayType{ PlayTypeId = PlayType.Faceoff, NhlCode = "FACEOFF", PlayTypeName = "Faceoff" },
                //	new RefPlayType{ PlayTypeId = PlayType.GameEnd, NhlCode = "GAME_END", PlayTypeName = "Game End" },
                //	new RefPlayType{ PlayTypeId = PlayType.GameScheduled, NhlCode = "GAME_SCHEDULED", PlayTypeName = "Game Scheduled" },
                //	new RefPlayType{ PlayTypeId = PlayType.Giveaway, NhlCode = "GIVEAWAY", PlayTypeName = "Giveaway" },
                //	new RefPlayType{ PlayTypeId = PlayType.Goal, NhlCode = "GOAL", PlayTypeName = "Goal" },
                //	new RefPlayType{ PlayTypeId = PlayType.Hit, NhlCode = "HIT", PlayTypeName = "Hit" },
                //	new RefPlayType{ PlayTypeId = PlayType.MissedShot, NhlCode = "MISSED_SHOT", PlayTypeName = "Missed Shot" },
                //	new RefPlayType{ PlayTypeId = PlayType.Penalty, NhlCode = "PENALTY", PlayTypeName = "Penalty" },
                //	new RefPlayType{ PlayTypeId = PlayType.PeriodEnd, NhlCode = "PERIOD_END", PlayTypeName = "Period End" },
                //	new RefPlayType{ PlayTypeId = PlayType.PeriodOfficial, NhlCode = "PERIOD_OFFICIAL", PlayTypeName = "Period Official" },
                //	new RefPlayType{ PlayTypeId = PlayType.PeriodReady, NhlCode = "PERIOD_READY", PlayTypeName = "Period Ready" },
                //	new RefPlayType{ PlayTypeId = PlayType.PeriodStart, NhlCode = "PERIOD_START", PlayTypeName = "Period Start" },
                //	new RefPlayType{ PlayTypeId = PlayType.Shot, NhlCode = "SHOT", PlayTypeName = "Shot" },
                //	new RefPlayType{ PlayTypeId = PlayType.Stoppage, NhlCode = "STOP", PlayTypeName = "Stoppage" },
                //	new RefPlayType{ PlayTypeId = PlayType.Takeaway, NhlCode = "TAKEAWAY", PlayTypeName = "Takeaway" },
                //});
                //context.SaveChanges();

                //context.RefStoppageTypes.AddRange(new List<RefStoppageType>
                //{
                //	new RefStoppageType{ StoppageTypeId = StoppageType.Unknown, NhlDescription = null , StoppageName = "Unknown"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.GoalieStopped, NhlDescription = "Goalie Stopped", StoppageName = "Goalie Stopped"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.Icing, NhlDescription = "Icing", StoppageName = "Icing"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.NetOff, NhlDescription = "Net Off", StoppageName = "Net Off"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.Offside, NhlDescription = "Offside", StoppageName = "Offside"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.PuckFrozen, NhlDescription = "Puck Frozen", StoppageName = "Puck Frozen"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.PuckInBenches, NhlDescription = "Puck In Benches", StoppageName = "Puck in Benches"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.PuckInCrowd, NhlDescription = "Puck In Crowd", StoppageName = "Puck in Crowd"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.PuckInNetting, NhlDescription = "Puck In Netting", StoppageName = "Puck in Netting"},
                //	new RefStoppageType{ StoppageTypeId = StoppageType.TVTimeout, NhlDescription = "TV Timeout", StoppageName = "TV timeout"},
                //});
                //context.SaveChanges();

                //context.Leagues.Add(new League { LeagueName = "National Hockey League", LeagueAbbr = "NHL" });
                //context.SaveChanges();

                //context.Players.Add(new Player { NhlPlayerId = 0, FullName = "Unknown" });
                //context.SaveChanges();

                var seasonsProcessor = new SeasonsProcessor();
                seasonsProcessor.Run(context);

                //var dbSeasons = context.Seasons.Where(x => x.SeasonId > 100 && x.NhlSeasonKey != "20192020").OrderBy(x => x.SeasonId).ToList();
                var dbSeasons = context.Seasons.Where(x => x.NhlSeasonKey == "20192020").OrderBy(x => x.SeasonId).ToList();
                //var dbSeasons = context.Seasons.OrderBy(x => x.SeasonId).ToList();
                for (int i = 0; i < dbSeasons.Count; i++)
                {
                    var dbSeason     = dbSeasons[i];
                    var nhlSeasonKey = dbSeason.NhlSeasonKey;
                    Console.WriteLine($"PROCESS TEAMS - {nhlSeasonKey}");
                    var teamsProcessor = new TeamsProcessor(nhlSeasonKey);
                    teamsProcessor.Run(context);

                    Console.WriteLine($"PROCESS GAMES - {nhlSeasonKey}");
                    var gamesProcessor = new GamesProcessor(nhlSeasonKey);
                    gamesProcessor.Run(context);

                    var seasonGames = context.Games.Where(x => x.SeasonId == dbSeason.SeasonId).ToList();
                    for (int j = 0; j < seasonGames.Count; j++)
                    {
                        Console.WriteLine($"PROCESS GAMES - {nhlSeasonKey} - BOXES: {j}/{seasonGames.Count}");
                        var seasonGame        = seasonGames[j];
                        var gameLiveProcessor = new GameLiveProcessor(seasonGame.NhlGameId, seasonGame.GameDateEst);
                        gameLiveProcessor.Run(context);

                        if (j % 30 == 29)
                        {
                            context.Dispose();
                            context = new HockeyDataContext(config);
                        }
                    }

                    if (i % 10 == 9)
                    {
                        context.Dispose();
                        context = new HockeyDataContext(config);
                    }
                }
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
Beispiel #4
0
        public void Run(HockeyDataContext dbContext)
        {
            var url     = Feeds.GamesFeed.GetFeedUrl(this.NhlSeasonKey);
            var rawJson = this.JsonUtility.GetRawJsonFromUrl(url);
            var feed    = Feeds.GamesFeed.FromJson(rawJson);

            int dbSeasonId;

            if (this.NhlSeasonKey == null)
            {
                dbSeasonId = dbContext.Seasons.OrderByDescending(x => x.NhlSeasonKey).First().SeasonId;
            }
            else
            {
                dbSeasonId = dbContext.Seasons.Single(x => x.NhlSeasonKey == this.NhlSeasonKey).SeasonId;
            }

            var teamsDict = dbContext.Teams.ToDictionary(x => x.NhlTeamId, y => y);
            var apiTeams  = feed.Dates.SelectMany(x => x.Games.SelectMany(y => new[] { y.Teams?.Home?.Team, y.Teams?.Away?.Team })).Distinct().ToList();

            foreach (var apiTeam in apiTeams)
            {
                if (apiTeam != null && !teamsDict.TryGetValue(apiTeam.Id.Value, out Team dbTeam))
                {
                    dbTeam = new Team
                    {
                        NhlTeamId    = apiTeam.Id.Value,
                        TeamFullName = apiTeam.Name
                    };
                    teamsDict.Add(apiTeam.Id.Value, dbTeam);
                    dbContext.Teams.Add(dbTeam);
                    dbContext.SaveChanges();
                }
            }

            teamsDict = dbContext.Teams.ToDictionary(x => x.NhlTeamId, y => y);
            var gameTypesDict            = dbContext.RefGameTypes.ToDictionary(x => x.NhlGameTypeKey, y => y.GameTypeId);
            var gameStatusesDict         = dbContext.RefGameStatuses.ToDictionary(x => x.NhlStatusCode, y => y.GameStatusId);
            var detailedGameStatusesDict = dbContext.RefGameStatuses.ToDictionary(x => x.NhlStatusCode, y => y.DetailedGameStatusId);
            var gamesDict = dbContext.Games.Where(x => NhlSeasonKey == null || x.Season.NhlSeasonKey == this.NhlSeasonKey).ToDictionary(x => x.NhlGameId, y => y);

            var apiGames = feed.Dates.SelectMany(x => x.Games).OrderBy(x => x.GameDate).ToList();

            foreach (var apiGame in apiGames)
            {
                var apiDetailedStatus  = detailedGameStatusesDict[apiGame.Status.StatusCode];
                var apiStatus          = gameStatusesDict[apiGame.Status.StatusCode];
                int?apiHomeGamesPlayed = GetGamesPlayed(apiGame.Teams?.Home?.LeagueRecord);
                int?apiAwayGamesPlayed = GetGamesPlayed(apiGame.Teams?.Away?.LeagueRecord);
                var homeScore          = apiGame.Teams?.Home?.Score;
                var awayScore          = apiGame.Teams?.Away?.Score;
                var gameTimeUtc        = apiGame.GameDate.UtcDateTime;
                var homeTeamId         = apiGame.Teams?.Home?.Team == null ? (int?)null : teamsDict[apiGame.Teams.Home.Team.Id.Value].TeamId;
                var awayTeamId         = apiGame.Teams?.Away?.Team == null ? (int?)null : teamsDict[apiGame.Teams.Away.Team.Id.Value].TeamId;

                if (!gamesDict.TryGetValue(apiGame.GamePk, out Game dbGame))
                {
                    dbGame = new Game
                    {
                        NhlGameId            = apiGame.GamePk,
                        GameTimeUtc          = gameTimeUtc,
                        GameDateEst          = gameTimeUtc.AddHours(-5).Date,
                        SeasonId             = dbSeasonId,
                        HomeTeamId           = homeTeamId,
                        AwayTeamId           = awayTeamId,
                        HomeScore            = homeScore,
                        AwayScore            = awayScore,
                        NhlVenueId           = apiGame.Venue?.Id,
                        VenueName            = apiGame.Venue?.Name,
                        DetailedGameStatusId = apiDetailedStatus,
                        GameStatusId         = apiStatus,
                        GameTypeId           = gameTypesDict[apiGame.GameType],
                        AwayGamesPlayed      = apiAwayGamesPlayed,
                        AwayLosses           = apiGame.Teams?.Away?.LeagueRecord.Losses,
                        AwayWins             = apiGame.Teams?.Away?.LeagueRecord.Wins,
                        AwayTies             = apiGame.Teams?.Away?.LeagueRecord.Ties,
                        AwayOvertimes        = apiGame.Teams?.Away?.LeagueRecord.Overtimes,
                        HomeGamesPlayed      = apiHomeGamesPlayed,
                        HomeLosses           = apiGame.Teams?.Home?.LeagueRecord.Losses,
                        HomeWins             = apiGame.Teams?.Home?.LeagueRecord.Wins,
                        HomeTies             = apiGame.Teams?.Home?.LeagueRecord.Ties,
                        HomeOvertimes        = apiGame.Teams?.Home?.LeagueRecord.Overtimes,
                    };
                    gamesDict.Add(dbGame.NhlGameId, dbGame);
                    dbContext.Games.Add(dbGame);
                }
                else if (
                    dbGame.DetailedGameStatusId != apiDetailedStatus ||
                    dbGame.GameStatusId != apiStatus ||
                    dbGame.HomeScore != homeScore ||
                    dbGame.AwayScore != awayScore ||
                    dbGame.HomeGamesPlayed != apiHomeGamesPlayed ||
                    dbGame.AwayGamesPlayed != apiAwayGamesPlayed ||
                    dbGame.GameTimeUtc != gameTimeUtc ||
                    dbGame.HomeTeamId != homeTeamId ||
                    dbGame.AwayTeamId != awayTeamId
                    )
                {
                    dbGame.DetailedGameStatusId = apiDetailedStatus;
                    dbGame.GameStatusId         = apiStatus;
                    dbGame.HomeScore            = homeScore;
                    dbGame.AwayScore            = awayScore;
                    dbGame.HomeGamesPlayed      = apiHomeGamesPlayed;
                    dbGame.AwayGamesPlayed      = apiAwayGamesPlayed;
                    dbGame.GameTimeUtc          = gameTimeUtc;
                    dbGame.HomeTeamId           = homeTeamId;
                    dbGame.AwayTeamId           = awayTeamId;
                }
            }
            dbContext.SaveChanges();
        }