-
Notifications
You must be signed in to change notification settings - Fork 3
/
viewer.py
483 lines (384 loc) · 17.1 KB
/
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
"""Viewer application."""
import argparse
import asyncio
import json
import logging
import os
import random
import websockets
import pygame
from consts import RANKS, Tiles
from mapa import Map
from game import reduce_score
from scores import HighScoresFetch
logging.basicConfig(level=logging.DEBUG)
logger_websockets = logging.getLogger("websockets")
logger_websockets.setLevel(logging.WARN)
logger = logging.getLogger("Map")
logger.setLevel(logging.DEBUG)
MAP_X_INCREASE = 4
MAP_Y_INCREASE = 2
RIGHT_INFO_MARGIN_TOP = 35
RIGHT_INFO_MARGIN_RIGHT = 25
RIGHT_INFO_MARGIN_LEFT = 25
RIGHT_INFO_MARGIN_BOTTOM = 40
DATA_INDEX_BEST_ROUND = ["level", "timestamp", "timestamp2", "score", "total_moves", "total_pushes", "total_steps"]
BEST_ROUND_TITLE = "Best Round"
DATA_INDEX_CURR_ROUND = ["Moves", "Pushes", "Steps"]
CURR_ROUND_TITLE = "Current Round"
SCORE_INFO = {
"level": "Level",
"score": "Score",
"timestamp": "Date",
"timestamp2": "",
"total_moves": "Moves",
"total_pushes": "Pushes",
"total_steps": "Steps"
}
KEEPER = {
"up": (3 * 64, 4 * 64),
"left": (3 * 64, 6 * 64),
"down": (0, 4 * 64),
"right": (0, 6 * 64),
}
BOX = (7 * 64, 0)
BOX_ON_GOAL = (9 * 64, 0)
GOAL = (12 * 64, 5 * 64)
WALL = (8 * 64, 6 * 64)
PASSAGE = (12 * 64, 6 * 64)
GREEN_PASSAGE = (10 * 64, 6 * 64)
GRAY_PASSAGE = (11 * 64, 6 * 64)
BLACK_SURFACE = (11 * 64, 0)
CHAR_LENGTH = 64
CHAR_SIZE = CHAR_LENGTH, CHAR_LENGTH
SCALE = 1
COLORS = {
"white": (255, 255, 255),
"black": (0, 0, 0),
"red": (255, 0, 0),
"pink": (255, 105, 180),
"blue": (135, 206, 235),
"orange": (255, 165, 0),
"yellow": (255, 255, 0),
"grey": (120, 120, 120),
"light_blue": (58, 240, 240),
}
SPRITES = None
SCREEN = None
async def messages_handler(websocket_path, queue):
"""Handles server side messages, putting them into a queue."""
async with websockets.connect(websocket_path) as websocket:
await websocket.send(json.dumps({"cmd": "join"}))
while True:
update = await websocket.recv()
queue.put_nowait(update)
class Artifact(pygame.sprite.Sprite):
"""Representation of moving pieces."""
def __init__(self, *args, **kw):
x, y = kw.pop("pos", ((kw.pop("x", 0), kw.pop("y", 0))))
new_pos = scale((x, y))
self.x, self.y = new_pos[0], new_pos[1]
self.image = pygame.Surface(CHAR_SIZE)
self.rect = pygame.Rect(new_pos + CHAR_SIZE)
self.update((x, y))
super().__init__(*args, **kw)
def update(self, pos=None):
"""Updates the sprite with a new position."""
if not pos:
pos = self.x, self.y
else:
pos = scale(pos)
self.rect = pygame.Rect(pos + CHAR_SIZE)
self.image.fill((0, 0, 230))
self.image.blit(SPRITES, (0, 0), (*PASSAGE, *scale((1, 1))))
self.image.blit(*self.sprite)
# self.image = pygame.transform.scale(self.image, scale((1, 1)))
self.x, self.y = pos
class Keeper(Artifact):
"""Handles Keeper Sprites."""
def __init__(self, *args, **kw):
self.direction = "left"
self.sprite = (SPRITES, (0, 0), (*KEEPER[self.direction], *scale((1, 1))))
super().__init__(*args, **kw)
def update(self, pos=None):
x, y = scale(pos)
if x > self.x:
self.direction = "right"
if x < self.x:
self.direction = "left"
if y > self.y:
self.direction = "down"
if y < self.y:
self.direction = "up"
self.sprite = (SPRITES, (0, 0), (*KEEPER[self.direction], *scale((1, 1))))
super().update(tuple(pos))
class Box(Artifact):
"""Handles Box Sprites."""
def __init__(self, *args, **kw):
self.sprite = (SPRITES, (0, 0), (*BOX, *scale((1, 1))))
if kw.pop("stored"):
self.sprite = (SPRITES, (0, 0), (*BOX_ON_GOAL, *scale((1, 1))))
super().__init__(*args, **kw)
def clear_callback(surf, rect):
"""Beneath everything there is a passage."""
surf.blit(SPRITES, (rect.x, rect.y), (*PASSAGE, rect.width, rect.height))
def scale(pos):
"""Scale positions according to gfx."""
x, y = pos
return int(x * CHAR_LENGTH / SCALE), int(y * CHAR_LENGTH / SCALE)
def draw_background(mapa):
"""Create background surface."""
map_x, map_y = mapa.size
background = pygame.Surface(scale((map_x+MAP_X_INCREASE, map_y+MAP_Y_INCREASE)))
separator = True
for x in range(map_x+MAP_X_INCREASE):
if x == map_x+1:
separator = False
for y in range(map_y+MAP_Y_INCREASE):
wx, wy = scale((x, y))
if x < map_x and y < map_y:
background_sprite = sprite = PASSAGE
if mapa.get_tile((x, y)) == Tiles.WALL:
sprite = WALL
if mapa.get_tile((x, y)) in [Tiles.GOAL, Tiles.BOX_ON_GOAL, Tiles.MAN_ON_GOAL]:
sprite = GOAL
else:
background_sprite = GRAY_PASSAGE
if y > map_y:
sprite = GRAY_PASSAGE
else:
if separator or y == map_y:
sprite = BLACK_SURFACE
else:
sprite = GREEN_PASSAGE
# needed to fill the background of sprites with transparency
background.blit(SPRITES, (wx, wy), (*background_sprite, *scale((1, 1))))
background.blit(SPRITES, (wx, wy), (*sprite, *scale((1, 1))))
return background
def draw_info(surface, text, pos, color=COLORS["black"], background=None, size=24):
"""Creates text based surfaces for information display."""
myfont = pygame.font.Font(None, int(size / SCALE))
textsurface = myfont.render(text, True, color, background)
x, y = pos
if x > surface.get_width():
pos = surface.get_width() - (textsurface.get_width() + 10), y
if y > surface.get_height():
pos = x, surface.get_height() - textsurface.get_height()
if background:
surface.blit(background, pos)
else:
erase = pygame.Surface(textsurface.get_size())
erase.fill(COLORS["grey"])
surface.blit(textsurface, pos)
return textsurface.get_width(), textsurface.get_height()
# get size of draw without drawing it
def get_draw_size(text):
textsurface = pygame.font.Font(None, int(24 / SCALE)).render(text, True, COLORS["black"])
return textsurface.get_width(), textsurface.get_height()
# draw a table providing position for top and margin for right
def draw_table_right_top(canvas, area_width, col_l_info, col_r_info, title_info, max_width, positions):
col_l, col_l_color = col_l_info
col_r, col_r_color = col_r_info
title, title_color = title_info
margin_left, pos_top, margin_right = positions
canvas_width = canvas.get_width()
title_width, title_height = get_draw_size(title)
# draw title of table
draw_info(canvas, title, (canvas_width-center_text_margin(area_width, title_width), pos_top), title_color)
current_height = initial_height = pos_top+title_height+20
for i, col in enumerate(col_l):
current_height = initial_height + i*20
col_r_content = col_r[i]
# draw left side of column positioned within the area_width with a margin on the left
draw_info(canvas, col, (canvas_width-area_width+margin_left, current_height), col_l_color)
# draw right side of colum positioned within the area_width with a margin on the right
draw_info(canvas, col_r_content, (canvas_width-get_draw_size(col_r_content)[0]-margin_right, current_height), col_r_color)
return current_height
def get_largest_width_for_table(col1, col2, title):
return max([get_draw_size(max(col1, key=lambda s:len(s)))[0]+get_draw_size(max(col2, key=lambda s:len(s)))[0]]+[get_draw_size(title)[0]])
def center_text_margin(canvas_width, text_width):
return (canvas_width-text_width)/2+text_width
async def main_loop(queue):
"""Processes events from server and display's."""
global SPRITES, SCREEN
main_group = pygame.sprite.LayeredUpdates()
boxes_group = pygame.sprite.OrderedUpdates()
logging.info("Waiting for map information from server")
state = await queue.get() # first state message includes map information
logging.debug("Initial game status: %s", state)
newgame_json = json.loads(state)
GAME_SPEED = newgame_json["fps"]
try:
mapa = Map(newgame_json["map"])
except (KeyError, FileNotFoundError):
mapa = Map("levels/1.xsb") # Fallback to initial map
map_x, map_y = mapa.size
SCREEN = pygame.display.set_mode(scale((map_x+MAP_X_INCREASE, map_y+MAP_Y_INCREASE)))
SPRITES = pygame.image.load("data/sokoban.png").convert_alpha()
BACKGROUND = draw_background(mapa)
SCREEN.blit(BACKGROUND, (0, 0))
main_group.add(Keeper(pos=mapa.keeper))
state = {
"score": 0,
"player": "player1",
"keeper": mapa.keeper,
"boxes": mapa.boxes,
}
new_event = True
player = last_player = state['player']
hs = HighScoresFetch(name=state['player'])
best_entry = None
while True:
if "player" in state:
curr_player = state['player']
SCREEN.blit(BACKGROUND, (0, 0))
pygame.event.pump()
if pygame.key.get_pressed()[pygame.K_ESCAPE]:
asyncio.get_event_loop().stop()
main_group.clear(SCREEN, clear_callback)
boxes_group.clear(SCREEN, clear_callback)
# size of each square
map_square_size = (SCREEN.get_width()/(map_x+MAP_X_INCREASE), SCREEN.get_height()/(map_y+MAP_Y_INCREASE))
# width of extra space added to the map, except the separating black wall
extra_available_space_width = map_square_size[0]*(MAP_X_INCREASE-1)
if "score" in state and "player" in state:
if last_player != curr_player:
hs = HighScoresFetch(name=state['player'])
if hs.data != []:
best_entry = hs.get_best_entry(type="max", key="score")
split_timestamp = best_entry["timestamp"].split("T")
# adjust best_entry dict
best_entry["timestamp"] = split_timestamp[0]
best_entry["timestamp2"] = split_timestamp[1]
formated_col_l, formated_col_r = [SCORE_INFO[d] for d in DATA_INDEX_BEST_ROUND], [str(best_entry[info]) for info in DATA_INDEX_BEST_ROUND]
fixed_width = get_largest_width_for_table(formated_col_l, formated_col_r, BEST_ROUND_TITLE)+RIGHT_INFO_MARGIN_RIGHT
player = state['player']
last_player = curr_player
# draw player info
player_h_from_top = SCREEN.get_height() - RIGHT_INFO_MARGIN_BOTTOM
player_w, _ = get_draw_size(player)
draw_info(SCREEN, player, (SCREEN.get_width()-player_w-RIGHT_INFO_MARGIN_RIGHT, player_h_from_top), COLORS["light_blue"])
draw_info(SCREEN, "Player: ", (SCREEN.get_width()-player_w-get_draw_size("Player: ")[0]-RIGHT_INFO_MARGIN_RIGHT-5, player_h_from_top), COLORS["white"])
current_height = RIGHT_INFO_MARGIN_TOP
if hs != None and hs.data != []:
# table for best round
current_height = draw_table_right_top(SCREEN, extra_available_space_width, (formated_col_l, COLORS["black"]), (formated_col_r, COLORS["white"]), (BEST_ROUND_TITLE, COLORS["yellow"]), fixed_width, (RIGHT_INFO_MARGIN_LEFT, current_height, RIGHT_INFO_MARGIN_RIGHT))
# some conditions to coop with state['score']
if not isinstance(state['score'], int) and len(state['score']) > len(DATA_INDEX_CURR_ROUND):
curr_round_data_fetch = [str(state['score'][i+1]) for i in range(len(DATA_INDEX_CURR_ROUND))]
# if there is no data from best round, adapt the fixed size to the data of current round
if hs.data == []:
fixed_width = get_largest_width_for_table(curr_round_data_fetch, DATA_INDEX_CURR_ROUND, CURR_ROUND_TITLE)+RIGHT_INFO_MARGIN_RIGHT
else:
# if there is best round table, then create a separation with the current round table
current_height+= RIGHT_INFO_MARGIN_TOP
# table for current round
current_height = draw_table_right_top(SCREEN, extra_available_space_width, (DATA_INDEX_CURR_ROUND, COLORS["black"]), (curr_round_data_fetch, COLORS["white"]), (CURR_ROUND_TITLE, COLORS["red"]), fixed_width, (RIGHT_INFO_MARGIN_LEFT, current_height, RIGHT_INFO_MARGIN_RIGHT))
if "level" in state:
draw_info(
SCREEN,
f"{state['level']}",
(SCREEN.get_width()-extra_available_space_width+RIGHT_INFO_MARGIN_LEFT, current_height+RIGHT_INFO_MARGIN_TOP),
color=COLORS["white"], size=50
)
if "level" not in state and "highscores" not in state:
for i, word in enumerate(["Run a client ", "to see scores!"]):
word_w, word_h = get_draw_size(word)
draw_info(SCREEN, word, (SCREEN.get_width()-center_text_margin(extra_available_space_width, word_w), RIGHT_INFO_MARGIN_TOP+word_h+i*20), COLORS["white"])
if "boxes" in state:
boxes_group.empty()
for box in state["boxes"]:
boxes_group.add(
Box(
pos=box,
stored=mapa.get_tile(box) in [Tiles.GOAL, Tiles.BOX_ON_GOAL],
)
)
boxes_group.draw(SCREEN)
main_group.draw(SCREEN)
# Highscores Board
if "highscores" in state and "player" in state:
if new_event:
highscores = state["highscores"]
highscores.append(
(f"<{state['player']}>", reduce_score(*state["score"]))
)
highscores = sorted(highscores, key=lambda s: s[1])
highscores = highscores[: len(RANKS)]
HIGHSCORES = pygame.Surface((256, 280))
HIGHSCORES.fill((30, 30, 30))
COLS = [20, 80, 150]
draw_info(HIGHSCORES, "THE 10 BEST PLAYERS", (20, 10), COLORS["white"])
for value, column in zip(["RANK", "SCORE", "NAME"], COLS):
draw_info(HIGHSCORES, value, (column, 30), COLORS["orange"])
for i, highscore in enumerate(highscores):
color = (
random.randrange(66, 222),
random.randrange(66, 222),
random.randrange(66, 222),
)
for value, column in zip(
[RANKS[i + 1], str(highscore[1]), highscore[0]], COLS
):
draw_info(HIGHSCORES, value, (column, 60 + i * 20), color)
SCREEN.blit(
HIGHSCORES,
(
(SCREEN.get_width() - HIGHSCORES.get_width()) / 2,
(SCREEN.get_height() - HIGHSCORES.get_height()) / 2,
),
)
if "keeper" in state:
main_group.update(state["keeper"])
pygame.display.flip()
try:
state = json.loads(queue.get_nowait())
new_event = True
if "map" in state:
logger.debug("New Level!")
# New level! lets clean everything up!
try:
mapa = Map(state["map"])
except FileNotFoundError:
logger.error(
"Can't find levels/%s.xsb, means we have a WINNER!",
state["level"],
)
continue
map_x, map_y = mapa.size
SCREEN = pygame.display.set_mode(scale((map_x+MAP_X_INCREASE, map_y+MAP_Y_INCREASE)))
BACKGROUND = draw_background(mapa)
SCREEN.blit(BACKGROUND, (0, 0))
boxes_group.empty()
main_group.empty()
main_group.add(Keeper(pos=mapa.keeper))
pygame.display.flip()
except asyncio.queues.QueueEmpty:
await asyncio.sleep(1.0 / GAME_SPEED)
new_event = False
continue
if __name__ == "__main__":
SERVER = os.environ.get("SERVER", "localhost")
PORT = os.environ.get("PORT", "8000")
parser = argparse.ArgumentParser()
parser.add_argument("--server", help="IP address of the server", default=SERVER)
parser.add_argument(
"--scale", help="reduce size of window by x times", type=int, default=1
)
parser.add_argument("--port", help="TCP port", type=int, default=PORT)
arguments = parser.parse_args()
SCALE = arguments.scale
LOOP = asyncio.get_event_loop()
pygame.font.init()
q = asyncio.Queue()
PROGRAM_ICON = pygame.image.load("data/icon.png")
pygame.display.set_icon(PROGRAM_ICON)
ws_path = f"ws://{arguments.server}:{arguments.port}/viewer"
try:
LOOP.run_until_complete(
asyncio.gather(messages_handler(ws_path, q), main_loop(q))
)
except RuntimeError as err:
logger.error(err)
finally:
LOOP.stop()