ソースを参照

placement with troops

Bastien Sevajol 6 年 前
コミット
a8e6a5e022
共有4 個のファイルを変更した73 個の追加2 個の削除を含む
  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 ファイルの表示

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

+ 52 - 1
opencombat/strategy/troops.py ファイルの表示

@@ -8,6 +8,7 @@ from synergine2.config import Config
8 8
 from synergine2.log import get_logger
9 9
 
10 10
 from opencombat.simulation.base import TileStrategySimulation
11
+from opencombat.simulation.subject import TileSubject
11 12
 from opencombat.strategy.team.model import TeamModel
12 13
 from opencombat.strategy.team.stash import TeamStash
13 14
 from opencombat.strategy.unit.stash import UnitStash
@@ -15,6 +16,56 @@ from opencombat.util import get_class_from_string_path, pretty_xml
15 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 69
 class TroopDumper(object):
19 70
     def __init__(
20 71
         self,
@@ -126,7 +177,7 @@ class TroopLoader(object):
126 177
 
127 178
         schema_file_path = self._config.get(
128 179
             'global.troop_schema',
129
-            'opencombat/strategy/troop.xsd',
180
+            'opencombat/strategy/troops.xsd',
130 181
         )
131 182
         self._xml_validator = XmlValidator(
132 183
             config,

+ 17 - 0
opencombat/strategy/troops.xsd ファイルの表示

@@ -0,0 +1,17 @@
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 ファイルの表示

@@ -14,6 +14,7 @@ from synergine2.terminals import TerminalManager
14 14
 from opencombat.simulation.base import TileStrategySimulation
15 15
 from opencombat.simulation.base import TileStrategySubjects
16 16
 from opencombat.state import StateConstructorBuilder
17
+from opencombat.strategy.troops import TroopConstructorBuilder
17 18
 from opencombat.terminal.base import CocosTerminal
18 19
 
19 20
 
@@ -57,7 +58,7 @@ def main(
57 58
     elif troops_file_path:
58 59
         troop_loader_builder = TroopConstructorBuilder(config, simulation)
59 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 62
         subjects.extend(troops.subjects)
62 63
 
63 64
     simulation.subjects = subjects