impr(backend-server): move some funtionality into functions (#52)

This commit is contained in:
lucas authored and GitHub committed 2026-07-07 21:15:28 +02:00
1 parent bf6abfb2fa
commit eae3f376a7
4 files changed
+65 -35

No files matched your search

+1
View File
@@ -37,6 +37,7 @@ class Client:
def disconnect(self): def disconnect(self):
self.state = ClientState.DISCONNECTED self.state = ClientState.DISCONNECTED
self.websocket = None self.websocket = None
self.ready = False
async def send(self, msg_dict: MessageRaw_T) -> None: async def send(self, msg_dict: MessageRaw_T) -> None:
assert self.websocket is not None assert self.websocket is not None
+62 -34
View File
@@ -1,6 +1,7 @@
import json import json
import asyncio import asyncio
from websockets import Data
from websockets.asyncio.server import ServerConnection, serve from websockets.asyncio.server import ServerConnection, serve
from websockets.exceptions import ConnectionClosedOK from websockets.exceptions import ConnectionClosedOK
@@ -54,7 +55,8 @@ class AutobeerpongServer:
await client.send(msg_raw) await client.send(msg_raw)
async def update_server_state(self) -> None: async def update_server_state(self) -> None:
if self.context.state is ServerState.LOBBY: state = self.context.state
if state is ServerState.LOBBY:
players = list( players = list(
filter( filter(
lambda x: x.state is ClientState.PLAYER, lambda x: x.state is ClientState.PLAYER,
@@ -64,23 +66,14 @@ class AutobeerpongServer:
if self.context.lobby.is_players_full() and all([player.ready for player in players]): if self.context.lobby.is_players_full() and all([player.ready for player in players]):
# all players are ready, advance server state to match # all players are ready, advance server state to match
self.context.state = ServerState.MATCH await self.transition_to_match()
# send match start message to all connected clients
start_match = StartMatchMessage(
{"messageType": "startMatch", "messageContent": {}}
)
await self.broadcast(start_match.to_raw()) elif state is ServerState.MATCH:
if not self.context.lobby.is_players_full():
match = Match() await self.transition_to_lobby()
match.resolve()
await self.broadcast(match.to_raw())
elif self.context.state is ServerState.MATCH:
pass pass
def get_lobby_state(self) -> LobbyStateMessage: async def broadcast_lobby_state(self):
players = list( players = list(
filter( filter(
lambda x: x.state is ClientState.PLAYER, lambda x: x.state is ClientState.PLAYER,
@@ -94,7 +87,7 @@ class AutobeerpongServer:
) )
) )
return LobbyStateMessage( lobby_message = LobbyStateMessage(
{ {
"messageType": "lobbyState", "messageType": "lobbyState",
"messageContent": { "messageContent": {
@@ -104,11 +97,62 @@ class AutobeerpongServer:
} }
) )
await self.broadcast(lobby_message.to_raw())
async def transition_to_match(self):
self.context.state = ServerState.MATCH
# send match start message to all connected clients
start_match = StartMatchMessage(
{"messageType": "startMatch", "messageContent": {}}
)
await self.broadcast(start_match.to_raw())
match = Match()
match.resolve()
await self.broadcast(match.to_raw())
async def transition_to_lobby(self):
self.context.state = ServerState.LOBBY
players = filter(
lambda x: x.state is ClientState.PLAYER,
self.context.lobby._clients.values(),
)
for player in players:
player.ready = False
await self.broadcast_lobby_state()
async def handler(self, websocket: ServerConnection) -> None: async def handler(self, websocket: ServerConnection) -> None:
client_context = ClientContext(websocket) client_context = ClientContext(websocket)
async for msg in websocket: async for msg in websocket:
message_raw: MessageRaw_T = json.loads(msg) message = self.to_message(msg)
await message.execute(
server_context=self.context,
client_context=client_context,
)
if message.triggers_broadcast:
await self.broadcast_lobby_state()
await self.update_server_state()
if client_context.has_joined:
self.context.clients[client_context.get_client_id()].disconnect()
try:
await self.broadcast_lobby_state()
except ConnectionClosedOK:
pass
finally:
await self.update_server_state()
def to_message(self, data: Data) -> Message:
message_raw: MessageRaw_T = json.loads(data)
message_type = str(message_raw.get("messageType")) message_type = str(message_raw.get("messageType"))
if message_type is None: if message_type is None:
@@ -119,23 +163,7 @@ class AutobeerpongServer:
if MessageClass is None: if MessageClass is None:
raise Exception(f"Unknown messageType: {message_type}.") raise Exception(f"Unknown messageType: {message_type}.")
message = MessageClass(message_raw) return MessageClass(message_raw)
await message.execute(
server_context=self.context,
client_context=client_context,
)
if message.triggers_broadcast:
await self.broadcast(self.get_lobby_state().to_raw())
await self.update_server_state()
if client_context.has_joined:
self.context.clients[client_context.get_client_id()].disconnect()
try:
await self.broadcast(self.get_lobby_state().to_raw())
except ConnectionClosedOK:
pass
async def _start(self) -> None: async def _start(self) -> None:
async with serve( async with serve(
+1
View File
@@ -39,6 +39,7 @@ export class Ball extends BaseActor {
} }
onInitialize(engine: Engine): void { onInitialize(engine: Engine): void {
this.z = -1; // Place the ball behind other actors.
const { configuration } = this; const { configuration } = this;
const { radius } = configuration; const { radius } = configuration;
+1 -1
View File
@@ -14,7 +14,7 @@ export class BaseScene extends Scene {
} }
protected updateActorVisibility(actor: Actor, visible: boolean): void { protected updateActorVisibility(actor: Actor, visible: boolean): void {
const previousVisible = this.actors.includes(actor); const previousVisible = actor.isAdded;
if (visible && !previousVisible) { if (visible && !previousVisible) {
this.add(actor); this.add(actor);