Code organization and refactoring
This commit is contained in:
75
routers/factions.py
Normal file
75
routers/factions.py
Normal file
@@ -0,0 +1,75 @@
|
||||
"""Faction data population and status management endpoints."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from fastapi import APIRouter
|
||||
|
||||
from models import FactionRequest
|
||||
from services.server_state import STATE
|
||||
from services.torn_api import populate_friendly, populate_enemy, start_friendly_status_loop, start_enemy_status_loop
|
||||
from utils import load_json_list
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["factions"])
|
||||
|
||||
|
||||
@router.post("/populate_friendly")
|
||||
async def api_populate_friendly(data: FactionRequest):
|
||||
await populate_friendly(data.faction_id)
|
||||
# Return members list for frontend (already in STATE from populate_friendly)
|
||||
members = [m.model_dump() for m in STATE.friendly.values()]
|
||||
return {"status": "friendly populated", "id": data.faction_id, "members": members}
|
||||
|
||||
|
||||
@router.post("/populate_enemy")
|
||||
async def api_populate_enemy(data: FactionRequest):
|
||||
await populate_enemy(data.faction_id)
|
||||
# Return members list for frontend (already in STATE from populate_enemy)
|
||||
members = [m.model_dump() for m in STATE.enemy.values()]
|
||||
return {"status": "enemy populated", "id": data.faction_id, "members": members}
|
||||
|
||||
|
||||
@router.post("/start_friendly_status")
|
||||
async def api_start_friendly_status(data: FactionRequest):
|
||||
await start_friendly_status_loop(data.faction_id, data.interval)
|
||||
return {"status": "friendly status loop started", "id": data.faction_id, "interval": data.interval}
|
||||
|
||||
|
||||
@router.post("/start_enemy_status")
|
||||
async def api_start_enemy_status(data: FactionRequest):
|
||||
await start_enemy_status_loop(data.faction_id, data.interval)
|
||||
return {"status": "enemy status loop started", "id": data.faction_id, "interval": data.interval}
|
||||
|
||||
|
||||
@router.get("/friendly_members")
|
||||
async def get_friendly_members():
|
||||
# Return list, but prefer STATE if populated
|
||||
if STATE.friendly:
|
||||
return [m.model_dump() for m in STATE.friendly.values()]
|
||||
# fallback to file
|
||||
path = Path("data/friendly_members.json")
|
||||
return load_json_list(path)
|
||||
|
||||
|
||||
@router.get("/enemy_members")
|
||||
async def get_enemy_members():
|
||||
if STATE.enemy:
|
||||
return [m.model_dump() for m in STATE.enemy.values()]
|
||||
path = Path("data/enemy_members.json")
|
||||
return load_json_list(path)
|
||||
|
||||
|
||||
@router.get("/friendly_status")
|
||||
async def api_friendly_status():
|
||||
path = Path("data/friendly_status.json")
|
||||
if not path.exists():
|
||||
return {}
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
@router.get("/enemy_status")
|
||||
async def api_enemy_status():
|
||||
path = Path("data/enemy_status.json")
|
||||
if not path.exists():
|
||||
return {}
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
Reference in New Issue
Block a user