remove hidden attribute from serialization

This commit is contained in:
2024-04-20 09:20:44 +02:00
parent e400a7e770
commit 39e4af2dd0
4 changed files with 31 additions and 19 deletions
+13 -7
View File
@@ -3,24 +3,30 @@ from typing import Callable, List, Any
from netcode import WSServer
from netcode.ws_server import WebSocketServerProtocol
from netcode.models import Message
from model import Game
from controller import ClientController, GameControllerInterface
from model import Game, Player
from controller import (
ClientController,
ClientControllerInterface,
GameControllerInterface,
)
class GameController(GameControllerInterface):
server: WSServer
game: Game
clients: List[ClientController]
clients: List[ClientControllerInterface]
def __init__(self, game: Game, server: WSServer):
self.server = server
self.game = game
self.server: WSServer = server
self.game: Game = game
self.server.on_new_connection_callback = self.on_new_connection
self.game.listeners.append(self.broadcast_announce_dict)
self.clients = []
self.clients: List[ClientController] = []
async def on_new_connection(self, websocket, callback: Callable):
async def on_new_connection(
self, websocket: WebSocketServerProtocol, callback: Callable
):
client = ClientController(self, websocket)
self.clients.append(client)
await asyncio.gather(client.listen(), callback())
+15 -9
View File
@@ -17,6 +17,8 @@ class Player(Serializable):
hand: List[Card]
board: List[Card]
game: Serializable
_ready: bool = False
@property
@@ -31,24 +33,26 @@ class Player(Serializable):
def ready(self):
del self._ready
def __init__(self, username: str, team: Team):
def __init__(self, game: Serializable, username: str, team: Team):
super().__init__()
self.stack_pile = []
self.discard_pile = []
self.hand = []
self.board = []
self.game = game
self.stack_pile: List[Card] = []
self.discard_pile: List[Card] = []
self.hand: List[Card] = []
self.board: List[Card] = []
self.team = team
self.username = username
def serialize_guest(self) -> dict:
"""Get data available to the public"""
return super().serialize_guest() | {
"stack_pile": [None for card in self.stack_pile],
data = super().serialize_guest() | {
"" "stack_pile": [None for card in self.stack_pile],
"discard_pile": [card.uuid for card in self.discard_pile],
"team": self.team.uuid,
"hand": [None for card in self.hand],
"board": [card.uuid for card in self.board],
}
return {k: data[k] for k in data.keys() - ["game"]}
def serialize_team(self) -> dict:
"""Get data available to members of the same team"""
@@ -81,5 +85,7 @@ class Player(Serializable):
self.hand.append(self.stack_pile.pop())
amount = amount - 1
finally:
pass
# await self.team.notify_controller("update", self.serialize_team())
await self.team.notify_controller("update", self.serialize_team())
await self.game.notify_controller(
"update", self.serialize_guest(), lambda p: p.team != self.team
)
+2 -2
View File
@@ -16,11 +16,11 @@ class Serializable:
def serialize(self):
"""Returns a dict to be saved on the database"""
return self.__dict__ | {"listeners": None}
return {k: self.__dict__[k] for k in self.__dict__.keys() - ["listeners"]}
def serialize_guest(self):
"""Returns a dict with some elements masked for the players"""
return self.__dict__ | {"listeners": None}
return {k: self.__dict__[k] for k in self.__dict__.keys() - ["listeners"]}
async def notify_controller(
self,
+1 -1
View File
@@ -1,5 +1,5 @@
import asyncio
from typing import Set, Callable
from typing import Set, Callable, Any, Awaitable
from websockets.server import serve, WebSocketServerProtocol
from websockets import broadcast