From ed4d61b37bf3ae99484b378bf483ac279423cfdd Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Sat, 25 Mar 2023 18:45:31 +0100 Subject: [PATCH] Frontend --- README.md | 6 --- frontend/index.html | 120 ++++++++++++++++++++++++++++++++++++++++++++ frontend/styles.css | 20 ++++++++ hopper/engine.py | 4 +- hopper/ws_server.py | 11 +++- 5 files changed, 152 insertions(+), 9 deletions(-) create mode 100644 frontend/index.html create mode 100644 frontend/styles.css diff --git a/README.md b/README.md index 14fc2a9..a2c89e4 100644 --- a/README.md +++ b/README.md @@ -63,12 +63,6 @@ To activate virtual environment: poetry shell ``` -### Starting WebSockets Server - -```sh -make run-ws -``` - WebSockets server runs on port **8011**. To run WS Server on different port, edit `settings.py` configuration. diff --git a/frontend/index.html b/frontend/index.html new file mode 100644 index 0000000..03429f8 --- /dev/null +++ b/frontend/index.html @@ -0,0 +1,120 @@ + + + + + + + + + + Document + + + +
+

FairHopper WS Client

+ +
+
+
+
+
+
+
+

Players

+
    +
    +
    +
    + + + + + + \ No newline at end of file diff --git a/frontend/styles.css b/frontend/styles.css new file mode 100644 index 0000000..ac801b5 --- /dev/null +++ b/frontend/styles.css @@ -0,0 +1,20 @@ +body { + background-color: whitesmoke; +} + +.board-container { + background-color: white; + border: 1px solid black; +} + +.flex-grid { + display: flex; + justify-content: space-between; + grid-gap: 2px; + padding-bottom: 2px; +} +.cell { + flex: 1; + text-align: center; + background-color: beige; +} diff --git a/hopper/engine.py b/hopper/engine.py index 7cea8bf..6193380 100644 --- a/hopper/engine.py +++ b/hopper/engine.py @@ -47,7 +47,9 @@ class GameEngine: def _start_inactivity_watchdog(self) -> None: if not self._inacivity_watchdog: self._inacivity_watchdog = InactivityWatchdog( - players=self.players, daemon=True + players=self.players, + ws_server=self.ws_server, + daemon=True, ) self._inacivity_watchdog.start() diff --git a/hopper/ws_server.py b/hopper/ws_server.py index 2e0bca5..e87866f 100644 --- a/hopper/ws_server.py +++ b/hopper/ws_server.py @@ -5,6 +5,7 @@ from threading import Thread import websockets from websockets import WebSocketServerProtocol +from websockets.exceptions import ConnectionClosedOK from hopper.models.ws_dto import GameStateDto from settings import settings @@ -15,12 +16,18 @@ class WSServer(Thread): self.connected_clients = set[WebSocketServerProtocol]() super().__init__(*args, **kwargs) - async def ws_handler(self, websocket: WebSocketServerProtocol) -> None: + async def handler(self, websocket: WebSocketServerProtocol) -> None: self.connected_clients.add(websocket) logging.info(f"Add client: {websocket.id}") try: await self.send_game_state_to_client(websocket) + connected = True + while connected: + try: + message = await websocket.recv() + except ConnectionClosedOK: + connected = False finally: self.connected_clients.remove(websocket) logging.info(f"Remove client: {websocket.id}") @@ -63,7 +70,7 @@ class WSServer(Thread): ) async with websockets.serve( - ws_handler=self.ws_handler, + ws_handler=self.handler, host=settings.ws_server.HOST, port=settings.ws_server.PORT, ):