Bladeren bron

save state with S key

Bastien Sevajol 6 jaren geleden
bovenliggende
commit
292cdca934
6 gewijzigde bestanden met toevoegingen van 56 en 9 verwijderingen
  1. 1 0
      README.md
  2. 1 0
      config.yaml
  3. 28 3
      opencombat/simulation/state.py
  4. 18 2
      opencombat/state.py
  5. 6 2
      run.py
  6. 2 2
      tests/test_state.py

+ 1 - 0
README.md Bestand weergeven

@@ -39,3 +39,4 @@ When unit selected:
39 39
 * C: crouch
40 40
 * M: move
41 41
 * F: fire (not implemented)
42
+* S: Save current state into OpenCombat dir (or dir specified with `--state-save-dir`)

+ 1 - 0
config.yaml Bestand weergeven

@@ -22,6 +22,7 @@ game:
22 22
 
23 23
 global:
24 24
     state_loader: "opencombat.state.StateLoader"
25
+    state_dumper: "opencombat.state.StateDumper"
25 26
     state_schema: "opencombat/state.xsd"
26 27
     state_template: "opencombat/state_template.xml"
27 28
     cache_dir_path: 'cache'

+ 28 - 3
opencombat/simulation/state.py Bestand weergeven

@@ -1,15 +1,40 @@
1
+import os
2
+import time
1 3
 import typing
2 4
 
3
-from synergine2.simulation import SimulationBehaviour, Event
5
+from synergine2.config import Config
6
+from synergine2.simulation import SimulationBehaviour
7
+from synergine2.simulation import Event
8
+from synergine2.simulation import Simulation
9
+
10
+from opencombat.state import StateConstructorBuilder
4 11
 
5 12
 
6 13
 class SaveStateSimulationAction(SimulationBehaviour):
14
+    def __init__(
15
+        self,
16
+        config: Config,
17
+        simulation: Simulation,
18
+    ):
19
+        super().__init__(config, simulation)
20
+        self.state_dumper = StateConstructorBuilder(
21
+            config,
22
+            simulation,
23
+        ).get_state_dumper()
24
+        self.state_save_dir = self.config.resolve('_runtime.state_save_dir')
25
+
7 26
     def run(self, data):
8 27
         pass
9 28
 
10 29
     def action(self, data) -> typing.List[Event]:
11
-        # TODO BS 2018-06-14: dump state here
12
-        pass
30
+        state_file_path = os.path.join(
31
+            self.state_save_dir,
32
+            'state_{}.xml'.format(time.time())
33
+        )
34
+        with open(state_file_path, 'w+') as file:
35
+            file.write(self.state_dumper.get_state_dump())
36
+
37
+        return []
13 38
 
14 39
     @classmethod
15 40
     def merge_data(cls, new_data, start_data=None):

+ 18 - 2
opencombat/state.py Bestand weergeven

@@ -259,13 +259,13 @@ class StateLoader(object):
259 259
         return doc
260 260
 
261 261
 
262
-class StateLoaderBuilder(object):
262
+class StateConstructorBuilder(object):
263 263
     def __init__(
264 264
         self,
265 265
         config: Config,
266 266
         simulation: TileStrategySimulation,
267 267
     ) -> None:
268
-        self._logger = get_logger('StateLoader', config)
268
+        self._logger = get_logger('StateConstructorBuilder', config)
269 269
         self._config = config
270 270
         self._simulation = simulation
271 271
 
@@ -284,3 +284,19 @@ class StateLoaderBuilder(object):
284 284
             self._config,
285 285
             self._simulation,
286 286
         )
287
+
288
+    def get_state_dumper(
289
+        self,
290
+    ) -> StateDumper:
291
+        class_address = self._config.resolve(
292
+            'global.state_dumper',
293
+            'opencombat.state.StateDumper',
294
+        )
295
+        state_loader_class = get_class_from_string_path(
296
+            self._config,
297
+            class_address,
298
+        )
299
+        return state_loader_class(
300
+            self._config,
301
+            self._simulation,
302
+        )

+ 6 - 2
run.py Bestand weergeven

@@ -12,7 +12,7 @@ from synergine2.terminals import TerminalManager
12 12
 
13 13
 from opencombat.simulation.base import TileStrategySimulation
14 14
 from opencombat.simulation.base import TileStrategySubjects
15
-from opencombat.state import StateLoaderBuilder
15
+from opencombat.state import StateConstructorBuilder
16 16
 from opencombat.terminal.base import CocosTerminal
17 17
 
18 18
 
@@ -27,6 +27,10 @@ def main(
27 27
 
28 28
     config = Config()
29 29
     config.load_yaml('config.yaml')
30
+
31
+    # Runtime config
32
+    config.setdefault('_runtime', {})['state_save_dir'] = state_save_dir
33
+
30 34
     level = logging.getLevelName(config.resolve('global.logging_level', 'ERROR'))
31 35
     logger = get_default_logger(level=level)
32 36
 
@@ -36,7 +40,7 @@ def main(
36 40
     subjects = TileStrategySubjects(simulation=simulation)
37 41
 
38 42
     if state_file_path:
39
-        state_loader_builder = StateLoaderBuilder(config, simulation)
43
+        state_loader_builder = StateConstructorBuilder(config, simulation)
40 44
         state_loader = state_loader_builder.get_state_loader()
41 45
         state = state_loader.get_state(state_file_path)
42 46
         subjects.extend(state.subjects)

+ 2 - 2
tests/test_state.py Bestand weergeven

@@ -9,7 +9,7 @@ from opencombat.exception import StateLoadError
9 9
 from opencombat.simulation.base import TileStrategySimulation
10 10
 from opencombat.simulation.base import TileStrategySubjects
11 11
 from opencombat.simulation.subject import ManSubject
12
-from opencombat.state import StateLoaderBuilder, StateDumper
12
+from opencombat.state import StateConstructorBuilder, StateDumper
13 13
 from opencombat.state import StateLoader
14 14
 from opencombat.const import FLAG
15 15
 from opencombat.const import SIDE
@@ -69,7 +69,7 @@ def test_state_loader_builder__ok__nominal_case(
69 69
             'state_loader': 'tests.test_state.MyStateLoader',
70 70
         }
71 71
     })
72
-    builder = StateLoaderBuilder(config, simulation)
72
+    builder = StateConstructorBuilder(config, simulation)
73 73
     state_loader = builder.get_state_loader()
74 74
     assert type(state_loader) == MyStateLoader
75 75