diff --git a/README.md b/README.md index b428d79..3cf1cd2 100644 --- a/README.md +++ b/README.md @@ -234,8 +234,8 @@ loop #lightyellow Product select countdown timer (60s) Game ->o WS: Timer timeout activate Game activate WS #coral - WS o-> Client1: Cancel selection - WS o-> Client2: Cancel selection + WS o-> Client1: Selection timeout + WS o-> Client2: Selection timeout deactivate WS Game -> Game: Unlock game deactivate Game @@ -400,6 +400,8 @@ Response body: ### Game state structure +Direction: Game server -> Clients + Message: `game_dump` Data: @@ -503,109 +505,42 @@ Data: } ``` -### Product purchase start +### Player reached destination -Message: `product_purchase_start` +Direction: Game server -> Clients + +Message: `player_reached_destination` Data: ```json { "player": { - "id": "test-player-pero", - "name": "Pero", + "id": "2e0f1a50-eaa6-4efd-b0c3-adbf7000eec2", + "name": "Joso", "active": true, "position": { - "x": 10, - "y": 10 + "x": 5, + "y": 5 }, - "move_count": 1, - "move_attempt_count": 1, - "state": "ON_DESTINATION" - }, - "products": [ - { - "name": "CocaCola", - "id": "cocacola-id", - "description": null - }, - { - "name": "Pepsi", - "id": "pepsi-id", - "description": null - }, - { - "name": "Fanta", - "id": "fanta-id", - "description": null - }, - { - "name": "Snickers", - "id": "snickers-id", - "description": null - }, - { - "name": "Mars", - "id": "mars-id", - "description": null - }, - { - "name": "Burek", - "id": "burek-id", - "description": null - } - ], - "timeout": 5 -} -``` - -### Product purchase timer tick - -Message: `product_purchase_timer_tick` - -Data: -```json -{ - "time_left": 4, - "player": { - "id": "test-player-pero", - "name": "Pero", - "active": true, - "position": { - "x": 10, - "y": 10 - }, - "move_count": 1, - "move_attempt_count": 1, + "move_count": 6, + "move_attempt_count": 6, "state": "ON_DESTINATION" } } ``` -### Product purchase timer done +### Product selection timeout -Message: `product_purchase_done` +Direction: Game server -> Clients -Data: -```json -{ - "player": { - "id": "test-player-pero", - "name": "Pero", - "active": true, - "position": { - "x": 10, - "y": 10 - }, - "move_count": 1, - "move_attempt_count": 1, - "state": "ON_DESTINATION" - }, - "product": { - "name": "CocaCola", - "id": "cocacola-id", - "description": null - } -} -``` +Message: `product_selection_timeout` -If product selection timeout occured, product will be null. +Data: `null` + +### Product selection done + +Message: `product_selection_done` + +Direction: Client -> Game server, Game server -> Clients + +Data: `null` diff --git a/frontend/index.html b/frontend/index.html index c61962f..c836545 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -22,9 +22,6 @@

FairHopper Visualisation Client

-

@@ -68,18 +65,6 @@

- - - \ No newline at end of file diff --git a/frontend/js/frontend.js b/frontend/js/frontend.js index 27d3a4a..c560940 100644 --- a/frontend/js/frontend.js +++ b/frontend/js/frontend.js @@ -104,6 +104,10 @@ function playerReachedDestination(data) { playerOnDestinationModal.show(); } +function productSelectionTimeout() { + playerOnDestinationModal.hide(); +} + function productSelectionDone() { playerOnDestinationModal.hide(); } @@ -127,6 +131,9 @@ function wsConnect() { case "player_reached_destination": playerReachedDestination(wsMessage.data); break; + case "product_selection_timeout": + productSelectionTimeout(); + break; case "product_selection_done": productSelectionDone(); break; diff --git a/hopper/engine.py b/hopper/engine.py index 201b1b8..5646d61 100644 --- a/hopper/engine.py +++ b/hopper/engine.py @@ -185,7 +185,7 @@ class GameEngine: logging.info("Ding ding! Product selection countdown timer timeout") self._purchase_countdown_timer = None asyncio.run( - self.ws_server.send_product_selection_done_message() + self.ws_server.send_product_selection_timeout_message() ) self.game_state = GameState.RUNNING asyncio.run(self.send_game_dump()) diff --git a/hopper/models/ws_dto.py b/hopper/models/ws_dto.py index 53e24bd..d5ed16c 100644 --- a/hopper/models/ws_dto.py +++ b/hopper/models/ws_dto.py @@ -6,13 +6,7 @@ from typing import Optional, TypeVar from pydantic import Field from pydantic.generics import GenericModel -from hopper.api.dto import ( - BaseModel, - BoardDto, - DestinationDto, - PlayerDto, - PositionDto, -) +from hopper.api.dto import BaseModel, BoardDto, DestinationDto, PlayerDto, PositionDto from hopper.enums import ObjectType @@ -65,6 +59,10 @@ class WSProductSelectionDoneMessage(WSMessage): message: str = "product_selection_done" +class WSProductSelectionTimeoutMessage(WSMessage): + message: str = "product_selection_timeout" + + class WSPlayerReachedDestinationMessage(WSMessage): message: str = "player_reached_destination" data: PlayerReachedDestinationDto diff --git a/hopper/ws_server.py b/hopper/ws_server.py index aab7914..6ee8809 100644 --- a/hopper/ws_server.py +++ b/hopper/ws_server.py @@ -15,6 +15,7 @@ from hopper.models.ws_dto import ( WSMessage, WSPlayerReachedDestinationMessage, WSProductSelectionDoneMessage, + WSProductSelectionTimeoutMessage, ) @@ -63,7 +64,9 @@ class WSServer(Thread): try: # we're expecting nothing from client, but read if client sends a message rcv_data = await websocket.recv() - await self.handle_rcv_message(client=websocket, raw_message=rcv_data) + await self.handle_rcv_message( + client=websocket, raw_message=rcv_data + ) except ConnectionClosedOK: logging.info(f"Connection closed OK for client: {websocket.id}") connected = False @@ -126,6 +129,10 @@ class WSServer(Thread): message = WSProductSelectionDoneMessage() await self.send_message_to_clients(message) + async def send_product_selection_timeout_message(self) -> None: + message = WSProductSelectionTimeoutMessage() + await self.send_message_to_clients(message) + async def run_async(self) -> None: logging.info( f"Starting FairHopper Websockets Server on {self.host}:{self.port}"