Ejemplo n.º 1
0
        public ActionResult ListData(int? week, bool? isMaster)
        {
            #region validation

            if (!User.IsInRole("Admin"))
            {
                isMaster = null;
            }

            #endregion

            var masterUserId = Guid.Empty.ToString();
            var userId = (isMaster.GetValueOrDefault(false) ? masterUserId : User.Identity.GetUserId());
            var user = new UserListViewModel();
            var currentWeek = SchedulesDb.GetCurrentWeek();
            var picksWeek = week.GetValueOrDefault(currentWeek);

            // picks already made
            var picks = PickViewModel.GetListForUser(userId, picksWeek);
            if (!isMaster.GetValueOrDefault(false) && picksWeek < currentWeek)
            {
                // get master picks to see if picks were correct
                var masterPicks = PickViewModel.GetListForUser(masterUserId, picksWeek);
                foreach (var p in picks)
                {
                    var masterPick = masterPicks.Find(m => m.Schedule.Id == p.Schedule.Id);
                    p.IsCorrect = (masterPick != null && masterPick.PickHomeTeam == p.PickHomeTeam);
                }
            }

            // full schedule for this week
            var schedules = ScheduleViewModel.GetList(picksWeek);

            // add all missing schedules to the picks list
            foreach (var schedule in schedules.Where(s => !picks.Exists(p => p.Schedule.Id == s.Id)))
            {
                picks.Add(new PickViewModel
                {
                    CanPick = schedule.IsPickable,
                    IsPick = false,

                    GameDateDisplay = schedule.Date.ToString("dddd, MMMM d"),
                    GameTimeDisplay = schedule.Date.ToString("t") + " EST",

                    Schedule = schedule,
                    User = user
                });
            }

            var tieBreaker = picks.Find(p => p.Schedule.RequireScore);
            if (tieBreaker != null)
            {
                tieBreaker.GameDateDisplay = " " + tieBreaker.GameDateDisplay;
            }

            if (isMaster.GetValueOrDefault(false))
            {
                // for master pick, always allow picking
                picks.ForEach(p => p.CanPick = true);
            }
            else if (picksWeek > currentWeek)
            {
                // if looking at future picks, don't allow picking
                picks.ForEach(p => p.CanPick = false);
            }

            return Json(new { data = picks.OrderBy(p => p.Schedule.RequireScore).ThenBy(p => p.Schedule.Date).ThenBy(p => p.Schedule.Id).ToList() }, JsonRequestBehavior.AllowGet);
        }
Ejemplo n.º 2
0
        private static AllPicksViewModel ForUserFromPicks(List<PickViewModel> picks, UserListViewModel user, List<ScheduleViewModel> schedules, AllPicksViewModel masterPicks)
        {
            var model = new AllPicksViewModel
            {
                User = user,
                CorrectPicks = 0
            };

            // add all schedules with null pick
            foreach (var schedule in schedules.OrderBy(s => s.Date).ThenBy(s => s.Id))
            {
                model.PickedTeams.Add(schedule.Id, null);
            }

            if (picks != null)
            {
                // only add the teams for locked games
                foreach (var pick in picks.Where(p => p.User.Id == user.Id && !p.CanPick))
                {
                    var userPick = pick.PickHomeTeam.GetValueOrDefault(false) ? pick.Schedule.HomeTeam : pick.Schedule.AwayTeam;
                    model.PickedTeams[pick.Schedule.Id] = userPick;
                    if (pick.Schedule.RequireScore)
                    {
                        model.CombinedScore = pick.CombinedScore.GetValueOrDefault(0);
                    }

                    if (masterPicks != null)
                    {
                        // find the master pick for this schedule
                        var masterPick = masterPicks.PickedTeams.ContainsKey(pick.Schedule.Id) ? masterPicks.PickedTeams[pick.Schedule.Id] : null;
                        if (masterPick != null && masterPick.Id == userPick.Id)
                        {
                            // match
                            model.CorrectPicks++;
                        }
                    }
                }
            }

            return model;
        }