WSMessage object
This commit is contained in:
@ -3,6 +3,7 @@ from dataclasses import dataclass
|
||||
from typing import List, Optional
|
||||
|
||||
from hopper.models.player import Player
|
||||
from hopper.models.product import Product
|
||||
|
||||
|
||||
@dataclass
|
||||
@ -44,4 +45,5 @@ class Settings:
|
||||
ws_server: WSServerSettings
|
||||
purchase_timeout: int = 10 # seconds
|
||||
log_level: int = logging.INFO
|
||||
products: Optional[List[Product]] = None
|
||||
debug: Optional[DebugSettings] = None
|
||||
|
||||
8
hopper/models/product.py
Normal file
8
hopper/models/product.py
Normal file
@ -0,0 +1,8 @@
|
||||
from dataclasses import dataclass, field
|
||||
import uuid
|
||||
|
||||
|
||||
@dataclass
|
||||
class Product:
|
||||
name: str
|
||||
uuid: str = field(default_factory=lambda: str(uuid.uuid4()))
|
||||
@ -1,6 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import TypeVar, Generic
|
||||
|
||||
from pydantic import Field
|
||||
from pydantic.generics import GenericModel
|
||||
|
||||
from hopper.api.dto import BaseModel, BoardDto, DestinationDto, PlayerDto, PositionDto
|
||||
from hopper.enums import ObjectType
|
||||
@ -15,6 +19,7 @@ class LayerDto(BaseModel):
|
||||
name: str
|
||||
objects: list[LayerObjectDto]
|
||||
|
||||
|
||||
class GameDumpPlayerDto(PlayerDto):
|
||||
...
|
||||
|
||||
@ -24,3 +29,22 @@ class GameDumpDto(BaseModel):
|
||||
destination: DestinationDto
|
||||
players: list[GameDumpPlayerDto]
|
||||
layers: list[LayerDto]
|
||||
|
||||
|
||||
TMessageData = TypeVar("TMessageData", bound=BaseModel)
|
||||
|
||||
|
||||
class WSMessage(GenericModel):
|
||||
message: str
|
||||
data: TMessageData
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.to_str()
|
||||
|
||||
def to_str(self) -> str:
|
||||
return json.dumps(self.dict())
|
||||
|
||||
|
||||
class WSGameDumpMessage(WSMessage):
|
||||
message: str = "game_dump"
|
||||
data: GameDumpDto
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from threading import Thread
|
||||
|
||||
@ -7,7 +6,7 @@ import websockets
|
||||
from websockets import WebSocketServerProtocol
|
||||
from websockets.exceptions import ConnectionClosedOK
|
||||
|
||||
from hopper.models.ws_dto import GameDumpDto
|
||||
from hopper.models.ws_dto import GameDumpDto, WSGameDumpMessage
|
||||
from settings import settings
|
||||
|
||||
|
||||
@ -32,7 +31,7 @@ class WSServer(Thread):
|
||||
self.connected_clients.remove(websocket)
|
||||
logging.info(f"Remove client: {websocket.id}")
|
||||
|
||||
def _create_game_dump_message(self) -> str:
|
||||
def _create_game_dump_message(self) -> WSGameDumpMessage:
|
||||
# avoid circular imports
|
||||
from hopper.api.dependencies import get_game_engine
|
||||
|
||||
@ -44,7 +43,7 @@ class WSServer(Thread):
|
||||
players=engine.players,
|
||||
layers=engine.get_board_layout().layers,
|
||||
)
|
||||
return json.dumps(game_dump.dict())
|
||||
return WSGameDumpMessage(data=game_dump)
|
||||
|
||||
async def send_game_dump_to_client(
|
||||
self, websocket: WebSocketServerProtocol
|
||||
@ -52,7 +51,7 @@ class WSServer(Thread):
|
||||
"""Send game dump to the client"""
|
||||
message = self._create_game_dump_message()
|
||||
logging.debug(f"Sending game dump to client: {websocket.id}")
|
||||
await websocket.send(message)
|
||||
await websocket.send(message.to_str())
|
||||
|
||||
async def send_game_dump(self) -> None:
|
||||
"""Broadcast game state to all connected clients"""
|
||||
|
||||
Reference in New Issue
Block a user