-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworking.py
More file actions
49 lines (40 loc) · 1.62 KB
/
Copy pathNetworking.py
File metadata and controls
49 lines (40 loc) · 1.62 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
from Packets.PacketHandler import Clientbound
from Packets.PacketMap import GameState
from Util import enforce_annotations
import socket
class Networking:
def __init__(self):
self.connected_clients = 0
self.clients = {}
#TODO: maybe add annotation for packets
@enforce_annotations
def broadcast(self, packet, gamestate: GameState):
for client in self.clients:
Clientbound(client).send_encrypted(packet, gamestate, self.get_encryptor(client))
@enforce_annotations
def send_to_others(self, packet, sender: socket.socket, gamestate: GameState):
for client in self.clients:
if client != sender:
Clientbound(client).send_encrypted(packet, gamestate, self.get_encryptor(client))
@enforce_annotations
def add_encryptor(self, socket: socket.socket, encryptor):
self.clients[socket] = encryptor
@enforce_annotations
def get_encryptor(self, socket: socket.socket):
return self.clients[socket]
@enforce_annotations
def remove_encryptor(self, socket: socket.socket):
self.clients.pop(socket)
@enforce_annotations
def add_client(self, socket: socket.socket):
self.connected_clients += 1
self.clients[socket] = "encryptor"
return self.connected_clients
@enforce_annotations
def remove_client(self, socket: socket.socket):
self.connected_clients -= 1
if socket in self.clients:
self.clients.pop(socket)
else:
print(f"Error: {socket} not in clients")
return self.connected_clients