Exemple #1
0
        public static Standings GetStandings(int year, int week)
        {
            List <TeamRecord> teamRecords = null;

            if (week == 0)
            {
                // create default standings
                var teams = GameHandler.GetTeamsInYear(year);

                teamRecords = new List <TeamRecord>();

                foreach (var team in teams)
                {
                    teamRecords.Add(new TeamRecord
                    {
                        Year   = year,
                        Week   = 0,
                        TeamId = team.Id,
                        // create H2H records against every other team
                        Head2HeadRecords = teams.Where(t => t.Id != team.Id).Select(t => new Head2HeadRecord
                        {
                            Year       = year,
                            Week       = week,
                            TeamId     = team.Id,
                            OpponentId = t.Id
                        }).ToList()
                    });
                }
            }
            else
            {
                teamRecords = TeamRecordHandler.GetTeamRecordsByWeek(year, week);
            }

            if (week <= SeasonInfoHandler.GetSeason(year).RegularSeasonLength)
            {
                return(new RegularSeasonStandings(teamRecords));
            }

            return(new PlayoffStandings(teamRecords));
        }
Exemple #2
0
        public static Utils.Standings CalculateStandings(int year, int week, bool force = false)
        {
            if (week < 1 || week > 16)
            {
                throw new ArgumentException();
            }

            var weekExists = TeamRecordHandler.WeekExists(year, week);

            if (weekExists)
            {
                if (force)
                {
                    TeamRecordHandler.DeleteTeamRecordsInWeek(year, week);
                }
                else
                {
                    return(Utils.Standings.GetStandings(year, week));
                }
            }

            var prevStandings = Utils.Standings.GetStandings(year, week - 1);

            if (!prevStandings.IsValid())
            {
                // previous week's standings does not exist yet
                prevStandings = CalculateStandings(year, week - 1, force: force);
            }

            Console.WriteLine("Calculating standings for {0} week {1}", year, week);

            Utils.Standings newStandings = null;

            var seasonInfo = SeasonInfoHandler.GetSeason(year);

            if (week <= seasonInfo.RegularSeasonLength)
            {
                newStandings = new Utils.RegularSeasonStandings(prevStandings);
            }
            else
            {
                newStandings = new Utils.PlayoffStandings(prevStandings);
            }

            var games = GameHandler.GetGamesByWeek(year, week);

            foreach (var game in games)
            {
                if (game.HasValidResult)
                {
                    newStandings.AddResult(game);
                }
            }

            newStandings.SortStandings();

            TeamRecordHandler.Add(newStandings.TeamRecords);

            // update season info
            if (week <= seasonInfo.RegularSeasonLength)
            {
                var highestPointsForRecord = newStandings.GetHighestPointsForRecord();
                seasonInfo.HighestPointsFor       = highestPointsForRecord.PointsFor;
                seasonInfo.HighestPointsForTeamId = highestPointsForRecord.TeamId;
                seasonInfo.HighestPointsForTeam   = highestPointsForRecord.Team;

                if (week == seasonInfo.RegularSeasonLength)
                {
                    seasonInfo.RegularSeasonChampionId = newStandings.TeamRecords[0].TeamId;
                }
            }
            else if (week == seasonInfo.SeasonLength)
            {
                seasonInfo.ChampionId    = newStandings.TeamRecords[0].TeamId;
                seasonInfo.SecondPlaceId = newStandings.TeamRecords[1].TeamId;
                seasonInfo.ThirdPlaceId  = newStandings.TeamRecords[2].TeamId;
                seasonInfo.SackoId       = newStandings.TeamRecords.Last().TeamId;
            }

            SeasonInfoHandler.Update(seasonInfo);

            return(newStandings);
        }
Exemple #3
0
        public static void Add(string scheduleFile, bool force = false)
        {
            if (string.IsNullOrEmpty(scheduleFile))
            {
                return;
            }

            var schedule = JsonConvert.DeserializeObject <Models.Import.Schedule>(File.ReadAllText(scheduleFile));

            var weeksInDb = GameHandler.GetWeeksInYear(schedule.Year);

            if (weeksInDb.Count > 0 && force)
            {
                GameHandler.DeleteGamesInYear(schedule.Year);
                weeksInDb.Clear();
            }

            var teams      = TeamHandler.GetAllTeams().ToDictionary(t => t.Name);
            var gamesToAdd = new List <Game>();

            foreach (var week in schedule.Weeks)
            {
                if (weeksInDb.Contains(week.Week))
                {
                    // this week already exists in database
                    continue;
                }

                foreach (var game in week.Games)
                {
                    if (!teams.ContainsKey(game.Team1))
                    {
                        var team = TeamHandler.Add(new Team {
                            Name = game.Team1
                        });
                        teams.Add(team.Name, team);
                    }
                    if (!teams.ContainsKey(game.Team2))
                    {
                        var team = TeamHandler.Add(new Team {
                            Name = game.Team2
                        });
                        teams.Add(team.Name, team);
                    }

                    var team1 = teams[game.Team1];
                    var team2 = teams[game.Team2];

                    gamesToAdd.Add(new Game
                    {
                        Year       = schedule.Year,
                        Week       = week.Week,
                        GameScores = new List <GameScore>
                        {
                            new GameScore
                            {
                                Year   = schedule.Year,
                                Week   = week.Week,
                                TeamId = team1.Id
                            },
                            new GameScore
                            {
                                Year   = schedule.Year,
                                Week   = week.Week,
                                TeamId = team2.Id
                            }
                        }
                    });
                }
            }

            GameHandler.AddGames(gamesToAdd);

            if (SeasonInfoHandler.GetSeason(schedule.Year) == null)
            {
                SeasonInfoHandler.Add(new SeasonInfo
                {
                    Year                = schedule.Year,
                    NumTeams            = schedule.NumTeams,
                    NumPlayoffTeams     = schedule.NumPlayoffTeams,
                    RegularSeasonLength = schedule.RegularSeasonLength,
                    PlayoffLength       = schedule.PlayoffLength
                });
            }
        }