Saving player data to json file
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
*.venv
|
*.venv
|
||||||
temp.md
|
temp.md
|
||||||
|
*data
|
||||||
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,6 @@
|
|||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from services.torn_api import fetch_enemy_members
|
from services.torn_api import update_enemy_faction, update_friendly_faction
|
||||||
from services.ffscouter import fetch_batch_stats
|
|
||||||
|
|
||||||
class HitCommands(commands.Cog):
|
class HitCommands(commands.Cog):
|
||||||
def __init__(self, bot, enrolled_attackers, enemy_queue):
|
def __init__(self, bot, enrolled_attackers, enemy_queue):
|
||||||
@@ -26,29 +26,19 @@ class HitCommands(commands.Cog):
|
|||||||
self.enrolled_attackers.remove(user_id)
|
self.enrolled_attackers.remove(user_id)
|
||||||
await ctx.send(f"{ctx.author.mention} has been removed from the rotation.")
|
await ctx.send(f"{ctx.author.mention} has been removed from the rotation.")
|
||||||
|
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def stats(self, ctx):
|
async def update_enemy(self, ctx):
|
||||||
members = await fetch_enemy_members()
|
success = await update_enemy_faction()
|
||||||
if not members:
|
if success:
|
||||||
await ctx.send("No active members found.")
|
await ctx.send("Enemy faction data updated with estimated stats!")
|
||||||
return
|
else:
|
||||||
|
await ctx.send("Failed to update enemy faction data.")
|
||||||
|
|
||||||
ids = [m["id"] for m in members if m.get("status", {}).get("state") in ("Okay", "Idle")]
|
@commands.command()
|
||||||
ff_map = await fetch_batch_stats(ids)
|
async def update_friendly(self, ctx):
|
||||||
|
success = await update_friendly_faction()
|
||||||
lines = []
|
if success:
|
||||||
for m in members:
|
await ctx.send("Friendly faction data updated with estimated stats!")
|
||||||
pid = str(m["id"])
|
else:
|
||||||
est = ff_map.get(pid, {}).get("bs_estimate_human", "?")
|
await ctx.send("Failed to update friendly faction data.")
|
||||||
if m.get("status", {}).get("state") not in ("Okay", "Idle"):
|
|
||||||
continue
|
|
||||||
lines.append(f"**{m['name']}** (ID:{pid}) | Lv {m['level']} | Estimated BS: {est}")
|
|
||||||
|
|
||||||
chunk = ""
|
|
||||||
for line in lines:
|
|
||||||
if len(chunk) + len(line) > 1900:
|
|
||||||
await ctx.send(chunk)
|
|
||||||
chunk = ""
|
|
||||||
chunk += line + "\n"
|
|
||||||
if chunk:
|
|
||||||
await ctx.send(chunk)
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# Torn API
|
# Torn API
|
||||||
TORN_API_KEY = "9VLK0Wte1BwXOheB"
|
TORN_API_KEY = "9VLK0Wte1BwXOheB"
|
||||||
ENEMY_FACTION_ID = 52935
|
ENEMY_FACTION_ID = 55325
|
||||||
YOUR_FACTION_ID = 654321
|
YOUR_FACTION_ID = 52935
|
||||||
ALLOWED_CHANNEL_ID = 1442876328536707316
|
ALLOWED_CHANNEL_ID = 1442876328536707316
|
||||||
|
|
||||||
# FFScouter API
|
# FFScouter API
|
||||||
|
|||||||
Binary file not shown.
@@ -1,12 +1,68 @@
|
|||||||
|
# services/torn_api.py
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from config import TORN_API_KEY, ENEMY_FACTION_ID
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from config import TORN_API_KEY, ENEMY_FACTION_ID, YOUR_FACTION_ID
|
||||||
|
from .ffscouter import fetch_batch_stats
|
||||||
|
|
||||||
|
ENEMY_FILE = Path("data/enemy_faction.json")
|
||||||
|
FRIENDLY_FILE = Path("data/friendly_faction.json")
|
||||||
|
|
||||||
|
|
||||||
|
async def fetch_and_save_faction(faction_id: int, file_path: Path) -> bool:
|
||||||
|
"""
|
||||||
|
Fetches faction members from Torn, fetches their estimated BS from FFScouter,
|
||||||
|
and saves everything to a JSON file.
|
||||||
|
"""
|
||||||
|
url = f"https://api.torn.com/v2/faction/{faction_id}?selections=members&key={TORN_API_KEY}"
|
||||||
|
|
||||||
async def fetch_enemy_members():
|
|
||||||
url = f"https://api.torn.com/v2/faction/{ENEMY_FACTION_ID}?selections=members&key={TORN_API_KEY}"
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(url) as resp:
|
async with session.get(url) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
print("Torn faction fetch error:", resp.status)
|
print(f"Torn faction fetch error: {resp.status}")
|
||||||
return []
|
return False
|
||||||
data = await resp.json()
|
data = await resp.json()
|
||||||
return data.get("members", [])
|
|
||||||
|
members_list = data.get("members", [])
|
||||||
|
if not members_list:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Build list of IDs (Torn uses 'id', not 'player_id')
|
||||||
|
member_ids = [info.get("id") for info in members_list if "id" in info]
|
||||||
|
if not member_ids:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Fetch batch FFScouter stats
|
||||||
|
ff_data = await fetch_batch_stats(member_ids) # returns dict keyed by player_id
|
||||||
|
|
||||||
|
# Build final faction data
|
||||||
|
faction_data = []
|
||||||
|
for info in members_list:
|
||||||
|
pid = info.get("id")
|
||||||
|
if pid is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
est = ff_data.get(str(pid), {}).get("bs_estimate_human", "?")
|
||||||
|
member = {
|
||||||
|
"id": pid,
|
||||||
|
"name": info.get("name", "Unknown"),
|
||||||
|
"level": info.get("level", 0),
|
||||||
|
"estimate": est
|
||||||
|
}
|
||||||
|
faction_data.append(member)
|
||||||
|
|
||||||
|
# Save to file
|
||||||
|
file_path.parent.mkdir(exist_ok=True, parents=True) # ensure folder exists
|
||||||
|
with open(file_path, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(faction_data, f, indent=2)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user