-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
282 lines (227 loc) · 8.78 KB
/
game.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
import settings, string, utils, random, json
import asyncio
from collections import Counter
from bot import Bot
class Game:
def __init__(self, game_id, debug=False):
self.game_id = game_id
self._last_id = 0
self._players = {}
self._free_tiles = []
self._bag = []
self._word_list = None
self.bot = None
self.running = False
self.finished = False
self.debug = debug
self.player_idx_to_draw = 0
self._initialize_dict()
self._initialize_bag()
def _initialize_bag(self):
freq = settings.TILE_FREQUENCIES
self._bag = []
for i in range(26):
self._bag += [string.ascii_lowercase[i]] * freq[i]
def _initialize_dict(self):
self._word_list = utils.Trie()
with open(settings.DICT_FILE) as f:
for line in f:
line = line.strip()
if len(line) > 2:
self._word_list.add(line)
@property
def free_tiles(self):
"""Returns the list of free tiles in play"""
return self._free_tiles
@property
def all_words(self):
"""Returns the list of all words in play"""
words = [(word, player.id, i) for player in self.players.values() for (i, word) in enumerate(player.words)]
if self.bot:
words.extend([(word, self.bot.id, i) for (i, word) in enumerate(self.bot.words)])
return words
@property
def players(self):
return self._players
@property
def bag(self):
return self._bag
def add_player(self, player):
"""
:param player:
:return:
"""
self._players[player.id] = player
def join(self, player):
if player.id not in self._players:
self.add_player(player)
if player.active:
# should never happen because the join button should be disabled
if self.debug:
print("ERROR: active player joining a game")
return
if len(self._players) >= settings.MAX_PLAYERS:
self.send_personal(player.ws, "game_full")
return
if self.debug:
print(str(self._players))
if self.bot:
print(self.bot)
player.active = True
self.send_all("p_joined", player.id, player.name)
self.update_play_field()
def player_disconnected(self, player):
player.active = False
name = player.name
del self._players[player.id]
self.send_all("dc", name)
self.update_play_field()
if len(self._players) == 0:
self.finished = True
def create_bot(self):
if self.bot:
self.bot = None
self._last_id += 1
bot_id = self._last_id
while bot_id in self._players:
self._last_id += 1
bot_id = self._last_id
self.bot = Bot(self, bot_id)
self.send_all("p_joined", 0, self.bot.name)
self.update_play_field()
def remove_bot(self):
if not self.bot:
return None
name = self.bot.name
self.bot = None
self.send_all("dc", name)
self.update_play_field()
def play_word(self, word, player):
"""
:param word: string: proposed word
:param player: Player: player who proposed the word
:return: bool: if word was accepted or not
"""
pid, index, used_chars = self.is_valid(word)
if pid != -1:
# delete used_chars from free
for c in used_chars:
self._free_tiles.remove(c)
# update player scores
new_word_length = len(word)
player.score += new_word_length
if pid != 0:
if self.bot and pid == self.bot.id:
old_word_length = len(self.bot.words[index])
self.bot.score -= old_word_length
else:
old_word_length = len(self._players[pid].words[index])
self._players[pid].score -= old_word_length
# update the player word lists
player.words.append(word)
if pid != 0:
if self.bot and pid == self.bot.id:
del self.bot.words[index]
else:
del self._players[pid].words[index]
self.update_play_field()
self.send_all("valid_word", word, player.name)
return True
self.send_personal(player.ws, "invalid_word", word)
return False
def send_personal(self, ws, *args):
msg = json.dumps(args)
if self.debug:
print("(game) sending message: " + msg)
asyncio.ensure_future(ws.send_str(msg))
def send_all(self, *args):
# TODO: make this asynchronous, await the send_str
msg = json.dumps(args)
if self.debug:
print("(game) sending message to all: {}".format(msg))
for player in self._players.values():
asyncio.ensure_future(player.ws.send_str(msg))
# if player.ws:
# asyncio.ensure_future(player.ws.send_str(msg))
# print('sent to {}'.format(player))
def draw_tile(self, player):
if self.debug:
print("{} drew a tile".format(player.name))
if len(self._bag) == 0:
self.finished = True
return
tile = random.choice(self._bag)
self._bag.remove(tile)
self._free_tiles.append(tile)
self.player_idx_to_draw = (self.player_idx_to_draw + 1) % len(self._players)
self.update_play_field()
def reset(self):
self._initialize_bag()
self._free_tiles = []
self._last_id = 0
def is_valid(self, proposed_word):
"""
Checks if the word is valid or not. Checks if it is in the dictionary and if
it can be built with the words/characters already in play.
Returns a tuple with the following information:
(player_id, index, combo), where
player_id: either the player id, 0 for free words, and -1 for invalid words
index: index of the base word in player_id's list of words. For free words and invalid words, index = -1
combo: tuple of the characters used from free
:param proposed_word: string
:return: (int, int, tuple):
"""
if not self._word_list.has_word(proposed_word):
return -1, -1, ()
proposed_word_counter = Counter(proposed_word)
words = self.all_words
powerset = list(utils.powerset(self._free_tiles))[1:]
# check all existing words + free chars
for (word, player_id, index) in words:
if len(word) < len(proposed_word):
for combo in powerset:
if Counter(word + ''.join(combo)) == proposed_word_counter:
return player_id, index, combo
# check free chars
if len(proposed_word) <= len(self._free_tiles):
for combo in powerset:
if Counter(combo) == proposed_word_counter:
return 0, -1, combo
return -1, -1, ()
def update_current_players(self, ws):
message = "update_names"
names = [player.name for player in self._players.values() if player.active]
self.send_personal(ws, message, names)
def get_updated_play_field_payload(self):
message = "update"
num_tiles_left = len(self._bag)
free_tiles = self.free_tiles
player_words = {}
drawing_player = ""
i = 0
for player in self._players.values():
if i == self.player_idx_to_draw:
drawing_player = player.name
player_words[player.name] = player.words
i += 1
if self.bot:
player_words[self.bot.name] = self.bot.words
return message, free_tiles, num_tiles_left, player_words, drawing_player
def update_play_field(self):
message, free_tiles, num_tiles_left, player_words, drawer = self.get_updated_play_field_payload()
self.send_all(message, free_tiles, num_tiles_left, player_words, drawer)
self.update_score_field()
# make the bot think every time the board changes
if self.bot:
if self.bot.thinking:
self.bot.interrupted = True # TODO: fix the bugginess of this
else:
self.bot.think()
def update_score_field(self):
scores = [[player.name, player.score] for player in self._players.values() if player.active]
if self.bot:
scores.append([self.bot.name, self.bot.score])
self.send_all("scores", sorted(scores, key=lambda x: -x[1]))
def is_ready(self):
"""The game is okay to start"""
return not any((self._last_id, len(self._free_tiles), len(self._players)))