public ActionResult AcceptPicks(MyPredictViewModel vm, FormCollection formValues) { var pPicks = (from formValue in formValues.AllKeys where formValue.Contains("_team_playoff_") && bool.Parse(Request.Form.GetValues(formValue)[0]) let idx = formValue.LastIndexOf("_") select new TeamViewModel() {Id = int.Parse(formValue.Substring(idx + 1, formValue.Length - (idx + 1)))} into team select new SuperbowlPlayoffPickViewModel() { IsPlayoff = true, Team = team, UserId = User.Identity.Name }).ToList(); var sPicks = (from formValue in formValues.AllKeys where formValue.Contains("_team_superbowl_") && bool.Parse(Request.Form.GetValues(formValue)[0]) let idx = formValue.LastIndexOf("_") select new TeamViewModel() { Id = int.Parse(formValue.Substring(idx + 1, formValue.Length - (idx + 1))) } into team select new SuperbowlPlayoffPickViewModel() { IsPlayoff = true, Team = team, UserId = User.Identity.Name}).ToList(); vm.MyPlayoffPicks = pPicks; vm.MySuperbowlPicks = sPicks; if (vm.AreStandardPicks) _svc.SaveStandardPicks(vm, User.Identity.Name); else { _svc.SavePick(vm, User.Identity.Name); if (User.IsInRole("Admins")) _svc.UpdateWeeklyScores(vm); } return Json("success"); }
public void UpdateWeeklyScores(MyPredictViewModel vm) { // which games have we updated var scoredGames = from p in vm.MyPicks where p.Game.HomeTeamScore > 0 || p.Game.AwayTeamScore > 0 select p; // have we updated any games? foreach (var sg in scoredGames.ToList()) { var scoredGame = sg; // get the game from the db var dbGame = _ctx.Games.Where(g => g.Id == scoredGame.Game.Id).Single(); // set the scores dbGame.HomeTeamScore = sg.Game.HomeTeamScore; dbGame.AwayTeamScore = sg.Game.AwayTeamScore; // get the winner from the game var winner = 0; winner = dbGame.HomeTeamScore > dbGame.AwayTeamScore ? dbGame.HomeTeam_Id : dbGame.AwayTeam_Id; // get the picks for the game var picks = _ctx.Picks.GetCurrentWeekPicks(vm.CurrentWeek).Where(p => p.Game.Id == scoredGame.Game.Id).ToList(); // did we win? foreach (var pick in picks) pick.IsWinner = pick.Team_Id == winner; // get the standard picks for the game var sPicks = _ctx.StandardPicks.GetCurrentWeekStandardPicks(vm.CurrentWeek).Where(p => p.Game.Id == scoredGame.Game.Id).ToList(); // did we win? foreach (var sp in sPicks) sp.IsWinner = sp.Team_Id == winner; } // persist the changes to the database _ctx.SaveChanges(); }
public void SaveStandardPicks(MyPredictViewModel vm, string userId) { var cwp = _ctx.StandardPicks.GetCurrentWeekStandardPicks(vm.CurrentWeek).ToList(); // if (cwp.Count > 0) { foreach (var standardPick in cwp) { standardPick.Active = false; standardPick.UpdateBy = userId; standardPick.UpdateDate = DateTime.Now; } // } // foreach (var pick in vm.MyPicks) { if (pick.Game != null && pick.Game.Id > 0 && pick.Team != null && pick.Team.Id > 0) { var p = new StandardPick() { Game_Id = pick.Game.Id, PointTotal = pick.PointTotal, Team_Id = pick.Team.Id, UpdateBy = userId, UpdateDate = DateTime.Now, Active = true }; // _ctx.AddToStandardPicks(p); } } // var cwpp = _ctx.StandardPlayoffSuperbowlPicks.GetCurrentWeekPlayoffStandardPicks(vm.CurrentWeek).ToList(); if (cwpp.Count > 0) { foreach (var standardPick in cwpp) { standardPick.Active = false; standardPick.UpdateBy = userId; standardPick.UpdateDate = DateTime.Now; } // } // foreach (var pick in vm.MyPlayoffPicks) { var p = new StandardPlayoffSuperbowlPick() { IsPlayoff = true, Active = true, Week = vm.CurrentWeek, PointTotal = (_ctx.Games.GetMaxRegularSeasonWeekCurrentSeason() - vm.CurrentWeek) + 1, Season_Id = vm.TheSeason.Id, Team_Id = pick.Team.Id, UpdateBy = userId, UpdateDate = DateTime.Now }; // _ctx.AddToStandardPlayoffSuperbowlPicks(p); } // var cwsp = _ctx.StandardPlayoffSuperbowlPicks.GetCurrentWeekSuperbowlStandardPicks(vm.CurrentWeek); if (cwpp.Count > 0) { foreach (var standardPick in cwsp) { standardPick.Active = false; standardPick.UpdateBy = userId; standardPick.UpdateDate = DateTime.Now; } // } // foreach (var pick in vm.MySuperbowlPicks) { var p = new StandardPlayoffSuperbowlPick() { IsSuperbowl = true, Active = true, Week = vm.CurrentWeek, PointTotal = ((_ctx.Games.GetMaxRegularSeasonWeekCurrentSeason() - vm.CurrentWeek) + 1)*2, Season_Id = vm.TheSeason.Id, Team_Id = pick.Team.Id, UpdateBy = userId, UpdateDate = DateTime.Now }; // _ctx.AddToStandardPlayoffSuperbowlPicks(p); } // _ctx.SaveChanges(); // }
public MyPredictViewModel SetupWeek(int week, string userId) { // setup auto-mapper AutoMapperConfiguration.Setup(_ctx); // create the season view model var season = Mapper.Map<Season, SeasonViewModel>(_ctx.Seasons.GetCurrentSeason().Single()); // create the games view model var games = Mapper.Map<List<Game>, List<GameViewModel>>(_ctx.Games.GetCurrentWeekGames(week).ToList()); // do we need to completly disable this week? var disabled = true; if (season.StartDate <= DateTime.Now && games.Count > 0) { // select the latest game for the week var maxGameDate = games.Select(g => g.GameDateTime).Max(); disabled = DateTime.Now > maxGameDate; } // Get the picks for the current week for the current user var picks = Mapper.Map<List<Pick>, List<PickViewModel>>(_ctx.Picks.GetCurrentWeekPicks(week, userId).ToList()); // get the games with no picks var noPicks = games.GetGamesWithNoPicks(picks).ToList(); // we need a pick for each game that has no pick for the current user picks.AddRange(noPicks.Select(gvm => new PickViewModel { Game = gvm, UserId = userId })); // get the user's playoff and superbowl picks var psPicks = Mapper.Map<List<PlayoffSuperbowlPick>, List<SuperbowlPlayoffPickViewModel>>(_ctx.PlayoffSuperbowlPicks.GetCurrentWeekPlayoffSuperbowlPicks(week, userId).ToList()); // get all the regular standard picks //var standardPicks = Mapper.Map<List<StandardPick>, List<PickViewModel>>(_ctx.StandardPicks.GetCurrentWeekStandardPicks(week).ToList()); // get all the standard playoff picks //var standardPlayoffPicks = Mapper.Map<List<StandardPlayoffSuperbowlPick>, List<SuperbowlPlayoffPickViewModel>>(_ctx.StandardPlayoffSuperbowlPicks.GetCurrentWeekPlayoffStandardPicks(week).ToList()); // get the standard superbowl picks //var standardSuperbowlPicks = Mapper.Map<List<StandardPlayoffSuperbowlPick>, List<SuperbowlPlayoffPickViewModel>>(_ctx.StandardPlayoffSuperbowlPicks.GetCurrentWeekSuperbowlStandardPicks(week).ToList()); // get the teams var teams = Mapper.Map<List<Team>, List<TeamViewModel>>(_ctx.Teams.ToList()); // get the week points var pointTotal = picks.Where(p => p.IsWinner).Sum(p => p.PointTotal); // do we have any playoff/superbowl points? var psPointTotal = psPicks.Where(p => p.IsWinner).Sum(p => p.PointTotal); // set the points list var pointsList = new List<int>(); var pfGames = games.Count(g => g.IsPlayoff); var sGames = games.Count(g => g.IsSuperbowl); var maxRWeek = 0; if (games.Count > 0) maxRWeek = _ctx.Games.GetMaxRegularSeasonWeekCurrentSeason(); var showPlayoffSuperbowlPicks = (pfGames == 0 && sGames == 0); var showSuperbowlPicksOnly = (pfGames == games.Count); if (pfGames == games.Count && games.Count == 4) { // this is the first playoff week pointsList.Add(0); pointsList.Add(5); pointsList.Add(10); pointsList.Add(15); pointsList.Add(20); } else if (pfGames == games.Count && games.Count == 2) { pointsList.Add(0); pointsList.Add(20); pointsList.Add(30); } else if (sGames == games.Count) { pointsList.Add(50); } else { // this is a normal week for (var i = 0; i <= games.Count; i++) { pointsList.Add(i); } } if (picks.Count > 0) { foreach (var pick1 in _ctx.Picks.GetCurrentWeekPicks(week, userId).Where(p=>p.Game.GameDateTime < DateTime.Now).ToList().Where(pick1 => pick1.PointTotal > 0)) { pointsList.RemoveAll(p => p == pick1.PointTotal); } } // create the view model var vm = new MyPredictViewModel() { TheSeason = season, //StandardPlayoffPicks = standardPlayoffPicks, //StandardSuperbowlPicks = standardSuperbowlPicks, //StandardWeeklyPicks = standardPicks, MyPicks = picks, MyPlayoffPicks = psPicks.Where(p => p.IsPlayoff).ToList(), MySuperbowlPicks = psPicks.Where(p => p.IsSuperbowl).ToList(), CurrentWeek = week, AfcTeams = teams.Where(t => t.Conference.ToUpper().Equals("AFC")).ToList(), NfcTeams = teams.Where(t => t.Conference.ToUpper().Equals("NFC")).ToList(), Disabled = disabled, WeekPointTotal = pointTotal, PointList = pointsList, PossiblePlayoffPointTotal = maxRWeek > 0 ? (maxRWeek - week) + 1 : 0, ShouldHavePlayoffSuperbowlPicks = showPlayoffSuperbowlPicks, PlayoffSuperbowlPointTotal = psPointTotal, ShouldHaveSuperbowlPicksOnly = showSuperbowlPicksOnly, IsSuperbowlWeek = sGames == 1 }; // set the superbowl possible points if (maxRWeek > 0 && !vm.ShouldHaveSuperbowlPicksOnly) vm.PossibleSuperbowlPointTotal = ((maxRWeek - week) + 1) * 2; else if (vm.ShouldHaveSuperbowlPicksOnly && week == maxRWeek + 1) vm.PossibleSuperbowlPointTotal = 30; else if (vm.ShouldHavePlayoffSuperbowlPicks && week == maxRWeek + 2) vm.PossibleSuperbowlPointTotal = 20; else if (vm.ShouldHavePlayoffSuperbowlPicks && week == maxRWeek + 3) vm.PossibleSuperbowlPointTotal = 10; else vm.PossibleSuperbowlPointTotal = 0; if (maxRWeek < week) { switch (week - maxRWeek) { case 1: vm.PossibleSuperbowlPointTotal = 30; break; case 2: vm.PossibleSuperbowlPointTotal = 20; break; case 3: vm.PossibleSuperbowlPointTotal = 10; break; } } // return the newely created view model return vm; }
public void SavePick(MyPredictViewModel vm, string userId) { // do we have any picks for this week? foreach (var pvm in vm.MyPicks) { var p = _ctx.Picks.Where(pick => pick.Game.Id == pvm.Game.Id && pick.UserId == userId).SingleOrDefault(); if (p != null && p.Team_Id > 0 && p.Game != null && p.Game.GameDateTime >= DateTime.Now) { p.Game.Id = pvm.Game.Id; p.PointTotal = pvm.PointTotal; p.UserId = userId; p.Team_Id = pvm.Team.Id; p.UserId = userId; } else if (p == null) { var game = _ctx.Games.Where(g => g.Id == pvm.Game.Id).SingleOrDefault(); if (game != null && pvm.Team != null && game.GameDateTime >= DateTime.Now) { var newPick = new Pick() { Game_Id = pvm.Game.Id, PointTotal = pvm.PointTotal, Team_Id = pvm.Team.Id, UserId = userId }; _ctx.AddToPicks(newPick); } else if (game != null && pvm.Team != null) { var newPick = new Pick() { Game_Id = pvm.Game.Id, PointTotal = 0, Team_Id = game.HomeTeam_Id, UserId = userId }; _ctx.AddToPicks(newPick); } // } } // save changes _ctx.SaveChanges(); // if (vm.MyPlayoffPicks.Count > 0) { var pPicks = _ctx.PlayoffSuperbowlPicks.GetCurrentWeekPlayoffPicks(vm.CurrentWeek, userId).ToList(); var delPicks = from p in pPicks where !(from ps in vm.MyPlayoffPicks select ps.Team.Id).ToList().Contains(p.Team_Id) select p; foreach (var playoffSuperbowlPick in delPicks) { _ctx.PlayoffSuperbowlPicks.DeleteObject(playoffSuperbowlPick); } var addPicks = from p in vm.MyPlayoffPicks where !(from ps in pPicks select ps.Team_Id).ToList().Contains(p.Team.Id) select p; foreach (var superbowlPlayoffPickViewModel in addPicks) { _ctx.AddToPlayoffSuperbowlPicks(new PlayoffSuperbowlPick() { IsPlayoff = true, IsSuperbowl = false, Team_Id = superbowlPlayoffPickViewModel.Team.Id, PointTotal = (vm.TheSeason.NumberOfWeeks - vm.CurrentWeek) + 1, Season_Id = vm.TheSeason.Id, UserId = userId, Week = vm.CurrentWeek, IsWinner = false }); } // _ctx.SaveChanges(); } if (vm.MySuperbowlPicks.Count > 0) { var sPicks = _ctx.PlayoffSuperbowlPicks.GetCurrentWeekSuperbowlPicks(vm.CurrentWeek, userId).ToList(); var delPicks = from p in sPicks where!(from ps in vm.MySuperbowlPicks select ps.Team.Id).Contains(p.Team_Id) select p; foreach (var playoffSuperbowlPick in delPicks) { _ctx.PlayoffSuperbowlPicks.DeleteObject(playoffSuperbowlPick); } var addPicks = from p in vm.MySuperbowlPicks where !(from ps in sPicks select ps.Team_Id).Contains(p.Team.Id) select p; foreach (var superbowlPlayoffPickViewModel in addPicks) { _ctx.AddToPlayoffSuperbowlPicks(new PlayoffSuperbowlPick() { IsPlayoff = false, IsSuperbowl = true, Team_Id = superbowlPlayoffPickViewModel.Team.Id, PointTotal = ((vm.TheSeason.NumberOfWeeks - vm.CurrentWeek) + 1)*2, Season_Id = vm.TheSeason.Id, UserId = userId, Week = vm.CurrentWeek, IsWinner = false }); } // save changes _ctx.SaveChanges(); } }