change callback to filter by player instead of client

This commit is contained in:
2024-04-20 09:20:33 +02:00
parent 792f18e274
commit e400a7e770
6 changed files with 18 additions and 38 deletions
+3 -3
View File
@@ -6,7 +6,7 @@ from controller import GameControllerInterface, ClientControllerInterface
from netcode.models import Message, LoginMessage from netcode.models import Message, LoginMessage
from model import Player from model import Player, Team
def vibe_check(t): def vibe_check(t):
@@ -69,9 +69,9 @@ class ClientController(ClientControllerInterface):
self, self,
notification_type: str, notification_type: str,
data: dict, data: dict,
client_filter: Callable[["ClientController"], Any] = lambda x: True, player_filter: Callable[[Player], Any] = lambda x: True,
): ):
if client_filter(self): if hasattr(self, "player") is False or player_filter(self.player):
await self.send( await self.send(
Message(type=notification_type, data_type="object", data=data) Message(type=notification_type, data_type="object", data=data)
) )
+2 -3
View File
@@ -36,12 +36,11 @@ class GameController(GameControllerInterface):
self, self,
notification_type: str, notification_type: str,
data: dict, data: dict,
client_filter: Callable[["ClientController"], Any], player_filter: Callable[[Player], Any],
): ):
messages = [] messages = []
for c in self.clients: for c in self.clients:
if hasattr(c, "player") is False or player_filter(c.player):
if client_filter(c):
messages.append( messages.append(
c.send( c.send(
Message( Message(
+3 -3
View File
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Callable, Any, List from typing import Callable, Any, List
from model import Game from model import Game, Player
from netcode import WSServer from netcode import WSServer
from netcode.models import Message, LoginMessage from netcode.models import Message, LoginMessage
from netcode.ws_server import WebSocketServerProtocol from netcode.ws_server import WebSocketServerProtocol
@@ -25,7 +25,7 @@ class ClientControllerInterface(ABC):
self, self,
notification_type: str, notification_type: str,
data: dict, data: dict,
client_filter: Callable = lambda x: True, player_filter: Callable[[Player], bool] = lambda x: True,
): ):
pass pass
@@ -69,7 +69,7 @@ class GameControllerInterface(ABC):
self, self,
notification_type: str, notification_type: str,
data: dict, data: dict,
client_filter: Callable[[ClientControllerInterface], Any], player_filter: Callable[[Player], Any],
): ):
pass pass
+4 -26
View File
@@ -3,9 +3,7 @@ from typing import List
import random import random
from model import ( from model import (
Card, Card,
CardType,
CardRole, CardRole,
CardFaction,
CardParser, CardParser,
Player, Player,
Team, Team,
@@ -87,7 +85,6 @@ class Game(Serializable):
async def create_card(self, card: Card) -> Card: async def create_card(self, card: Card) -> Card:
self.cards.append(card) self.cards.append(card)
await self.notify_controller("create", card.serialize_guest()) await self.notify_controller("create", card.serialize_guest())
return card return card
async def create_base_deck(self, base_deck_name: str = "base") -> List[Card]: async def create_base_deck(self, base_deck_name: str = "base") -> List[Card]:
@@ -120,7 +117,7 @@ class Game(Serializable):
if team is None: if team is None:
team = await self.create_team() team = await self.create_team()
player = Player(username, team) player = Player(self, username, team)
self.players.append(player) self.players.append(player)
player.stack_pile = await self.create_base_deck("base") player.stack_pile = await self.create_base_deck("base")
@@ -152,8 +149,7 @@ class Game(Serializable):
await self.notify_controller("update", self.serialize_guest()) await self.notify_controller("update", self.serialize_guest())
def serialize_guest(self) -> dict: def serialize_guest(self) -> dict:
return super().serialize_guest() | { data = super().serialize_guest() | {
"loaded_cards": None,
"cards": [card.uuid for card in self.cards], "cards": [card.uuid for card in self.cards],
"market_stack": [None for card in self.market_stack], "market_stack": [None for card in self.market_stack],
"market": [None if card is None else card.uuid for card in self.market], "market": [None if card is None else card.uuid for card in self.market],
@@ -165,6 +161,8 @@ class Game(Serializable):
"turn": None if self.turn is None else self.turn.uuid, "turn": None if self.turn is None else self.turn.uuid,
} }
return {k: data[k] for k in data.keys() - ["loaded_cards"]}
async def start_game(self): async def start_game(self):
self.started = True self.started = True
self.turn = random.choice(self.teams) self.turn = random.choice(self.teams)
@@ -183,26 +181,6 @@ class Game(Serializable):
notifications = [] notifications = []
# Send cards in hand to teams
# TODO : Migrate this to player (when player draw)
for p in self.players:
notifications.append(p.team.notify_controller("update", p.serialize_team()))
# for t in self.teams:
# current_team = [p for p in self.players if p.team == t]
# for p in current_team:
# for o in current_team:
# notifications.append(
# p.notify_controller("update", o.serialize_team())
# )
# Send cards in hand to all others
for p in self.players:
for o in self.players:
if p.team == o.team:
continue
notifications.append(p.notify_controller("update", o.serialize_guest()))
await asyncio.gather(*notifications) await asyncio.gather(*notifications)
print( print(
+3 -2
View File
@@ -26,7 +26,8 @@ class Serializable:
self, self,
notification_type: str, notification_type: str,
game_object: dict, game_object: dict,
client_filter: Callable = lambda _: True, # TODO : Player interface to typing
player_filter: Callable[[Any], bool] = lambda _: True,
): ):
for l in self.listeners: for l in self.listeners:
await l(notification_type, game_object, client_filter) await l(notification_type, game_object, player_filter)
+3 -1
View File
@@ -8,7 +8,9 @@ class WSServer:
"""Handles Client-Server communication""" """Handles Client-Server communication"""
connections: Set[WebSocketServerProtocol] connections: Set[WebSocketServerProtocol]
on_new_connection_callback: Callable[[WebSocketServerProtocol], None] on_new_connection_callback: Callable[
[WebSocketServerProtocol, Callable[[Any], Awaitable[None]]], Awaitable[None]
]
port: int port: int