-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
438 lines (344 loc) · 14.6 KB
/
app.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
from operator import le
from typing import Any, Callable
from enum import Enum
import random
import datetime
from functools import cache
import abc
import os
import json
import time
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
import boto3
class Wordlist:
def __init__(self, filename: str = "/usr/share/dict/words"):
self.filename = filename
@cache
def get_words(self):
with open(self.filename) as f:
return f.read().splitlines()
@cache
def words_of_length(self, length: int):
return [word for word in self.get_words() if len(word) == length]
def __contains__(self, word: str):
return word in self.get_words()
def get_random_word(self, length: int = None):
if length is None:
return random.choice(self.get_words())
else:
return random.choice(self.words_of_length(length))
class Score(str, Enum):
INCORRECT_LETTER = "INCORRECT_LETTER"
WRONG_LOCATION = "WRONG_LOCATION"
CORRECT = "CORRECT"
INVALID_GUESS = "INVALID_GUESS"
OUT_OF_GUESSES = "OUT_OF_GUESSES"
NOT_A_WORD = "NOT_A_WORD"
# common5.txt in the same directory as this file
wordlist = Wordlist(filename=os.path.join(os.path.dirname(__file__), "common5.txt"))
wordlist10 = Wordlist(filename=os.path.join(os.path.dirname(__file__), "common10.txt"))
all_valid_words = Wordlist(
filename=os.path.join(os.path.dirname(__file__), "wordlist.txt")
)
class Game:
def __init__(
self,
answer: str = None,
number_of_guesses: int = 6,
guesses_remaining: int = None,
guesses: Any = None,
scores: Any = None,
):
self._number_of_guesses = number_of_guesses
self._guesses_remaining = guesses_remaining or number_of_guesses
self._guesses = guesses or []
self._scores = scores or []
self._answer = (answer or wordlist.get_random_word(length=5)).lower()
self._all_valid_words = all_valid_words
@staticmethod
def from_game_state(game_state: dict):
return Game(
answer=game_state["answer"],
number_of_guesses=game_state["number_of_guesses"],
guesses_remaining=game_state["guesses_remaining"],
guesses=game_state["guesses"],
scores=game_state["scores"],
)
def to_game_state(self):
return {
"answer": self._answer,
"number_of_guesses": self._number_of_guesses,
"guesses_remaining": self._guesses_remaining,
"guesses": self._guesses,
"scores": self._scores,
}
def get_answer(self):
return self._answer
def get_guesses_remaining(self):
return self._guesses_remaining
def guess(self, guess: str):
"""
Doctests:
>>> game = Game(answer="CADDY")
>>> game.guess("DADDY")
[<Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>]
>>> game = Game(answer="CADET")
>>> game.guess("CATTY")
[<Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>, <Score.WRONG_LOCATION: 'WRONG_LOCATION'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>]
>>> game = Game(answer="FROGS")
>>> game.guess("CATTY")
[<Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>]
>>> game = Game(answer="DADDY")
>>> game.guess("DOUBT")
[<Score.CORRECT: 'CORRECT'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>]
>>> game = Game(answer="CRANK")
>>> game.guess("BILLY")
[<Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>]
>>> game = Game(answer="CRANK")
>>> game.guess("TEALS")
[<Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.CORRECT: 'CORRECT'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>]
>>> game = Game(answer="CRANK")
>>> game.guess("SALAD")
[<Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.WRONG_LOCATION: 'WRONG_LOCATION'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>]
>>> game = Game(answer="CRANK")
>>> game.guess("GRAPE")
[<Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>]
>>> game = Game(answer="CRANK")
>>> game.guess("CARRY")
[<Score.CORRECT: 'CORRECT'>, <Score.WRONG_LOCATION: 'WRONG_LOCATION'>, <Score.WRONG_LOCATION: 'WRONG_LOCATION'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>, <Score.INCORRECT_LETTER: 'INCORRECT_LETTER'>]
>>> game = Game(answer="CRANK")
>>> game.guess("CRANK")
[<Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>, <Score.CORRECT: 'CORRECT'>]
"""
guess = guess.lower()
if self.get_guesses_remaining() == 0:
return Score.OUT_OF_GUESSES
if len(guess) != len(self._answer):
return Score.INVALID_GUESS
if guess not in self._all_valid_words:
return Score.NOT_A_WORD
self._guesses_remaining -= 1
if guess == self._answer:
return [Score.CORRECT for _ in guess]
scores = [Score.INCORRECT_LETTER for _ in guess]
letters_marked_correct = {letter: 0 for letter in guess}
# First, mark the obviously-right letters:
for i, (guess_letter, answer_letter) in enumerate(zip(guess, self._answer)):
if guess_letter == answer_letter:
scores[i] = Score.CORRECT
letters_marked_correct[guess_letter] = 1
# Then, mark the letters that are in the wrong location.
# To be considered in the wrong location,
# the guess letter must be in the answer
# and not already marked as correct.
for i, (guess_letter, answer_letter) in enumerate(zip(guess, self._answer)):
if (
guess_letter in self._answer
and scores[i] != Score.CORRECT
and letters_marked_correct[guess_letter] == 0
):
scores[i] = Score.WRONG_LOCATION
letters_marked_correct[guess_letter] -= 1
return scores
class GameStatePersister(abc.ABC):
def save_game(self, uuid: str, state: dict):
...
def load_game(self, uuid: str):
...
class JSONFileGameStatePersister(GameStatePersister):
def __init__(self, filename: str = "game_state.json"):
self._filename = filename
# Create the file if it doesn't exist:
if not os.path.exists(filename):
with open(filename, "w") as f:
f.write("{}")
def _get_contents(self):
with open(self._filename, "r") as f:
return json.load(f)
def save_game(self, uuid: str, state: dict):
print("Saving game state", uuid, state)
# Get the current contents:
games = self._get_contents()
games[uuid] = state
# Write the new contents:
with open(self._filename, "w") as f:
json.dump(games, f, indent=4)
def load_game(self, uuid: str):
print("Loading game state", uuid)
games = self._get_contents()
game = games.get(uuid, None)
if game and game.get("game_status", "None") == "IN_PROGRESS":
return game
class DynamoDBGameStatePersister(GameStatePersister):
def __init__(self, dynamodb_table_name: str):
self._dynamodb_table_name = dynamodb_table_name
# Check to see if the table exists
dynamodb = boto3.resource("dynamodb")
table_exists = False
try:
dynamodb.Table(self._dynamodb_table_name).load()
table_exists = True
except:
pass
if not table_exists:
self._initialize_database()
def _initialize_database(self):
# Initialize the database by creating the table. The table has auto-
# scaling enabled, so it will automatically scale up and down as
# needed. (There is no provisioned read/write capacity.)
# Primary key is "uuid"
# Sort key is "game_status"
dynamodb = boto3.resource("dynamodb")
table = dynamodb.create_table(
TableName=self._dynamodb_table_name,
KeySchema=[
{"AttributeName": "uuid", "KeyType": "HASH"},
{"AttributeName": "game_status", "KeyType": "RANGE"},
],
AttributeDefinitions=[
{"AttributeName": "uuid", "AttributeType": "S"},
{"AttributeName": "game_status", "AttributeType": "S"},
],
BillingMode="PAY_PER_REQUEST",
)
# Wait until the table exists.
table.meta.client.get_waiter("table_exists").wait(
TableName=self._dynamodb_table_name
)
def save_game(self, uuid: str, state: dict):
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(self._dynamodb_table_name)
# Get the current contents:
response = table.get_item(Key={"uuid": uuid, "game_status": "IN_PROGRESS"})
if "Item" in response:
# Delete the old item:
table.delete_item(Key={"uuid": uuid, "game_status": "IN_PROGRESS"})
# Write the new item:
status = state.get("game_status", "IN_PROGRESS")
if status != "IN_PROGRESS":
status = f"-{int(time.time() * 1000)}"
table.put_item(
Item={
"uuid": uuid,
"game_status": status,
"state": json.dumps(state),
"last_updated": datetime.datetime.now().isoformat(),
}
)
def load_game(self, uuid: str):
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(self._dynamodb_table_name)
# Only get IN_PROGRESS games:
response = table.get_item(Key={"uuid": uuid, "game_status": "IN_PROGRESS"})
if "Item" in response:
state = response["Item"]["state"]
print(state)
return json.loads(state)
return None
class CustomFlask(Flask):
jinja_options = Flask.jinja_options.copy()
jinja_options.update(
dict(
block_start_string="<%",
block_end_string="%>",
variable_start_string="%%",
variable_end_string="%%",
comment_start_string="<#",
comment_end_string="#>",
)
)
# Uncomment this line to run a production database in AWS DynamoDB:
_game_state_store_factory = lambda: DynamoDBGameStatePersister("wordgame-state")
# Uncommon this line to run a local debug database:
# _game_state_store_factory = lambda: JSONFileGameStatePersister()
app = CustomFlask(__name__)
CORS(app)
def _index_endpoint():
return render_template("index.html")
def _disappointment_endpoint():
return render_template("disappointment.html")
def _get_game(uuid: str):
game_state_store = _game_state_store_factory()
return game_state_store.load_game(uuid)
def _save_game(uuid: str, state: dict):
game_state_store = _game_state_store_factory()
game_state_store.save_game(uuid, state)
def _create_game(uuid: str):
# Create a new game
game = Game(answer=wordlist.get_random_word(length=5))
# Save the game state to the database
state = game.to_game_state()
_save_game(uuid, state)
return state
def _create_disappointing_game(uuid: str):
# Create a new game
game = Game(number_of_guesses=20, answer=wordlist10.get_random_word(length=10))
# Save the game state to the database
state = game.to_game_state()
_save_game(uuid, state)
return state
def _game_endpoint_generic(uuid: str, wordlength: int = 5):
# if this is a GET, we just return the game state
game_state = _get_game(uuid)
if request.method == "GET":
if game_state is None:
return jsonify({"error": "NOT_FOUND"})
else:
# If the game is in progress, remove `answer`:
if game_state.get("game_status", None) == "IN_PROGRESS":
game_state["answer"] = None
return jsonify(game_state)
# get the guess from the request
guess = request.json["guess"]
if game_state is None:
game_state = (
_create_game(uuid) if wordlength == 5 else _create_disappointing_game(uuid)
)
# score the guess
game = Game.from_game_state(game_state)
score = game.guess(guess)
if isinstance(score, str):
return jsonify({"error": score})
# update the game state
game_state["guesses"].append(guess)
game_state["scores"].append(score)
game_state["guesses_remaining"] -= 1
# if you guessed correctly, the game is over
if game.get_answer() == guess:
game_state["game_status"] = "WON"
game_state["guesses_remaining"] = 0
# if you guessed incorrectly, and you have no guesses left, the game is over
elif game_state["guesses_remaining"] == 0:
game_state["game_status"] = "LOST"
game_state["guesses_remaining"] = 0
# otherwise, the game is still in progress
else:
game_state["game_status"] = "IN_PROGRESS"
# Save the game state to the database
_save_game(uuid, game_state)
# Return the game state
# If the game is in progress, remove `answer`:
if game_state.get("game_status", None) == "IN_PROGRESS":
game_state["answer"] = None
return jsonify(game_state)
def _game_endpoint_5(uuid: str):
return _game_endpoint_generic(uuid, 5)
def _game_endpoint_10(uuid: str):
return _game_endpoint_generic(uuid, 10)
app.add_url_rule(
"/game/<uuid>",
"game",
_game_endpoint_5,
methods=["POST", "GET"],
)
app.add_url_rule(
"/disappointing-game/<uuid>",
"disappointing-game",
_game_endpoint_10,
methods=["POST", "GET"],
)
app.add_url_rule("/", "index", _index_endpoint, methods=["GET"])
app.add_url_rule(
"/disappointment", "disappointment", _disappointment_endpoint, methods=["GET"]
)