public List<UserGameResult> BuildUserGameResults(int gameFilterId, List<GameResult> thisGameFiltersGameResults, List<UserGameSelection> thisGameFiltersUsersGameSelections, List<User> users, Team noBetPickTeam, int extraPointFactorPerBetOverMin) { string logProcess = "AdminController.GameResultProcessing.BuildUserGameResults"; try { var userGameResults = new List<UserGameResult>(); foreach (var user in users) { var thisUsersGameSelectionsForThisGameFilter = thisGameFiltersUsersGameSelections.Where(ugs => ugs.UserId == user.UserId).ToList(); var userGameResult = CalculateUserGameResults(user.UserId, gameFilterId, thisGameFiltersGameResults, thisUsersGameSelectionsForThisGameFilter, noBetPickTeam, extraPointFactorPerBetOverMin); userGameResults.AddRange(userGameResult); } return userGameResults; } catch (Exception ex) { logMessages.Add(new Log() { GameDateTime = DateTime.Now, Type = "Error", Process = logProcess, Message = ex.Message }); throw; } finally { logMessages.Add(new Log() { GameDateTime = DateTime.Now, Type = "Info", Process = logProcess, Message = "Ending" }); } }
public void SerializeTest() { var team = new Team() { TeamId = 1, TeamLongName = "abc", TeamShortName = "def" }; var expected = "{\"teamId\":1,\"teamLongName\":\"abc\",\"teamShortName\":\"def\"}"; var actual = team.ToJson(); Assert.AreEqual(expected, actual); }
public ActionResult Create(Team team) { if (ModelState.IsValid) { db.Teams.Add(team); db.SaveChanges(); return RedirectToAction("Index"); } return View(team); }
public List<UserGameSelection> CreateDefaultUserGameSelections(List<GameSpread> gameSpreads, User user, Team noBetTeam) { var userGameSelections = new List<UserGameSelection>(); // initialize the userGameSelections with No Bets foreach (var gameSpread in gameSpreads) { userGameSelections.Add(CreateDefaultUserGameSelection(gameSpread, user, noBetTeam)); } return userGameSelections; }
public ResultsByGameGameRow(GameSpread gameSpread, Team noWinnerTeam) { if (gameSpread == null) throw new ArgumentNullException("gameSpread"); if (noWinnerTeam == null) throw new ArgumentNullException("noWinnerTeam"); GameSpreadId = gameSpread.GameSpreadId; FavoriteTeamId = gameSpread.FavoriteTeamId; FavoriteTeamName = gameSpread.FavoriteTeam.TeamLongName; UnderdogTeamId = gameSpread.UnderdogTeamId; UnderdogTeamName = gameSpread.UnderdogTeam.TeamLongName; Spread = gameSpread.Spread; WinnerTeamId = noWinnerTeam.TeamId; WinnerTeamName = noWinnerTeam.TeamLongName; UsersData = new List<ResultsByGameGameRowUserData>(); }
public UserGameSelection CreateDefaultUserGameSelection(GameSpread gameSpread, User user, Team noBetTeam) { if (gameSpread == null) throw new ArgumentNullException("gameSpread"); if (noBetTeam == null) throw new ArgumentNullException("noBetTeam"); if (user == null) throw new ArgumentNullException("user"); // create the userGameSelection with No Bets var userGameSelection = new UserGameSelection() { Bet = 0, GameSpreadId = gameSpread.GameSpreadId, PickTeamId = noBetTeam.TeamId, GameSpread = gameSpread, PickTeam = noBetTeam, UserId = user.UserId, User = user, Saved = false }; return userGameSelection; }
public void CalculateUserGameResultsTestAllFavoriteWins() { var adminController = new AdminController(); int userId = 1; int gameFilterId = 1; int extraPointFactorPerBetOverMin = 2; Team noBetPickTeam = new Team() { TeamId = 1, TeamLongName = "No Bet", TeamOtherName = "No Bet", TeamShortName = "X " }; var gameResults = new List<GameResult>() { new GameResult(){ GameSpreadId = 1, WinnerTeamId = 10, GameSpread = new GameSpread(){UnderdogTeamId = 11, Game = new Game(){GameFilterId =1}}}, new GameResult(){ GameSpreadId = 2, WinnerTeamId = 20, GameSpread = new GameSpread(){UnderdogTeamId = 21, Game = new Game(){GameFilterId =1}}}, new GameResult(){ GameSpreadId = 3, WinnerTeamId = 30, GameSpread = new GameSpread(){UnderdogTeamId = 31, Game = new Game(){GameFilterId =1}}} }; var userGameSelections = new List<UserGameSelection>() { new UserGameSelection(){ UserGameSelectionId = 1, UserId = 1, GameSpreadId = 1, PickTeamId = 10, Bet = 3, GameSpread = new GameSpread(){Game = new Game(){GameFilterId =1}}}, new UserGameSelection(){ UserGameSelectionId = 2, UserId = 1, GameSpreadId = 2, PickTeamId = 20, Bet = 2, GameSpread = new GameSpread(){Game = new Game(){GameFilterId =1}}}, new UserGameSelection(){ UserGameSelectionId = 3, UserId = 1, GameSpreadId = 3, PickTeamId = 30, Bet = 1, GameSpread = new GameSpread(){Game = new Game(){GameFilterId =1}}} }; var expected = new List<UserGameResult>() { new UserGameResult(){ UserGameSelectionId=1, BetResult=1, BetPoints=3}, new UserGameResult(){ UserGameSelectionId=2, BetResult=1, BetPoints=2}, new UserGameResult(){ UserGameSelectionId=3, BetResult=1, BetPoints=1} }; var actual = adminController.CalculateUserGameResults(userId, gameFilterId, gameResults, userGameSelections, noBetPickTeam, extraPointFactorPerBetOverMin); Assert.AreEqual(expected.Count, actual.Count); foreach (var exp in expected) { Assert.AreEqual(exp.UserGameSelectionId, actual.FirstOrDefault(a => a.UserGameSelectionId == exp.UserGameSelectionId).UserGameSelectionId); Assert.AreEqual(exp.BetPoints, actual.FirstOrDefault(a => a.UserGameSelectionId == exp.UserGameSelectionId).BetPoints); Assert.AreEqual(exp.BetResult, actual.FirstOrDefault(a => a.UserGameSelectionId == exp.UserGameSelectionId).BetResult); } }
public void AddUserDataSelection(UserGameSelection userGameSelection, Team noBetTeam, Team noPicksTeam) { UsersData.Add(new ResultsByGameGameRowUserData(userGameSelection, noBetTeam, noPicksTeam)); }
public ResultsByGameGameRowUserData(UserGameSelection userGameSelection, Team noBetTeam, Team noPicksTeam) { var found = false; if (userGameSelection == null) throw new ArgumentException("userGameSelection is null"); UserGameSelectionId = userGameSelection.UserGameSelectionId; UserId = userGameSelection.UserId; if (userGameSelection.GameSpread == null) throw new ArgumentException("userGameSelection.GameSpread is null"); if (userGameSelection.PickTeam == null) throw new ArgumentException("userGameSelection.PickTeam is null"); if (userGameSelection.GameSpread.FavoriteTeamId == userGameSelection.GameSpread.UnderdogTeamId) throw new ArgumentException(string.Format("favoriteTeamId ({0}) cannot equal underdogTeamId ({1})", userGameSelection.GameSpread.FavoriteTeamId, userGameSelection.GameSpread.UnderdogTeamId)); if (userGameSelection.GameSpread.FavoriteTeamId == noBetTeam.TeamId) throw new ArgumentException(string.Format("favoriteTeamId ({0}) cannot equal noBetTeamId ({1})", userGameSelection.GameSpread.FavoriteTeamId, noBetTeam.TeamId)); if (userGameSelection.GameSpread.FavoriteTeamId == noPicksTeam.TeamId) throw new ArgumentException(string.Format("favoriteTeamId ({0}) cannot equal noPicksTeamId ({1})", userGameSelection.GameSpread.FavoriteTeamId, noPicksTeam.TeamId)); if (userGameSelection.GameSpread.UnderdogTeamId == noBetTeam.TeamId) throw new ArgumentException(string.Format("underdogTeamId ({0}) cannot equal noBetTeamId ({1})", userGameSelection.GameSpread.UnderdogTeamId, noBetTeam.TeamId)); if (userGameSelection.GameSpread.UnderdogTeamId == noPicksTeam.TeamId) throw new ArgumentException(string.Format("underdogTeamId ({0}) cannot equal noPicksTeamId ({1})", userGameSelection.GameSpread.UnderdogTeamId, noPicksTeam.TeamId)); ; if (noBetTeam.TeamId == noPicksTeam.TeamId) throw new ArgumentException(string.Format("noBetTeamId ({0}) cannot equal noPicksTeamId ({1})", noBetTeam.TeamId, noPicksTeam.TeamId)); if (userGameSelection.PickTeamId == userGameSelection.GameSpread.FavoriteTeamId) { PickedFavorite = true; found = true; } else if (userGameSelection.PickTeamId == userGameSelection.GameSpread.UnderdogTeamId) { PickedUnderdog = true; found = true; } else if (userGameSelection.PickTeamId == noBetTeam.TeamId) { PickedNoBet = true; found = true; } else if (userGameSelection.PickTeamId == noPicksTeam.TeamId) { PickedNoPicks = true; found = true; } if (found == false) { throw new ArgumentException(string.Format("pickTeamId ({0}) not found", userGameSelection.PickTeamId)); } Pick = userGameSelection.PickTeam.TeamShortName; Bet = userGameSelection.Bet; Result = 0; if (PickedUnderdog) { PotentialGain = userGameSelection.Bet * 2; //TODO remove hardcoding and factor in upset bonus } else { PotentialGain = userGameSelection.Bet; } PotentialLoss = userGameSelection.Bet * -1; }
public ActionResult Edit(Team team) { if (ModelState.IsValid) { db.Entry(team).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(team); }
public void GivenTheDataIsLoadedForGameFilterId(int p0) { gameFilterId = p0; users = Users; var teams = Teams; gameSpreads = GameSpreads.Where(_ => _.Game.GameFilterId == gameFilterId).ToList(); gameResults = GameResults.Where(_ => _.GameSpread.Game.GameFilterId == gameFilterId).ToList(); userGameSelections = UserGameSelections.Where(_ => _.GameSpread.Game.GameFilterId == gameFilterId).ToList(); userGameResults = UserGameResults.Where(_ => _.UserGameSelection.GameSpread.Game.GameFilterId == gameFilterId).ToList(); noBetTeam = Teams.FirstOrDefault(_ => _.TeamLongName == "No Bet"); noPicksTeam = Teams.FirstOrDefault(_ => _.TeamLongName == "No Picks"); noWinnerTeam = Teams.FirstOrDefault(_ => _.TeamLongName == "No Winner"); }
public void CalculateUserGameResultsTestWrongUserIdShouldError() { var adminController = new AdminController(); int userId = 1; int gameFilterId = 1; int extraPointFactorPerBetOverMin = 2; Team noBetPickTeam = new Team() { TeamId = 1, TeamLongName = "No Bet", TeamOtherName = "No Bet", TeamShortName = "X " }; var gameResults = new List<GameResult>() { new GameResult(){ GameSpreadId = 1, WinnerTeamId = 10, GameSpread = new GameSpread(){UnderdogTeamId = 11, Game = new Game(){GameFilterId =1}}}, new GameResult(){ GameSpreadId = 2, WinnerTeamId = 20, GameSpread = new GameSpread(){UnderdogTeamId = 21, Game = new Game(){GameFilterId =1}}}, new GameResult(){ GameSpreadId = 3, WinnerTeamId = 30, GameSpread = new GameSpread(){UnderdogTeamId = 31, Game = new Game(){GameFilterId =1}}} }; var userGameSelections = new List<UserGameSelection>() { new UserGameSelection(){ UserGameSelectionId = 1, UserId = 1, GameSpreadId = 1, PickTeamId = 10, Bet = 3, GameSpread = new GameSpread(){Game = new Game(){GameFilterId =1}}}, new UserGameSelection(){ UserGameSelectionId = 2, UserId = 2, GameSpreadId = 2, PickTeamId = 20, Bet = 2, GameSpread = new GameSpread(){Game = new Game(){GameFilterId =1}}}, new UserGameSelection(){ UserGameSelectionId = 3, UserId = 1, GameSpreadId = 4, PickTeamId = 30, Bet = 1, GameSpread = new GameSpread(){Game = new Game(){GameFilterId =1}}} }; var expected = new List<UserGameResult>() { new UserGameResult(){ UserGameSelectionId=1, BetResult=1, BetPoints=3}, new UserGameResult(){ UserGameSelectionId=2, BetResult=1, BetPoints=2}, new UserGameResult(){ UserGameSelectionId=3, BetResult=1, BetPoints=1} }; try { var actual = adminController.CalculateUserGameResults(userId, gameFilterId, gameResults, userGameSelections, noBetPickTeam, extraPointFactorPerBetOverMin); Assert.Fail("Should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentException)); Assert.IsTrue(ex.Message.Contains("The user game selection list has records for the wrong user (1) and/or game filter (0)")); } }
public List<UserGameResult> CalculateUserGameResults(int userId, int gameFilterId, List<GameResult> thisGameFiltersGameResults, List<UserGameSelection> thisUsersGameSelectionsForThisGameFilter, Team noBetPickTeam, int extraPointFactorPerBetOverMin) { string logProcess = "AdminController.GameResultProcessing.BuildUserGameResults.CalculateUserGameResults"; try { var userGameResults = new List<UserGameResult>(); var wrongGameFilterIdCount = thisGameFiltersGameResults .Where(gr => gr.GameSpread.Game.GameFilterId != gameFilterId) .Count(); if (wrongGameFilterIdCount > 0) { throw new ArgumentException(string.Format("The game result list has records for the wrong game filter ({0})", wrongGameFilterIdCount)); } var wrongUserIdCount = thisUsersGameSelectionsForThisGameFilter .Where(ugs => ugs.UserId != userId) .Count(); wrongGameFilterIdCount = thisUsersGameSelectionsForThisGameFilter .Where(ugs => ugs.GameSpread.Game.GameFilterId != gameFilterId) .Count(); if (wrongUserIdCount > 0 || wrongGameFilterIdCount > 0) { throw new ArgumentException(string.Format("The user game selection list has records for the wrong user ({0}) and/or game filter ({1})", wrongUserIdCount, wrongGameFilterIdCount)); } // loop through each game to see if user was correct or not foreach (var gameResult in thisGameFiltersGameResults) { var userGameSelection = thisUsersGameSelectionsForThisGameFilter.FirstOrDefault(ugs => ugs.GameSpreadId == gameResult.GameSpreadId); if (userGameSelection == null) { string toLog = string.Format("UserId:{0} is missing a selection for GameSpreadId:{1}", userId, gameResult.GameSpreadId); logMessages.Add(new Log() { GameDateTime = DateTime.Now, Type = "Warning", Process = logProcess, Message = toLog }); // create a no bet usergameselection userGameSelection = new UserGameSelection() { UserId = userId, GameSpreadId = gameResult.GameSpreadId, PickTeamId = noBetPickTeam.TeamId, Bet = 0 }; //TODO see if no UserGameSelectionId will cause error on save userGameResults.Add(new UserGameResult() { //UserGameSelectionId = userGameSelection.UserGameSelectionId, UserGameSelection = userGameSelection, BetResult = 0, // no result since it is a no bet BetPoints = 0 // no points since it is a no bet }); } else { if (userGameSelection.PickTeamId == noBetPickTeam.TeamId) { //user didn't bet, he picked no bet userGameResults.Add(new UserGameResult() { UserGameSelectionId = userGameSelection.UserGameSelectionId, UserGameSelection = userGameSelection, BetResult = 0, // no result since it is a no bet BetPoints = 0 // no points since it is a no bet }); } else { //user picked a team, betted on this game //determine if they won the bet var result = userGameSelection.PickTeamId == gameResult.WinnerTeamId; var underdog = userGameSelection.PickTeamId == gameResult.GameSpread.UnderdogTeamId; var betPoints = 0; if (underdog && result) { // only if picked the underdog and won, do you get the extra points betPoints = userGameSelection.Bet + (userGameSelection.Bet * extraPointFactorPerBetOverMin); } else { betPoints = userGameSelection.Bet; } if (result) { //won the bet userGameResults.Add(new UserGameResult() { UserGameSelectionId = userGameSelection.UserGameSelectionId, UserGameSelection = userGameSelection, BetResult = 1, // bet was won BetPoints = betPoints }); } else { //lost the bet userGameResults.Add(new UserGameResult() { UserGameSelectionId = userGameSelection.UserGameSelectionId, UserGameSelection = userGameSelection, BetResult = -1, // bet was won BetPoints = betPoints * -1 }); } } } } return userGameResults; } catch (Exception ex) { logMessages.Add(new Log() { GameDateTime = DateTime.Now, Type = "Error", Process = logProcess, Message = ex.Message }); throw; } finally { logMessages.Add(new Log() { GameDateTime = DateTime.Now, Type = "Info", Process = logProcess, Message = "Ending" }); } }
public ResultsByGameViewModel BuildResultsByGameViewModel(int currentGameFilterId, ref string viewBagMessageToUser, List<User> users, List<GameSpread> gameSpreads, List<GameResult> gameResults, List<UserGameSelection> userGameSelections, List<UserGameResult> userGameResults, Team noBetTeam, Team noPicksTeam, Team noWinnerTeam) { #region validate inputs if (users == null) throw new ArgumentNullException("users"); if (gameSpreads == null) throw new ArgumentNullException("gameSpreads"); if (gameResults == null) throw new ArgumentNullException("gameResults"); if (userGameSelections == null) throw new ArgumentNullException("userGameSelections"); if (userGameResults == null) throw new ArgumentNullException("userGameResults"); if (noBetTeam == null) throw new ArgumentException("No Bet team was not found"); if (noPicksTeam == null) throw new ArgumentException("No Picks team was not found"); if (noWinnerTeam == null) throw new ArgumentException("No Winner team was not found"); // make sure all lists are for this gameFilterId var gameSpreadsBad = gameSpreads.FirstOrDefault(_ => _.Game.GameFilterId != currentGameFilterId); if (gameSpreadsBad != null) throw new ArgumentOutOfRangeException("gameSpreadsBad", "Contains records from an incorrect GameFilterId"); var gameResultsBad = gameResults.FirstOrDefault(_ => _.GameSpread.Game.GameFilterId != currentGameFilterId); if (gameResultsBad != null) throw new ArgumentOutOfRangeException("gameResultsBad", "Contains records from an incorrect GameFilterId"); var userGameSelectionsBad = userGameSelections.FirstOrDefault(_ => _.GameSpread.Game.GameFilterId != currentGameFilterId); if (userGameSelectionsBad != null) throw new ArgumentOutOfRangeException("userGameSelectionsBad", "Contains records from an incorrect GameFilterId"); var userGameResultsBad = userGameResults.FirstOrDefault(_ => _.UserGameSelection.GameSpread.Game.GameFilterId != currentGameFilterId); if (userGameResultsBad != null) throw new ArgumentOutOfRangeException("userGameResultsBad", "Contains records from an incorrect GameFilterId"); // make sure the lists are sorted correctly users = users.OrderBy(_ => _.UserId).ToList(); gameSpreads = gameSpreads.OrderBy(_ => _.GameId).ToList(); gameResults = gameResults.OrderBy(_ => _.GameSpread.GameId).ToList(); userGameSelections = userGameSelections.OrderBy(_ => _.GameSpread.GameId).ThenBy(_ => _.UserId).ToList(); userGameResults = userGameResults.OrderBy(_ => _.UserGameSelection.GameSpread.GameId).ThenBy(_ => _.UserGameSelection.UserId).ToList(); #endregion var utilities = new ModelUtilities(); var vm = new ResultsByGameViewModel(currentGameFilterId); vm.Users = users; // add the users to the view model (this will be column headers on page) // loop through each game, creating the initial game row record foreach (var gameSpread in gameSpreads) { vm.GameRows.Add(new ResultsByGameGameRow(gameSpread, noWinnerTeam)); } // now that the gamerows have been created, now lets update them with game results...if they exist foreach (var gameResult in gameResults) { var gameRow = vm.GameRows.FirstOrDefault(_ => _.GameSpreadId == gameResult.GameSpreadId); if (gameRow == null) { throw new ArgumentException(string.Format("gameRow was not found for gameResult.GameSpreadId ({0})",gameResult.GameSpreadId)); } else { gameRow.UpdateWinner(gameResult); } } // need to add the user game selections in the correct order of each row (GameRows) and in the order of each column (Users) // loop through each gameRow and then... foreach (var gameRow in vm.GameRows) { // ...loop through each user and then... foreach (var user in users) { // check to see if this user has made user selections already var userGameSelection = userGameSelections.FirstOrDefault(_ => _.UserId == user.UserId && _.GameSpreadId == gameRow.GameSpreadId); if (userGameSelection == null) { var gameSpread = gameSpreads.FirstOrDefault(_=>_.GameSpreadId == gameRow.GameSpreadId); // the user hasn't made any selections yet...so create the default ones for that user userGameSelection = utilities.CreateDefaultUserGameSelection(gameSpread, user, noBetTeam); } gameRow.AddUserDataSelection(userGameSelection, noBetTeam, noPicksTeam); } } // update any user game results if they exist foreach (var userGameResult in userGameResults) { var gameRow = vm.GameRows.FirstOrDefault(_ => _.GameSpreadId == userGameResult.UserGameSelection.GameSpreadId); if (gameRow == null) { throw new ArgumentException(string.Format("gameRow was not found for userGameResult.UserGameSelection.GameSpreadId ({0})", userGameResult.UserGameSelection.GameSpreadId)); } else { var userData = gameRow.UsersData.FirstOrDefault(_ => _.UserGameSelectionId == userGameResult.UserGameSelectionId && _.UserId == userGameResult.UserGameSelection.UserId); userData.UpdateResult(userGameResult); } } if (vm.GameRows.Count > 0) { // make sure each game has each user data in the same order // get the user order from the first item var userDataFirst = vm.GameRows[0].UsersData; // now check all the subsequent userGameResults for (int i = 1; i < vm.GameRows.Count; i++) { var userDataNext = vm.GameRows[i].UsersData; if (userDataFirst.Count != userDataNext.Count) { throw new ArgumentException( string.Format( "The number of users does not match GameSpreadId:{0} and GameSpreadId:{1} for UsersData", vm.GameRows[0].GameSpreadId, vm.GameRows[i].GameSpreadId)); } // now that we know the lengths are equal, make sure each item is in the same // userid order as the first for (int x = 0; x < userDataNext.Count; x++) { if (userDataFirst[x].UserId != userDataNext[x].UserId) { throw new ArgumentException( string.Format( "The users (UserId:{2}, UserId:{3}) in position:{4} do not match for GameSpreadId:{0} and GameSpreadId:{1} for UsersData", vm.GameRows[0].GameSpreadId, vm.GameRows[i].GameSpreadId, userDataFirst[x].UserId, userDataNext[x].UserId, x)); } } } // TODO Move this logic inside view model List<int> totals = new List<int>(); List<int> gains = new List<int>(); List<int> losses = new List<int>(); // calculate totals foreach (var user in users) { int total = 0; int gain = 0; int loss = 0; foreach (var gameRow in vm.GameRows) { total += gameRow.UsersData .Where(_ => _.UserId == user.UserId) .Sum(_ => _.Result); gain += gameRow.UsersData .Where(_ => _.UserId == user.UserId) .Sum(_ => _.PotentialGain); loss += gameRow.UsersData .Where(_ => _.UserId == user.UserId) .Sum(_ => _.PotentialLoss); } totals.Add(total); gains.Add(gain); losses.Add(loss); } vm.Totals = totals; vm.PotentialGains = gains; vm.PotentialLosses = losses; } else { viewBagMessageToUser = string.Format("Missing games for given GameFilterId:{0}", currentGameFilterId); } return vm; }