예제 #1
0
class TroopLoader(object):
    def __init__(
        self,
        config: Config,
        simulation: TileStrategySimulation,
    ) -> None:
        self._logger = get_logger('TroopLoader', config)
        self._config = config
        self._simulation = simulation

        schema_file_path = self._config.get(
            'global.troop_schema',
            'opencombat/strategy/troops.xsd',
        )
        self._xml_validator = XmlValidator(
            config,
            schema_file_path,
        )

    def get_troop(
        self,
        troop_file_path: str,
    ) -> Troop:
        return Troop(
            self._config,
            self._validate_and_return_state_element(troop_file_path),
            self._simulation,
        )

    def _validate_and_return_state_element(
        self,
        troop_file_path: str,
    ) -> Element:
        return self._xml_validator.validate_and_return(troop_file_path)
예제 #2
0
class StateLoader(object):
    def __init__(
        self,
        config: Config,
        simulation: TileStrategySimulation,
    ) -> None:
        self._logger = get_logger('StateLoader', config)
        self._config = config
        self._simulation = simulation

        schema_file_path = self._config.get(
            'global.state_schema',
            'opencombat/state.xsd',
        )
        self._xml_validator = XmlValidator(
            config,
            schema_file_path,
        )

    def get_state(
        self,
        state_file_path: str,
    ) -> State:
        return State(
            self._config,
            self._validate_and_return_state_element(state_file_path),
            self._simulation,
        )

    def _validate_and_return_state_element(
        self,
        state_file_path: str,
    ) -> Element:
        return self._xml_validator.validate_and_return(state_file_path)
예제 #3
0
    def __init__(
        self,
        config: Config,
        simulation: TileStrategySimulation,
    ) -> None:
        self._logger = get_logger('TroopLoader', config)
        self._config = config
        self._simulation = simulation

        schema_file_path = self._config.get(
            'global.troop_schema',
            'opencombat/strategy/troops.xsd',
        )
        self._xml_validator = XmlValidator(
            config,
            schema_file_path,
        )
예제 #4
0
    def __init__(
        self,
        config: Config,
        units_file_path: str,
    ) -> None:
        self._config = config
        self._units = None  # type: typing.List[UnitModel]

        self.schema_file_path = self._config.get(
            'global.teams_schema',
            'opencombat/strategy/units.xsd',
        )
        self._xml_validator = XmlValidator(
            config,
            self.schema_file_path,
        )
        self._root_element = self._xml_validator.validate_and_return(
            units_file_path, )
예제 #5
0
class TeamStash(object):
    def __init__(
        self,
        config: Config,
        teams_file_path: str,
        unit_stash: UnitStash,
    ) -> None:
        self._config = config
        self._teams = None  # type: typing.List[TeamModel]
        self._unit_stash = unit_stash
        self._teams_file_path = teams_file_path

        self.schema_file_path = self._config.get(
            'global.teams_schema',
            'opencombat/strategy/teams.xsd',
        )
        self._xml_validator = XmlValidator(
            config,
            self.schema_file_path,
        )
        self._root_element = self._xml_validator.validate_and_return(
            self._teams_file_path, )

    def _get_computed_teams(self) -> typing.List[TeamModel]:
        teams = []

        for team_element in self._root_element.findall('team'):
            team_element = typing.cast(Element, team_element)

            team_id = team_element.attrib['id']
            team_country = team_element.attrib['country']
            team_name = get_text_xml_element(team_element, 'name')
            team_units = []

            units_element = team_element.find('units')
            for unit_element in units_element.findall('unit'):
                unit_id = get_text_xml_element(unit_element, 'id')
                unit = self._unit_stash.get_unit(unit_id, team_country)
                team_units.append(unit)

            teams.append(
                TeamModel(id_=team_id,
                          country=team_country,
                          name=team_name,
                          units=team_units))

        return teams

    @property
    def teams(self) -> typing.List[TeamModel]:
        if self._teams is None:
            self._teams = self._get_computed_teams()

        return self._teams

    def get_team(
        self,
        team_id: str,
        team_country: str,
    ) -> TeamModel:
        for team in self.teams:
            if team.id == team_id and team.country == team_country:
                return team

        raise NotFoundException(
            'No team matching with id "{}" and country "{}" in "{}"'.format(
                team_id,
                team_country,
                self.schema_file_path,
            ))

    def get_team_by_name(
        self,
        team_name: str,
        team_country: str,
    ) -> TeamModel:
        for team in self.teams:
            if team.name == team_name and team.country == team_country:
                return team

        raise NotFoundException(
            'No team matching with name "{}" and country "{}" in "{}"'.format(
                team_name,
                team_country,
                self.schema_file_path,
            ))

    def get_team_by_country(self, country: str) -> typing.List[TeamModel]:
        teams = []  # type: typing.List[TeamModel]

        for team in self.teams:
            if team.country == country:
                teams.append(team)

        return teams
예제 #6
0
class UnitStash(object):
    def __init__(
        self,
        config: Config,
        units_file_path: str,
    ) -> None:
        self._config = config
        self._units = None  # type: typing.List[UnitModel]

        self.schema_file_path = self._config.get(
            'global.teams_schema',
            'opencombat/strategy/units.xsd',
        )
        self._xml_validator = XmlValidator(
            config,
            self.schema_file_path,
        )
        self._root_element = self._xml_validator.validate_and_return(
            units_file_path, )

    def _get_computed_units(self) -> typing.List[UnitModel]:
        units = []

        for unit_element in self._root_element.findall('unit'):
            unit_element = typing.cast(Element, unit_element)

            unit_id = unit_element.attrib['id']
            unit_country = unit_element.attrib['country']
            unit_name = get_text_xml_element(unit_element, 'name')
            unit_class_path = get_text_xml_element(unit_element, 'type')
            unit_class = get_class_from_string_path(
                self._config,
                unit_class_path,
            )

            units.append(
                UnitModel(
                    id_=unit_id,
                    name=unit_name,
                    class_=unit_class,
                    country=unit_country,
                ))

        return units

    @property
    def units(self) -> typing.List[UnitModel]:
        if self._units is None:
            self._units = self._get_computed_units()

        return self._units

    def get_unit(
        self,
        unit_id: str,
        unit_country: str,
    ) -> UnitModel:
        for unit in self.units:
            if unit.id == unit_id and unit.country == unit_country:
                return unit

        raise NotFoundException(
            'No unit matching with id "{}" and country "{}" in "{}"'.format(
                unit_id,
                unit_country,
                self.schema_file_path,
            ))