1
0
Fork 0
taleweave-ai/adventure/server.py

115 lines
2.4 KiB
Python
Raw Normal View History

2024-05-04 04:18:21 +00:00
import asyncio
2024-05-04 20:35:42 +00:00
from collections import deque
2024-05-04 04:18:21 +00:00
from json import dumps
from logging import getLogger
from threading import Thread
import websockets
from adventure.models import Actor, Room, World
from adventure.state import snapshot_world, world_json
logger = getLogger(__name__)
connected = set()
2024-05-04 20:35:42 +00:00
recent_events = deque(maxlen=10)
recent_world = None
2024-05-04 04:18:21 +00:00
async def handler(websocket):
2024-05-04 20:35:42 +00:00
logger.info("Client connected")
2024-05-04 04:18:21 +00:00
connected.add(websocket)
2024-05-04 20:35:42 +00:00
try:
if recent_world:
await websocket.send(recent_world)
for message in recent_events:
await websocket.send(message)
except Exception:
logger.exception("Failed to send recent messages to new client")
2024-05-04 04:18:21 +00:00
while True:
try:
message = await websocket.recv()
print(message)
except websockets.ConnectionClosedOK:
break
connected.remove(websocket)
2024-05-04 20:35:42 +00:00
logger.info("Client disconnected")
2024-05-04 04:18:21 +00:00
socket_thread = None
static_thread = None
2024-05-04 20:35:42 +00:00
def server_json(obj):
if isinstance(obj, Actor):
return obj.name
if isinstance(obj, Room):
return obj.name
return world_json(obj)
def send_and_append(message):
json_message = dumps(message, default=server_json)
recent_events.append(json_message)
websockets.broadcast(connected, json_message)
return json_message
2024-05-04 04:18:21 +00:00
def launch_server():
global socket_thread, static_thread
def run_sockets():
asyncio.run(server_main())
socket_thread = Thread(target=run_sockets)
socket_thread.start()
async def server_main():
async with websockets.serve(handler, "", 8001):
logger.info("Server started")
await asyncio.Future() # run forever
def server_system(world: World, step: int):
2024-05-04 20:35:42 +00:00
global recent_world
2024-05-04 04:18:21 +00:00
json_state = {
**snapshot_world(world, step),
"type": "world",
}
2024-05-04 20:35:42 +00:00
recent_world = send_and_append(json_state)
2024-05-04 04:18:21 +00:00
def server_result(room: Room, actor: Actor, action: str):
json_action = {
2024-05-04 20:35:42 +00:00
"actor": actor,
2024-05-04 04:18:21 +00:00
"result": action,
2024-05-04 20:35:42 +00:00
"room": room,
2024-05-04 04:18:21 +00:00
"type": "result",
}
2024-05-04 20:35:42 +00:00
send_and_append(json_action)
2024-05-04 04:18:21 +00:00
2024-05-04 20:35:42 +00:00
def server_action(room: Room, actor: Actor, message: str):
2024-05-04 04:18:21 +00:00
json_input = {
2024-05-04 20:35:42 +00:00
"actor": actor,
2024-05-04 04:18:21 +00:00
"input": message,
2024-05-04 20:35:42 +00:00
"room": room,
"type": "action",
}
send_and_append(json_input)
def server_event(message: str):
json_broadcast = {
"message": message,
"type": "event",
2024-05-04 04:18:21 +00:00
}
2024-05-04 20:35:42 +00:00
send_and_append(json_broadcast)