51 lines
1011 B
Python
51 lines
1011 B
Python
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
|
|
|
|
|
|
class LayerObjectDto(BaseModel):
|
|
type: ObjectType = Field(..., alias="type_")
|
|
position: PositionDto
|
|
|
|
|
|
class LayerDto(BaseModel):
|
|
name: str
|
|
objects: list[LayerObjectDto]
|
|
|
|
|
|
class GameDumpPlayerDto(PlayerDto):
|
|
...
|
|
|
|
|
|
class GameDumpDto(BaseModel):
|
|
board: BoardDto
|
|
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
|