def _calc_map_name(self, settings: dict) -> str:
     map_name: str
     if 'map' in settings:
         map_name = settings['map']
     else:
         # If settings doesn't specify a map, pick a random one from the
         # list of supported ones.
         unowned_maps = _map.get_unowned_maps()
         valid_maps: List[str] = [
             m for m in self.get_supported_maps(type(self.session))
             if m not in unowned_maps
         ]
         if not valid_maps:
             _ba.screenmessage(Lstr(resource='noValidMapsErrorText'))
             raise Exception('No valid maps')
         map_name = valid_maps[random.randrange(len(valid_maps))]
     return map_name
示例#2
0
def filter_playlist(playlist: PlaylistType,
                    sessiontype: Type[_session.Session],
                    add_resolved_type: bool = False,
                    remove_unowned: bool = True,
                    mark_unowned: bool = False) -> PlaylistType:
    """Return a filtered version of a playlist.

    Strips out or replaces invalid or unowned game types, makes sure all
    settings are present, and adds in a 'resolved_type' which is the actual
    type.
    """
    # pylint: disable=too-many-locals
    # pylint: disable=too-many-branches
    # pylint: disable=too-many-statements
    import _ba
    from ba import _map
    from ba import _general
    from ba import _gameactivity
    goodlist: List[Dict] = []
    unowned_maps: Sequence[str]
    if remove_unowned or mark_unowned:
        unowned_maps = _map.get_unowned_maps()
        unowned_game_types = _ba.app.meta.get_unowned_game_types()
    else:
        unowned_maps = []
        unowned_game_types = set()

    for entry in copy.deepcopy(playlist):
        # 'map' used to be called 'level' here.
        if 'level' in entry:
            entry['map'] = entry['level']
            del entry['level']

        # We now stuff map into settings instead of it being its own thing.
        if 'map' in entry:
            entry['settings']['map'] = entry['map']
            del entry['map']

        # Update old map names to new ones.
        entry['settings']['map'] = _map.get_filtered_map_name(
            entry['settings']['map'])
        if remove_unowned and entry['settings']['map'] in unowned_maps:
            continue

        # Ok, for each game in our list, try to import the module and grab
        # the actual game class. add successful ones to our initial list
        # to present to the user.
        if not isinstance(entry['type'], str):
            raise TypeError('invalid entry format')
        try:
            # Do some type filters for backwards compat.
            if entry['type'] in ('Assault.AssaultGame',
                                 'Happy_Thoughts.HappyThoughtsGame',
                                 'bsAssault.AssaultGame',
                                 'bs_assault.AssaultGame'):
                entry['type'] = 'bastd.game.assault.AssaultGame'
            if entry['type'] in ('King_of_the_Hill.KingOfTheHillGame',
                                 'bsKingOfTheHill.KingOfTheHillGame',
                                 'bs_king_of_the_hill.KingOfTheHillGame'):
                entry['type'] = 'bastd.game.kingofthehill.KingOfTheHillGame'
            if entry['type'] in ('Capture_the_Flag.CTFGame',
                                 'bsCaptureTheFlag.CTFGame',
                                 'bs_capture_the_flag.CTFGame'):
                entry['type'] = (
                    'bastd.game.capturetheflag.CaptureTheFlagGame')
            if entry['type'] in ('Death_Match.DeathMatchGame',
                                 'bsDeathMatch.DeathMatchGame',
                                 'bs_death_match.DeathMatchGame'):
                entry['type'] = 'bastd.game.deathmatch.DeathMatchGame'
            if entry['type'] in ('ChosenOne.ChosenOneGame',
                                 'bsChosenOne.ChosenOneGame',
                                 'bs_chosen_one.ChosenOneGame'):
                entry['type'] = 'bastd.game.chosenone.ChosenOneGame'
            if entry['type'] in ('Conquest.Conquest', 'Conquest.ConquestGame',
                                 'bsConquest.ConquestGame',
                                 'bs_conquest.ConquestGame'):
                entry['type'] = 'bastd.game.conquest.ConquestGame'
            if entry['type'] in ('Elimination.EliminationGame',
                                 'bsElimination.EliminationGame',
                                 'bs_elimination.EliminationGame'):
                entry['type'] = 'bastd.game.elimination.EliminationGame'
            if entry['type'] in ('Football.FootballGame',
                                 'bsFootball.FootballTeamGame',
                                 'bs_football.FootballTeamGame'):
                entry['type'] = 'bastd.game.football.FootballTeamGame'
            if entry['type'] in ('Hockey.HockeyGame', 'bsHockey.HockeyGame',
                                 'bs_hockey.HockeyGame'):
                entry['type'] = 'bastd.game.hockey.HockeyGame'
            if entry['type'] in ('Keep_Away.KeepAwayGame',
                                 'bsKeepAway.KeepAwayGame',
                                 'bs_keep_away.KeepAwayGame'):
                entry['type'] = 'bastd.game.keepaway.KeepAwayGame'
            if entry['type'] in ('Race.RaceGame', 'bsRace.RaceGame',
                                 'bs_race.RaceGame'):
                entry['type'] = 'bastd.game.race.RaceGame'
            if entry['type'] in ('bsEasterEggHunt.EasterEggHuntGame',
                                 'bs_easter_egg_hunt.EasterEggHuntGame'):
                entry['type'] = 'bastd.game.easteregghunt.EasterEggHuntGame'
            if entry['type'] in ('bsMeteorShower.MeteorShowerGame',
                                 'bs_meteor_shower.MeteorShowerGame'):
                entry['type'] = 'bastd.game.meteorshower.MeteorShowerGame'
            if entry['type'] in ('bsTargetPractice.TargetPracticeGame',
                                 'bs_target_practice.TargetPracticeGame'):
                entry['type'] = (
                    'bastd.game.targetpractice.TargetPracticeGame')

            gameclass = _general.getclass(entry['type'],
                                          _gameactivity.GameActivity)

            if remove_unowned and gameclass in unowned_game_types:
                continue
            if add_resolved_type:
                entry['resolved_type'] = gameclass
            if mark_unowned and entry['settings']['map'] in unowned_maps:
                entry['is_unowned_map'] = True
            if mark_unowned and gameclass in unowned_game_types:
                entry['is_unowned_game'] = True

            # Make sure all settings the game defines are present.
            neededsettings = gameclass.get_available_settings(sessiontype)
            for setting in neededsettings:
                if setting.name not in entry['settings']:
                    entry['settings'][setting.name] = setting.default
            goodlist.append(entry)
        except ImportError as exc:
            print(f'Import failed while scanning playlist: {exc}')
        except Exception:
            from ba import _error
            _error.print_exception()
    return goodlist