예제 #1
0
        public ActionResult PlayerMainContent()
        {
            var context = this.DB.Contexts.First();
            var playerID = this.User.Identity.UserId();
            var questionID = this.DB.Contexts.First().CurrentQuestionID;
            var ansewer = this.DB.Answers.FirstOrDefault(a => a.PlayerID == playerID && a.QuestionID == questionID);
            if (ansewer == null)
            {
                ansewer = new Answer { PlayerID = playerID, QuestionID = questionID, ChoosedOptionIndex = -1 };
                this.DB.Answers.Add(ansewer);
                this.DB.SaveChanges();
            }

            return PartialView("PlayerMainContent_" + context.CurrentState.ToString(), this.DB);
        }
 /// <summary>
 /// Throws exception in case of concurrency on database
 /// </summary>
 private bool SelectAnswer(int answerId, int answerIndex, string playerId)
 {
     var context = DB.Contexts.First();
     if (context.CurrentState != ContextStateType.ChooseTheAnswer)
     {
         return false; // Illegal state: can not answer question in other state than ChooseTheAnswer
     }
     var questionId = context.CurrentQuestionId;
     Answer answer = null;
     if (answerId > 0)
     {
         // 1. By Id
         answer = DB.Answers.Find(answerId);
         if (answer != null
             && (answer.PlayerId != playerId
                 ||  answer.QuestionId != questionId))
         {                    
             return false; // you shouldn't answer in someone else's name, or to another question at this time
         }
     }
     if (answer == null)
     {
         // 2. By Unique business key
         answer = DB.Answers.FirstOrDefault(a => a.PlayerId == playerId && a.QuestionId == questionId);
     }
     if (answer == null)
     {
         // 3. adding new
         answer = new Answer { PlayerId = playerId, QuestionId = questionId, ChosenOptionIndex = -1, Status = AnswerStateType.NoEntry };
         DB.Answers.Add(answer);
     }
     answer.ChosenOptionIndex = answerIndex;
     answer.AssignedValue = 1;
     answer.Status = AnswerStateType.Pending; // entried, not validated so far, important for showing on dashboard
     DB.SaveChanges();
     return true;
 }