public void SaveDelivery(ScoreCardViewModel scoreCardViewModel, DeliveryInputViewModel deliveryInputViewModel)
        {
            ScoreCardFrame currentFrame = scoreCardViewModel.ScoreCardRows[scoreCardViewModel.CurrentScoreCardRowIndex]
                                          .ScoreCardFrames[scoreCardViewModel.CurrentFrameId];

            ScoreCardFrame previousFrame = new ScoreCardFrame();

            if (scoreCardViewModel.CurrentFrameId > 0)
            {
                previousFrame = scoreCardViewModel.ScoreCardRows[scoreCardViewModel.CurrentScoreCardRowIndex]
                                .ScoreCardFrames[scoreCardViewModel.CurrentFrameId - 1];
            }

            ScoreCardFrame twoFramesBackFrame = new ScoreCardFrame();

            if (scoreCardViewModel.CurrentFrameId > 1)
            {
                twoFramesBackFrame = scoreCardViewModel.ScoreCardRows[scoreCardViewModel.CurrentScoreCardRowIndex]
                                     .ScoreCardFrames[scoreCardViewModel.CurrentFrameId - 2];
            }

            if (deliveryInputViewModel.CurrDeliveryInFrameIndex == 0 &&
                deliveryInputViewModel.FrameIndex < 9)
            {
                MarkFirstDelivery(currentFrame, deliveryInputViewModel);
            }
            else if (deliveryInputViewModel.CurrDeliveryInFrameIndex == 1 &&
                     deliveryInputViewModel.FrameIndex < 9)
            {
                MarkSecondDelivery(currentFrame, deliveryInputViewModel);
                ScorePrevFrameForStrikesAndSpares(twoFramesBackFrame, previousFrame, currentFrame, deliveryInputViewModel);
                ScoreRegularFrames(previousFrame, currentFrame, deliveryInputViewModel);
            }

            if (deliveryInputViewModel.FrameIndex == 9 &&
                deliveryInputViewModel.CurrDeliveryInFrameIndex == 0)
            {
                MarkFirstDelivery(currentFrame, deliveryInputViewModel);
            }
            else if (deliveryInputViewModel.FrameIndex == 9 &&
                     deliveryInputViewModel.CurrDeliveryInFrameIndex == 1)
            {
                MarkSecondDelivery(currentFrame, deliveryInputViewModel);
                if (deliveryInputViewModel.FrameIndex == 9 &&
                    deliveryInputViewModel.SelectedDeliveryCode != (int)FrameStatusEnum.Strike &&
                    deliveryInputViewModel.SelectedDeliveryCode != (int)FrameStatusEnum.Spare)
                {
                    ScoreTenthFrame(previousFrame, currentFrame, deliveryInputViewModel);
                    scoreCardViewModel.IsUserGameComplete = true;
                }
            }
            else if (deliveryInputViewModel.FrameIndex == 9 &&
                     deliveryInputViewModel.CurrDeliveryInFrameIndex == 2)
            {
                MarkTenthFrameBonusDeliveries(currentFrame, deliveryInputViewModel);
                ScoreTenthFrame(previousFrame, currentFrame, deliveryInputViewModel);
                scoreCardViewModel.IsUserGameComplete = true;
            }
        }
        public IActionResult Index()
        {
            ScoreCardViewModel scoreCardViewModel = new ScoreCardViewModel();

            HttpContext.Session.SetString("GameFullData", JsonConvert.SerializeObject(scoreCardViewModel));

            return(View());
        }
        public IActionResult BowlingDeliveryInput(DeliveryInputViewModel deliveryInputViewModel)
        {
            //using session state in place of database for small demo and persistence between pages.
            ScoreCardViewModel scoreCardViewModel = JsonConvert.DeserializeObject <ScoreCardViewModel>(HttpContext.Session.GetString("GameFullData"));

            //validation failed, selection and field info persists, dpdn collections must be reloaded...
            if (!ModelState.IsValid)
            {
                DeliveryInputViewModel invalidViewModel = LoadDeliveryInputViewModel(deliveryInputViewModel.CurrentRowIndex,
                                                                                     deliveryInputViewModel.FrameIndex,
                                                                                     deliveryInputViewModel.CurrDeliveryInFrameIndex,
                                                                                     deliveryInputViewModel.PreviousDeliveryTypeCode,
                                                                                     deliveryInputViewModel.PreviousDeliveryPinsDown);
                return(View(invalidViewModel));
            }

            _deliveryService.SaveDelivery(scoreCardViewModel, deliveryInputViewModel);


            //for non10th frames where first roll is not a strike
            //hand off values to cache collection and increment delivery/roll index to never exceed 0 or 1
            if (scoreCardViewModel.CurrentFrameId < 9 &&
                deliveryInputViewModel.CurrDeliveryInFrameIndex == 0 &&
                deliveryInputViewModel.SelectedDeliveryCode != (int)FrameStatusEnum.Strike)
            {
                scoreCardViewModel.PreviousPinDownCount        = deliveryInputViewModel.SelectedPinsDownCount;
                scoreCardViewModel.PreviousDeliveryType        = deliveryInputViewModel.SelectedDeliveryCode;
                scoreCardViewModel.CurrentDeliveryInFrameIndex =
                    deliveryInputViewModel.CurrDeliveryInFrameIndex == 1
                        ? 0
                        : ++deliveryInputViewModel.CurrDeliveryInFrameIndex;
            }//strike or second roll for non10th frames, zero out roll data and increment frame index;
            else if (scoreCardViewModel.CurrentFrameId < 9 &&
                     (deliveryInputViewModel.CurrDeliveryInFrameIndex == 1 ||
                      deliveryInputViewModel.SelectedDeliveryCode == (int)FrameStatusEnum.Strike))
            {
                scoreCardViewModel.PreviousPinDownCount        = 0;
                scoreCardViewModel.PreviousDeliveryType        = 0;
                scoreCardViewModel.CurrentDeliveryInFrameIndex = 0;
                ++scoreCardViewModel.CurrentFrameId;
            }
            else//handle 10th frame, never exceed 2 in roll index and pass selections to cache
            {
                scoreCardViewModel.PreviousPinDownCount        = deliveryInputViewModel.SelectedPinsDownCount;
                scoreCardViewModel.PreviousDeliveryType        = deliveryInputViewModel.SelectedDeliveryCode;
                scoreCardViewModel.CurrentDeliveryInFrameIndex =
                    deliveryInputViewModel.CurrDeliveryInFrameIndex == 3
                        ? 0
                        : ++deliveryInputViewModel.CurrDeliveryInFrameIndex;
            }

            HttpContext.Session.SetString("GameFullData", JsonConvert.SerializeObject(scoreCardViewModel));

            return(RedirectToAction("BowlingGame"));
        }
        public IActionResult CreateBowlingUser(BowlingUserViewModel userViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            ScoreCardViewModel scoreCardViewModel = JsonConvert.DeserializeObject <ScoreCardViewModel>(HttpContext.Session.GetString("GameFullData"));

            _userService.CreateUser(scoreCardViewModel, userViewModel);

            HttpContext.Session.SetString("GameFullData", JsonConvert.SerializeObject(scoreCardViewModel));
            return(View("BowlingGame", scoreCardViewModel));
        }
        public List <SelectListItem> GetUserSelectListItems(ScoreCardViewModel viewModel)
        {
            List <SelectListItem> selectListItems = new List <SelectListItem>();

            foreach (var scoreCardRow in viewModel.ScoreCardRows)
            {
                selectListItems.Add(new SelectListItem()
                {
                    Text = scoreCardRow.FirstName + " " + scoreCardRow.LastName, Value = scoreCardRow.PlayerId.ToString()
                });
            }

            return(selectListItems);
        }
        public void CreateUser(ScoreCardViewModel viewModel, BowlingUserViewModel userViewModel)
        {
            List <ScoreCardFrame> frames = new List <ScoreCardFrame>();

            for (int i = 1; i <= 10; i++)
            {
                frames.Add(new ScoreCardFrame()
                {
                    FrameId = i
                });
            }

            viewModel.ScoreCardRows.Add(new ScoreCardRow()
            {
                FirstName       = userViewModel.FirstName,
                LastName        = userViewModel.LastName,
                PlayerId        = viewModel.ScoreCardRows.Count + 1,
                ScoreCardFrames = frames
            });
        }
