About this project

Torn Faction Attacks is a Cloudflare Worker and React dashboard designed to import Torn faction attack data into Cloudflare D1 and track war-level and member-level faction performance. Key features include scheduled ingestion from the Torn API, immutable attack storage, war tracking (active, scheduled, ended, imported), automatic assignment of attacks to war windows, summary tables for war totals and member stats, JSON API endpoints, and a React dashboard. Stack: Cloudflare Workers, D1, Wrangler, TypeScript, Torn API v2. Project structure is modular with files for ingestion, wars, reports, summaries, enemy scouting, auth, SQL helpers, constants, types, and utilities. Migrations incrementally add tables for attacks, wars, sync state, summaries, member stats, enemy/home faction members, activity heatmaps, admin users, and auth sessions. Configuration requires a D1 binding named `DB`, a secret `TORN_API_KEY`, and optional secrets for BSP and FFScouter APIs. A shared secret `MONITOR_TICKET_SECRET` signs WebSocket tickets for an enemy hospital monitor. The home faction ID is configured in `src/constants.ts`. Database migrations can be applied locally or remotely via Wrangler. The main tables are listed in the README. API endpoints include health checks, manual ingestion, rebuild stats, list attacks, create/import/end wars, get war summaries, enemy scouting, scouting comparison, activity heatmaps, chain bonuses, and member respect recalculation. Authentication uses Torn keys and admin access is granted to faction members, optionally auto-adding admins with faction access. A Discord slash command `/war me` shows the caller's attack performance and respect gained privately, subject to a 30-second cooldown per war/member. Scheduled ingestion runs every minute via cron, handling war creation/activation, attack fetching, summary updates, score tracking, and enemy scouting. A maintenance run every 15 minutes samples activity, refreshes revivable status, fetches missing reports, and retries stat estimates. Development instructions cover installation, checks, local dev, and deployment. The React dashboard in `dashboard/` is deployed via Cloudflare Pages with build settings and an environment variable for the API base URL. The admin model is intentionally lightweight while the project is evolving.