skeleton webpage

This commit is contained in:
2025-11-26 16:35:23 -05:00
parent dbfd3537fe
commit 1dde34d266
12 changed files with 175 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
# services/torn_api.py
import aiohttp
import json
import asyncio
from pathlib import Path
from config import TORN_API_KEY, ENEMY_FACTION_ID, YOUR_FACTION_ID
from .ffscouter import fetch_batch_stats
@@ -8,6 +9,13 @@ from .ffscouter import fetch_batch_stats
ENEMY_FILE = Path("data/enemy_faction.json")
FRIENDLY_FILE = Path("data/friendly_faction.json")
# Track running tasks + current faction IDs
enemy_task = None
friendly_task = None
current_enemy_id = None
current_friendly_id = None
async def fetch_and_save_faction(faction_id: int, file_path: Path) -> bool:
"""
@@ -59,10 +67,59 @@ async def fetch_and_save_faction(faction_id: int, file_path: Path) -> bool:
return True
# Wrappers for clarity
async def update_enemy_faction() -> bool:
return await fetch_and_save_faction(ENEMY_FACTION_ID, ENEMY_FILE)
async def update_friendly_faction() -> bool:
return await fetch_and_save_faction(YOUR_FACTION_ID, FRIENDLY_FILE)
#Loop for the constant update of members and the stop function
async def stop_task_if_running(task: asyncio.Task | None):
"""Cancel an existing running task safely."""
if task and not task.done():
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
async def faction_loop(faction_id: int, file_path: Path, interval: int):
"""
Runs fetch_and_save_faction() in a loop forever, waiting `interval`
seconds between iterations.
"""
while True:
try:
await fetch_and_save_faction(faction_id, file_path)
except Exception as e:
print(f"Error during faction loop for {faction_id}: {e}")
await asyncio.sleep(interval)
#Functions to call the loop, maybe add one to just call once?
async def update_enemy_faction(new_faction_id: int, interval: int):
global enemy_task, current_enemy_id
# If faction ID changes → stop old loop
if new_faction_id != current_enemy_id:
print(f"[ENEMY] Changing faction from {current_enemy_id}{new_faction_id}")
await stop_task_if_running(enemy_task)
current_enemy_id = new_faction_id
# Start new loop
enemy_task = asyncio.create_task(
faction_loop(new_faction_id, ENEMY_FILE, interval)
)
async def update_friendly_faction(new_faction_id: int, interval: int):
global friendly_task, current_friendly_id
if new_faction_id != current_friendly_id:
print(f"[FRIENDLY] Changing faction from {current_friendly_id}{new_faction_id}")
await stop_task_if_running(friendly_task)
current_friendly_id = new_faction_id
friendly_task = asyncio.create_task(
faction_loop(new_faction_id, FRIENDLY_FILE, interval)
)