Browse Source

add troop builder code

Bastien Sevajol 5 years ago
parent
commit
0bf95c18ab
1 changed files with 66 additions and 0 deletions
  1. 66 0
      opencombat/strategy/troops.py

+ 66 - 0
opencombat/strategy/troops.py View File

@@ -1,15 +1,18 @@
1 1
 # coding: utf-8
2 2
 import typing
3
+from _elementtree import Element
3 4
 
4 5
 from lxml import etree
5 6
 
6 7
 from synergine2.config import Config
7 8
 from synergine2.log import get_logger
8 9
 
10
+from opencombat.simulation.base import TileStrategySimulation
9 11
 from opencombat.strategy.team.model import TeamModel
10 12
 from opencombat.strategy.team.stash import TeamStash
11 13
 from opencombat.strategy.unit.stash import UnitStash
12 14
 from opencombat.util import get_class_from_string_path, pretty_xml
15
+from opencombat.xml import XmlValidator
13 16
 
14 17
 
15 18
 class TroopDumper(object):
@@ -109,3 +112,66 @@ class TroopClassBuilder(object):
109 112
         return class_(
110 113
             self._config,
111 114
         )
115
+
116
+
117
+class TroopLoader(object):
118
+    def __init__(
119
+        self,
120
+        config: Config,
121
+        simulation: TileStrategySimulation,
122
+    ) -> None:
123
+        self._logger = get_logger('TroopLoader', config)
124
+        self._config = config
125
+        self._simulation = simulation
126
+
127
+        schema_file_path = self._config.get(
128
+            'global.troop_schema',
129
+            'opencombat/strategy/troop.xsd',
130
+        )
131
+        self._xml_validator = XmlValidator(
132
+            config,
133
+            schema_file_path,
134
+        )
135
+
136
+    def get_troop(
137
+        self,
138
+        troop_file_path: str,
139
+    ) -> Troop:
140
+        return Troop(
141
+            self._config,
142
+            self._validate_and_return_state_element(troop_file_path),
143
+            self._simulation,
144
+        )
145
+
146
+    def _validate_and_return_state_element(
147
+        self,
148
+        troop_file_path: str,
149
+    ) -> Element:
150
+        return self._xml_validator.validate_and_return(troop_file_path)
151
+
152
+
153
+class TroopConstructorBuilder(object):
154
+    def __init__(
155
+        self,
156
+        config: Config,
157
+        simulation: TileStrategySimulation,
158
+    ) -> None:
159
+        self._logger = get_logger('TroopConstructorBuilder', config)
160
+        self._config = config
161
+        self._simulation = simulation
162
+
163
+    def get_troop_loader(
164
+        self,
165
+    ) -> TroopLoader:
166
+        class_address = self._config.resolve(
167
+            'global.troop_loader',
168
+            'opencombat.strategy.troops.TroopLoader',
169
+        )
170
+        troop_loader_class = get_class_from_string_path(
171
+            self._config,
172
+            class_address,
173
+        )
174
+        return troop_loader_class(
175
+            self._config,
176
+            self._simulation,
177
+        )