Esempio n. 1
0
        //id == orderNumber
        public ActionResult Game(int id)
        {
            string userID = User.Identity.GetUserId();

            var userActiveEvent = _userService.UserActiveEvent(userID);
            var validateEvent   = _eventService.GetEventById(id);

            //Event doesnt exist , its finished or it is not started yet
            if (validateEvent == null || validateEvent.End < DateHelper.GetCurrentTime() || validateEvent.Start > DateHelper.GetCurrentTime())
            {
                return(RedirectToAction("Start"));
            }

            //Has no active events start a new one
            if (userActiveEvent == null)
            {
                EventSnippets firstSnippet = _userService.BeginEvent(User.Identity.GetUserId(), validateEvent.ID);

                //Already participated and finished
                if (firstSnippet == null)
                {
                    TempData["EventFinished"] = true;
                    return(RedirectToAction("Result"));
                }
                _snippetService.CreateInitialAnswer(userID, firstSnippet.EventID, firstSnippet.SnippetID);
                userActiveEvent         = _userService.UserActiveEvent(userID);
                ViewBag.lastOrderNumber = userActiveEvent.OrderNumber;
                firstSnippet.Event      = validateEvent;
                return(View(firstSnippet));
                //return view with first snippet
            }

            //Has active event but wants to start another one
            if (userActiveEvent.EventID != validateEvent.ID)
            {
                // should redirect to lobby of the event
                TempData["RedirectToLobbyMsg"] = true;
                return(RedirectToAction("Lobby", new { id = userActiveEvent.EventID }));
            }

            int lastAnsweredOrderNumber = _snippetService.GetLastAnsweredSnippetOrderNumber(userID, userActiveEvent.EventID);

            if (++lastAnsweredOrderNumber > userActiveEvent.OrderNumber)
            {
                _eventService.FinishEventForUser(userActiveEvent.EventID, userID);
                TempData["EventFinished"] = true;
                return(RedirectToAction("Result"));
            }


            var model = _snippetService.GetSnippetWithOrderNumber(lastAnsweredOrderNumber, userActiveEvent.EventID);

            _snippetService.CreateInitialAnswer(userID, userActiveEvent.EventID, model.SnippetID);
            ViewBag.lastOrderNumber = userActiveEvent.OrderNumber;

            model.Event = validateEvent;

            return(View(model));
        }