/
client.py
295 lines (214 loc) · 9.41 KB
/
client.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
import pygame
import datetime
import time
from collections import Counter
import twl
import api
import sys
from network import Network
import pickle
from banana import Banana
from banana import GameState
from take import Take
from graphics_tiles import Graphics
import pygame, sys
from pygame.locals import *
time_check = False
print_check = True
no_prefix_suffix = True
# Need to display pygame
not_allowed_prefixes = ['UN', 'RE']
not_allowed_suffixes = ['S', 'ED', 'D', 'ES', 'ER', 'R', 'OR', 'ING', 'EST', 'IEST', 'LY', 'TION', 'SION']
word_add_twl = ['acai', 'roo', 'tix']
FPS = 30
letter_keys = [K_a, K_b, K_c, K_d, K_e, K_d, K_e, K_f, K_g, K_h, K_i, K_j, K_k, K_l, K_m, K_n, K_o, K_p, K_q, K_r, K_s, K_t, K_u, K_t, K_v, K_w, K_x, K_y, K_z]
def other_player(player):
if player == 0:
return 1
else:
return 0
##### MAIN #####
global FPSCLOCK, DISPLAYSURF
pygame.init()
FPSCLOCK = pygame.time.Clock()
# DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Anagrams')
# dict to send to server
send_dict = {'event': 'none',
'take': None,
'time_since_update': None,
}
# get the initial game state
guess = ''
status = ''
last_update = 0
last_type = 0
taker = None
clock = pygame.time.Clock()
net = Network()
print("Getting ID")
player = net.get_id()
print("Initializing graphics")
graphics = Graphics()
print("Sending send_dict")
game = Banana(net.send(send_dict))
print("Displaying initial elements")
# DISPLAYSURF.fill(BGCOLOR)
graphics.printstatus(game, player, ['flip', 'status', 'guess'], guess, status, taker)
# pygame.display.update()
# Time check variables
while True:
FPSCLOCK.tick(60)
graphics_to_update = []
if time_check:
start_loop = time.time()
total_take = 0
total_send = 0
total_update = 0
total_display = 0
# DISPLAYSURF.fill(BGCOLOR)
# -------------
# | GAME LOOP |
# -------------
last = None
for event in pygame.event.get():
print(f"EVENT: {event}")
if event.type == QUIT:
pygame.quit()
sys.exit()
if game.game_state.game_over and time.time() - last_update > 3.0:
if player == 0:
status = f"No more tiles! Your score: {sum([len(i) for i in game.game_state.player1words_list])}, Opponent's score: {sum([len(i) for i in game.game_state.player2words_list])}"
else:
status = f"No more tiles! Your score: {sum([len(i) for i in game.game_state.player2words_list])}, Opponent's score: {sum([len(i) for i in game.game_state.player1words_list])}"
graphics_to_update = graphics_to_update + ['status']
if event.type == KEYDOWN:
if event.key == K_BACKSPACE:
if guess == '':
pass
else:
# Delete one letter from the guess
guess = guess[:-1]
graphics_to_update = graphics_to_update + ['guess']
last_type = time.time()
elif event.key == K_SPACE:
# Don't do anything if input is a space
pass
elif event.key == K_RETURN:
# if Return and no guess is present, then flip next tile. If guess is present, see if it's a take
if guess == '':
if not game.game_state.game_over:
send_dict['event'] = 'flip_request'
graphics_to_update = graphics_to_update + ['flip_status']
else:
take_start_time = time.time()
if time_check:
start_take = take_start_time
guess_upper = guess.upper()
result, take_obj = game.take(guess_upper, player, last_update)
guess = ''
graphics_to_update = graphics_to_update + ['guess', 'status']
if result == 'steal':
send_dict['event'] = 'steal'
send_dict['take'] = take_obj
else:
send_dict['event'] = 'none'
if result == 'tiles':
status = f"Tiles aren't there! ({guess_upper})"
elif result == 'trivial':
status = f"Same root! ({game.same_root_word} and {guess_upper} share root {game.root})"
elif result == 'prefix_suffix':
status = f"Prefix or suffix not allowed! ({guess_upper})"
elif result == 'not_word':
status = f'Not a word! ({guess_upper})'
elif result == 'short':
status = f'Too short! ({guess_upper})'
else:
print("Error! Result is none of the options!")
if time_check:
end_take = time.time()
total_take = end_take - start_take
elif event.key in letter_keys:
if event.key != last:
# if letter is typed then add it to the current guess
# guess = guess + event.unicode.upper()
guess = guess + pygame.key.name(event.key).upper()
graphics_to_update = graphics_to_update + ['guess']
last_type = time.time()
last = event.key
# ----------
# | UPDATE |
# ----------
current_time = time.time()
print(f"SEND DICT: {send_dict}")
if send_dict['event'] != 'none' or time.time() - last_type > 0.2:
# Send our send_dict, receive a Banana
send_dict['time_since_update'] = time.time() - last_update
# print(send_dict)
if time_check:
start_send = time.time()
recv_game_state = net.send(send_dict)
if time_check:
end_send = time.time()
total_send = end_send - start_send
send_dict['event'] = 'none'
if recv_game_state and recv_game_state.update_number > game.game_state.update_number:
if time_check:
start_update = time.time()
game.game_state = recv_game_state
print(f"My ID: {player}")
print(f"Recv Game current: {recv_game_state.current}")
print(f"Game current: {game.game_state.current}")
print(f"Update event: {game.game_state.update_event}")
print(f"Recv flip status: {recv_game_state.flip_status}")
print(f"Flip status: {game.game_state.flip_status}")
print(f"Recv Player 1 words: {recv_game_state.player1words_list}")
print(f"Recv Player 2 words: {recv_game_state.player2words_list}")
print(f"Player 1 words: {game.game_state.player1words_list}")
print(f"Player 2 words: {game.game_state.player2words_list}")
if game.game_state.update_event == 'take':
last_update = time.time()
graphics_to_update = graphics_to_update + ['tiles', 'self_words', 'opp_words', 'status']
if game.game_state.last_take.taker == player:
taker = 'self'
taker_text = "You"
if game.game_state.last_take.victim == player:
victim = "yourself"
elif game.game_state.last_take.victim == other_player(player):
victim = "opponent"
else:
victim = "the middle"
else:
taker = 'opp'
taker_text = "Opponent"
if game.game_state.last_take.victim == player:
victim = "you"
elif game.game_state.last_take.victim == other_player(player):
victim = "themselves"
else:
victim = "the middle"
if victim == "the middle":
status = f"{taker_text} took {game.game_state.last_take.candidate} from {victim}!"
else:
status = f"{taker_text} took {game.game_state.last_take.candidate} from {victim}! ({game.game_state.last_take.taken_word} -> {game.game_state.last_take.candidate})"
elif game.game_state.update_event == 'flip_request':
graphics_to_update = graphics_to_update + ['flip_status']
elif game.game_state.update_event == 'flip':
graphics_to_update = graphics_to_update + ['flip_status', 'tiles']
last_update = time.time()
if time_check:
end_update = time.time()
total_update = end_update - start_update
if time_check:
start_printstatus = time.time()
graphics.printstatus(game, player, graphics_to_update, guess, status, taker)
if time_check:
total_printstatus = time.time() - start_printstatus
start_display = time.time()
# print(f"Updating the displays: {rects_to_update}")
# pygame.display.update(rects_to_update)
if time_check:
total_display = time.time() - start_display
if time_check:
total_loop = time.time() - start_loop
print(f"Total time: {total_loop}, Take time: {total_take}, Send time: {total_send}, Update time: {total_update}, Printstatus time: {total_printstatus}, Display time: {total_display} ")