This repository has been archived on 2024-07-31. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
back-python/src/controller/game.py
T
legonzaur 8c44a2f238
release-tag / release-image (push) Successful in 14s
Fix game creation being locked by back
Closes legonzaur/heron-realms-front#56
2024-05-16 23:38:08 +02:00

124 lines
3.6 KiB
Python

import asyncio
from typing import Callable, List, Any, TYPE_CHECKING
from websockets.exceptions import ConnectionClosedOK
from netcode import WSServer
from netcode.ws_server import WebSocketServerProtocol
from netcode.models import Message, MessageType
from model import Game, Player
from controller import (
ClientController,
)
if TYPE_CHECKING:
from controller import LobbyController
class GameController:
lobby_controller: "LobbyController"
game: Game
clients: list[ClientController]
def __init__(self, game: Game, lobby_controller: "LobbyController") -> None:
self.lobby_controller: "LobbyController" = lobby_controller
self.game: Game = game
self.clients: list[ClientController] = []
self.game.listeners.append(self.broadcast_announce_dict)
game.end_game_callback = self.end_game
async def send(self, message: Message, websocket: WebSocketServerProtocol):
try:
await websocket.send(message.model_dump_json())
except ConnectionClosedOK:
print("Connection Closed")
async def broadcast(self, message: Message):
for c in self.clients:
await c.send(message)
async def broadcast_announce_dict(
self,
notification_type: MessageType,
data: dict,
player_filter: Callable[[Player], Any],
):
messages = []
for c in self.clients:
if hasattr(c, "player") is False or player_filter(c.player):
messages.append(
c.send(
Message(
type=notification_type,
data_type="object",
data=data,
)
)
)
await asyncio.gather(*messages)
async def announce_dict(
self,
notification_type: MessageType,
data: dict,
websocket: WebSocketServerProtocol,
):
await self.send(
Message(
type=notification_type,
data_type="object",
data=data,
),
websocket,
)
async def announce_game(self, websocket: WebSocketServerProtocol):
for effect in self.game.effects:
await self.announce_dict(
MessageType.CREATE,
effect.serialize(),
websocket,
)
for card in self.game.cards:
await self.announce_dict(
MessageType.CREATE,
card.serialize(),
websocket,
)
for team in self.game.teams:
await self.announce_dict(
MessageType.CREATE,
team.serialize(),
websocket,
)
for player in self.game.players:
# TODO : fix for coop
await self.announce_dict(
MessageType.CREATE,
player.turn.serialize(),
websocket,
)
await self.announce_dict(
MessageType.CREATE,
player.serialize_guest(),
websocket,
)
await self.announce_dict(
MessageType.CREATE,
self.game.serialize_guest(),
websocket,
)
async def start_game(self):
await self.game.start_game()
for c in self.clients:
c.on_message = c.game_context
await self.broadcast(
Message(type=MessageType.CONTEXT, data_type="game_context_id", data="game")
)
async def end_game(self):
self.lobby_controller.game_controllers.remove(self)