-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrisk_game_viewer.py
430 lines (361 loc) · 15 KB
/
risk_game_viewer.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""
This displays RISK game logs so that the games can be viewed
It was modified from the RISK.pyw program included in this assignment
which was written by John Bauman
"""
from PIL import Image
from PIL import ImageDraw
from PIL import ImageTk
import tkinter as tk
import xml.dom.minidom
import random
import io
import zipfile
import gc
import sys
import risktools
territories = {}
canvas = None
root = None
totim = None
zfile = None
statbrd = None
class Territory:
"""Contains the graphics info for a territory"""
def __init__(self, name, x, y, w, h, cx, cy):
self.name = str(name)
self.x = x
self.y = y
self.w = w
self.h = h
self.cx = cx
self.cy = cy
class PlayerStats:
"""This is used to display the stats for a single player"""
def __init__(self, master, **kwargs):
self.pframe = tk.Frame(master, **kwargs)
self.pack = self.pframe.pack
self.statlabel = tk.Label(self.pframe)
self.statlabel.pack(fill=tk.X)
self.statlabel2 = tk.Label(self.pframe)
self.statlabel2.pack(fill=tk.X)
def set_id(self, id):
self.id = id
def update_stats(self, player, state):
"""Update the player stats"""
num_armies = 0
num_territories = 0
for i in range(len(state.owners)):
if state.owners[i] == player.id:
num_territories += 1
num_armies += state.armies[i]
self.statlabel.configure(text=player.name + " : " + str(num_armies) + " in " + str(num_territories) + " territories. " + str(player.free_armies) + " free", fg=playercolors[player.id], bg=backcolors[player.id])
#Make card string of first letters of card pictures
cardstring = ""
for c in player.cards:
if len(cardstring) > 0:
cardstring = cardstring + ","
cardstring = cardstring + str(state.board.cards[c].picture)[0]
self.statlabel2.configure(text="Cur Reinf: " + str(risktools.getReinforcementNum(state,player.id)) + " | Cards : " + cardstring, fg=playercolors[player.id], bg=backcolors[player.id])
self.pack()
class PlayerList:
"""Actually lists the players."""
def __init__(self, master, **kwargs):
"""Actually initialize it."""
self.listframe = tk.Frame(master, **kwargs)
self.pack = self.listframe.pack
self.pstats = []
def append(self, player):
"""Append a player to this list."""
newpstat = PlayerStats(self.listframe)
newpstat.pack()
newpstat.set_id(player.id)
self.pstats.append(newpstat)
self.pack(fill=tk.X)
def updateList(self, state):
for p in state.players:
for ps in self.pstats:
if p.id == ps.id:
ps.update_stats(p, state)
class StatBoard:
"""Displays stats about current state"""
def __init__(self, master, **kwargs):
"""Initialize it."""
self.statframe = tk.Frame(master, **kwargs)
self.pack = self.statframe.pack
self.pstats = PlayerList(self.statframe)
self.pstats.pack(fill=tk.X)
self.sep3 = tk.Frame(self.statframe,height=1,width=50,bg="black")
self.sep3.pack(fill=tk.X)
self.curturnnum = tk.Label(self.statframe, text="Turn Number: ")
self.curturnnum.pack(fill=tk.X)
self.curturnin = tk.Label(self.statframe, text="Next Card Turn-in Value: ")
self.curturnin.pack(fill=tk.X)
self.sep1 = tk.Frame(self.statframe,height=1,width=50,bg="black")
self.sep1.pack(fill=tk.X)
self.curplayer = tk.Label(self.statframe, text="Current Player:")
self.curplayer.pack(fill=tk.X)
self.turntype = tk.Label(self.statframe, text="Turn Type:")
self.turntype.pack(fill=tk.X)
self.sep1 = tk.Frame(self.statframe,height=1,width=50,bg="black")
self.sep1.pack(fill=tk.X)
self.lastplayer = tk.Label(self.statframe, text="Last Player:")
self.lastplayer.pack(fill=tk.X)
self.action = tk.Label(self.statframe, text="Last Action:")
self.action.pack(fill=tk.X)
self.sep2 = tk.Frame(self.statframe,height=1,width=50,bg="black")
self.sep2.pack(fill=tk.X)
def add_player(self, player):
"""Add a player to the statBoard"""
self.pstats.append(player)
self.pack()
def update_statBoard(self, state, last_action, last_player):
"""Update the players on the statBoard"""
self.pstats.updateList(state)
self.curturnnum.configure(text="Turn # " + str(turn_number) + " - State # " + str(state_number))
self.curturnin.configure(text="Next Card Turn-in Value: " + str(risktools.getTurnInValue(state)))
self.curplayer.configure(text="Current Player: " + state.players[state.current_player].name)
if last_player is not None:
self.lastplayer.configure(text="Last Player: " + state.players[last_player].name)
self.turntype.configure(text="Turn Type: " + state.turn_type)
self.action.configure(text="Last Action: " + last_action.description(True))
def log_over(self):
self.curplayer.configure(text="LOG FILE IS OVER!")
def opengraphic(fname):
"""Load an image from the specified zipfile."""
stif = io.BytesIO(zfile.read(fname))
im = Image.open(stif)
im.load()
stif.close()
return im
def drawarmy(t, from_territory=0):
"""Draw a territory's army"""
terr = territories[riskboard.territories[t].name]
canvas.delete(terr.name + "-a")
if current_state.owners[t] is not None:
canvas.create_rectangle(terr.cx + terr.x - 7, terr.cy + terr.y - 7,
terr.cx + terr.x + 7, terr.cy + terr.y+ 7,
fill=backcolors[current_state.owners[t]],
tags=(terr.name + "-a",))
canvas.create_text(terr.cx + terr.x, terr.cy + terr.y,
text=str(current_state.armies[t]), tags=(riskboard.territories[t].name + "-a",), fill=playercolors[current_state.owners[t]])
else:
canvas.create_text(terr.cx + terr.x, terr.cy + terr.y,
text=str(0), tags=(terr.name + "-a",))
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
ti = int(lv/3)
return (int(value[0:ti],16), int(value[ti:2*ti],16), int(value[2*ti:lv],16), 255)
def drawterritory(t, color=None):
"""Draw an entire territory (will draw in color provided, default is owning player's color)"""
terr = territories[str(riskboard.territories[t].name)]
#Create colored version of the image
canvas.delete(terr.name)
if len(backcolors) > 0 and current_state.owners[t] is not None:
for fp in terr.floodpoints:
if color:
ImageDraw.floodfill(terr.photo, fp, color)
else:
ImageDraw.floodfill(terr.photo, fp, hex_to_rgb(backcolors[current_state.owners[t]]))
terr.currentimage = ImageTk.PhotoImage(terr.photo)
canvas.create_image(terr.x, terr.y, anchor=tk.NW,
image=terr.currentimage, tags=(terr.name,))
drawarmy(t, 1)
#make 7 possible colors - should be enough for anyone
possiblecolors = [(0,0,128),(128,0,0),(128,0,128),(0,128,0),(255,128,0),(0,128,255), (255,0,0), (0,255,255)]
def makeplayercolors(player):
"""Make the colors for a player"""
colo = possiblecolors[0]
possiblecolors.remove(colo)
col = colo[0] * 2**16 + colo[1] * 2**8 + colo[2]
back = 2**24-1
pc = hex(col)[2:]
pc = "0" * (6 - len(pc)) + pc
backcolors.append("#" + pc)
pc = hex(back)[2:]
pc = "0" * (6 - len(pc)) + pc
playercolors.append("#" + pc)
playercolors = []
backcolors = []
def newplayer(p):
"""Create a new player"""
makeplayercolors(p)
statbrd.add_player(p)
def loadterritorygraphics(xmlFile):
"""Load graphics information/graphics from a file"""
global territories
territories = {}
territoryStructure = xmlFile.getElementsByTagName("territory")
for i in territoryStructure:
tname = i.getAttribute("name")
grafile = i.getElementsByTagName("file")[0].childNodes[0].data
attributes = i.getElementsByTagName("attributes")[0]
x = int(attributes.getAttribute("x"))
y = int(attributes.getAttribute("y"))
w = int(attributes.getAttribute("w"))
h = int(attributes.getAttribute("h"))
cx = int(attributes.getAttribute("cx"))
cy = int(attributes.getAttribute("cy"))
floodpoints = i.getElementsByTagName("floodpoint")
fps = []
for fp in floodpoints:
fpx = int(fp.getAttribute("x"))
fpy = int(fp.getAttribute("y"))
fps.append((fpx,fpy))
shaded = opengraphic(grafile)
t = Territory(tname, x, y, w, h, cx, cy)
t.photo = shaded
t.shadedimage = ImageTk.PhotoImage(shaded)
t.currentimage = ImageTk.PhotoImage(t.photo.point(lambda x:1.2*x))
territories[tname] = t
t.floodpoints = fps
del shaded
playing = False
def toggle_playing():
global playing, play_button
if playing:
playing = False
play_button.config(text="Play")
else:
playing = True
play_button.config(text="Pause")
play_button = None
def play_log():
global current_state
if playing:
nextstate()
#delay = 500
delay = 50
#if current_state.turn_type == 'Place' or current_state.turn_type == 'PrePlace':
if current_state.turn_type == 'Place':
num_free = current_state.players[current_state.current_player].free_armies
if num_free > 3:
delay = 50
elif num_free > 1:
delay = 250
root.after(delay, play_log)
def setupdata():
"""Start the game"""
global territories, canvas, root, gameMenu, playerMenu
global totim, zfile, statbrd, play_button
root = tk.Tk()
root.title("PyRiskGameViewer")
zfile = zipfile.ZipFile("world.zip")
graphics = xml.dom.minidom.parseString(zfile.read("territory_graphics.xml"))
loadterritorygraphics(graphics)
totalframe = tk.Frame(root)
lframe = tk.Frame(totalframe)
statbrd = StatBoard(lframe, bd=2)
statbrd.pack(anchor=tk.N, fill=tk.Y, expand=tk.NO)
tk.Button(lframe, text="Next State",
command = nextstate).pack(padx=15,pady=15)
lframe.pack(side=tk.LEFT, anchor=tk.S, fill=tk.Y)
play_button = tk.Button(lframe, text="Play", command = toggle_playing)
play_button.pack(padx=15,pady=15)
lframe.pack(side=tk.LEFT, anchor=tk.S, fill=tk.Y)
canvas = tk.Canvas(totalframe,
height=graphics.childNodes[0].getAttribute("height"),
width=graphics.childNodes[0].getAttribute("width"))
boardname = graphics.getElementsByTagName("board")[0].childNodes[0].data
total = opengraphic(boardname)
totim = ImageTk.PhotoImage(total)
canvas.create_image(0, 0, anchor=tk.NW, image=totim, tags=("bgr",))
for terr in range(len(riskboard.territories)):
drawterritory(terr, 0)
del total
canvas.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.BOTH)
totalframe.pack(expand=tk.YES, fill=tk.BOTH)#set up message area
gc.collect()
play_log()
logfile = None
current_state = None
riskboard = None
turn_number = 0
state_number = 0
def display_current_state(last_action):
if len(backcolors) == 0:
#Load players from current state
for p in current_state.players:
newplayer(p)
#Update stats display
statbrd.update_statBoard(current_state, last_action, previous_player)
#Draw territories that might have changed
changed_t = [last_action.to_territory, last_action.from_territory]
prev_t = []
if previous_action and previous_action.type != 'TurnInCards':
prev_t = [previous_action.to_territory, previous_action.from_territory]
if last_action.type != 'TurnInCards':
for t in changed_t:
if t is not None and t not in prev_t:
tidx = current_state.board.territory_to_id[t]
drawterritory(tidx, (255,255,0, 255))
if t is not None:
tidx = current_state.board.territory_to_id[t]
drawarmy(tidx)
#Redraw the old territories normal again
if previous_action and previous_action.type != 'TurnInCards':
for t in prev_t:
if t is not None and t not in changed_t:
tidx = current_state.board.territory_to_id[t]
drawterritory(tidx)
if previous_action and previous_action.type == 'TurnInCards':
# toggle_playing()
# print("TURNED IN CARDS!!!")
for i in [previous_action.to_territory, previous_action.from_territory, previous_action.troops]:
if i is not None:
t = current_state.board.cards[i].territory
drawterritory(t) #Cards store territory id
def nextstate(read_action=True):
global current_state, logover, previous_action, previous_player, playing, turn_number, state_number
if logover:
print('LOG IS OVER NOW!')
statbrd.log_over()
playing = False
return
last_action = risktools.RiskAction(None,None,None,None)
if logfile is not None:
if read_action:
newline = logfile.readline()
splitline = newline.split('|')
if not newline or splitline[0] == 'RISKRESULT':
print('We have reached the end of the logfile')
print(newline)
logover = True
else:
last_action.from_string(newline) #This gets next action
if not logover:
current_state.from_string(logfile.readline(),riskboard)
if not logover:
# print("CURRENT STATE THAT IS BEING DISPLAYED:")
# current_state.print_state()
display_current_state(last_action)
previous_action = last_action
if current_state.current_player != previous_player:
turn_number += 1
state_number += 1
previous_player = current_state.current_player
if not logover and current_state.turn_type == 'GameOver':
logover = True
if logfile is not None:
newline = logfile.readline()
print('GAME OVER: RESULT:')
print(newline)
previous_action = None
logover = False
previous_player = None
if __name__ == "__main__":
#Set things up then call root.mainloop
if len(sys.argv) < 2:
print('Requires logfile!')
sys.exit()
logfile = open(sys.argv[1]) #This is passed in
l1 = logfile.readline() #Board
#Set up risk stuff
riskboard = risktools.loadBoard('world.zip')
current_state = risktools.getInitialState(riskboard)
#Set up display stuff
setupdata()
#Call to get things started
nextstate(False)
root.mainloop()