-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
50 lines (44 loc) · 1.58 KB
/
server.py
File metadata and controls
50 lines (44 loc) · 1.58 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
import socket
import threading
import avocado
import atexit
MAX_USERS: int = 16
players_lock = threading.Lock()
printlock = threading.Lock()
players: dict[str, avocado.BasePlayer] = {}
def serve(conn: socket.socket, addr):
request_type = conn.recv(3)
username = conn.recv(8).decode('utf-8') # usernames MUST be 8 chars long
with printlock:
print(f"\033[1m{request_type.decode()!r} request from {username} ({addr[0]} on port {addr[1]})\033[0m")
if request_type == b'JON': # JOIN request
with players_lock:
players[username] = avocado.BasePlayer(username)
elif request_type == b'GET': # request data about a given player
with players_lock:
data = players[username].export_location()
conn.send(data)
elif request_type == b'SET': # set data about a given player
data = conn.recv(avocado.network.ENTITY_POS_FRMT_LEN)
with players_lock:
players[username].update_location(data)
elif request_type == b'LSP': # list players
with players_lock:
players_list = players.keys()
with printlock:
print("Players:", players_list)
for player in players_list:
conn.send(player.encode('utf-8'))
conn.send(b'.' * 8)
conn.close()
def main():
def cleanup():
s.close()
atexit.register(cleanup)
with avocado.network.new_sock() as s:
s.bind(('0.0.0.0', avocado.PORT))
while 1:
s.listen(MAX_USERS)
threading.Thread(target=serve, args=s.accept()).start()
if __name__ == '__main__':
main()