Browse Source

architecture of troop selection

Bastien Sevajol 5 years ago
parent
commit
188aeb956e

+ 17 - 0
opencombat/gui.py View File

@@ -0,0 +1,17 @@
1
+# coding: utf-8
2
+from logging import Logger
3
+from tkinter import Tk
4
+
5
+from synergine2.config import Config
6
+from synergine2.log import get_logger
7
+
8
+
9
+class Gui(object):
10
+    def __init__(
11
+        self,
12
+        config: Config,
13
+        master: Tk,
14
+    ) -> None:
15
+        self._config = config
16
+        self._logger = get_logger(self.__class__.__name__, config)
17
+        self._master = master

+ 1 - 0
opencombat/strategy/__init__.py View File

@@ -0,0 +1 @@
1
+# coding: utf-8

+ 20 - 0
opencombat/strategy/manager.py View File

@@ -0,0 +1,20 @@
1
+# coding: utf-8
2
+from synergine2.config import Config
3
+from synergine2.log import get_logger
4
+
5
+from opencombat.strategy.selection.troops import TroopClassBuilder
6
+
7
+
8
+class TroopManager(object):
9
+    def __init__(
10
+        self,
11
+        config: Config,
12
+        units_file_path: str,
13
+        teams_file_path: str,
14
+    ) -> None:
15
+        self._config = config
16
+        self._logger = get_logger('TroopManager', config)
17
+
18
+        builder = TroopClassBuilder(config)
19
+        self._unit_stash = builder.get_unit_stash(units_file_path)
20
+        self._team_stash = builder.get_team_stash(teams_file_path)

+ 1 - 0
opencombat/strategy/selection/__init__.py View File

@@ -0,0 +1 @@
1
+# coding: utf-8

+ 24 - 0
opencombat/strategy/selection/gui.py View File

@@ -0,0 +1,24 @@
1
+# coding: utf-8
2
+from tkinter import Tk
3
+from tkinter import Label
4
+from tkinter import W
5
+
6
+from synergine2.config import Config
7
+
8
+from opencombat.gui import Gui
9
+from opencombat.strategy.manager import TroopManager
10
+
11
+
12
+class SelectTroopsGui(Gui):
13
+    def __init__(
14
+        self,
15
+        config: Config,
16
+        master: Tk,
17
+        troop_manager: TroopManager,
18
+    ) -> None:
19
+        super().__init__(config, master)
20
+        self._master.title('Troops selection')
21
+        self.label = Label(master, text="Hello")
22
+
23
+        # Layout
24
+        self.label.grid(row=0, column=0, sticky=W)

+ 32 - 0
opencombat/strategy/selection/troops.py View File

@@ -0,0 +1,32 @@
1
+# coding: utf-8
2
+from synergine2.config import Config
3
+from synergine2.log import get_logger
4
+
5
+from opencombat.strategy.unit.stash import UnitStash
6
+from opencombat.util import get_class_from_string_path
7
+
8
+
9
+class TroopClassBuilder(object):
10
+    def __init__(
11
+        self,
12
+        config: Config,
13
+    ) -> None:
14
+        self._logger = get_logger('TroopManagerBuilder', config)
15
+        self._config = config
16
+
17
+    def get_unit_stash(
18
+        self,
19
+        units_file_path: str,
20
+    ) -> UnitStash:
21
+        class_address = self._config.resolve(
22
+            'global.unit_stash',
23
+            'opencombat.strategy.unit.stash.UnitStash',
24
+        )
25
+        class_ = get_class_from_string_path(
26
+            self._config,
27
+            class_address,
28
+        )
29
+        return class_(
30
+            self._config,
31
+            units_file_path,
32
+        )

+ 1 - 0
opencombat/strategy/team/__init__.py View File

@@ -0,0 +1 @@
1
+# coding: utf-8

+ 13 - 0
opencombat/strategy/team/model.py View File

@@ -0,0 +1,13 @@
1
+# coding: utf-8
2
+
3
+
4
+class UnitModel(object):
5
+    def __init__(
6
+        self,
7
+        name: str,
8
+    ) -> None:
9
+        self._name = name
10
+
11
+    @property
12
+    def name(self) -> str:
13
+        return self._name

+ 19 - 0
opencombat/strategy/team/stash.py View File

@@ -0,0 +1,19 @@
1
+# coding: utf-8
2
+import typing
3
+
4
+from opencombat.strategy.unit.model import TeamModel
5
+
6
+
7
+class TeamStash(object):
8
+    def __init__(
9
+        self,
10
+        teams_file_path: str,
11
+    ) -> None:
12
+        # TODO Load xml, validate
13
+        self._teams = None  # typing.List[TeamModel]
14
+
15
+    def get_teams(self) -> typing.List[TeamModel]:
16
+        pass
17
+
18
+    def get_team(self, unit_id: str) -> TeamModel:
19
+        pass

