WSMessage object

This commit is contained in:
Eden Kirin
2023-03-30 18:53:24 +02:00
parent b80130d942
commit 6111d07f09
5 changed files with 54 additions and 12 deletions

View File

@ -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
View 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()))

View File

@ -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