-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.py
32 lines (24 loc) · 1007 Bytes
/
shared.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import utils
import json
class InvalidAction(utils.HopliteError):
pass
def CreateAction(action):
"""Function used to create a Action object from a json dictionary"""
# Import this here to avoid cyclical dependencies....
# TODO: Fix these dependencies.
from actions import Action
for klass in utils.all_subclasses(Action):
if action['type'] == klass.__name__:
action['target'] = tuple(action['target'])
return klass(**action)
raise InvalidAction("%s is not a valid move." % json.dumps(action, indent=2))
def CreateUnit(**kwargs):
"""Function used to create a Unit object from a json dictionary"""
# Import this here to avoid cyclical dependencies....
# TODO: Fix these dependencies.
from units import Hero, Unit
# Special case for Heroes, who need to use a subclass that doesn't check abilities for actions
if kwargs.get('type', None) == "Hero":
return Hero(**kwargs)
else:
return Unit(**kwargs)