Exemplo n.º 1
0
        public async Task <ActionResult> JoinMatch([FromBody] MatchRegistrationDto dto)
        {
            var userId = User.Claims.GetUserId();

            if (userId == null)
            {
                return(Forbid());
            }

            var match = await Context.Matches
                        .Include(o => o.Registrations)
                        .ThenInclude(o => o.Fighter)
                        .Where(o => o.Started == null)
                        .FirstOrDefaultAsync(o => o.Id == dto.MatchId);

            var fighter = await Context.Fighters
                          .Where(o => o.UserId == userId)
                          .FirstOrDefaultAsync(o => o.Id == dto.FighterId);

            if (match == null)
            {
                return(BadRequest(new InvalidMatchException()));
            }

            if (fighter == null)
            {
                return(BadRequest(new InvalidFighterException()));
            }

            if (match.Options.MaxFightersPerUser != null &&
                match.Registrations
                .Where(o => o.Fighter.UserId == userId)
                .Count() >= match.Options.MaxFightersPerUser)
            {
                return(BadRequest(new MatchFighterLimitExceededException()));
            }

            if (match.Options.MaxPowerlevel != null &&
                fighter.PowerLevel() > match.Options.MaxPowerlevel)
            {
                return(BadRequest(new IllegalFighterPowerlevelException()));
            }

            var registration = new MatchRegistration()
            {
                MatchId   = match.Id,
                FighterId = fighter.Id,
                Date      = dateTimeProvider.Now,
            };

            Context.MatchRegistrations.Add(registration);
            await Context.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <ActionResult> LeaveMatch([FromBody] MatchRegistrationDto dto)
        {
            var userId = User.Claims.GetUserId();

            if (userId == null)
            {
                return(Forbid());
            }

            var match = await Context.Matches
                        .Where(o => o.Started == null)
                        .FirstOrDefaultAsync(o => o.Id == dto.MatchId);

            var fighter = await Context.Fighters
                          .Where(o => o.UserId == userId)
                          .FirstOrDefaultAsync(o => o.Id == dto.FighterId);

            if (match == null)
            {
                return(BadRequest(new InvalidMatchException()));
            }

            if (fighter == null)
            {
                return(BadRequest(new InvalidFighterException()));
            }

            var registration = await Context.MatchRegistrations
                               .FirstOrDefaultAsync(o => o.FighterId == dto.FighterId && o.MatchId == dto.MatchId);

            if (registration == null)
            {
                return(BadRequest(new InvalidMatchRegistrationException()));
            }

            Context.MatchRegistrations.Remove(registration);
            await Context.SaveChangesAsync();

            return(Ok());
        }