# Locker Sync Contract Binding contract for limited-arsenal storage sync between the Arma 3 mods and the web app. It covers **personal lockers** (web `locker-storages`, one per member) and **structure item storage** (web `game-structures.storedAssets`, e.g. the Superstructure). Implement exactly what is written here; do not redesign while implementing. ## Overview In-game, each storage is a limited ACE Arsenal: every item class has a finite quantity. The game server is authoritative for what a player physically takes or deposits in a session; the web app is authoritative for everything outside the session (member UI edits, admin edits, future crafting/market flows). Two existing bridge channels carry the data; **no new extension channels** (same precedent as crate sync — the Python module is closed): - **Events (mod to web)**: the `locker.*` event family, delivered through the existing `flush_events` ingestion (`POST /api/arma/v1/events`). - **Commands (web to mod)**: the `locker.*` command types, delivered through the existing `pull_commands` / `ack_command` loop. ## Instance identity An `instanceRef` string identifies one storage: | Form | Meaning | |---|---| | `personal:` | The personal locker of the member whose `users.steamId` matches. | | `structure:` | The `storedAssets` of the `game-structures` row with this `storageSlug` (e.g. `structure:superstructure`). | - The mod NEVER embeds a server identifier in an instanceRef. The web resolves the owning server authoritatively from the authenticated connection (`eventId = ${serverId}:${event.id}`), exactly as in crate sync. - `personal:` refs are derived from the opening player's UID at open time; the same physical locker box serves every player with their own stock. - `structure:` refs are bound to boxes by the TWWK Eden/Zeus module (`StructureStorageSlug` argument). Only structures with a `storageSlug` expose item storage to the game. ## Event family Payloads are flat arrays (`fn_queueEvent` stores `[_id, _type, _payload, serverTime]`, payload defaults to `[]`). | Type | Payload | Meaning | |---|---|---| | `locker.sync` | `[instanceRef, [[itemClass, count], ...]]` | Full stock snapshot for one instance after a committed change. | | `locker.request` | `[instanceRef]` | The game asks the web to push current state for this instance (mission start, player connect, availability refresh). | `locker.sync` snapshot rules: - TOP-LEVEL stock only: `[itemClass, count]` pairs, one per class, counts are integers >= 0. Classes with count 0 MAY be included (explicit depletion) or omitted (treated as unknown/absent, not as zero — see merge rules). - Emitted after every committed stock mutation (take settlement, deposit, Zeus/Eden edit, `locker.apply` application) while the bridge is enabled. - Dedup is web-side by `eventId`; retried flushes never double-apply. Snapshots are idempotent wholesale state, so re-application is safe. ## Web-side consumption rules `locker.sync` merges into the instance's item array keyed by Arma class name: - `personal:`: resolve `users.steamId` → that user's `locker-storages` row (created lazily if a linked user has none). Unlinked Steam IDs are logged and skipped (same rule as money sync); the game keeps a session-only stock for them. - `structure:`: resolve the `game-structures` row by `storageSlug`. Unknown slug is logged and skipped. - Merge per entry by `asset.className`: - Existing web entry whose class appears in the snapshot → `quantity` set to the snapshot count. **All web-managed metadata is preserved**: `gridX`, `gridY`, `rotated`, and the `equipment` group (attachments, skin, customName) are never touched by the merge. - Snapshot class with no web entry → create the entry (asset resolved by `className`; unknown classes are skipped with a warning) and **auto-place** it in the next free grid cell (row-major scan, unrotated). Equipment left empty. - Web entry whose class is absent from the snapshot → left untouched (absence is not deletion; the game only reports classes it tracks). - `itemCount` keeps deriving from the array length via its existing hook. - Every merged write links the raw `arma-sync-events` row as provenance where the storage model supports it. `locker.request` handling: - Enqueue exactly one `locker.apply` command for the requesting server with the web's current full snapshot for that instanceRef. Unknown/unlinked instanceRefs enqueue nothing. ## Command envelope and schemas Envelope: `[commandId, commandType, payload]` (what `fn_pollCommands` passes and `fn_executeCommand` destructures), stored in `arma-commands`. | Type | Payload | Effect | |---|---|---| | `locker.apply` | `[instanceRef, [[itemClass, count], ...]]` | Server replaces the instance's stock wholesale with the given pairs, pushes availability to clients, and acks. | - The web enqueues `locker.apply` when: (a) a member/admin edits locker items or structure `storedAssets` in the UI (afterChange hook), or (b) a `locker.request` event arrives. Target server: the most recently heartbeated game server (single-server assumption, same as crate UI flows). - The mod-side handler is registered through the bridge's command-handler registry (`ptf_bridge_fnc_registerCommandHandler`); the bridge mod itself stays storage-agnostic. - Unknown instanceRef → the mod acks FAILED with an error string; the command is marked `failed` web-side and is not retried by the mod. - Ack shape is the existing `[commandId, success, result, error]`; successful acks carry the applied instanceRef in `result` for traceability. - Echo guard: web writes that originate from a `locker.sync` merge mark `req.context.lockerSyncOrigin = true`; the afterChange enqueue hook skips such writes so a game-originated change never bounces back as a command. ## Failure rules - Web event processors never throw: malformed payloads are logged and skipped; one bad event never aborts the batch or rolls back raw-event ingestion. - Duplicate eventIds are deduped (counted as duplicates, no double-apply). - Bridge disabled/unreachable: the mod keeps full session-local stock; queued `locker.sync` events flush when the bridge returns (queue cap drops oldest, per existing bridge behavior). - Game store is volatile per mission (like crate registry): on mission start the mod emits `locker.request` for every bound structure instance and per connecting player, and the web replies with `locker.apply`. ## Security - All channels sit behind `requireArmaServer` (`ARMA_BRIDGE_API_KEY` + `x-arma-server-id`); server identity is always derived from the connection. - The `storageAccess` group on `game-structures` (anyone / faction / roles / users) governs **web UI** visibility/editing of `storedAssets` only; it does not constrain the bridge (server-to-server) or in-game physical access. ## Mod-side behavior (TWWK `twwk_arsenal`) - Server store: `instanceRef → (className → count)` in missionNamespace. - Availability: a class is in the ACE Arsenal virtual-item set iff count > 0 (shared instances: global `addVirtualItems`/`removeVirtualItems`; personal: targeted per-player local set). - Settlement: client snapshots gear on `ace_arsenal_displayOpened`, diffs on `ace_arsenal_displayClosed`, server validates/clamps and commits; live over-take into containers is clamped per `ace_arsenal_cargoChanged`. - Deposits run through the same commit path with the sign flipped.