def _play_single_tribe(self, tribe: Tribe, gamedb: Database, engine: Engine) -> Team: while gamedb.count_teams(active_team_predicate_value=True) > 1: log_message("Teams remaining = {}.".format( gamedb.count_teams(active_team_predicate_value=True))) log_message("Getting new challenge.") challenge = self._get_challenge(gamedb=gamedb) log_message("Running challenge {}.".format(challenge)) self._run_challenge(challenge=challenge, gamedb=gamedb, engine=engine) log_message("Scoring entries.") winning_teams, losing_teams = self._score_entries_top_k_teams(k=self._options.single_tribe_top_k_threshold, tribe=tribe, challenge=challenge, gamedb=gamedb, engine=engine) log_message("Running single tribe council.") self._run_single_tribe_council(winning_teams=winning_teams, losing_teams=losing_teams, gamedb=gamedb, engine=engine) log_message("Merging teams.") self._merge_teams(target_team_size=self._options.target_team_size, tribe=tribe, gamedb=gamedb, engine=engine) return gamedb.list_teams(active_team_predicate_value=True)[0]
def _merge_teams(self, target_team_size: int, tribe: Tribe, gamedb: Database, engine: Engine): # team merging is only necessary when the size of the team == 2 # once a team size == 2, it should be merged with another team. the optimal # choice is to keep team sizes as close to the intended size as possible # find all teams with size = 2, these players need to be merged small_teams = gamedb.stream_teams( from_tribe=tribe, team_size_predicate_value=2) merge_candidates = Queue() for team in small_teams: log_message("Found team of 2. Deacticating team {}.".format(team)) # do not deactivate the last active team in the tribe if gamedb.count_teams(from_tribe=tribe, active_team_predicate_value=True) > 1: gamedb.deactivate_team(team) for player in gamedb.list_players(from_team=team): log_message("Adding merge candidate {}.".format(player)) merge_candidates.put(player) sorted_teams = gamedb.stream_teams( from_tribe=tribe, order_by_size=True, descending=False) log_message("Redistributing merge candidates...") # round robin redistribution strategy # simplest case, could use more thought. visited = {} while not merge_candidates.empty() and sorted_teams: for team in sorted_teams: other_options_available = team.id not in visited visited[team.id] = True if (team.size >= target_team_size and other_options_available): log_message("Team {} has size >= target {} and other options are available. " "Continuing search...".format(team, target_team_size)) continue player = merge_candidates.get() if player.team_id == team.id: continue log_message("Merging player {} from team {} into team {}.".format( player, player.team_id, team.id)) player.team_id = team.id team.size = team.size + 1 gamedb.save(team) gamedb.save(player) # notify player of new team assignment engine.add_event(events.NotifyTeamReassignmentEvent(game_id=self._game_id, game_options=self._options, player=player, team=team))
def _merge_teams(self, target_team_size: int, tribe: Tribe, gamedb: Database, engine: Engine): with engine: # team merging is only necessary when the size of the team == 2 # once a team size == 2, it should be merged with another team, because # a self preservation vote lands in a deadlock. in general, the optimal # choice is to keep team sizes as close to the intended size as possible # up until a merge becomes necessary. # find all teams with size == 2, these players need to be merged small_teams = gamedb.stream_teams(from_tribe=tribe, team_size_predicate_value=2) merge_candidates = Queue() for team in small_teams: # do not deactivate the last active team in the tribe count_teams = gamedb.count_teams( from_tribe=tribe, active_team_predicate_value=True) if count_teams > 1: log_message( message="Found team of 2. Deactivating team {}.". format(team), game_id=self._game_id) gamedb.deactivate_team(team) for player in gamedb.list_players(from_team=team): log_message( message="Adding merge candidate {}.".format(player), game_id=self._game_id) merge_candidates.put(player) sorted_teams = gamedb.stream_teams(from_tribe=tribe, order_by_size=True, descending=False) log_message(message="Redistributing merge candidates...", game_id=self._game_id) # round robin redistribution strategy # simplest case, could use more thought. visited = {} while not merge_candidates.empty() and sorted_teams: for team in itertools.cycle(sorted_teams): team.count_players = _team_count_players(team, gamedb) other_options_available = team.id not in visited visited[team.id] = True if (team.count_players >= target_team_size and other_options_available): log_message( message= "Team {} has size >= target {} and other options are available. " "Continuing search...".format( team, target_team_size), game_id=self._game_id) continue player = None try: player = merge_candidates.get_nowait() except Empty: log_message(message="Merge candidates empty.") return if player.team_id == team.id: log_message( message= f"Player {str(player)} already on team {str(team)}. Continuing." ) continue log_message( message="Merging player {} from team {} into team {}.". format(player, player.team_id, team.id), game_id=self._game_id) player.team_id = team.id team.count_players += 1 gamedb.save(team) gamedb.save(player) # notify player of new team assignment engine.add_event( events.NotifyTeamReassignmentEvent( game_id=self._game_id, game_options=self._options, player=player, team=team))