Пример #1
0
 def _merge_tribes(self, tribe1: Tribe, tribe2: Tribe, new_tribe_name: Text,
                   gamedb: Database, engine: Engine) -> Tribe:
     log_message(message=f"Merging tribes into {new_tribe_name}.")
     with engine:
         new_tribe = gamedb.tribe(name=new_tribe_name)
         gamedb.batch_update_tribe(from_tribe=tribe1, to_tribe=new_tribe)
         gamedb.batch_update_tribe(from_tribe=tribe2, to_tribe=new_tribe)
         # after tribes merge, sweep the teams to ensure no size of 2
         self._merge_teams(target_team_size=self._options.target_team_size,
                           tribe=new_tribe,
                           gamedb=gamedb,
                           engine=engine)
         game = gamedb.game_from_id(gamedb.get_game_id())
         game.count_tribes = 1
         gamedb.save(game)
         return new_tribe
Пример #2
0
 def messages(self, gamedb: Database) -> List[SMSEventMessage]:
     return [
         SMSEventMessage(
             content=messages.NOTIFY_FINAL_TRIBAL_COUNCIL_EVENT_MSG_FMT.format(
                 header=messages.VIR_US_HOSTNAME,
                 players=len(self.finalists),
                 game=gamedb.game_from_id(id=self.game_id).hashtag,
                 time=self.game_options.game_schedule.localized_time_string(
                     self.game_options.game_schedule.daily_tribal_council_end_time
                 ),
                 options=messages.players_as_formatted_options_list(
                     players=self.finalists)
             ),
             recipient_phone_numbers=[p.phone_number for p in gamedb.stream_players(
                 active_player_predicate_value=True)]
         )
     ]
Пример #3
0
 def messages(self, gamedb: Database) -> List[SMSEventMessage]:
     game_hashtag = gamedb.game_from_id(id=self.game_id).hashtag
     return [
         SMSEventMessage(
             content=messages.
             NOTIFY_WINNER_ANNOUNCEMENT_EVENT_WINNER_MSG_FMT.format(
                 header=messages.game_sms_header(gamedb=gamedb),
                 game=game_hashtag),
             recipient_phone_numbers=[self.winner.phone_number]),
         SMSEventMessage(
             content=messages.
             NOTIFY_WINNER_ANNOUNCEMENT_EVENT_GENERAL_MSG_FMT.format(
                 header=messages.game_sms_header(gamedb=gamedb),
                 player=messages.format_tiktok_username(self.winner.tiktok),
                 game=game_hashtag),
             recipient_phone_numbers=[
                 p.phone_number for p in gamedb.stream_players(
                     active_player_predicate_value=False)
             ])
     ]
Пример #4
0
 def messages(self, gamedb: Database) -> List[SMSEventMessage]:
     options_map = messages.players_as_formatted_options_map(
         players=self.finalists)
     # TODO(brandon): this is slow and expensive, but it should work.
     for player in gamedb.stream_players():
         gamedb.ballot(player_id=player.id,
                       challenge_id=None,
                       options=options_map.options,
                       is_for_win=True)
     return [
         SMSEventMessage(
             content=messages.NOTIFY_FINAL_TRIBAL_COUNCIL_EVENT_MSG_FMT.
             format(
                 header=messages.game_sms_header(gamedb=gamedb),
                 players=len(self.finalists),
                 game=gamedb.game_from_id(id=self.game_id).hashtag,
                 time=self.game_options.game_schedule.localized_time_string(
                     self.game_options.game_schedule.
                     daily_tribal_council_end_time),
                 options=options_map.formatted_string),
             recipient_phone_numbers=[
                 p.phone_number for p in gamedb.stream_players()
             ])
     ]
Пример #5
0
def game_sms_header(gamedb: Database = None, hashtag: str = None) -> str:
    if gamedb:
        game = gamedb.game_from_id(gamedb.get_game_id())
        hashtag = game.hashtag
    return f'{hashtag}\ntiktok.com/tag/{hashtag}'
Пример #6
0
    def generate_tribes(cls, game_id: str, players: List[DocumentSnapshot],
                        game_options: GameOptions, gamedb: Database) -> dict:
        tribes = list()
        teams = list()
        count_players = len(players)
        if count_players < game_options.target_team_size:
            raise MatchMakerError("Insufficient players for given team size")
        if count_players < game_options.multi_tribe_min_tribe_size * 2:
            raise MatchMakerError("Insufficient players to make two tribes")
        # generate tribes
        for tribe_name in [
                _DEFAULT_TRIBE_NAMES[int(n)]
                for n in random.sample(range(0, len(_DEFAULT_TRIBE_NAMES)), 2)
        ]:
            tribe = database.Tribe(id=str(uuid.uuid4()),
                                   name="{}".format(tribe_name))
            tribes.append(tribe)

        # generate teams
        for n in range(
                0, math.floor(count_players / game_options.target_team_size)):
            team = database.Team(
                id=str(uuid.uuid4()),
                name="{}".format(n),
            )
            teams.append(team)

        count_tribes = len(tribes)
        count_teams = len(teams)
        team_to_tribe_map = {}

        # randomly assign team, tribe to each player
        set_of_mutable_players = set()
        set_of_mutable_users = set()
        for n, player in enumerate(players):
            mutable_user = gamedb.find_user(
                phone_number=player.get('phone_number'))
            mutable_user.game_id = game_id
            mutable_player = gamedb.player_from_id(player.id)
            tribe = tribes[n % count_tribes]
            team = teams[n % count_teams]
            if team.id not in team_to_tribe_map:
                team_to_tribe_map[team.id] = tribe
                tribe.count_teams += 1
            mutable_player.tribe_id = tribe.id
            mutable_player.team_id = team.id
            team.tribe_id = tribe.id
            tribe.count_players += 1
            team.count_players += 1
            set_of_mutable_players.add(mutable_player)
            set_of_mutable_users.add(mutable_user)

        # Save data
        game = gamedb.game_from_id(game_id)
        game.count_tribes = count_tribes
        game.count_teams = count_teams
        game.count_players = count_players
        gamedb.save(game)

        for tribe in tribes:
            gamedb.save(tribe)
        for team in teams:
            gamedb.save(team)
        for player in set_of_mutable_players:
            gamedb.save(player)
        for user in set_of_mutable_users:
            gamedb.save(user)

        d = {}
        d['players'] = players
        d['teams'] = teams
        d['tribes'] = tribes
        d['game'] = game
        return d