add make command make reformat
reformat python files with black
This commit is contained in:
parent
66ed711fec
commit
431ce1c8d0
3 changed files with 82 additions and 58 deletions
54
main.py
54
main.py
|
|
@ -19,9 +19,10 @@ app = FastAPI()
|
|||
live_snapshots: Dict[str, dict] = {}
|
||||
|
||||
SHARED_SECRET = "your_shared_secret"
|
||||
#LOG_FILE = "telemetry_log.jsonl"
|
||||
# LOG_FILE = "telemetry_log.jsonl"
|
||||
# ------------------------------------------------------------------
|
||||
ACTIVE_WINDOW = timedelta(seconds=30) # player is “online” if seen in last 30 s
|
||||
ACTIVE_WINDOW = timedelta(seconds=30) # player is “online” if seen in last 30 s
|
||||
|
||||
|
||||
class TelemetrySnapshot(BaseModel):
|
||||
character_name: str
|
||||
|
|
@ -29,9 +30,9 @@ class TelemetrySnapshot(BaseModel):
|
|||
session_id: str
|
||||
timestamp: datetime
|
||||
|
||||
ew: float # +E / –W
|
||||
ns: float # +N / –S
|
||||
z: float
|
||||
ew: float # +E / –W
|
||||
ns: float # +N / –S
|
||||
z: float
|
||||
|
||||
kills: int
|
||||
kills_per_hour: Optional[str] = None # now optional
|
||||
|
|
@ -51,8 +52,7 @@ def on_startup():
|
|||
@app.post("/position")
|
||||
@app.post("/position/")
|
||||
async def receive_snapshot(
|
||||
snapshot: TelemetrySnapshot,
|
||||
x_plugin_secret: str = Header(None)
|
||||
snapshot: TelemetrySnapshot, x_plugin_secret: str = Header(None)
|
||||
):
|
||||
if x_plugin_secret != SHARED_SECRET:
|
||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||
|
|
@ -64,10 +64,12 @@ async def receive_snapshot(
|
|||
save_snapshot(snapshot.dict())
|
||||
|
||||
# optional log-file append
|
||||
#with open(LOG_FILE, "a") as f:
|
||||
# with open(LOG_FILE, "a") as f:
|
||||
# f.write(json.dumps(snapshot.dict(), default=str) + "\n")
|
||||
|
||||
print(f"[{datetime.now()}] {snapshot.character_name} @ NS={snapshot.ns:+.2f}, EW={snapshot.ew:+.2f}")
|
||||
print(
|
||||
f"[{datetime.now()}] {snapshot.character_name} @ NS={snapshot.ns:+.2f}, EW={snapshot.ew:+.2f}"
|
||||
)
|
||||
|
||||
return {"status": "ok"}
|
||||
|
||||
|
|
@ -90,18 +92,19 @@ def get_live_players():
|
|||
cutoff = datetime.utcnow().replace(tzinfo=timezone.utc) - ACTIVE_WINDOW
|
||||
|
||||
players = [
|
||||
dict(r) for r in rows
|
||||
if datetime.fromisoformat(
|
||||
r["timestamp"].replace('Z', '+00:00')
|
||||
) > cutoff
|
||||
dict(r)
|
||||
for r in rows
|
||||
if datetime.fromisoformat(r["timestamp"].replace("Z", "+00:00")) > cutoff
|
||||
]
|
||||
|
||||
return JSONResponse(content={"players": players})
|
||||
|
||||
|
||||
@app.get("/history/")
|
||||
@app.get("/history")
|
||||
def get_history(
|
||||
from_ts: str | None = Query(None, alias="from"),
|
||||
to_ts: str | None = Query(None, alias="to"),
|
||||
to_ts: str | None = Query(None, alias="to"),
|
||||
):
|
||||
"""
|
||||
Returns a time‐ordered list of telemetry snapshots:
|
||||
|
|
@ -142,15 +145,16 @@ def get_history(
|
|||
|
||||
data = [
|
||||
{
|
||||
"timestamp": row["timestamp"],
|
||||
"character_name":row["character_name"],
|
||||
"kills": row["kills"],
|
||||
"kph": row["kph"],
|
||||
"timestamp": row["timestamp"],
|
||||
"character_name": row["character_name"],
|
||||
"kills": row["kills"],
|
||||
"kph": row["kph"],
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
return JSONResponse(content={"data": data})
|
||||
|
||||
|
||||
# ------------------------ GET Trails ---------------------------------
|
||||
@app.get("/trails")
|
||||
@app.get("/trails/")
|
||||
|
|
@ -162,7 +166,9 @@ def get_trails(
|
|||
for the past `seconds` seconds.
|
||||
"""
|
||||
# match the same string format as stored timestamps (via str(datetime))
|
||||
cutoff_dt = datetime.utcnow().replace(tzinfo=timezone.utc) - timedelta(seconds=seconds)
|
||||
cutoff_dt = datetime.utcnow().replace(tzinfo=timezone.utc) - timedelta(
|
||||
seconds=seconds
|
||||
)
|
||||
cutoff = str(cutoff_dt)
|
||||
conn = sqlite3.connect(DB_FILE)
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
|
@ -173,16 +179,16 @@ def get_trails(
|
|||
WHERE timestamp >= ?
|
||||
ORDER BY character_name, timestamp
|
||||
""",
|
||||
(cutoff,)
|
||||
(cutoff,),
|
||||
).fetchall()
|
||||
conn.close()
|
||||
trails = [
|
||||
{
|
||||
"timestamp": r["timestamp"],
|
||||
"timestamp": r["timestamp"],
|
||||
"character_name": r["character_name"],
|
||||
"ew": r["ew"],
|
||||
"ns": r["ns"],
|
||||
"z": r["z"],
|
||||
"ew": r["ew"],
|
||||
"ns": r["ns"],
|
||||
"z": r["z"],
|
||||
}
|
||||
for r in rows
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue