Browse Source

save state with S key

Bastien Sevajol 5 years ago
parent
commit
292cdca934
6 changed files with 56 additions and 9 deletions
  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 View File

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

+ 1 - 0
config.yaml View File

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

+ 28 - 3
opencombat/simulation/state.py View File

1
+import os
2
+import time
1
 import typing
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
 class SaveStateSimulationAction(SimulationBehaviour):
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
     def run(self, data):
26
     def run(self, data):
8
         pass
27
         pass
9
 
28
 
10
     def action(self, data) -> typing.List[Event]:
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
     @classmethod
39
     @classmethod
15
     def merge_data(cls, new_data, start_data=None):
40
     def merge_data(cls, new_data, start_data=None):

+ 18 - 2
opencombat/state.py View File

259
         return doc
259
         return doc
260
 
260
 
261
 
261
 
262
-class StateLoaderBuilder(object):
262
+class StateConstructorBuilder(object):
263
     def __init__(
263
     def __init__(
264
         self,
264
         self,
265
         config: Config,
265
         config: Config,
266
         simulation: TileStrategySimulation,
266
         simulation: TileStrategySimulation,
267
     ) -> None:
267
     ) -> None:
268
-        self._logger = get_logger('StateLoader', config)
268
+        self._logger = get_logger('StateConstructorBuilder', config)
269
         self._config = config
269
         self._config = config
270
         self._simulation = simulation
270
         self._simulation = simulation
271
 
271
 
284
             self._config,
284
             self._config,
285
             self._simulation,
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 View File

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

+ 2 - 2
tests/test_state.py View File

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