aboutsummaryrefslogtreecommitdiff
path: root/kennel/main.py
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth@simponic.xyz>2024-09-05 00:15:14 -0700
committerElizabeth Hunt <elizabeth@simponic.xyz>2024-09-05 00:15:14 -0700
commit45726367eee475a7717d7dcad78895de348f0b97 (patch)
tree937abae9cd74d8f99f149cf3c3ee534169c723f8 /kennel/main.py
parentc39367ac7e163395ae726bd169926004d4d99d67 (diff)
downloadkennel.hatecomputers.club-45726367eee475a7717d7dcad78895de348f0b97.tar.gz
kennel.hatecomputers.club-45726367eee475a7717d7dcad78895de348f0b97.zip
checkpoint; working position updates
Diffstat (limited to 'kennel/main.py')
-rw-r--r--kennel/main.py33
1 files changed, 18 insertions, 15 deletions
diff --git a/kennel/main.py b/kennel/main.py
index c1106fe..0808eb5 100644
--- a/kennel/main.py
+++ b/kennel/main.py
@@ -1,6 +1,6 @@
import asyncio
import uuid
-from typing import Annotated, Optional
+from typing import Annotated, Optional, List
from fastapi import (
Cookie,
@@ -82,8 +82,8 @@ class WebSocketClient(Publishable):
def __init__(self, websocket: WebSocket):
self.websocket = websocket
- async def publish(self, event: Event):
- await self.websocket.send_json([event.event_type, event.data])
+ async def publish(self, events: List[Event]):
+ await self.websocket.send_json([event.to_dict() for event in events])
@app.websocket("/ws")
@@ -95,27 +95,30 @@ async def websocket_endpoint(
client = WebSocketClient(websocket)
logger.info(f"Websocket connection established for session {session}")
- initial_state = InitialStateEvent(
- config.WORLD_WIDTH, config.WORLD_HEIGHT, kennel.entity_manager.to_dict()
- )
- await client.publish(initial_state)
-
session_entities = create_session_controllable_entities(session)
logger.info(f"Creating {len(session_entities)} entities for session {session}")
try:
network_system = system_manager.get_system(SystemType.NETWORK)
if network_system is None:
raise "Network system not found"
+ network_system.add_client(session, WebSocketClient(websocket))
+
+ network_system.client_downstream_event(
+ session,
+ InitialStateEvent(
+ config.WORLD_WIDTH, config.WORLD_HEIGHT, kennel.entity_manager.to_dict()
+ ),
+ )
for entity in session_entities:
logger.info(f"Adding entity {entity.id} for session {session}")
entity_manager.add_entity(entity)
- network_system.add_event(EntityBornEvent(entity))
- set_controllable_event = SetControllableEvent(entity.id, session)
- await client.publish(set_controllable_event)
+ network_system.server_global_event(EntityBornEvent(entity))
+ network_system.client_downstream_event(
+ session, SetControllableEvent(entity.id, session)
+ )
- network_system.add_client(session, WebSocketClient(websocket))
while True:
message = await websocket.receive_json()
if not isinstance(message, list):
@@ -125,20 +128,20 @@ async def websocket_endpoint(
logger.info(f"Invalid events in: {message}"[:100])
continue
for event in events:
- network_system.client_event(session, event)
+ network_system.client_upstream_event(session, event)
except WebSocketDisconnect:
logger.info(f"Websocket connection closed by client: {session}")
except Exception as e:
logger.error("Exception occurred", exc_info=e)
finally:
- logger.info("Websocket connection closed")
+ network_system.remove_client(session)
for entity in session_entities:
logger.info(f"Removing entity {entity.id} for session {session}")
entity_manager.remove_entity(entity.id)
network_system.add_event(Event(EventType.ENTITY_DEATH, {"id": entity.id}))
- network_system.remove_client(session)
await websocket.close()
+ logger.info("Websocket connection closed")
@app.get("/healthcheck")