Esempio n. 1
0
        public bool Add(MatchUserScoreModel model)
        {
            var score = _db.MatchUserScores.FirstOrDefault(x => x.MatchId == model.MatchId && x.UserId == model.UserId);

            if (score == null)
            {
                var newScore = new MatchUserScore()
                {
                    UserId  = model.UserId,
                    MatchId = model.MatchId,
                    Score   = model.Score
                };
                _db.MatchUserScores.Add(newScore);
                _db.SaveChanges();
            }
            else
            {
                score.Score            = model.Score;
                _db.Entry(score).State = EntityState.Modified;
                _db.SaveChanges();
            }

            return(true);
        }
        public ActionResult GenerateScore(int matchId)
        {
            if (!db.MatchQuestions.Any(x => x.MatchId == matchId))
            {
                ViewBag.ErrorMessage = "No Questions Available";
                ViewBag.matchId      = new SelectList(db.Matches, "MatchId", "Number");
                return(View());
            }

            if (db.MatchQuestions.Any(x => x.MatchId == matchId && x.Answer == null))
            {
                ViewBag.ErrorMessage = "Question answers are not filled";
                ViewBag.matchId      = new SelectList(db.Matches, "MatchId", "Number");
                return(View());
            }

            var categories = db.MatchQuestions.Where(x => x.MatchId == matchId).Select(y => y.PredictionCategoryId)
                             .Distinct().ToList();

            var matchAnswers = (from pc in db.PredictionCategories
                                where categories.Contains(pc.PredictionCategoryId)
                                select new MatchQuestionAnswerModel()
            {
                BonusTimes = pc.BonusTimes,
                CategoryTypeId = pc.CategoryTypeId,
                LossPoints = pc.LossPoints,
                WinPoints = pc.WinPoints,
                Answers = (from mq in db.MatchQuestions
                           where mq.MatchId == matchId && mq.PredictionCategoryId == pc.PredictionCategoryId
                           select new QuestionAnswerModel()
                {
                    Answer = mq.Answer ?? 0,
                    MatchQuestionId = mq.MatchQuestionId
                }).ToList()
            }).ToList();



            var userAnswer = (from mua in db.MatchUserAnswers
                              where mua.MatchQuestion.MatchId == matchId
                              select mua).ToList();

            var users = userAnswer.Select(x => x.UserId).Distinct().ToList();

            var scoreService = new PredictionScoreCalculation();

            foreach (var user in users)
            {
                var matchUserAnswers = (from ua in userAnswer
                                        where ua.UserId == user
                                        select new QuestionAnswerModel()
                {
                    MatchQuestionId = ua.MatchQuestionId,
                    Answer = ua.Answer
                }).ToList();

                int userScore = scoreService.GetUserMatchScore(matchAnswers, matchUserAnswers);

                var matchUserScore = new MatchUserScore()
                {
                    MatchId = matchId,
                    UserId  = user,
                    Score   = userScore
                };

                db.MatchUserScores.Add(matchUserScore);
            }

            db.SaveChanges();

            return(RedirectToAction("Index", new
            {
                matchId = matchId
            }));
        }