From 01fd2d63f5c06278ed661c88bdc9dd3e00b57d3b Mon Sep 17 00:00:00 2001 From: legonzaur Date: Tue, 26 Mar 2024 17:20:41 +0100 Subject: [PATCH] ugly ws controller --- src/controller/__init__.py | 1 + src/controller/client.py | 15 +++++++++++++++ src/game/card.py | 4 ++-- src/game/player.py | 5 +++++ src/main.py | 19 ++++++++++++++++++- src/netcode/ws_server.py | 22 +++++++++++----------- 6 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 src/controller/__init__.py create mode 100644 src/controller/client.py diff --git a/src/controller/__init__.py b/src/controller/__init__.py new file mode 100644 index 0000000..ff5281a --- /dev/null +++ b/src/controller/__init__.py @@ -0,0 +1 @@ +from controller.client import Client diff --git a/src/controller/client.py b/src/controller/client.py new file mode 100644 index 0000000..c079866 --- /dev/null +++ b/src/controller/client.py @@ -0,0 +1,15 @@ +from websockets.server import WebSocketServerProtocol + + +class Client: + websocket: WebSocketServerProtocol + + def __init__(self, websocket: WebSocketServerProtocol): + self.websocket = websocket + + async def listen(self): + async for message in self.websocket: + await self.on_message(message) + + async def on_message(self, message): + print(message) diff --git a/src/game/card.py b/src/game/card.py index 65153f6..71c1af6 100644 --- a/src/game/card.py +++ b/src/game/card.py @@ -35,8 +35,8 @@ class Card: pass -class CardGuard(Card): - """Represents any guard card""" +class CardChampion(Card): + """Represents any champion card""" defense: int guard: bool diff --git a/src/game/player.py b/src/game/player.py index bb3c6e4..adb7496 100644 --- a/src/game/player.py +++ b/src/game/player.py @@ -9,7 +9,12 @@ class Player: discard_pile: List[Card] team: Team + hand: List[Card] + board: List[Card] + def __init__(self, team: Team): self.stack_pile = [] self.discard_pile = [] + self.hand = [] + self.board = [] self.team = team diff --git a/src/main.py b/src/main.py index 821e04c..c70a3c6 100644 --- a/src/main.py +++ b/src/main.py @@ -2,11 +2,28 @@ import asyncio from game import Game +from netcode.ws_server import WebSocketServerProtocol from netcode import WSServer +from controller import Client gameServer = WSServer() hero_game = Game() hero_game.broadcast = gameServer.broadcast -asyncio.run(gameServer.serve()) +playerServer = WSServer() + + +async def handler(websocket: WebSocketServerProtocol): + client = Client(websocket) + await client.listen() + + +playerServer.message_callbacks.append(handler) + + +async def main(): + await asyncio.gather(gameServer.serve(), playerServer.serve(port=8766)) + + +asyncio.run(main()) diff --git a/src/netcode/ws_server.py b/src/netcode/ws_server.py index cdb5053..cd2cd4d 100644 --- a/src/netcode/ws_server.py +++ b/src/netcode/ws_server.py @@ -1,5 +1,5 @@ import asyncio -from typing import Set +from typing import Set, Callable, Any, List from websockets.server import serve, WebSocketServerProtocol from websockets import broadcast @@ -9,23 +9,23 @@ class WSServer: connections: Set[WebSocketServerProtocol] + message_callbacks: List[Callable[[WebSocketServerProtocol], Any]] + def __init__(self): self.connections = set() + self.message_callbacks = [] - async def _handler(self, websocket: WebSocketServerProtocol): + async def handler(self, websocket: WebSocketServerProtocol): """Is called everytime a client connects to the websocket""" - - await asyncio.wait( - [self._track_connection(websocket), self._message_handler(websocket)] + await asyncio.gather( + self.track_connection(websocket), self.run_callbacks(websocket) ) - async def _message_handler(self, websocket: WebSocketServerProtocol): + 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 for message in websocket: - await websocket.send(message) - - async def _track_connection(self, websocket: WebSocketServerProtocol): + async def track_connection(self, websocket: WebSocketServerProtocol): """Keeps track of connected clients in the connections property""" try: @@ -36,7 +36,7 @@ class WSServer: async def serve(self, address="0.0.0.0", port=8765): """Call to start the websocket""" - async with serve(self._handler, address, port): + async with serve(self.handler, address, port): print("Started server on ws://" + address + ":" + str(port)) await asyncio.Future() # run forever