Пример #1
0
        public ActionResult Bid(string id, int?pagenumber, string pagename, BidClass model)
        {
            RacingGame.Models.Game game = (RacingGame.Models.Game)Session["Game"];
            ViewData["UserID"]     = id;
            ViewData["PageNumber"] = pagenumber;
            ViewData["PageName"]   = pagename;

            //if ((int)Session["BidSubmitted"] != 1)
            //{
            ViewBag.Account = game.Account;

            //ViewBag.TimeSaved = game.TimeSaved;
            ViewBag.CurrentSection = game.CurrentSection;
            ViewBag.RoadSections   = game.RoadSections;

            ViewBag.FreewaySpeed = game.SpeedSet[game.CurrentSection - 1].VelocityFreeway;

            ViewBag.HighwaySpeed = game.SpeedSet[game.CurrentSection - 1].VelocityHighway;

            if (!ModelState.IsValidField("Bid"))
            {
                ModelState.AddModelError("Bid", "Please enter a Bid");
            }

            if (model.Bid > game.Account)
            {
                ModelState.AddModelError("Bid", string.Format("Please enter a number between 0 and {0}.", game.Account));
                return(View("Bid"));
            }

            if (!ModelState.IsValid)
            {
                if (model.Bid > game.Account)
                {
                    ModelState.AddModelError("Bid", string.Format("Please enter a number between 0 and {0}.", game.Account));
                }

                return(View("Bid"));
            }
            else
            {
                TempData["BidModel"] = model;
                return(RedirectToAction("BidResult", new { id = id, pagenumber = pagenumber, pagename = "BidResult" }));
            }
            //}
            //else
            //{
            //    return RedirectToAction("PageAlreadyVisited",
            //                            new RouteValueDictionary(new { controller = "Game", action = "PageAlreadyVisited", Id = id })
            //                           );
            //}


            //}
        }
