def join_tournament(tournament_id: TournamentId, bot_id: BotId, user_id: UserId = Security(get_user_id)): try: tournament = Tournament.get(tournament_id) bot = Bot.get(bot_id) except InstanceNotFound as e: logger.warning(f"Failed to add bot to tournament\n{e!r}") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) if bot.user.id != user_id: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) try: return add_player(bot, tournament) except AlreadyInTournament as e: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Already in tournament") from e
def add_player(bot: Bot, tournament: Tournament) -> Participant: logger.debug("Getting tournament lock: %s", tournament.id) with tournament.lock(): logger.debug("Got lock for tournament: %s", tournament.id) participants = tournament.participants participant_ids = {participant.bot.id for participant in participants} if bot.id in participant_ids: raise AlreadyInTournament index = max(x.index for x in participants) + 1 if participants else 1 participant = Participant.create(index=index, bot=bot, tournament=tournament) for opponent in participants: if opponent.disqualified: continue Match.create( index=100000 * index + opponent.index, tournament=tournament, players=[participant, opponent], state=MatchState.pending, ) return participant
def pick_match(tournament: Tournament) -> Match: with tournament.lock(): queued_players = PlayerQueueEntry.list(tournament_id=tournament.id) participants_by_id = { participant.id: participant for participant in tournament.participants } participant_sids: Dict[Participant, List[PlayerSID]] = defaultdict( default_factory=list) for player in queued_players: participant = participants_by_id[player.participant_id] participant_sids[participant].append(player.sid) online_participants = set(participant_sids) matches = [ match for match in tournament.matches if match.state == MatchState.pending and not set(match.players) - online_participants ] sorted_matches = sorted(matches, key=operator.attrgetter("index")) match = sorted_matches[0] match.state = MatchState.running match.save() return match
def admin_get_tournament(tournament_id: TournamentId): return Tournament.get(tournament_id)
def create_tournament(data: CreateTournamentSchema): return Tournament.create(name=data.name, description=data.description, game=data.game)
def get_tournament(tournament_id: TournamentId): return Tournament.get(tournament_id)
def list_tournaments(): return Tournament.list()