Example #7
0
        public async Task <IActionResult> CreateScoreCard(string courseID)
        {
            var dto         = _scoreCardService.GetScoreCardCreateInformation(courseID);
            var userFriends = await _userService.GetFriendsAsync();

            var model = new ScoreCardViewModel
            {
                CourseID    = dto.CourseID,
                EndDate     = dto.EndDate,
                StartDate   = dto.StartDate,
                UserID      = dto.UserID,
                PlayerCards = _mapper.Map <List <PlayerCardViewModel> >(dto.PlayerCards),
                ScoreCardID = dto.ScoreCardID,
                UserName    = dto.UserName,
                Friends     = userFriends.Select(x => x.UserName).ToList(),
                CourseName  = _scoreCardService.GetCourseName(dto.CourseID)
            };

            return(View(model));
        }
        private DeliveryInputViewModel LoadDeliveryInputViewModel(int currRowIndex, int currFrameId, int currDeliveryInFrameIndex,
                                                                  int prevDeliveryType, int prevPinsDown)
        {
            ScoreCardViewModel scoreCardViewModel = JsonConvert.DeserializeObject <ScoreCardViewModel>(HttpContext.Session.GetString("GameFullData"));

            DeliveryInputViewModel deliveryInputViewModel = new DeliveryInputViewModel();

            deliveryInputViewModel.FrameIndex = currFrameId;
            deliveryInputViewModel.PreviousDeliveryPinsDown = prevPinsDown;
            deliveryInputViewModel.PreviousDeliveryTypeText = _deliveryService.GetDeliveryStatusText(prevDeliveryType);
            deliveryInputViewModel.FirstName                = scoreCardViewModel.ScoreCardRows[currRowIndex].FirstName;
            deliveryInputViewModel.LastName                 = scoreCardViewModel.ScoreCardRows[currRowIndex].LastName;
            deliveryInputViewModel.CurrentRowIndex          = currRowIndex;
            deliveryInputViewModel.PreviousDeliveryTypeCode = prevDeliveryType;

            if (currFrameId < 10)
            {
                deliveryInputViewModel.CurrDeliveryInFrameIndex = currDeliveryInFrameIndex;
            }
            deliveryInputViewModel.DeliveryTypes = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "Split", Value = ((int)FrameStatusEnum.Split).ToString()
                },
                new SelectListItem()
                {
                    Text = "Missed Pins", Value = ((int)FrameStatusEnum.OpenFrame).ToString()
                },
                new SelectListItem()
                {
                    Text = "Foul", Value = ((int)FrameStatusEnum.Foul).ToString()
                },
                new SelectListItem()
                {
                    Text = "Standard Roll", Value = ((int)FrameStatusEnum.StandardRoll).ToString()
                }
            };

            //first 9 frames should offer a strike only on first roll/delivery, as that ends the frame
            if (prevDeliveryType == 0 && currFrameId < 9)
            {
                deliveryInputViewModel.DeliveryTypes.Add(new SelectListItem()
                {
                    Text = "Strike", Value = ((int)FrameStatusEnum.Strike).ToString()
                });
            }//tenth frame should provide option for all strikes
            else if (prevDeliveryType != 0 && currFrameId < 9)//only 2nd roll should provide a spare option
            {
                deliveryInputViewModel.DeliveryTypes.Add(new SelectListItem()
                {
                    Text = "Spare", Value = ((int)FrameStatusEnum.Spare).ToString()
                });
            }
            else if (currFrameId == 9)
            {//
                if (currDeliveryInFrameIndex == 0)
                {
                    deliveryInputViewModel.DeliveryTypes.Add(new SelectListItem()
                    {
                        Text = "Strike", Value = ((int)FrameStatusEnum.Strike).ToString()
                    });
                }
                else
                {
                    if (prevDeliveryType == (int)FrameStatusEnum.Strike ||
                        prevDeliveryType == (int)FrameStatusEnum.Spare)
                    {
                        deliveryInputViewModel.DeliveryTypes.Add(new SelectListItem()
                        {
                            Text = "Strike", Value = ((int)FrameStatusEnum.Strike).ToString()
                        });
                    }
                    else
                    {
                        deliveryInputViewModel.DeliveryTypes.Add(new SelectListItem()
                        {
                            Text = "Spare", Value = ((int)FrameStatusEnum.Spare).ToString()
                        });
                    }
                }
            }

            deliveryInputViewModel.CountOfPinsAvailable = new List <SelectListItem>();
            //can't be null, but not sure if populating is needed given js manipulation
            for (int i = 1; i < 10 - prevPinsDown; i++)
            {
                deliveryInputViewModel.CountOfPinsAvailable.Add(
                    new SelectListItem()
                {
                    Text = i.ToString(), Value = i.ToString()
                });
            }

            return(deliveryInputViewModel);
        }
        public IActionResult BowlingGame()
        {
            ScoreCardViewModel scoreCardViewModel = JsonConvert.DeserializeObject <ScoreCardViewModel>(HttpContext.Session.GetString("GameFullData"));

            return(View(scoreCardViewModel));
        }