User Log and Persistent Faction Information
This commit is contained in:
@@ -17,10 +17,10 @@ enemy_lock = asyncio.Lock()
|
||||
|
||||
# Populate faction (memory only)
|
||||
async def populate_faction(faction_id: int, kind: str):
|
||||
"""
|
||||
Fetch members + FFScouter estimates once and store in STATE.
|
||||
kind: "friendly" or "enemy"
|
||||
"""
|
||||
|
||||
#Fetch members + FFScouter estimates once and store in STATE.
|
||||
#kind: "friendly" or "enemy"
|
||||
|
||||
url = f"https://api.torn.com/v2/faction/{faction_id}?selections=members&key={TORN_API_KEY}"
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
@@ -65,9 +65,7 @@ async def populate_faction(faction_id: int, kind: str):
|
||||
|
||||
# Status refresh loop
|
||||
async def refresh_status_loop(faction_id: int, kind: str, lock: asyncio.Lock, interval: int):
|
||||
"""
|
||||
Periodically refresh member statuses in STATE.
|
||||
"""
|
||||
#Periodically refresh member statuses in STATE.
|
||||
while True:
|
||||
try:
|
||||
url = f"https://api.torn.com/v2/faction/{faction_id}?selections=members&key={TORN_API_KEY}"
|
||||
@@ -100,6 +98,8 @@ async def populate_friendly(faction_id: int):
|
||||
return await populate_faction(faction_id, "friendly")
|
||||
|
||||
async def populate_enemy(faction_id: int):
|
||||
# Store enemy faction ID
|
||||
STATE.enemy_faction_id = faction_id
|
||||
return await populate_faction(faction_id, "enemy")
|
||||
|
||||
async def start_friendly_status_loop(faction_id: int, interval: int):
|
||||
@@ -109,6 +109,9 @@ async def start_friendly_status_loop(faction_id: int, interval: int):
|
||||
friendly_status_task = asyncio.create_task(
|
||||
refresh_status_loop(faction_id, "friendly", friendly_lock, interval)
|
||||
)
|
||||
# Save state
|
||||
STATE.friendly_status_interval = interval
|
||||
STATE.friendly_status_running = True
|
||||
|
||||
async def start_enemy_status_loop(faction_id: int, interval: int):
|
||||
global enemy_status_task
|
||||
@@ -117,3 +120,26 @@ async def start_enemy_status_loop(faction_id: int, interval: int):
|
||||
enemy_status_task = asyncio.create_task(
|
||||
refresh_status_loop(faction_id, "enemy", enemy_lock, interval)
|
||||
)
|
||||
# Save state
|
||||
STATE.enemy_status_interval = interval
|
||||
STATE.enemy_status_running = True
|
||||
|
||||
async def stop_friendly_status_loop():
|
||||
global friendly_status_task
|
||||
if friendly_status_task and not friendly_status_task.done():
|
||||
friendly_status_task.cancel()
|
||||
try:
|
||||
await friendly_status_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
STATE.friendly_status_running = False
|
||||
|
||||
async def stop_enemy_status_loop():
|
||||
global enemy_status_task
|
||||
if enemy_status_task and not enemy_status_task.done():
|
||||
enemy_status_task.cancel()
|
||||
try:
|
||||
await enemy_status_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
STATE.enemy_status_running = False
|
||||
|
||||
Reference in New Issue
Block a user