Change terminology game state -> game dump

This commit is contained in:
Eden Kirin
2023-03-30 11:32:16 +02:00
parent 48cb1a3798
commit 413e395a75
7 changed files with 40 additions and 35 deletions

View File

@ -7,7 +7,7 @@ import websockets
from websockets import WebSocketServerProtocol
from websockets.exceptions import ConnectionClosedOK
from hopper.models.ws_dto import GameStateDto
from hopper.models.ws_dto import GameDumpDto
from settings import settings
@ -18,8 +18,8 @@ class WSServer(Thread):
logging.info(f"Add client: {websocket.id}")
try:
# send initial game state to connected client
await self.send_game_state_to_client(websocket)
# send initial game dump to connected client
await self.send_game_dump_to_client(websocket)
# loop and do nothing while client is connected
connected = True
while connected:
@ -32,36 +32,36 @@ class WSServer(Thread):
self.connected_clients.remove(websocket)
logging.info(f"Remove client: {websocket.id}")
def _create_game_state_message(self) -> str:
def _create_game_dump_message(self) -> str:
# avoid circular imports
from hopper.api.dependencies import get_game_engine
engine = get_game_engine()
game_state = GameStateDto(
game_dump = GameDumpDto(
board=engine.board,
destination=engine.board.destination,
players=engine.players,
layers=engine.get_board_layout().layers,
)
return json.dumps(game_state.dict())
return json.dumps(game_dump.dict())
async def send_game_state_to_client(
async def send_game_dump_to_client(
self, websocket: WebSocketServerProtocol
) -> None:
"""Send game state to the client"""
message = self._create_game_state_message()
logging.debug(f"Sending game state to client: {websocket.id}")
"""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)
async def send_game_state(self) -> None:
async def send_game_dump(self) -> None:
"""Broadcast game state to all connected clients"""
if not self.connected_clients:
return
message = self._create_game_state_message()
message = self._create_game_dump_message()
logging.debug(
f"Sending game state to clients: {self.connected_clients}: {message}"
f"Sending game dump to clients: {self.connected_clients}: {message}"
)
for client in self.connected_clients:
await client.send(message)