-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunits.py
More file actions
108 lines (93 loc) · 3.37 KB
/
units.py
File metadata and controls
108 lines (93 loc) · 3.37 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
import functools
class Unit:
"""
Base class for game units.
Need to know:
- who it belongs to: `home_power`
- where it is: `current_province`
- what it should do: `order`
example order outputs:
Hold: F London Holds
Move: A Paris-Burgundy
(Army Paris move to Burgundy)
Support: A Par S A Mar-Bur
(Army Paris support army Marseille to Burgundy)
Convoy: F Bla C A Ank-Sev
(Fleet Black Sea convoy army Ankara to Sevastopol)
"""
def __init__(self, home_power, current_province, dislodged=False):
self.home_power = home_power
self.current_province = current_province
self.dislodged = dislodged
def __repr__(self):
return f"Unit at {self.current_province}"
def to_output(self):
return {"type":self.type,
"home_power":self.home_power,
"current_province": self.current_province}
def base_order(order):
@functools.wraps(order)
def wrapper(*args, **kwargs):
self = args[0]
order_prefix = f"{self.type} {self.current_province}"
return order_prefix + order(*args)
return wrapper
@base_order
def hold(self):
order_string = " Holds"
return order_string
@base_order
def move(self, to_province, retreat=False):
# retreat is a special case of move
order_string = f"-{to_province}"
return order_string
@base_order
def support(self, order):
# some sort of wrapper for hold and move orders
order_to_support = order
if "Holds" in order_to_support:
order_string = f" S " + order_to_support.split(" Holds")[0]
else:
order_string = f" S " + order_to_support
return order_string
@base_order
def convoy(self, movement):
order_string = f" C " + movement
return order_string
class Army(Unit):
def __init__(self, home_power, current_province):
Unit.__init__(self, home_power, current_province)
self.type = "A"
self.allowed_province_types = ["inland", "coastal"]
def __repr__(self):
return f"Army at {self.current_province}"
class Fleet(Unit):
def __init__(self, home_power, current_province, coast=None):
Unit.__init__(self, home_power, current_province)
self.type = "F"
# self.coast = coast
if coast == "south":
self.current_province = current_province + "_SC"
elif coast == "north":
self.current_province = current_province + "_NC"
else:
pass
self.allowed_province_types = ["water", "coastal"]
def __repr__(self):
return f"Fleet at {self.current_province}"
def unit_from_gamestate(unit_dict):
if unit_dict["type"] == "A":
return Army(unit_dict["home_power"], unit_dict["current_province"])
if unit_dict["type"] == "F":
province = unit_dict["current_province"].split(" ")
if len(province) == 2:
coast = province[1][0]
if coast == "S":
coast = "south"
elif coast == "N":
coast = "north"
else:
coast = None
return Fleet(unit_dict["home_power"], province[0], coast=coast)
return Fleet(unit_dict["home_power"], unit_dict["current_province"])