import json import socket import threading class Server: __address:str __tcpPort:str __udpPort:str __tcpSocket:socket __udpSocket:socket __TCPclientThread:threading.Thread __UDPclientThread:threading.Thread def __init__(self, address:str, tcpPort:str, udpPort:str): self.__address = address self.__tcpPort = tcpPort self.__udpPort = udpPort self.__tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.__udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.startServer() # handles starting the server and assigning socket values to the local reference def startServer(self): self.__tcpSocket.bind(self.__address, self.__tcpPort) self.__tcpSocket.listen() print(f"tcp server started on: {self.__address}:{self.__tcpPort}") self.__udpSocket.bind(self.__address, self.__udpPort) self.__udpSocket.listen() print(f"tcp server started on: {self.__address}:{self.__udpPort}") # server loop forwards connection to handleConnection while True: # accept incoming connection # TODO: validate this connection is a valid game connection clientTcpSocket, clientTcpAddress = self.__tcpSocket.accept() # create network thread for connection self.__TCPClientThread = threading.Thread(target=self.handleTCPConnection, args=(clientTcpSocket, clientTcpAddress)) self.__TCPClientThread.start() clientUdpSocket, clientUdpAddress = self.__udpSocket.accept() self.__UDPClientThread = threading.Thread(target=self.handleUDPConnection, args=(clientUdpSocket, clientUdpAddress)) self.__UDPClientThread.start() # handles ticking the game loop server side converting data and passing of to the event handler def handleTCPConnection(self, socket:socket, address): # states that a connection has been established print(f"Connected with {address}") # Communication with client while True: data = socket.recv(1024) if not data: break # decode message for handling message = data.decode() messageJson = json.loads(message) if messageJson["user"] in self.__users: self.handleTCPEvents(messageJson) else: break print(f"received message from {address}: {message}") # handles ticking the game loop server side converting data and passing of to the event handler def handleUDPConnection(self, socket:socket, address): # states that a connection has been established print(f"Connected with {address}") # Communication with client while True: data = socket.recv(1024) if not data: break # decode message for handling message = data.decode() messageJson = json.loads(message) if messageJson["user"] in self.__users: self.handleUDPEvents(messageJson) else: break print(f"received message from {address}: {message}") # handles passing of event data to the right functions def handleTCPEvents(self, event): # decide which event should be performed if event["event"] == "login": pass elif event["event"] == "join_queue": # count queue # move start game if 2 players are in the queue # remove player from the queue once game starts pass elif event["event"] == "leave_queue": # just remove player from the queue pass def handleUDPEvents(self, event): if event["event"] == "placeCard": pass elif event["event"] == "removeCard": pass elif event["event"] == "attackCard": pass elif event["event"] == "attackPlayer": pass elif event["event"] == "activateeffectCard": pass elif event["event"] == "activatemonstereffect": pass