-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
39 lines (32 loc) · 1022 Bytes
/
client.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
class Client():
"""
Wrapper client class of geventwebsocket.websocket.WebSocket to
interface with Game class.
"""
def __init__(self, ws, bot=False, monster=False, manager=None):
self.ws = ws
self.id = 0
self.nickname = ''
self.snake = None
self.bot = bot
self.monster = monster
self.manager = manager
def setSnake(self, s):
self.snake = s
def setId(self, client_id):
self.id = client_id
# Msg nickname [ ID | SIZE | NICKNAME ]
self.nickname = chr(self.id) + chr(len(self.nickname)) + self.nickname
def setNickname(self, nickname):
self.nickname = nickname
def sendMessage(self, message, binary=False):
if self.bot:
return
if not self.ws.closed:
self.ws.send(message, binary=binary)
def close(self):
if self.bot:
self.manager.removeBot(self)
return
if not self.ws.closed:
self.ws.close()