Пример #2
0
        public ActionResult BidResult(string id, int?pagenumber, string pagename)
        {
            RacingGame.Models.Game game    = (RacingGame.Models.Game)Session["Game"];
            double     sectionCoverageTime = 0;
            double     timeSaved           = 0.0;
            ViewResult result = null;// new ViewResult();

            ViewData["UserID"]     = id;
            ViewData["PageNumber"] = pagenumber + 1;
            BidClass model = (BidClass)TempData["BidModel"];

            if (!IsVisitedPage(id, (int)pagenumber, pagename))
            {
                AddStateRecordToDB(id, (int)pagenumber, pagename, true);
                //Need to calculte a random number and compare it to the gamer bid
                int userBid = model.Bid;
                ViewBag.UserBid = userBid;

                int randomCost = RandomNumberGenerator.Next();
                ViewBag.RandomTollCost = randomCost;

                if (userBid < randomCost)
                {
                    //go by free way
                    //reduce time by the following calculation :
                    //time passed = section_length / freeway_speed;
                    //new time left = old time - time passed
                    sectionCoverageTime = Math.Round((10.0 / game.SpeedSet[game.CurrentSection - 1].VelocityFreeway) * 60, 2);

                    //account stays the same
                }
                else
                {
                    //go by highway
                    //reduce time by the following calculation :
                    //time passed = section_length / highway_speed;
                    //new time left = old time - time passed
                    //account = old_account - highway cost

                    sectionCoverageTime = Math.Round((10.0 / game.SpeedSet[game.CurrentSection - 1].VelocityHighway) * 60, 2);

                    //calculating time saved
                    //time to drive on freeway = 10 / game.SpeedSet[game.CurrentSection - 1].VelocityFreeway
                    double freewayTime = Math.Round((double)(10.0 / game.SpeedSet[game.CurrentSection - 1].VelocityFreeway) * 60, 2);

                    //time to drive on highway = 10 / game.SpeedSet[game.CurrentSection - 1].VelocityHighway
                    double highwayTime = Math.Round((double)((10.0 / game.SpeedSet[game.CurrentSection - 1].VelocityHighway) * 60), 2);
                    timeSaved = freewayTime - highwayTime;

                    game.TimeSaved += timeSaved;
                    game.Account    = game.Account - randomCost;
                }

                game.TimePassed            += sectionCoverageTime;
                ViewBag.SectionCoverageTime = sectionCoverageTime;
                ViewBag.FreewaySpeed        = game.SpeedSet[game.CurrentSection - 1].VelocityFreeway;
                ViewBag.HighwaySpeed        = game.SpeedSet[game.CurrentSection - 1].VelocityHighway;
                ViewBag.Account             = game.Account;
                ViewBag.CurrentSection      = game.CurrentSection;
                ViewBag.RoadSections        = game.RoadSections;

                Session["Game"] = game;
                //string userID = (string)Session["UserID"];

                game.GamePlays.Add(new GamePlay()
                {
                    UserID          = id,
                    Section         = game.CurrentSection,
                    FreewayVelocity = game.SpeedSet[game.CurrentSection - 1].VelocityFreeway,
                    TollwayVelocity = game.SpeedSet[game.CurrentSection - 1].VelocityHighway,
                    PriceSubject    = userBid,
                    PriceRandom     = randomCost,
                    CurrentAccount  = game.Account,
                    TimeSaved       = timeSaved
                });

                #region Write play to DB every play
                game.AddRecordToDB(id,
                                   game.CurrentSection,
                                   game.SpeedSet[game.CurrentSection - 1].VelocityFreeway,
                                   game.SpeedSet[game.CurrentSection - 1].VelocityHighway,
                                   userBid,
                                   randomCost,
                                   game.Account,
                                   timeSaved);
                #endregion

                game.CurrentSection++;

                if (game.CurrentSection <= game.RoadSections && game.Account > 0)
                {
                    //Go to Bid page
                    result = View();
                }
                else
                {
                    //Go to game end;
                    if (game.Account <= 0)
                    {
                        //in this case, the game ended because the user lost all money
                        //need to calculate how long it took the user to ride by the freeway

                        double time           = 0;
                        int    currentSection = game.CurrentSection + 1;

                        if (currentSection < game.GameSections)
                        {
                            while (currentSection < game.RoadSections)
                            {
                                time += 10 / game.SpeedSet[currentSection - 1].VelocityFreeway;
                                game.AddRecordToDB(id,
                                                   game.CurrentSection,
                                                   game.SpeedSet[game.CurrentSection - 1].VelocityFreeway,
                                                   game.SpeedSet[game.CurrentSection - 1].VelocityHighway,
                                                   0,
                                                   0,
                                                   0,
                                                   0);

                                currentSection++;
                            }
                        }

                        game.TimePassed += time;
                    }

                    ViewBag.MoneySaved      = game.Account;
                    ViewBag.MoneySpent      = game.StartingAccount - game.Account;
                    ViewBag.Sections        = game.GameSections;
                    ViewBag.StartingAccount = game.StartingAccount;
                    ViewBag.StartingTime    = game.StartingTime;
                    @ViewBag.TimeSaved      = game.TimeSaved;

                    double moneyTimeRatio = 0;
                    if (game.StartingAccount - game.Account > 0)
                    {
                        moneyTimeRatio = Math.Round(game.TimeSaved / (game.StartingAccount - game.Account), 2);
                    }
                    else
                    {
                        moneyTimeRatio = Math.Round(game.TimeSaved / (game.StartingAccount), 2);
                    }

                    ViewBag.MoneyTimeRatio = moneyTimeRatio;
                    ViewBag.Bonus          = moneyTimeRatio * 0.5;

                    List <GamePlay> gamePlays = game.GetGamePlaysForUser(id);

                    result = View("GameEnd", gamePlays);
                }
            }
            else
            {
                return(RedirectToAction("PageAlreadyVisited",
                                        new { id = id, pagenumber = pagenumber }
                                        ));
            }

            return(result);
        }