-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnz.py
53 lines (39 loc) · 1.21 KB
/
cnz.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
# -*- coding: utf-8 -*-
import tornado.escape
import event
import player
import stall
class CNZodiac(object):
def __init__(self):
self.players = player.Players(stall.Stalls())
self.funcs = {
event.LOGIN: self.login,
event.SAY: self.say,
event.FIGHT: self.fight,
event.FLIP: self.flip,
event.REPLY: self.reply,
}
def on_message(self, ws, raw):
p = self.players.get(ws)
message = tornado.escape.json_decode(raw)
fn = self.funcs.get(message.get('type'))
if not fn:
p.send(event.Err(p, 'invalid message'))
return
fn(p, message)
def flip(self, player, message):
self.players.flip(player)
def fight(self, player, message):
self.players.fight(player)
def logout(self, ws):
p = self.players.get(ws)
if not p:
return
self.players.logout(p)
def login(self, player, message):
player.name = message.get('body')
self.players.login(player)
def say(self, player, message):
self.players.say(player, message)
def reply(self, player, message):
self.players.reply(player, message)