Ejemplo n.º 1
0
        private static async Task<YearCalendar> GenerateWeekDateSpan(YearCalendar yearCalendar, string year, string season, string week) {
            string url;
            WeekDateSpan weekDateSpan;
            WeekSchedule weekSchedule;
            int gameCount;
            string startGameId;
            string endGameId;
            string gameIdYear = string.Empty;
            string gameIdMonth = string.Empty;
            string gameIdDay = string.Empty;

            url = NFLService.BuildWeekScheduleUrl(year, season, week);
            weekSchedule = await NFLService.GetWeekScheduleFromUrlAsync(url);
            if (weekSchedule.GameSchedules != null) {
                gameCount = weekSchedule.GameSchedules.Count;
                startGameId = weekSchedule.GameSchedules[0].GameId;
                endGameId = weekSchedule.GameSchedules[gameCount - 1].GameId;
                weekDateSpan = new WeekDateSpan();
                ParseGameId(startGameId, ref gameIdYear, ref gameIdMonth, ref gameIdDay);
                weekDateSpan.StartDate = new DateTime(int.Parse(gameIdYear), int.Parse(gameIdMonth), int.Parse(gameIdDay));
                weekDateSpan.EndDate = weekDateSpan.StartDate.AddDays(7);
                weekDateSpan.Year = year;
                weekDateSpan.Season = season;
                weekDateSpan.Week = week.Length == 1 ? "0" + week : week;
                yearCalendar.WeekDates.Add(weekDateSpan);
            }

            return yearCalendar;
        }
Ejemplo n.º 2
0
        private static async Task<WeekDateSpan> GenerateWeekDateSpan2(string year, string season, string week) {
            string url;
            WeekDateSpan weekDateSpan = null;
            WeekSchedule weekSchedule;
            int gameCount;
            string startGameId;
            string gameIdYear = string.Empty;
            string gameIdMonth = string.Empty;
            string gameIdDay = string.Empty;

            url = NFLService.BuildWeekScheduleUrl(year, season, week);
            weekSchedule = await NFLService.GetWeekScheduleFromUrlAsync(url);

            if (weekSchedule.GameSchedules != null) {
                weekDateSpan = new WeekDateSpan();
                weekDateSpan.Year = year;
                weekDateSpan.Season = season;
                weekDateSpan.Week = week;
                gameCount = weekSchedule.GameSchedules.Count;

                // 1st GameId
                startGameId = weekSchedule.GameSchedules[0].GameId;

                // Get year, month, day from GameId
                ParseGameId(startGameId, ref gameIdYear, ref gameIdMonth, ref gameIdDay);

                // 1st Span (enddate is calculated from startdate)
                weekDateSpan.StartDate = new DateTime(int.Parse(gameIdYear), int.Parse(gameIdMonth), int.Parse(gameIdDay));
                weekDateSpan.EndDate = weekDateSpan.StartDate.AddDays(7);
            }

            return weekDateSpan;
        }
Ejemplo n.º 3
0
        public async static Task<WeekDateSpan> GetCurrentWeekAsync(DateTime date, string fileName) {
            // Need to adjust year because NFL Season starts in Aug and continues on to Feb of next year.
            string year = date.Month > 6 ? date.Year.ToString() : (date.Year - 1).ToString();
            WeekDateSpan span = new WeekDateSpan();

            // Building calendar every time takes too long.
            //YearCalendar yearCalendar = await NFLService.BuildYearCalendarAsync(year);

            // Build calendar one time then save it for reuse.
            FileService fileService = new FileService();
            string readjson = await fileService.ReadYearCalendarAsync(fileName);
            YearCalendar yearCalendar;
            if (string.IsNullOrEmpty(readjson)) {
                yearCalendar = await NFLService.BuildYearCalendarAsync(year);
                string json = JsonConvert.SerializeObject(yearCalendar, Formatting.None);
                await fileService.WriteYearCalendarAsync("YearCalendar.json", json);
            }
            else {
                yearCalendar = JsonConvert.DeserializeObject<YearCalendar>(readjson);
            }

            for (int i = 0; i < yearCalendar.WeekDates.Count; i++) {
                if (date > yearCalendar.WeekDates[i].StartDate && date < yearCalendar.WeekDates[i].EndDate) {
                    span.Year = yearCalendar.WeekDates[i].Year;
                    span.Season = yearCalendar.WeekDates[i].Season;
                    span.Week = yearCalendar.WeekDates[i].Week.Length == 1 ? "0" + yearCalendar.WeekDates[i].Week :
                        yearCalendar.WeekDates[i].Week;
                    span.StartDate = DateTime.Now;
                    span.EndDate = DateTime.Now;
                    break;
                }
            }

            return span;
        }
Ejemplo n.º 4
0
        //public async static Task<WeekDateSpan> GetCurrentWeekAsync(DateTime date) {
        //    string year = date.Year.ToString();
        //    WeekDateSpan span = new WeekDateSpan();
        //    YearCalendar yearCalendar = new YearCalendar();
        //    yearCalendar.WeekDates = new List<WeekDateSpan>();

        //    string season = "PRE";
        //    bool bDateFoundInSpan = false;
        //    for (int i = 1; i <= 4; i++) {
        //        span = await NFLService.GenerateWeekDateSpan2(year, season, i.ToString());
        //        if (IsDateInSpan(date, span)) {
        //            bDateFoundInSpan = true;
        //            break;
        //        }
        //    }

        //    if (bDateFoundInSpan == false) {
        //        season = "REG";
        //        for (int i = 1; i <= 17; i++) {
        //            span = await NFLService.GenerateWeekDateSpan2(year, season, i.ToString());
        //            if (IsDateInSpan(date, span)) {
        //                bDateFoundInSpan = true;
        //                break;
        //            }
        //        }
        //    }

        //    if (bDateFoundInSpan == false) {
        //        season = "POST";
        //        for (int i = 18; i <= 20; i++) {
        //            span = await NFLService.GenerateWeekDateSpan2(year, season, i.ToString());
        //            if (IsDateInSpan(date, span)) {
        //                bDateFoundInSpan = true;
        //                break;
        //            }
        //        }
        //    }

        //    return span;
        //}

        private static bool IsDateInSpan(DateTime date, WeekDateSpan span) {
            if (date > span.StartDate && date < span.EndDate) {
                return true;
            }
            return false;
        }