+ 20 - 0
opencombat/strategy/teams.xml View File

@@ -0,0 +1,20 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<teams>
3
+    <team id="std_team" country="URSS">
4
+        <name>Standard team</name>
5
+        <units>
6
+            <unit>
7
+                <id>std_soldier</id>
8
+            </unit>
9
+            <unit>
10
+                <id>std_soldier</id>
11
+            </unit>
12
+            <unit>
13
+                <id>std_soldier</id>
14
+            </unit>
15
+            <unit>
16
+                <id>std_soldier</id>
17
+            </unit>
18
+        </units>
19
+    </team>
20
+</teams>

+ 1 - 0
opencombat/strategy/unit/__init__.py View File

@@ -0,0 +1 @@
1
+# coding: utf-8

+ 13 - 0
opencombat/strategy/unit/model.py View File

@@ -0,0 +1,13 @@
1
+# coding: utf-8
2
+
3
+
4
+class TeamModel(object):
5
+    def __init__(
6
+        self,
7
+        name: str,
8
+    ) -> None:
9
+        self._name = name
10
+
11
+    @property
12
+    def name(self) -> str:
13
+        return self._name

+ 19 - 0
opencombat/strategy/unit/stash.py View File

@@ -0,0 +1,19 @@
1
+# coding: utf-8
2
+import typing
3
+
4
+from opencombat.strategy.team.model import UnitModel
5
+
6
+
7
+class UnitStash(object):
8
+    def __init__(
9
+        self,
10
+        units_file_path: str,
11
+    ) -> None:
12
+        # TODO Load xml, validate
13
+        self._units = None  # typing.List[UnitModel]
14
+
15
+    def get_units(self) -> typing.List[UnitModel]:
16
+        pass
17
+
18
+    def get_unit(self, unit_id: str) -> UnitModel:
19
+        pass

+ 11 - 0
opencombat/strategy/units.xml View File

@@ -0,0 +1,11 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<units>
3
+    <unit id="std_soldier" country="URSS">
4
+        <name>Standard soldier</name>
5
+        <type>opencombat.simulation.subject.ManSubject</type>
6
+    </unit>
7
+    <unit id="std_soldier" country="DE">
8
+        <name>Standard soldier</name>
9
+        <type>opencombat.simulation.subject.ManSubject</type>
10
+    </unit>
11
+</units>

+ 3 - 1
run.py View File

@@ -71,7 +71,9 @@ def main(
71 71
     core.run()
72 72
 
73 73
 if __name__ == '__main__':
74
-    parser = argparse.ArgumentParser(description='Run TileStrategy')
74
+    parser = argparse.ArgumentParser(
75
+        description='Run a map'
76
+    )
75 77
     parser.add_argument('map_dir_path', help='map directory path')
76 78
     parser.add_argument('--seed', dest='seed', default=None)
77 79
     parser.add_argument('--state', dest='state', default=None)

+ 61 - 0
select_troops.py View File

@@ -0,0 +1,61 @@
1
+# coding: utf-8
2
+import argparse
3
+import typing
4
+from tkinter import Tk
5
+
6
+from synergine2.config import Config
7
+
8
+from opencombat.strategy.manager import TroopManager
9
+from opencombat.strategy.selection.gui import SelectTroopsGui
10
+
11
+
12
+def main(
13
+    units_file_path: str,
14
+    teams_file_path: str,
15
+    countries: typing.List[str],
16
+) -> None:
17
+    config = Config()
18
+    config.load_yaml('config.yaml')
19
+
20
+    troop_manager = TroopManager(
21
+        config,
22
+        units_file_path=units_file_path,
23
+        teams_file_path=teams_file_path,
24
+    )
25
+
26
+    master = Tk()
27
+    gui = SelectTroopsGui(
28
+        config,
29
+        master=master,
30
+        troop_manager=troop_manager,
31
+    )
32
+    master.mainloop()
33
+
34
+
35
+if __name__ == '__main__':
36
+    parser = argparse.ArgumentParser(
37
+        description='Display troops selection gui',
38
+    )
39
+    parser.add_argument(
40
+        '--units',
41
+        dest='units_file_path',
42
+        default='opencombat/strategy/units.xml',
43
+    )
44
+    parser.add_argument(
45
+        '--teams',
46
+        dest='teams_file_path',
47
+        default='opencombat/strategy/teams.xml',
48
+    )
49
+    parser.add_argument(
50
+        '--country',
51
+        action='append',
52
+        dest='countries',
53
+        default=['URSS', 'DE'],
54
+    )
55
+    args = parser.parse_args()
56
+
57
+    main(
58
+        units_file_path=args.units_file_path,
59
+        teams_file_path=args.teams_file_path,
60
+        countries=args.countries,
61
+    )