-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
148 lines (125 loc) · 3.69 KB
/
player.py
File metadata and controls
148 lines (125 loc) · 3.69 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
__author__ = 'meskill'
__date__ = '30.09.2014 19:10'
import logging
from struct import unpack
from threading import Lock
from game import Game
END = b'\x80' # mark of end of input
def read_msg(rfile):
cmd = rfile.read(1)[0]
line = []
while True:
b = rfile.read(1)
if b == END: break
line.append(b)
return cmd, b''.join(line)
def auth(id):
return True
class Player:
players = {}
def login(self, id):
'''Login
params: id: str - id
return: 0 - if not authorized
1 - if authorized
2 - case 1 and id is a host of not started game
3 - case 1 and id is a player in not started game
4 - case 1 and id is playing now'''
self.id = id
logging.debug('LoginID: %s for client %s', self.id, self.client_address)
r = 0
if auth(self.id):
r = 1
self.type = self.player
if self.id in self.players:
game = self.players[self.id]
if game.status == Game.LOBBY:
r = 3
if game.players[0] == self:
r = 2
self.type = self.host
elif game.status == Game.INGAME:
r = 4
self.wfile.write(bytes((r,)))
def get_gamesList(self, params):
'''Get list of games lobby
params: -
return: list of lobbies'''
for game in Game.gamesLobby:
self.wfile.write(game.get_info() + b'\n')
def create_game(self, params):
'''Create new game
params: n : int - size of map, m : int - max players in game
return: -'''
size, max_players = unpack('BB', params)
logging.debug('Creating game: id=%s,size=%d, max_players=%d by %s', self.id, size, max_players,
self.client_address)
game = Game(self, size, max_players)
self.players[self.id] = game
self.type = self.host
def connect_game(self, gameID):
'''Connect to existing game
params: id: str - game id
return: 1 if connected else 0'''
game = Game.gamesLobby.get(gameID)
if not game is None and self.id and len(game.players) < game.max_players:
game.players.append(self)
self.players[self.id] = gameID
self.wfile.write(b'\x01')
else:
self.wfile.write(b'\x00')
def start_game(self, params):
'''Start game (Host only)
params: -
return: -'''
game = self.players[self.id]
game.start_game()
def subscribe(self, params):
'''Subscribe to changing in games
params: -
return: -'''
logging.debug('%s - %s subscribed..', self.id, self.client_address)
Game.subscribers.add(self)
def get_help(self, params):
'''Get this help'''
for (i, x) in self.Commands.items():
self.wfile.write(('%d : %s\n\n' % (i, x.__doc__)).encode())
Commands = {0: get_help,
1: login,
11: create_game,
12: get_gamesList,
13: subscribe,
14: connect_game,
21: start_game,
42: lambda self: self.connection.wfile.write(b'fuck you, Stork!')}
new = {0, 1, 42}
player = new | {11, 12, 13, 14}
host = player | {21}
def __init__(self, rfile, wfile, client_address=None):
logging.debug('New Client: %s', client_address)
self.id = None
self.rfile = rfile
self.wfile = wfile
self.client_address = client_address
self.lock = Lock()
self.type = self.new
def serve(self):
try:
while True:
cmd, msg = read_msg(self.rfile)
with self.lock:
self.wfile.write(bytes((cmd,)))
logging.debug('Command: %d from %s', cmd, self.client_address)
if cmd in self.type:
self.Commands[cmd](self, msg)
else:
self.reject()
self.wfile.write(END)
except Exception as e:
logging.debug('Client disconnected: %s with Error: %s', self.client_address, e)
def reject(self):
self.wfile.write(b'reject')
logging.debug('Command rejected to %s', self.client_address)
def send(self, cmd, message):
with self.lock:
self.wfile.write(bytes((cmd,)) + message + END)