private async Task UpdateScoresAndResults(Match match, AddResultBindingModel resultModel)
        {
            foreach (var resultPrediction in match.MatchResultPredictions)
            {
                var user = await this.db
                           .Users
                           .FirstOrDefaultAsync(u => u.Id == resultPrediction.Prediction.OwnerId);

                if (resultPrediction.Result.Trim() == resultModel.Result.Trim())
                {
                    resultPrediction.Prediction.GuessedScores++;
                    user.TotalScore += GuessedScorePoints;
                }

                if (resultPrediction.WinnerSign.Trim() == resultModel.WinnerSign.Trim())
                {
                    resultPrediction.Prediction.GuessedResults++;
                    user.TotalScore += GuessedResultPoints;
                }

                this.db.Users.Update(user);
                this.db.Predictions.Update(resultPrediction.Prediction);
            }

            await this.db.SaveChangesAsync();
        }
        public async Task <IActionResult> Edit(int matchId, string result, string score)
        {
            var match = await this.ms.GetMatch(matchId);

            if (match == null)
            {
                this.TempData["WarningMsg"] = "Match you are trying to reach doesn't exist.";
                return(this.RedirectToAction("Index"));
            }

            var goalsArgs = score
                            .Split(":", StringSplitOptions.RemoveEmptyEntries)
                            .Select(int.Parse)
                            .ToArray();

            var model = new AddResultBindingModel
            {
                MatchId    = matchId,
                Result     = score,
                WinnerSign = (goalsArgs[0] > goalsArgs[1]) ? "1" : (goalsArgs[0] < goalsArgs[1] ? "2" : "x")
            };

            await this.ts.EditTeamsStatistics(match, score, result);

            await this.ps.EditUsersScores(match, score, result);

            await this.ms.UpdateMatchResults(model);

            return(this.RedirectToAction("Index"));
        }
        public async Task UpdateUsersScores(Group group, Match match, AddResultBindingModel resultModel)
        {
            await this.UpdateScoresAndResults(match, resultModel);

            if (group.MatchesPlayed == 12)
            {
                await this.UpdateRankingResults(group);
            }
        }
        public async Task <IActionResult> AddResult()
        {
            var model = new AddResultBindingModel()
            {
                AvailableGroups = this.SetAvailableGroups()
            };

            return(this.View(model));
        }
Exemple #5
0
        public async Task UpdateMatchResults(AddResultBindingModel model)
        {
            var match = await this.GetMatch(model.MatchId);

            match.Result     = model.Result;
            match.WinnerSign = model.WinnerSign.ToLower();

            this.db.Matches.Update(match);
            await this.db.SaveChangesAsync();
        }
        public async Task <IActionResult> AddResult(AddResultBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                model.AvailableGroups = this.SetAvailableGroups();

                return(this.View(model));
            }

            var group = await this.ts.GetGroupById(model.GroupId);

            var goalsArgs = model.Result
                            .Split(":", StringSplitOptions.RemoveEmptyEntries)
                            .Select(int.Parse)
                            .ToArray();

            model.WinnerSign = (goalsArgs[0] > goalsArgs[1]) ? "1" : (goalsArgs[0] < goalsArgs[1] ? "2" : "x");

            if (group.MatchesPlayed == 12)
            {
                model.AvailableGroups       = this.SetAvailableGroups();
                this.TempData["WarningMsg"] = $"All results for the games of this Group {group.Name} are already fixed.";

                return(this.View(model));
            }

            var match = await this.ms.GetMatch(model.MatchId);

            await this.ts.UpdateGroupMatchesCount(group);

            await this.ms.UpdateMatchResults(model);

            await this.ts.UpdateTeamsStatistics(match.HomeTeam, match.AwayTeam, model.Result, model.WinnerSign, group);

            await this.ps.UpdateUsersScores(group, match, model); // 3

            this.TempData["SuccessMsg"] = "Match result has been added successfully and users scores are updated.";

            return(this.RedirectToAction("Index"));
        }