-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamemanager.py
43 lines (31 loc) · 1006 Bytes
/
gamemanager.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
import time
import redisclient
import mastermind
def new_id():
c = 0
while True:
timestamp = repr(int(time.time()*1000000))
if not redisclient.get(timestamp):
return timestamp
c += 1
if c == 10:
raise RuntimeError("Error: Can't generate an unused game id")
def new_game(length, alphabet):
game_id = new_id()
code = mastermind.generate_code(length, alphabet)
redisclient.set(
game_id,
dict(alphabet=alphabet, length=length, code=code, historic=[]))
return game_id
def get_game(game_id):
game_info = redisclient.get(game_id)
if not game_info:
raise IndexError("Game not found")
return game_info
def check_guess(game_id, guess):
game_info = get_game(game_id)
[b, w] = mastermind.compute_answer(game_info['code'], guess)
game_info['historic'].append(
{'ts': time.time(), 'guess': guess, 'b': b, 'w': w})
redisclient.set(game_id, game_info)
return [b, w]