-
Notifications
You must be signed in to change notification settings - Fork 14
/
territory.py
142 lines (119 loc) · 4.06 KB
/
territory.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from copy import deepcopy
import logging
LOG = logging.getLogger("pyrisk")
class Territory(object):
def __init__(self, name, area):
self.name = name
self.area = area
self.owner = None
self.forces = 0
self.connect = set()
self.ord = None
@property
def border(self):
return any(t.owner and t.owner != self.owner for t in self.connect)
@property
def area_owned(self):
return self.owner == self.area.owner
@property
def area_border(self):
return any(t.area != self.area for t in self.connect)
def adjacent(self, friendly=None, thisarea=None):
for t in self.connect:
if friendly is None or friendly == (t.owner == self.owner):
if thisarea is None or thisarea == (t.area == self.area):
yield t
def adjacent_forces(self, friendly=None, thisarea=None):
return sum(t.forces for t in self.adjacent(friendly, thisarea))
def __repr__(self):
return "T;%s" % self.name
def __hash__(self):
return hash(("territory", self.name))
def __eq__(self, other):
if isinstance(other, Territory):
return self.name == other.name
return False
def __deepcopy__(self, memo):
newobj = Territory(self.name, None)
newobj.__dict__.update(deepcopy(self.__dict__, memo))
return newobj
class Area(object):
def __init__(self, name, value):
self.name = name
self.value = value
self.territories = set()
def __getinitargs__(self):
return (self.name, self.value)
def __repr__(self):
return "A;%s" % self.name
@property
def owner(self):
owners = set(t.owner for t in self.territories)
if len(owners) == 1:
return owners.pop()
else:
return None
@property
def forces(self):
return sum(t.forces for t in self.territories)
@property
def adjacent(self):
adj = set()
for t in self.territories:
for tt in t.connect:
if tt.area != self:
adj.add(tt.area)
return adj
def __hash__(self):
return hash(("area", self.name))
def __eq__(self, other):
if isinstance(other, Area):
return self.name == other.name
return False
def __deepcopy__(self, memo):
newobj = Area(self.name, None)
newobj.__dict__.update(deepcopy(self.__dict__, memo))
return newobj
class World(object):
ords = list(map(ord, r'\/|-+'))
def __init__(self):
self.territories = {}
self.areas = {}
def territory(self, t):
if t in self.territories.keys():
return self.territories[t]
elif t in self.territories.values():
return t
else:
return None
def area(self, a):
if a in self.areas.keys():
return self.areas[a]
elif a in self.areas.values():
return a
else:
return None
def load(self, areas, connections):
for name, (value, territories) in areas.items():
LOG.debug("Creating area=%s", name)
area = Area(name, value)
self.areas[name] = area
for t in territories:
LOG.debug("Creating territory=%s", t)
territory = Territory(t, area)
area.territories.add(territory)
self.territories[t] = territory
for line in filter(lambda l: l.strip(), connections.split('\n')):
joins = [t.strip() for t in line.split('--')]
for i in range(len(joins) - 1):
t0 = self.territories[joins[i]]
t1 = self.territories[joins[i+1]]
t0.connect.add(t1)
t1.connect.add(t0)
for t in self.territories.values():
avail = set(self.ords)
for c in t.connect:
if c.ord in avail:
avail.remove(c.ord)
assert avail
t.ord = avail.pop()