Add world state API + 29 tests
This commit is contained in:
BIN
__pycache__/multi_user_bridge.cpython-312.pyc
Normal file
BIN
__pycache__/multi_user_bridge.cpython-312.pyc
Normal file
Binary file not shown.
@@ -329,6 +329,7 @@ class SessionManager:
|
||||
|
||||
session_manager = SessionManager()
|
||||
presence_manager = PresenceManager()
|
||||
_server_start_time = time.time()
|
||||
|
||||
class BridgeHandler(BaseHTTPRequestHandler):
|
||||
"""HTTP handler for multi-user bridge."""
|
||||
@@ -369,6 +370,49 @@ class BridgeHandler(BaseHTTPRequestHandler):
|
||||
since = query.get('since', [None])[0]
|
||||
events = presence_manager.get_room_events(room, since)
|
||||
self._json_response({"room": room, "events": events})
|
||||
elif self.path == '/bridge/world-state':
|
||||
# GET /bridge/world-state — full world state
|
||||
state_file = WORLD_DIR / 'world_state.json'
|
||||
world_state = json.loads(state_file.read_text()) if state_file.exists() else {}
|
||||
|
||||
# Build room player lists and conversation counts
|
||||
rooms = world_state.get('rooms', {})
|
||||
room_details = {}
|
||||
for room_name, room_data in rooms.items():
|
||||
players = presence_manager.get_players_in_room(room_name)
|
||||
# Count messages across all sessions in this room
|
||||
conv_count = sum(
|
||||
len(s.messages) for s in session_manager.sessions.values()
|
||||
if s.room == room_name
|
||||
)
|
||||
room_details[room_name] = {
|
||||
**room_data,
|
||||
"players": players,
|
||||
"timmy_conversation_count": conv_count,
|
||||
}
|
||||
|
||||
self._json_response({
|
||||
"world": world_state,
|
||||
"rooms": room_details,
|
||||
"active_sessions": session_manager.list_sessions(),
|
||||
})
|
||||
elif self.path == '/bridge/stats':
|
||||
# GET /bridge/stats — aggregate stats
|
||||
total_messages = sum(len(s.messages) for s in session_manager.sessions.values())
|
||||
# Rooms with at least one player
|
||||
rooms_with_players = [
|
||||
room for room in presence_manager._rooms
|
||||
if presence_manager._rooms[room]
|
||||
]
|
||||
uptime_seconds = time.time() - _server_start_time
|
||||
self._json_response({
|
||||
"total_sessions": session_manager.get_session_count(),
|
||||
"total_messages": total_messages,
|
||||
"rooms_with_players": rooms_with_players,
|
||||
"rooms_with_players_count": len(rooms_with_players),
|
||||
"uptime_seconds": round(uptime_seconds, 1),
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
})
|
||||
else:
|
||||
self._json_response({"error": "not found"}, 404)
|
||||
|
||||
@@ -462,6 +506,8 @@ def main():
|
||||
print("Endpoints:")
|
||||
print(f" GET /bridge/health — Health check")
|
||||
print(f" GET /bridge/sessions — List active sessions")
|
||||
print(f" GET /bridge/world-state — Full world state (rooms, players, convos)")
|
||||
print(f" GET /bridge/stats — Aggregate stats (sessions, messages, uptime)")
|
||||
print(f" GET /bridge/room/<room>/players — List players in a room")
|
||||
print(f" GET /bridge/room/<room>/events — Room events (presence + chat)")
|
||||
print(f" POST /bridge/chat — Send message to Timmy")
|
||||
|
||||
BIN
tests/__pycache__/test_bridge.cpython-312-pytest-9.0.2.pyc
Normal file
BIN
tests/__pycache__/test_bridge.cpython-312-pytest-9.0.2.pyc
Normal file
Binary file not shown.
Reference in New Issue
Block a user