forked from zheplusplus/sgs
/
game.py
106 lines (95 loc) · 3.73 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
from hashlib import sha256
import time
from core.src.game_control import GameControl
from core.src.event import EventList
from core.src.action_stack import ActionStack
import core.src.card as card
import core.src.ret_code as ret_code
from ext.src.players_control import PlayersControl
from ext.src.player import Player
import ext.src.skills.bequeathed_strategy as bequeathed_strategy
import test_data
class GameRoom:
def __init__(self):
self.players_tokens = []
self.response = self.before_game_start
def before_game_start(self, path, request_body):
if path == '/ctrl/start':
return self.start()
if path == '/ctrl/add':
return self.add_player(time.time(), request_body)
return { 'code': 404 }
def after_game_start(self, path, request_body):
try:
if path == '/act':
return self.player_act(eval(request_body, dict(), dict()))
if path == '/info/events':
return {
'code': 200,
'events': self.get_events(eval(request_body,
dict(), dict()))
}
if path == '/info/hint':
hint = self.gc.hint()
hint['code'] = 200
return hint
return { 'code': 404 }
except (NameError, SyntaxError), e:
return {
'code': ret_code.BAD_REQUEST,
'reason': 'syntax error',
}
def add_player(self, time, name):
try:
token = sha256(name + str(time)).hexdigest()
self.players_tokens.append(token)
return {
'code': ret_code.OK,
'token': token,
}
except ValueError, e:
return {
'code': ret_code.BAD_REQUEST,
'reason': e.message,
}
def start(self):
if len(self.players_tokens) < 2:
return {
'code': ret_code.BAD_REQUEST,
'reason': 'Need at least 2 players',
}
pc = PlayersControl()
self.gc = GameControl(EventList(), card_pool, pc, ActionStack())
for token in self.players_tokens: pc.add_player(Player(token, 4))
self.gc.start()
self.response = self.after_game_start
return { 'code': ret_code.OK }
def get_events(self, args):
try:
return self.gc.get_events(args['token'], args['previous event id'])
except KeyError, e:
return {
'code': ret_code.BAD_REQUEST,
'reason': ret_code.BR_MISSING_ARG % e.message,
}
def player_act(self, args):
return self.gc.player_act(args)
game_room = GameRoom()
card_pool = test_data.CardPool(test_data.gen_cards([
test_data.CardInfo('slash', 1, card.SPADE),
test_data.CardInfo('slash', 2, card.CLUB),
test_data.CardInfo('slash', 3, card.HEART),
test_data.CardInfo('slash', 4, card.HEART),
test_data.CardInfo('dodge', 5, card.HEART),
test_data.CardInfo('dodge', 6, card.HEART),
test_data.CardInfo('dodge', 7, card.HEART),
test_data.CardInfo('dodge', 8, card.HEART),
test_data.CardInfo('slash', 9, card.SPADE),
test_data.CardInfo('slash', 10, card.CLUB),
test_data.CardInfo('dodge', 11, card.HEART),
test_data.CardInfo('dodge', 12, card.HEART),
test_data.CardInfo('dodge', 13, card.HEART),
test_data.CardInfo('dodge', 1, card.DIAMOND),
test_data.CardInfo('dodge', 2, card.DIAMOND),
test_data.CardInfo('dodge', 3, card.DIAMOND),
]))