Archived
remove hidden attribute from serialization
This commit is contained in:
+13
-7
@@ -3,24 +3,30 @@ from typing import Callable, List, Any
|
|||||||
from netcode import WSServer
|
from netcode import WSServer
|
||||||
from netcode.ws_server import WebSocketServerProtocol
|
from netcode.ws_server import WebSocketServerProtocol
|
||||||
from netcode.models import Message
|
from netcode.models import Message
|
||||||
from model import Game
|
from model import Game, Player
|
||||||
from controller import ClientController, GameControllerInterface
|
from controller import (
|
||||||
|
ClientController,
|
||||||
|
ClientControllerInterface,
|
||||||
|
GameControllerInterface,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class GameController(GameControllerInterface):
|
class GameController(GameControllerInterface):
|
||||||
server: WSServer
|
server: WSServer
|
||||||
game: Game
|
game: Game
|
||||||
|
|
||||||
clients: List[ClientController]
|
clients: List[ClientControllerInterface]
|
||||||
|
|
||||||
def __init__(self, game: Game, server: WSServer):
|
def __init__(self, game: Game, server: WSServer):
|
||||||
self.server = server
|
self.server: WSServer = server
|
||||||
self.game = game
|
self.game: Game = game
|
||||||
self.server.on_new_connection_callback = self.on_new_connection
|
self.server.on_new_connection_callback = self.on_new_connection
|
||||||
self.game.listeners.append(self.broadcast_announce_dict)
|
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)
|
client = ClientController(self, websocket)
|
||||||
self.clients.append(client)
|
self.clients.append(client)
|
||||||
await asyncio.gather(client.listen(), callback())
|
await asyncio.gather(client.listen(), callback())
|
||||||
|
|||||||
+15
-9
@@ -17,6 +17,8 @@ class Player(Serializable):
|
|||||||
hand: List[Card]
|
hand: List[Card]
|
||||||
board: List[Card]
|
board: List[Card]
|
||||||
|
|
||||||
|
game: Serializable
|
||||||
|
|
||||||
_ready: bool = False
|
_ready: bool = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -31,24 +33,26 @@ class Player(Serializable):
|
|||||||
def ready(self):
|
def ready(self):
|
||||||
del self._ready
|
del self._ready
|
||||||
|
|
||||||
def __init__(self, username: str, team: Team):
|
def __init__(self, game: Serializable, username: str, team: Team):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.stack_pile = []
|
self.game = game
|
||||||
self.discard_pile = []
|
self.stack_pile: List[Card] = []
|
||||||
self.hand = []
|
self.discard_pile: List[Card] = []
|
||||||
self.board = []
|
self.hand: List[Card] = []
|
||||||
|
self.board: List[Card] = []
|
||||||
self.team = team
|
self.team = team
|
||||||
self.username = username
|
self.username = username
|
||||||
|
|
||||||
def serialize_guest(self) -> dict:
|
def serialize_guest(self) -> dict:
|
||||||
"""Get data available to the public"""
|
"""Get data available to the public"""
|
||||||
return super().serialize_guest() | {
|
data = super().serialize_guest() | {
|
||||||
"stack_pile": [None for card in self.stack_pile],
|
"" "stack_pile": [None for card in self.stack_pile],
|
||||||
"discard_pile": [card.uuid for card in self.discard_pile],
|
"discard_pile": [card.uuid for card in self.discard_pile],
|
||||||
"team": self.team.uuid,
|
"team": self.team.uuid,
|
||||||
"hand": [None for card in self.hand],
|
"hand": [None for card in self.hand],
|
||||||
"board": [card.uuid for card in self.board],
|
"board": [card.uuid for card in self.board],
|
||||||
}
|
}
|
||||||
|
return {k: data[k] for k in data.keys() - ["game"]}
|
||||||
|
|
||||||
def serialize_team(self) -> dict:
|
def serialize_team(self) -> dict:
|
||||||
"""Get data available to members of the same team"""
|
"""Get data available to members of the same team"""
|
||||||
@@ -81,5 +85,7 @@ class Player(Serializable):
|
|||||||
self.hand.append(self.stack_pile.pop())
|
self.hand.append(self.stack_pile.pop())
|
||||||
amount = amount - 1
|
amount = amount - 1
|
||||||
finally:
|
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
|
||||||
|
)
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ class Serializable:
|
|||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
"""Returns a dict to be saved on the database"""
|
"""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):
|
def serialize_guest(self):
|
||||||
"""Returns a dict with some elements masked for the players"""
|
"""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(
|
async def notify_controller(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from typing import Set, Callable
|
from typing import Set, Callable, Any, Awaitable
|
||||||
from websockets.server import serve, WebSocketServerProtocol
|
from websockets.server import serve, WebSocketServerProtocol
|
||||||
from websockets import broadcast
|
from websockets import broadcast
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user