コード例 #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());
        }
コード例 #2
0
        public async Task <ActionResult> JoinTeam([FromBody] MatchTeamOperationDto dto)
        {
            var userId = User.Claims.GetUserId();

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

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

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

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

            var team = await Context.MatchTeams
                       .FirstOrDefaultAsync(o => o.Id == dto.Id);

            if (team == null)
            {
                return(BadRequest(new InvalidTeamException()));
            }

            if (!team.IsValidPassword(dto.Password))
            {
                return(BadRequest(new InvalidTeamPasswordException()));
            }

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

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

            var registration = await Context.MatchRegistrations
                               .AsTracking()
                               .Include(o => o.Fighter)
                               .Where(o => o.Fighter.UserId == userId)
                               .Where(o => o.MatchId == match.Id)
                               .FirstOrDefaultAsync(o => o.FighterId == dto.FighterId);

            if (registration == null)
            {
                registration = new MatchRegistration()
                {
                    MatchId   = match.Id,
                    FighterId = fighter.Id,
                    Date      = dateTimeProvider.Now,
                };
                Context.MatchRegistrations.Add(registration);
            }

            if (registration.TeamId == team.Id)
            {
                return(Ok());
            }

            registration.TeamId = team.Id;

            await Context.SaveChangesAsync();

            return(Ok());
        }