Skip to content

Commit

Permalink
troop selection: dump troop file
Browse files Browse the repository at this point in the history
  • Loading branch information
buxx committed Jun 23, 2018
1 parent e17d0b4 commit 7cb345e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ global:
team_stash: "opencombat.strategy.team.stash.TeamStash"
teams_schema: "opencombat/strategy/teams.xsd"
units_schema: "opencombat/strategy/units.xsd"
troop_dumper: "opencombat.strategy.troops.TroopDumper"
troop_schema: "opencombat/strategy/troops.xsd"
cache_dir_path: 'cache'
include_path:
maps:
Expand Down
16 changes: 13 additions & 3 deletions opencombat/strategy/manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# coding: utf-8
import typing

from synergine2.config import Config
from synergine2.log import get_logger

from opencombat.strategy.team.model import TeamModel
from opencombat.strategy.team.stash import TeamStash
from opencombat.strategy.troops import TroopClassBuilder
from opencombat.strategy.unit.stash import UnitStash
Expand All @@ -17,11 +20,11 @@ def __init__(
self._config = config
self._logger = get_logger('TroopManager', config)

builder = TroopClassBuilder(config)
self._unit_stash = builder.get_unit_stash(
self._builder = TroopClassBuilder(config)
self._unit_stash = self._builder.get_unit_stash(
units_file_path,
)
self._team_stash = builder.get_team_stash(
self._team_stash = self._builder.get_team_stash(
units_file_path,
teams_file_path,
)
Expand All @@ -33,3 +36,10 @@ def team_stash(self) -> TeamStash:
@property
def unit_stash(self) -> UnitStash:
return self._unit_stash

def get_troop_dump(
self,
countries_troops: typing.Dict[str, typing.List[TeamModel]],
):
dumper = self._builder.get_troop_dumper()
return dumper.get_troop_dump(countries_troops)
45 changes: 45 additions & 0 deletions opencombat/strategy/selection/gui.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# coding: utf-8
import os
import typing
from tkinter import Tk
from tkinter import Button
from tkinter import YES
from tkinter import StringVar
from tkinter import OptionMenu
from tkinter import W
from tkinter import E
from tkinter import messagebox
from tkinter.ttk import Combobox
from tkinter.ttk import Treeview


import time
from synergine2.config import Config

from opencombat.gui import Gui
from opencombat.strategy.manager import TroopManager
from opencombat.strategy.team.stash import TeamStash


Expand All @@ -21,11 +27,16 @@ def __init__(
config: Config,
master: Tk,
team_stash: TeamStash,
troop_manager: TroopManager,
countries: typing.List[str],
troops_dir_path: str = '.',
) -> None:
super().__init__(config, master)
self._master.title('Troops selection')
self._countries = countries
self._team_stash = team_stash
self._troop_manager = troop_manager
self._troops_dir_path = troops_dir_path
self._countries_troops = {} # type: typing.Dict[str, typing.List[TeamModel]] # nopep8

# Widgets
Expand Down Expand Up @@ -72,12 +83,21 @@ def __init__(
self._troops_view.column('#0', stretch=YES)
self._troops_view.column('#1', stretch=YES)

self._generate_troops_var = StringVar(self._master)
self._generate_troops_button = Button(
self._master,
textvariable=self._generate_troops_var,
command=self._generate_troops,
)
self._generate_troops_var.set('Generate troops')

# Layout
self._select_country_menu.grid(row=0, column=0, sticky=W)
self._teams_list.grid(row=1, column=0, sticky=W)
self._add_troop_button.grid(row=2, column=0, sticky=W)
self._troops_view.grid(row=3, column=0, sticky=W)
self._remove_troop_button.grid(row=4, column=0, sticky=W)
self._generate_troops_button.grid(row=4, column=0, sticky=E)

# Default behaviours
self._selected_country_var.set(countries[0])
Expand Down Expand Up @@ -153,3 +173,28 @@ def _update_troops_view(self, country: str) -> None:
text=team.name,
values=('o' * len(team.units,))
)

def _generate_troops(self, *args, **kwargs) -> None:
# Must have team(s) in all countries
if len(self._countries_troops.keys()) == len(self._countries) \
and all(self._countries_troops.values()):

troops_file_path = os.path.join(
self._troops_dir_path,
'troops_{}.xml'.format(str(time.time())),
)

self._logger.info('Generate troops into file "{}"'.format(
troops_file_path,
))

troops_xml = self._troop_manager.get_troop_dump(
self._countries_troops,
)
with open(troops_file_path, 'w+') as file:
file.write(troops_xml)
else:
messagebox.showinfo(
'Missing information',
'All countries must have teams',
)
59 changes: 58 additions & 1 deletion opencombat/strategy/troops.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
# coding: utf-8
import typing

from lxml import etree

from synergine2.config import Config
from synergine2.log import get_logger

from opencombat.strategy.team.model import TeamModel
from opencombat.strategy.team.stash import TeamStash
from opencombat.strategy.unit.stash import UnitStash
from opencombat.util import get_class_from_string_path
from opencombat.util import get_class_from_string_path, pretty_xml


class TroopDumper(object):
def __init__(
self,
config: Config,
) -> None:
self._config = config
self._logger = get_logger('TroopDumper', config)

def get_troop_dump(
self,
countries_troops: typing.Dict[str, typing.List[TeamModel]],
) -> str:
troops_template = self._config.resolve(
'global.troops_template',
'opencombat/strategy/troops_template.xml',
)
with open(troops_template, 'r') as xml_file:
template_str = xml_file.read()

parser = etree.XMLParser(remove_blank_text=True)
state_root = etree.fromstring(
template_str.encode('utf-8'),
parser,
)

for country, teams in countries_troops.items():
for team in teams:
troop_element = etree.SubElement(state_root, 'troop')
troop_element.attrib['country'] = country
troop_element.attrib['team_id'] = team.id

return pretty_xml(
etree.tostring(
state_root,
).decode('utf-8'),
)


class TroopClassBuilder(object):
Expand Down Expand Up @@ -52,3 +95,17 @@ def get_team_stash(
teams_file_path,
unit_stash=unit_stash,
)

def get_troop_dumper(self) -> TroopDumper:
class_address = self._config.resolve(
'global.troop_dumper',
'opencombat.strategy.troops.TroopDumper',
)
class_ = get_class_from_string_path(
self._config,
class_address,
)

return class_(
self._config,
)
9 changes: 9 additions & 0 deletions select_troops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def main(
units_file_path: str,
teams_file_path: str,
countries: typing.List[str],
troops_dir_path: str = '.',
) -> None:
config = Config()
config.load_yaml('config.yaml')
Expand All @@ -28,7 +29,9 @@ def main(
config,
master=master,
team_stash=troop_manager.team_stash,
troop_manager=troop_manager,
countries=countries,
troops_dir_path=troops_dir_path,
)
master.mainloop()

Expand All @@ -52,10 +55,16 @@ def main(
action='append',
dest='countries',
)
parser.add_argument(
'--troops-dir-path',
dest='troops_dir_path',
default='.',
)
args = parser.parse_args()

main(
units_file_path=args.units_file_path,
teams_file_path=args.teams_file_path,
countries=args.countries,
troops_dir_path=args.troops_dir_path,
)
2 changes: 2 additions & 0 deletions test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ global:
team_stash: "opencombat.strategy.team.stash.TeamStash"
teams_schema: "opencombat/strategy/teams.xsd"
units_schema: "opencombat/strategy/units.xsd"
troop_dumper: "opencombat.strategy.troops.TroopDumper"
troop_schema: "opencombat/strategy/troops.xsd"
cache_dir_path: 'cache'
include_path:
maps:
Expand Down

0 comments on commit 7cb345e

Please sign in to comment.