Example #1
0
        public void GetGameFilterByDateTestsSearchDateEqualsStartDateReturnsCorrect()
        {
            var modelUtils = new ModelUtilities();
            var initializer = new ModelDataInitializer();
            List<GameFilter> gameFilters = initializer.InitializeGameFilters();

            var actual = modelUtils.GetGameFilterByDate(gameFilters, new DateTime(2012, 09, 4));
            Assert.AreEqual("Week 01", actual.GameFilterName);
        }
Example #2
0
        public void GetGameFilterByDateTestsSearchDateBeforeStartDateReturnsError()
        {
            var modelUtils = new ModelUtilities();
            var initializer = new ModelDataInitializer();
            List<GameFilter> gameFilters = initializer.InitializeGameFilters();

            var actual = modelUtils.GetGameFilterByDate(gameFilters, new DateTime(2011, 07, 4));
            Assert.AreEqual(null, actual);
        }
Example #3
0
        public void GetBonusPointsTests9PlacedBetsReturnsMinRequired()
        {
            var modelUtils = new ModelUtilities();
            var initializer = new ModelDataInitializer();
            List<UserGameSelection> userGameSelections = initializer.InitializeUserGameSelections();
            userGameSelections = userGameSelections.Where(ugs => ugs.UserId == 3 && ugs.GameSpreadId >= 48 && ugs.GameSpreadId <= 61).ToList();
            int minPointsRequired = 6;
            int extraPointFactorPerBetOverMin = 2;

            var actual = modelUtils.GetBonusPoints(userGameSelections, minPointsRequired, extraPointFactorPerBetOverMin);
            Assert.AreEqual(12, actual);
        }
        public ActionResult Index()
        {
            var utilities = new ModelUtilities();
            var now = DateTime.Now;
            //var now = new DateTime(2012, 12, 16);
            string viewBagMessageToUser = string.Empty;

            try
            {
                var currentGameFilterId = utilities.GetGameFilterByDate(db.GameFilters.ToList(), now).GameFilterId;
                return RedirectToAction(string.Concat("GameFilter/", currentGameFilterId));
            }
            catch (Exception ex)
            {
                ViewBag.MessageToUser = string.Format("Sorry an error occurred. Please let the admin know. Error:{0}", ex.Message);
                SetViewBagGameFilterToFirst();
                return View();
            }
        }
Example #5
0
        public void GetPlacedBetsTests0PlacedBetsReturnsCorrectly()
        {
            var modelUtils = new ModelUtilities();
            var initializer = new ModelDataInitializer();
            List<UserGameSelection> userGameSelections = initializer.InitializeUserGameSelections();
            userGameSelections = userGameSelections.Where(ugs => ugs.UserId == -1).ToList();

            var actual = modelUtils.GetPlacedBets(userGameSelections);
            Assert.AreEqual(0, actual);
        }
Example #6
0
        public void GetSpentPointsTestsAlmostAllSpentPointsReturnsMinCorrectly()
        {
            var modelUtils = new ModelUtilities();
            var initializer = new ModelDataInitializer();
            List<UserGameSelection> userGameSelections = initializer.InitializeUserGameSelections();
            userGameSelections = userGameSelections.Where(ugs => ugs.UserId == 1 && ugs.GameSpreadId >= 1 && ugs.GameSpreadId <= 16).ToList();

            var actual = modelUtils.GetSpentPoints(userGameSelections);
            Assert.AreEqual(19, actual);
        }
Example #7
0
        public void GetPossibleBetsTests16PossibleBetsReturnsCorrectly()
        {
            var modelUtils = new ModelUtilities();
            var initializer = new ModelDataInitializer();
            List<UserGameSelection> userGameSelections = initializer.InitializeUserGameSelections();
            userGameSelections = userGameSelections.Where(ugs => ugs.UserId == 2 && ugs.GameSpreadId >= 17 && ugs.GameSpreadId <= 32).ToList();

            var actual = modelUtils.GetPossibleBets(userGameSelections);
            Assert.AreEqual(16, actual);
        }
