-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-simple.py
29 lines (20 loc) · 1.04 KB
/
server-simple.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
import netty
import pickle
import time
__PORT__ = 34893
STATES = {}
def handle_packet(packet, clients, server, client):
if packet['type'] == 'join':
print('%s has joined!' % packet['uid'][:10])
STATES[packet['uid']] = {'x': 0, 'y': 0, 'color': 'black'}
server.send_single(client, {'type': 'welcome_sync', 'data': {'players': STATES}, 'uid': 'SERVER', 'time': time.time()})
server.send_all({'type': 'join', 'data': packet['data'], 'uid': packet['uid'], 'time': time.time()})
elif packet['type'] == 'update':
STATES[packet['uid']] = packet['data']
server.send_all({'type': 'update', 'data': packet['data'], 'uid': packet['uid'], 'time': time.time()})
elif packet['type'] == 'leave':
server.send_all({'type': 'leave', 'data': packet['data'], 'uid': packet['uid'], 'time': time.time()})
server.remove_client(client)
del STATES[packet['uid']]
server = netty.connection.Server(__PORT__, on_receive=handle_packet, _newthread_client=False)
server.start()