architecture cleanup

This commit is contained in:
2024-04-04 21:46:39 +02:00
parent 55f61928ea
commit 45c6596914
4 changed files with 109 additions and 28 deletions
+2 -1
View File
@@ -1,7 +1,8 @@
"""controller handles the translation between """controller handles the translation between
netcode messages and model functions""" netcode messages and model functions"""
from controller.interfaces import ClientControllerInterface, GameControllerInterface
from controller.object_controller import ObjectController from controller.object_controller import ObjectController
from controller.card import Card from controller.card import Card
from controller.game import GameController
from controller.client import ClientController from controller.client import ClientController
from controller.game import GameController
+95
View File
@@ -0,0 +1,95 @@
from abc import ABC, abstractmethod
from typing import Callable, Any, List
from model import Game
from netcode import WSServer
from netcode.models import Message, LoginMessage
from netcode.ws_server import WebSocketServerProtocol
class ClientControllerInterface(ABC):
@abstractmethod
async def listen(self):
"""Initializes the client controller"""
@abstractmethod
async def send(self, message: Message):
pass
@abstractmethod
async def send_error(self, error: str):
pass
@abstractmethod
async def send_announce_dict(
self,
notification_type: str,
data: dict,
client_filter: Callable = lambda x: True,
):
pass
@abstractmethod
async def game_context(self, message: Message):
"""Handles messages when the client is logged in"""
@abstractmethod
async def login_context(self, message: LoginMessage):
"""Handles messages when the client isn't logged in"""
@abstractmethod
async def pregame_context(self, message: Message):
"""Waits for players"""
class GameControllerInterface(ABC):
server: WSServer
game: Game
clients: List[ClientControllerInterface]
@abstractmethod
def __init__(self, game: Game, server: WSServer):
pass
@abstractmethod
async def on_new_connection(self, websocket, callback: Callable):
pass
@abstractmethod
async def send(self, message: Message, websocket: WebSocketServerProtocol):
pass
@abstractmethod
async def broadcast(self, message: Message):
pass
@abstractmethod
async def broadcast_announce_dict(
self,
notification_type: str,
data: dict,
client_filter: Callable[[ClientControllerInterface], Any],
):
pass
@abstractmethod
async def announce_dict(
self,
notification_type: str,
data: dict,
websocket: WebSocketServerProtocol,
):
pass
@abstractmethod
async def announce_game(self, websocket: WebSocketServerProtocol):
pass
@abstractmethod
async def guest_handler(self, websocket: WebSocketServerProtocol):
pass
@abstractmethod
async def start_game(self):
pass
+1 -11
View File
@@ -11,20 +11,10 @@ gameServer = WSServer()
hero_game = Game() hero_game = Game()
game_controller = GameController(hero_game, gameServer) game_controller = GameController(hero_game, gameServer)
playerServer = WSServer()
async def handler(websocket: WebSocketServerProtocol):
client = ClientController(game_controller, websocket)
await client.listen()
playerServer.message_callbacks.append(handler)
async def main(): async def main():
await hero_game.init_game() await hero_game.init_game()
await asyncio.gather(gameServer.serve(), playerServer.serve(port=8766)) await gameServer.serve()
asyncio.run(main()) asyncio.run(main())
+10 -15
View File
@@ -1,18 +1,16 @@
import asyncio import asyncio
from typing import Set, Callable, Any, List from typing import Set, Callable
from websockets.server import serve, WebSocketServerProtocol from websockets.server import serve, WebSocketServerProtocol
from websockets import broadcast from websockets import broadcast
import time
class WSServer: class WSServer:
"""Handles Client-Server communication""" """Handles Client-Server communication"""
connections: Set[WebSocketServerProtocol] connections: Set[WebSocketServerProtocol]
on_new_connection_callback: Callable[[WebSocketServerProtocol], None]
message_callbacks: List[Callable[[WebSocketServerProtocol], Any]] port: int
port: 0
def __init__(self): def __init__(self):
self.connections = set() self.connections = set()
@@ -20,22 +18,19 @@ class WSServer:
async def handler(self, websocket: WebSocketServerProtocol): async def handler(self, websocket: WebSocketServerProtocol):
"""Is called everytime a client connects to the websocket""" """Is called everytime a client connects to the websocket"""
await asyncio.gather(
self.track_connection(websocket), self.run_callbacks(websocket)
)
async def run_callbacks(self, websocket: WebSocketServerProtocol):
"""Handles recieving messages from the client"""
await asyncio.gather(*[cb(websocket) for cb in self.message_callbacks])
async def track_connection(self, websocket: WebSocketServerProtocol):
"""Keeps track of connected clients in the connections property"""
self.connections.add(websocket) self.connections.add(websocket)
async def track_connection():
try: try:
await websocket.wait_closed() await websocket.wait_closed()
finally: finally:
self.connections.remove(websocket) self.connections.remove(websocket)
await self.on_new_connection_callback(websocket, track_connection)
async def track_connection(self, websocket: WebSocketServerProtocol):
"""Keeps track of connected clients in the connections property"""
async def serve(self, address="0.0.0.0", port=8765): async def serve(self, address="0.0.0.0", port=8765):
"""Call to start the websocket""" """Call to start the websocket"""