-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.py
134 lines (91 loc) · 3.1 KB
/
event.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
# -*- coding: utf-8 -*-
import tornado.escape
ERROR = 0x0
LOGIN = 0x1
LOGOUT = 0x2
SAY = 0x3
FIGHT = 0x5
ROUND = 0x6
FLIP = 0x7
REPLY = 0x8
REVEAL = 0x9
WIN = 0xa
class Event(object):
def __init__(self, type, player):
self.type = type
self.player = player
def json(self, ws):
return tornado.escape.json_encode(self.resp_obj(ws))
def resp_obj(self, ws):
dic = dict(type=self.type)
dic.update(self.player.resp_obj())
return dic
class Err(Event):
def __init__(self, player, message):
super(Err, self).__init__(ERROR, player)
self.message = message
def resp_obj(self, ws):
obj = super(Err, self).resp_obj(ws)
obj['body'] = self.message
return obj
class Message(Event):
def __init__(self, player, message):
super(Message, self).__init__(SAY, player)
self.name = self.player.name
self.message = message
def resp_obj(self, ws):
obj = super(Message, self).resp_obj(ws)
obj['html'] = ws.render_string('message.html', event=self).decode('utf-8')
return obj
class Fight(Event):
def __init__(self, player):
super(Fight, self).__init__(FIGHT, player)
class LoggedIn(Event):
def __init__(self, player, chessers):
super(LoggedIn, self).__init__(LOGIN, player)
self.chessers = chessers
def resp_obj(self, ws):
obj = super(LoggedIn, self).resp_obj(ws)
obj['message'] = '**提示** %s 加入了' % self.player.name
obj['chessers'] = [c.resp_obj() for c in self.chessers]
return obj
class Win(Event):
def __init__(self, player):
super(Win, self).__init__(WIN, player)
class LoggedOut(Event):
def __init__(self, player, new_round_chesser=None):
super(LoggedOut, self).__init__(LOGOUT, player)
self.new_round_chesser = new_round_chesser
def resp_obj(self, ws):
obj = super(LoggedOut, self).resp_obj(ws)
obj['message'] = '**提示** %s 离开了' % self.player.name
if self.new_round_chesser:
obj['new_round_chesser'] = self.new_round_chesser.resp_obj()
return obj
class Round(Message):
def __init__(self, player):
message = "**提示** 轮到 %s 了" % player.name
super(Round, self).__init__(player, message)
self.type = ROUND
class Reveal(Event):
def __init__(self, player, correct, next_chesser):
super(Reveal, self).__init__(REVEAL, player)
self.correct = correct
self.next_chesser = next_chesser
def resp_obj(self, ws):
obj = super(Reveal, self).resp_obj(ws)
obj['correct'] = self.correct
if self.next_chesser:
obj['next_chesser'] = self.next_chesser.resp_obj()
return obj
class Flip(Event):
def __init__(self, player, point, quiz):
super(Flip, self).__init__(FLIP, player)
self.point = point
self.type = FLIP
self.quiz = quiz
def resp_obj(self, ws):
obj = super(Flip, self).resp_obj(ws)
obj['point'] = self.point
obj['quiz'] = self.quiz.resp_obj()
return obj