stash.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # coding: utf-8
  2. import typing
  3. from _elementtree import Element
  4. from synergine2.config import Config
  5. from opencombat.exception import NotFoundException
  6. from opencombat.strategy.team.model import TeamModel
  7. from opencombat.strategy.unit.stash import UnitStash
  8. from opencombat.util import get_text_xml_element
  9. from opencombat.xml import XmlValidator
  10. class TeamStash(object):
  11. def __init__(
  12. self,
  13. config: Config,
  14. teams_file_path: str,
  15. unit_stash: UnitStash,
  16. ) -> None:
  17. self._config = config
  18. self._teams = None # type: typing.List[TeamModel]
  19. self._unit_stash = unit_stash
  20. self._teams_file_path = teams_file_path
  21. self.schema_file_path = self._config.get(
  22. 'global.teams_schema',
  23. 'opencombat/strategy/teams.xsd',
  24. )
  25. self._xml_validator = XmlValidator(
  26. config,
  27. self.schema_file_path,
  28. )
  29. self._root_element = self._xml_validator.validate_and_return(
  30. self._teams_file_path,
  31. )
  32. def _get_computed_teams(self) -> typing.List[TeamModel]:
  33. teams = []
  34. for team_element in self._root_element.findall('team'):
  35. team_element = typing.cast(Element, team_element)
  36. team_id = team_element.attrib['id']
  37. team_country = team_element.attrib['country']
  38. team_name = get_text_xml_element(team_element, 'name')
  39. team_units = []
  40. units_element = team_element.find('units')
  41. for unit_element in units_element.findall('unit'):
  42. unit_id = get_text_xml_element(unit_element, 'id')
  43. unit = self._unit_stash.get_unit(unit_id, team_country)
  44. team_units.append(unit)
  45. teams.append(
  46. TeamModel(
  47. id_=team_id,
  48. country=team_country,
  49. name=team_name,
  50. units=team_units
  51. )
  52. )
  53. return teams
  54. @property
  55. def teams(self) -> typing.List[TeamModel]:
  56. if self._teams is None:
  57. self._teams = self._get_computed_teams()
  58. return self._teams
  59. def get_team(
  60. self,
  61. team_id: str,
  62. team_country: str,
  63. ) -> TeamModel:
  64. for team in self.teams:
  65. if team.id == team_id and team.country == team_country:
  66. return team
  67. raise NotFoundException(
  68. 'No team matching with id "{}" and country "{}" in "{}"'.format(
  69. team_id,
  70. team_country,
  71. self.schema_file_path,
  72. )
  73. )
  74. def get_team_by_name(
  75. self,
  76. team_name: str,
  77. team_country: str,
  78. ) -> TeamModel:
  79. for team in self.teams:
  80. if team.name == team_name and team.country == team_country:
  81. return team
  82. raise NotFoundException(
  83. 'No team matching with name "{}" and country "{}" in "{}"'.format(
  84. team_name,
  85. team_country,
  86. self.schema_file_path,
  87. )
  88. )
  89. def get_team_by_country(self, country: str) -> typing.List[TeamModel]:
  90. teams = [] # type: typing.List[TeamModel]
  91. for team in self.teams:
  92. if team.country == country:
  93. teams.append(team)
  94. return teams