troops.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # coding: utf-8
  2. import typing
  3. from lxml import etree
  4. from synergine2.config import Config
  5. from synergine2.log import get_logger
  6. from opencombat.strategy.team.model import TeamModel
  7. from opencombat.strategy.team.stash import TeamStash
  8. from opencombat.strategy.unit.stash import UnitStash
  9. from opencombat.util import get_class_from_string_path, pretty_xml
  10. class TroopDumper(object):
  11. def __init__(
  12. self,
  13. config: Config,
  14. ) -> None:
  15. self._config = config
  16. self._logger = get_logger('TroopDumper', config)
  17. def get_troop_dump(
  18. self,
  19. countries_troops: typing.Dict[str, typing.List[TeamModel]],
  20. ) -> str:
  21. troops_template = self._config.resolve(
  22. 'global.troops_template',
  23. 'opencombat/strategy/troops_template.xml',
  24. )
  25. with open(troops_template, 'r') as xml_file:
  26. template_str = xml_file.read()
  27. parser = etree.XMLParser(remove_blank_text=True)
  28. state_root = etree.fromstring(
  29. template_str.encode('utf-8'),
  30. parser,
  31. )
  32. for country, teams in countries_troops.items():
  33. for team in teams:
  34. troop_element = etree.SubElement(state_root, 'troop')
  35. troop_element.attrib['country'] = country
  36. troop_element.attrib['team_id'] = team.id
  37. return pretty_xml(
  38. etree.tostring(
  39. state_root,
  40. ).decode('utf-8'),
  41. )
  42. class TroopClassBuilder(object):
  43. def __init__(
  44. self,
  45. config: Config,
  46. ) -> None:
  47. self._logger = get_logger('TroopManagerBuilder', config)
  48. self._config = config
  49. def get_unit_stash(
  50. self,
  51. units_file_path: str,
  52. ) -> UnitStash:
  53. class_address = self._config.resolve(
  54. 'global.unit_stash',
  55. 'opencombat.strategy.unit.stash.UnitStash',
  56. )
  57. class_ = get_class_from_string_path(
  58. self._config,
  59. class_address,
  60. )
  61. return class_(
  62. self._config,
  63. units_file_path,
  64. )
  65. def get_team_stash(
  66. self,
  67. units_file_path: str,
  68. teams_file_path: str,
  69. ) -> TeamStash:
  70. class_address = self._config.resolve(
  71. 'global.team_stash',
  72. 'opencombat.strategy.team.stash.TeamStash',
  73. )
  74. class_ = get_class_from_string_path(
  75. self._config,
  76. class_address,
  77. )
  78. unit_stash = self.get_unit_stash(units_file_path)
  79. return class_(
  80. self._config,
  81. teams_file_path,
  82. unit_stash=unit_stash,
  83. )
  84. def get_troop_dumper(self) -> TroopDumper:
  85. class_address = self._config.resolve(
  86. 'global.troop_dumper',
  87. 'opencombat.strategy.troops.TroopDumper',
  88. )
  89. class_ = get_class_from_string_path(
  90. self._config,
  91. class_address,
  92. )
  93. return class_(
  94. self._config,
  95. )