# `gamesrn-tw-counters` — version 1

The bulk file for the TW counters database: **core squads** and **counters** in one JSON. It is what
**Officer tools → Upload / export → Export** writes, and what the same box reads back, so the round trip
is: export → edit → Check → Publish.

| Endpoint | What it does |
|---|---|
| `GET /api/tw-squads` | The catalog: core squads with what is filed under each, and every defensive leader with its counts. |
| `GET /api/tw-squads?export=1` | Officers. This file, for the whole database. |
| `POST /api/tw-squads` `{ action:"import", mode:"validate", doc }` | Officers. What would change. Nothing is saved. Errors → HTTP 422 `{ ok:false, errors, warnings }`. |
| `POST /api/tw-squads` `{ action:"import", mode:"publish", doc }` | Officers. Applies it, logs it, re-files every counter and battle. Returns `{ ok, published, import_id, summary, changes, relinked }`. |
| `POST /api/tw-squads` `{ action:"undo", id }` | Officers. Puts back what one upload changed. |
| `POST /api/tw-squads` `{ action:"save_squad", squad }` / `{ action:"delete_squad", id }` | Officers. One squad (the Squad builder). |

`doc` may also be a **swgoh.gg squad-preset export** (`{ "type": "squad_preset_tabs", "tabs": [...] }`) or a
**HotUtils squad list** (`[ { "name", "combatType", "contents": [{ "characterId" }] } ]`). Those only list
squads, so see *Squad lists* below.

## Builds

The catalog can hold several **builds** — complete sets of core squads — with exactly one **active**. Only the active build files counters and battles; the rest are drafts that can be edited, exported, copied and made active at any time (switching re-files everything). Counters are shared by every build.

| Call | What it does |
|---|---|
| `GET /api/tw-squads?build=<id>` | View one build. For a draft, the counts show what it *would* file. |
| `GET /api/tw-squads?export=1&build=<id>` | Export that build's squads (plus the counters). The file carries `"build": "<name>"`. |
| `{ action:"new_build", name, copy_from }` | A new draft — empty, or a copy of another build. |
| `{ action:"rename_build", build, name }` · `{ action:"activate_build", build }` · `{ action:"delete_build", build }` | The active build can't be deleted. |
| `save_squad`, `import` | Take `build`; without it they go to the active build. |

## Top level

```json
{ "format": "gamesrn-tw-counters", "version": 1, "squads": [ … ], "counters": [ … ] }
```

Either list may be left out. Limits: 1,500 squads, 3,000 counters, 3 MB.

## `squads[]` — core squads

| Key | Notes |
|---|---|
| `id` | Optional. Present in an export; keep it to **edit** that squad. Without it the squad is matched by leader + core, else added. |
| `kind` | `"squad"` (default) or `"fleet"`. |
| `name` | Shown in the builder and on the Counters tab. |
| `lead` | Leader base id (capital ship for a fleet). Required. |
| `core` | Base ids that make it *this* squad, besides the leader. **Any five with the leader and every core unit is filed here.** May be empty (= every squad with that leader). At most 3 for a squad, 6 for a fleet is sensible — leave a slot free. |
| `flex` | The usual fills, most common first. Display only; never used for matching. |
| `roles` | `["defense","offense"]` (default), or one of them. A defense-only squad is never matched to an attacking five. |
| `note` | Optional. |
| `status` | `"hidden"` to keep it out of matching. |
| `delete` | `true` removes the squad. Its counters go back under "any version" of the leader. |

With several squads on one leader, the one with the **most core units** wins. Two squads may not share the same leader and core.

## `counters[]`

| Key | Notes |
|---|---|
| `id` | Optional. Present in an export; keep it to edit that counter. Without it, a counter with the same two unit lists and the same defensive datacron (set + L9 / focused) is updated, else a new one is added. |
| `kind` | `"squad"` or `"fleet"`. |
| `defense` | `{ "units": [lead, …], "datacron": { … } }` |
| `attack` | `{ "units": [lead, …], "datacron": { … } }` |
| `note` | Optional (an empty note never erases an existing one). |
| `source` | `"playbook"` (default — officer-curated), `"discord"` or `"member"`. |
| `sourceDetail` | Free text — where it came from. |
| `wins`, `losses` | Results known from outside the site (they add to battles logged on the site). |
| `verdict` | `"confirmed"` or `"failed"`. |
| `requiresDatacron`, `requiresOmicron` | `true` / `false`; leave out for "not sure". |
| `auto` | `"full"`, `"partial"` or `"manual"`. |
| `delete` | `true` with `id`: the counter is **hidden**, not erased — its logged battles, votes and comments stay, and Undo brings it back. |

`units` are base ids, leader first; up to 5 (squad) or 8 (fleet). Generic slots (`ANY`, `ANY_LS`, `ANY_F_JEDI`, `EMPTY`, …) are allowed after the leader.
A short defense is saved, with a warning, and sits under the squad's "any version".

`datacron`: `{ "none": true }` (no datacron), or `{ set, setName, lvl, focused, focusedLabel, l3, l6, l9, label }` with each ability as `{ id, label, text }` — the same shape the Counters tab stores.

**GAC projections are not in the file.** They are regenerated from data and stay as "potential" counters; an upload never touches them.

## Squad lists (swgoh.gg, HotUtils)

A squad list has no core, so the upload gives each squad one:

- **Several squads on one leader** — the units they don't all share are what tell them apart, so those become each squad's core (`Rey GAC` → General Kenobi, `Rey TW` → Fulcrum Ahsoka).
- **One squad on a leader** — the core the logged data suggests for that leader (units in at least 70% of the full squads on file), or none.

Everything else is flex. A squad list **never renames or re-roles** a squad that already exists with the same leader and core — it only adds fills. The "Squad lists come in as" boxes set whether imported squads are Defense, Attack or both.
Change any core afterwards in the Squads tab.

## Errors and notes

**Errors (nothing is saved):** not JSON; wrong `format` / `version`; a squad with no or an unknown leader, an unknown unit, a ship in a squad or a character in a fleet, a unit listed twice, more core units than slots; a counter with no units on a side, too many units, or a generic leader.

**Notes (saved anyway):** two squads in the file with the same leader and core (merged); a squad where every slot is core; a counter whose defense is not a full squad.

## Undo

Every publish is logged with the rows as they were. **Upload history → Undo** deletes squads the upload added, restores squads it changed or deleted, restores counters it changed, and hides counters it added.
