feat: handle inventory_delta messages and broadcast to browsers
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
176fb020ec
commit
664bd50388
1 changed files with 27 additions and 0 deletions
27
main.py
27
main.py
|
|
@ -1979,6 +1979,33 @@ async def ws_receive_snapshots(
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to process inventory for {data.get('character_name', 'unknown')}: {e}", exc_info=True)
|
logger.error(f"Failed to process inventory for {data.get('character_name', 'unknown')}: {e}", exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
# --- Inventory delta: single item add/remove/update ---
|
||||||
|
if msg_type == "inventory_delta":
|
||||||
|
try:
|
||||||
|
action = data.get("action")
|
||||||
|
char_name = data.get("character_name", "unknown")
|
||||||
|
|
||||||
|
if action == "remove":
|
||||||
|
item_id = data.get("item_id")
|
||||||
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||||
|
await client.delete(
|
||||||
|
f"{INVENTORY_SERVICE_URL}/inventory/{char_name}/item/{item_id}"
|
||||||
|
)
|
||||||
|
elif action in ("add", "update"):
|
||||||
|
item = data.get("item")
|
||||||
|
if item:
|
||||||
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||||
|
await client.post(
|
||||||
|
f"{INVENTORY_SERVICE_URL}/inventory/{char_name}/item",
|
||||||
|
json=item
|
||||||
|
)
|
||||||
|
|
||||||
|
# Broadcast delta to all browser clients
|
||||||
|
await _broadcast_to_browser_clients(data)
|
||||||
|
logger.debug(f"Inventory delta ({action}) for {char_name}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to process inventory delta: {e}", exc_info=True)
|
||||||
|
continue
|
||||||
# --- Vitals message: store character health/stamina/mana and broadcast ---
|
# --- Vitals message: store character health/stamina/mana and broadcast ---
|
||||||
if msg_type == "vitals":
|
if msg_type == "vitals":
|
||||||
payload = data.copy()
|
payload = data.copy()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue