-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaptools.py
69 lines (52 loc) · 1.61 KB
/
maptools.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
from elements import *
import json
#spain = Territory("Spain", "spn" )
#portu = Territory("Portugal", "ptg", -5, 0)
#midat = Territory("Mid-Atlantic", "mid", -5, 5)
#territories = [spain, portu, midat]
#borders = { (spain,portu): "all", (spain,midat) : "fleets" }
#occupations = {}
#units = { spain: Unit("p1"), portu: Unit("p2")}
def loadMap():
return (territories, borders, units, occupations)
def getConnection( g, A, B ):
if (A,B) in g:
return g[A,B]
elif (B,A) in g:
return g[B,A]
else:
return False
def getTerritory( territories, short ):
ts = [x for x in territories if x.short == short]
if len(ts) > 1: return False
if len(ts) == 0: return False
return ts[0]
def loadMapJSON( filename ):
with open( filename ) as data_file:
data = json.load(data_file)
# voll guter Oskar-foo:
# territories = [Territory(**t) for t in data['territories']]
territories = []
for t in data['territories']:
newTerritory = Territory(t['name'], t['short'], t['x'], t['y'])
if 'terrain' in t:
newTerritory.terrain = t['terrain']
territories.append( newTerritory )
#print(territories)
borders = {}
for b in data['borders']:
A = b['A'] #getTerritory( territories, b['A'] )
B = b['B'] #getTerritory( territories, b['B'] )
borders[(A,B)] = Border(**b) #A,B,b['type'])
#print(borders)
units = []
for n in data['nations']:
for terrUnitIn in n['armies']:
newUnit = Unit(n['name'], terrUnitIn, "Army")
units.append(newUnit)
occupations = {}
for n in data['nations']:
for o in n['home']:
print(n['name'] + ' ' + o)
occupations[o] = n['name']
return (territories, borders, units, occupations)