Example #8
0
        public ActionResult GameFilter(int id = 0)
        {
            try
            {
                int currentGameFilterId = validateGameFilterId(id);
                SetViewBagGameFilter(currentGameFilterId);

                bool selectionDisabledForThisGameFilter = !db.GameFilters.FirstOrDefault(gf => gf.GameFilterId == currentGameFilterId).GameFilterEnabled;

                var utilities = new ModelUtilities();

                var userId = GetUserIdentityId();

                var userGameSelections = db.UserGameSelections
                                                .Include(ugs => ugs.GameSpread)
                                                .Include(t => t.PickTeam)
                                                .Include(g => g.GameSpread.FavoriteTeam)
                                                .Include(g => g.GameSpread.UnderdogTeam)
                                                .Include(g => g.GameSpread.Game)
                                                .Include(u => u.User)
                                                .Where(u => u.UserId == userId)
                                                .Where(ugs => ugs.GameSpread.Game.GameFilterId == currentGameFilterId)
                                                .ToList();

                if (userGameSelections.Count == 0)
                {
                    var gameSpreads = db.GameSpreads
                                            .Where(_ => _.Game.GameFilterId == currentGameFilterId)
                                            .OrderBy(_ => _.GameId)
                                            .ToList();

                    var user = db.Users.FirstOrDefault(_=>_.UserId == userId);

                    var noBetTeam = db.Teams.FirstOrDefault(_ => _.TeamLongName == "No Bet");

                    userGameSelections = utilities.CreateDefaultUserGameSelections(gameSpreads, user, noBetTeam);

                    // save to the db so the output has unique row numbers (it uses UserGameSelectionId)
                    userGameSelections.ForEach(ugs => db.UserGameSelections.Add(ugs));
                    db.SaveChanges();
                }

                var selection = new Selection();

                //selection.UserGameSelections = userGameSelections;
                selection.PlacedBets = utilities.GetPlacedBets(userGameSelections);
                selection.BonusPoints = utilities.GetBonusPoints(userGameSelections,
                                                                         selection.MinSpentPointsForAnyOneWeek,
                                                                         selection.ExtraPointFactorPerBetOverMin);
                selection.SpentPoints = utilities.GetSpentPoints(userGameSelections);
                selection.PossibleBets = utilities.GetPossibleBets(userGameSelections);
                selection.SelectionDisabledForThisGameFilter = selectionDisabledForThisGameFilter;
                selection.GameRowsHaveBeenSaved = userGameSelections.Where(ugs => ugs.Saved == false).Count() == 0;  // all records are false, meaning all have been saved

                var gameRows = new List<SelectionGameRow>();

                foreach (var ugs in userGameSelections)
                {
                    var gameRow = new SelectionGameRow();
                    /*gameRow.Bet = ugs.Bet;
                    gameRow.GameSpread = ugs.GameSpread;
                    gameRow.GameSpreadId = ugs.GameSpreadId;
                    gameRow.PickTeam = ugs.PickTeam;
                    gameRow.PickTeamId = ugs.PickTeamId;
                    gameRow.Saved = ugs.Saved;
                    gameRow.User = ugs.User;
                    gameRow.UserGameSelectionId = ugs.UserGameSelectionId;
                    gameRow.UserId = ugs.UserId;*/

                    if (ugs.GameSpread.Game.HomeTeam == 1)
                    {
                        if (ugs.GameSpread.UnderdogTeam.TeamId == ugs.GameSpread.Game.Team1.TeamId)
                        {
                            ugs.GameSpread.UnderdogTeam.TeamLongName = string.Concat("@" + ugs.GameSpread.UnderdogTeam.TeamLongName);
                        }
                        else
                        {
                            ugs.GameSpread.FavoriteTeam.TeamLongName = string.Concat("@" + ugs.GameSpread.FavoriteTeam.TeamLongName);
                        }
                    }
                    else
                    {
                        if (ugs.GameSpread.UnderdogTeam.TeamId == ugs.GameSpread.Game.Team2.TeamId)
                        {
                            ugs.GameSpread.UnderdogTeam.TeamLongName = string.Concat("@" + ugs.GameSpread.UnderdogTeam.TeamLongName);
                        }
                        else
                        {
                            ugs.GameSpread.FavoriteTeam.TeamLongName = string.Concat("@" + ugs.GameSpread.FavoriteTeam.TeamLongName);
                        }
                    }
                    gameRow.UserGameSelection = ugs;

                    var betPickers = new List<BetPicker>();

                    for (int i = 0; i < selection.MaxBetForAnyOneGame; i++)
                    {
                        var betPicker = new BetPicker();
                        var berPickerValue = i + 1;
                        if (ugs.Bet == 0)
                        {
                            betPicker.Disabled = true;
                        }
                        else
                        {
                            betPicker.Disabled = selection.SelectionDisabledForThisGameFilter || ugs.Saved;
                        }
                        betPicker.Toggled = berPickerValue == ugs.Bet;
                        betPicker.BetValue = berPickerValue;

                        betPickers.Add(betPicker);
                    }

                    gameRow.BetPickers.AddRange(betPickers);
                    gameRows.Add(gameRow);
                }

                selection.GameRows = gameRows;

                // validation rules

                if (userGameSelections == null || userGameSelections.Count == 0)
                {
                    //throw new ArgumentException(string.Format("Missing user game selections for given GameFilterId:{0}", currentGameFilterId));
                    ViewBag.MessageToUser = string.Format("Missing user game selections for given GameFilterId:{0}", currentGameFilterId);
                }

                return View(selection);
            }
            catch (Exception ex)
            {
                return HttpNotFound(ex.Message);
            }
        }
        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;
        }