Browse Source

placement with troops

Bastien Sevajol 6 years ago
parent
commit
a8e6a5e022
4 changed files with 73 additions and 2 deletions
  1. 2 0
      config.yaml
  2. 52 1
      opencombat/strategy/troops.py
  3. 17 0
      opencombat/strategy/troops.xsd
  4. 2 1
      run.py

+ 2 - 0
config.yaml View File

31
     units_schema: "opencombat/strategy/units.xsd"
31
     units_schema: "opencombat/strategy/units.xsd"
32
     troop_dumper: "opencombat.strategy.troops.TroopDumper"
32
     troop_dumper: "opencombat.strategy.troops.TroopDumper"
33
     troop_schema: "opencombat/strategy/troops.xsd"
33
     troop_schema: "opencombat/strategy/troops.xsd"
34
+    units: "opencombat/strategy/units.xml"
35
+    teams: "opencombat/strategy/teams.xml"
34
     cache_dir_path: 'cache'
36
     cache_dir_path: 'cache'
35
     include_path:
37
     include_path:
36
       maps:
38
       maps:

+ 52 - 1
opencombat/strategy/troops.py View File

8
 from synergine2.log import get_logger
8
 from synergine2.log import get_logger
9
 
9
 
10
 from opencombat.simulation.base import TileStrategySimulation
10
 from opencombat.simulation.base import TileStrategySimulation
11
+from opencombat.simulation.subject import TileSubject
11
 from opencombat.strategy.team.model import TeamModel
12
 from opencombat.strategy.team.model import TeamModel
12
 from opencombat.strategy.team.stash import TeamStash
13
 from opencombat.strategy.team.stash import TeamStash
13
 from opencombat.strategy.unit.stash import UnitStash
14
 from opencombat.strategy.unit.stash import UnitStash
15
 from opencombat.xml import XmlValidator
16
 from opencombat.xml import XmlValidator
16
 
17
 
17
 
18
 
19
+class Troop(object):
20
+    def __init__(
21
+        self,
22
+        config: Config,
23
+        state_root: Element,
24
+        simulation: TileStrategySimulation,
25
+    ) -> None:
26
+        self._config = config
27
+        self._state_root = state_root
28
+        self._subjects = None  # type: typing.List[TileSubject]
29
+        self._simulation = simulation
30
+        self._builder = TroopClassBuilder(config)
31
+
32
+    @property
33
+    def subjects(self) -> typing.List[TileSubject]:
34
+        if self._subjects is None:
35
+            self._subjects = self.get_computed_subjects()
36
+
37
+        return self._subjects
38
+
39
+    def get_computed_subjects(self) -> typing.List[TileSubject]:
40
+        units_file_path = self._config.get(
41
+            'global.units',
42
+            'opencombat/strategy/units.xml',
43
+        )
44
+        teams_file_path = self._config.get(
45
+            'global.teams',
46
+            'opencombat/strategy/teams.xml',
47
+        )
48
+
49
+        team_stash = self._builder.get_team_stash(
50
+            units_file_path,
51
+            teams_file_path,
52
+        )
53
+
54
+        # Parse team, build Subjects
55
+        subjects = []  # type: typing.List[TileSubject]
56
+        for troop in self._state_root.findall('troop'):
57
+            country = troop.attrib['country']
58
+            team_id = troop.attrib['team_id']
59
+            team = team_stash.get_team(team_id, country)
60
+
61
+            for unit in team.units:
62
+                subject = unit.class_(self._config, self._simulation)
63
+                subjects.append(subject)
64
+
65
+        # TODO BS 2018-06-25: place subjects on map, set side, color, etc
66
+        return subjects
67
+
68
+
18
 class TroopDumper(object):
69
 class TroopDumper(object):
19
     def __init__(
70
     def __init__(
20
         self,
71
         self,
126
 
177
 
127
         schema_file_path = self._config.get(
178
         schema_file_path = self._config.get(
128
             'global.troop_schema',
179
             'global.troop_schema',
129
-            'opencombat/strategy/troop.xsd',
180
+            'opencombat/strategy/troops.xsd',
130
         )
181
         )
131
         self._xml_validator = XmlValidator(
182
         self._xml_validator = XmlValidator(
132
             config,
183
             config,

+ 17 - 0
opencombat/strategy/troops.xsd View File

1
+<?xml version="1.0" encoding="UTF-8" ?>
2
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3
+
4
+    <xs:element name="troops" type="troopstype"/>
5
+
6
+    <xs:complexType name="troopstype">
7
+        <xs:sequence>
8
+            <xs:element name="troop" type="trooptype" maxOccurs="unbounded"/>
9
+        </xs:sequence>
10
+    </xs:complexType>
11
+
12
+    <xs:complexType name="trooptype">
13
+        <xs:attribute name="country" type="xs:string" use="required"/>
14
+        <xs:attribute name="team_id" type="xs:string" use="required"/>
15
+    </xs:complexType>
16
+
17
+</xs:schema>

+ 2 - 1
run.py View File

14
 from opencombat.simulation.base import TileStrategySimulation
14
 from opencombat.simulation.base import TileStrategySimulation
15
 from opencombat.simulation.base import TileStrategySubjects
15
 from opencombat.simulation.base import TileStrategySubjects
16
 from opencombat.state import StateConstructorBuilder
16
 from opencombat.state import StateConstructorBuilder
17
+from opencombat.strategy.troops import TroopConstructorBuilder
17
 from opencombat.terminal.base import CocosTerminal
18
 from opencombat.terminal.base import CocosTerminal
18
 
19
 
19
 
20
 
57
     elif troops_file_path:
58
     elif troops_file_path:
58
         troop_loader_builder = TroopConstructorBuilder(config, simulation)
59
         troop_loader_builder = TroopConstructorBuilder(config, simulation)
59
         troop_loader = troop_loader_builder.get_troop_loader()
60
         troop_loader = troop_loader_builder.get_troop_loader()
60
-        troops = troop_loader.get_troops(troops_file_path)
61
+        troops = troop_loader.get_troop(troops_file_path)
61
         subjects.extend(troops.subjects)
62
         subjects.extend(troops.subjects)
62
 
63
 
63
     simulation.subjects = subjects
64
     simulation.subjects = subjects