Exemple #1
0
        public async Task <IActionResult> AddMatch(AddMatchModel addMatchModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var winnerCompetitor = await _db.Competitors.FindAsync(addMatchModel.WinnerCompetitor.CompetitorId);

            if (winnerCompetitor == null)
            {
                return(BadRequest("Winner competitor does not exist"));
            }
            var losingCompetitor = await _db.Competitors.FindAsync(addMatchModel.LosingCompetitor.CompetitorId);

            if (losingCompetitor == null)
            {
                return(BadRequest("Losing competitor does not exist"));
            }

            winnerCompetitor.Wins   += 1;
            winnerCompetitor.Wallet += addMatchModel.WinnerCompetitor.TrickShots + (addMatchModel.WinnerCompetitor.CalledTrickShots * 5) - addMatchModel.WinnerCompetitor.Fouls;
            losingCompetitor.Losses += 1;
            losingCompetitor.Wallet += addMatchModel.LosingCompetitor.TrickShots + (addMatchModel.LosingCompetitor.CalledTrickShots * 5) - addMatchModel.LosingCompetitor.Fouls;

            var currentFoulPot = (await _db.Matches.OrderByDescending(x => x.Timestamp).FirstOrDefaultAsync())?.Stake;

            var newMatch = new Match()
            {
                Stake          = addMatchModel.Stake,
                CurrentFoulPot = addMatchModel.LosingCompetitor.Fouls + addMatchModel.WinnerCompetitor.Fouls
            };

            _db.Competitors.Update(winnerCompetitor);
            _db.Competitors.Update(losingCompetitor);
            await _db.AddAsync(newMatch);

            await _db.SaveChangesAsync();

            var winnerMatchCompetitor = new MatchCompetitor()
            {
                CompetitorId = addMatchModel.WinnerCompetitor.CompetitorId, MatchId = newMatch.Id
            };
            var loserMatchCompetitor = new MatchCompetitor()
            {
                CompetitorId = addMatchModel.LosingCompetitor.CompetitorId, MatchId = newMatch.Id
            };

            await _db.AddRangeAsync(new { winnerMatchCompetitor, loserMatchCompetitor });

            await _db.SaveChangesAsync();

            return(Ok(newMatch.Id));
        }
Exemple #2
0
        public ActionResult AddMatch()
        {
            List <Team>       teams       = DatabaseManager.GetAllTeam();
            List <Tournament> tournaments = DatabaseManager.GetAllTournament();

            AddMatchModel model = new AddMatchModel();

            model.Teams       = teams.Select(t => ModelHelper.CreateTeamModel(t)).ToList();
            model.Tournaments = tournaments.Select(t => ModelHelper.CreateTournamentModel(t)).ToList();
            model.Date        = DateTime.Now.AddDays(27);
            model.Type        = MatchType.ThirdRound;

            return(View(model));
        }
Exemple #3
0
        public ActionResult AddMatch(AddMatchModel model)
        {
            DatabaseManager.AddMatch(model.HomeTeamSelectedId, model.AwayTeamSelectedId, model.TournamentSelectedId, model.Date, model.Type);

            return(Redirect("/admin/addmatch"));
        }