1
0
Fork 0
polaris-task-force/docs/arma-bridge/garage-sync.md

138 lines
7.5 KiB
Markdown

# Garage Sync Contract
Binding contract for vehicle-garage sync between the Arma 3 addon and the web
app. It covers **structure vehicle bays** (the Garage view at
`/logistics/garage`): parking, dispatching, and state (fuel/damage) of specific
vehicle **instances**. Implement exactly what is written here; do not redesign
while implementing.
## Overview
The Garage is a grid inventory of vehicle instances parked in structure vehicle
bays. The web app owns placement (grid cells are pure web bookkeeping, never
sent to the game); the game owns physical existence. Every physical vehicle
instance carries a `bridgeUid` assigned by the addon so two identical trucks are
distinguishable and fuel/damage stay honest per instance.
**Authority and physical despawn policy.** Until the addon ships, the garage is
**web-authoritative**: dispatch/store only change web state and enqueue commands
that no client consumes yet. Once the addon implements this contract:
- `garage.dispatch` means the physical object must **despawn from the bay**. The
unit relocates through game logic (cargo plane, sea port, scenario start),
never by driving out of the garage cell.
- `garage.store` means the addon must **spawn (or relocate) the instance at this
garage** with the given fuel and damage.
- `garage.apply` is a full reconciliation of the bay: spawn missing entries,
update existing ones, and despawn bay objects whose `bridgeUid` is absent
from the list.
- Cell coordinates are never transmitted. A web-side drag that only rearranges
cells must not produce any game-side movement.
## Instance identity
| Ref | Meaning |
|---|---|
| `garage:<storageSlug>` | One structure's vehicle bay. `<storageSlug>` is the structure's existing `storageSlug` field (the same slug the arsenal module binds to via `structure:<slug>`). No separate garage slug exists (assumption A1). |
A vehicle instance is identified on the wire by its `bridgeUid`: an opaque
addon-assigned string (1-64 chars, `[A-Za-z0-9_-]+`), stable for the lifetime of
the physical object and persisted across missions if the object persists. The
web claims unknown instances by classname match against unassigned vehicles at
that structure (first-tag-wins, provenance logged); otherwise it creates a new
`game-vehicles` row.
`fuel` is a fraction of tank capacity (0..1). `damage` is a fraction of total
damage (0 = intact, 1 = destroyed). Both are clamped by the web; out-of-range or
non-finite values fall back to 0.
## Event family
Payloads are flat arrays (`fn_queueEvent` stores `[_id, _type, _payload,
serverTime]`, payload defaults to `[]`). Events ride the existing
`POST /api/arma/v1/events` flush; dedup is by the unique `eventId`
(`<serverId>:<localId>`), so retries are no-ops.
| Type | Payload | Meaning |
|---|---|---|
| `garage.deposit` | `[garageRef, bridgeUid, classname, fuel, damage]` | An instance entered the bay and is parked. |
| `garage.withdraw` | `[garageRef, bridgeUid, classname, fuel, damage]` | A known instance physically left the bay (driven out, destroyed in place, deleted). |
| `garage.sync` | `[garageRef, [[bridgeUid, classname, fuel, damage], ...]]` | Full snapshot of instances currently parked in the bay. |
| `garage.request` | `[garageRef]` | The game asks the web to push current state for this garage (mission start, garage module load). |
## Web-side consumption rules
Processed from the `arma-sync-events` afterChange router (`src/lib/arma-bridge/garages.ts`):
1. **Claim or create.** Resolve `bridgeUid` to a `game-vehicles` row. Unknown
uid: claim the oldest untagged (null `bridgeUid`) instance whose populated
template `classname` matches, at the structure named by `garageRef`
(first-tag-wins; the claim is logged with the event id as provenance). No
candidate: create a new `game-vehicles` instance from the template matching
`classname`; unknown classnames are skipped with a warning.
2. **State merge.** Apply `fuel` (scaled to the template's `fuelCapacity`) and
`damage` (scaled to the template's `maxHealth`). `damage >= 1` sets status
`destroyed`; recovery below 1 clears `destroyed`/`damaged` back to `idle`.
Statuses `assigned` and `in_transit` are never overwritten by sync.
3. **Placement.** Deposit/sync entries without a cell are auto-placed row-major
into free bay cells. Cells already set on web rows are **preserved**; sync
never moves parked vehicles. A snapshot line for an unknown-to-the-game row
updates state only; absence from a snapshot is **not** deletion or
withdrawal.
4. **Withdraw.** Clears the instance's bay cells and `deployedAt`, sets status
`in_transit` (unless destroyed), keeps `bridgeUid`.
5. **Echo guard.** All writes made while processing garage events run with
`req.context.garageSyncOrigin = true`. Garage commands (`garage.apply`,
`garage.dispatch`, `garage.store`) are enqueued only by explicit user
actions, so sync-originated writes never bounce back as commands.
6. Processors **never throw**: malformed payloads log a warning and return;
unexpected errors are caught and logged so event ingestion cannot roll back.
## Command envelope and schemas
Envelope: `[commandId, commandType, payload]` (what `fn_pollCommands` passes
and `fn_executeCommand` destructures), stored in `arma-commands` and pulled via
`GET /api/arma/v1/commands/pull`. Commands target the most recently heartbeated
online game server (single-server assumption, same as locker sync).
| Type | Payload | Effect |
|---|---|---|
| `garage.apply` | `[garageRef, [[bridgeUid, classname, fuel, damage], ...]]` | Full state push for the bay; the addon reconciles as described above. Sent in response to `garage.request`. |
| `garage.dispatch` | `[garageRef, bridgeUid, classname, fuel, damage]` | Despawn this instance from the bay; it is now in transit (web cleared its cell and location). |
| `garage.store` | `[garageRef, bridgeUid, classname, fuel, damage]` | Spawn/relocate this instance into this garage bay. |
Instances without a `bridgeUid` (never seen by the game) produce no commands;
their dispatch/store stays web-only.
## Failure rules
- Unknown `garageRef` slug: warn and skip the event.
- Malformed payload shape (not an array, bad ref, non-string uid, non-finite
numbers): warn and skip the whole event, never partially apply.
- Snapshot arrays are capped at 200 lines; longer snapshots are truncated with
a warning.
- Commands are fire-and-forget: no online server means the enqueue is skipped
with a warning (the web state is already committed).
- Duplicate `eventId` deliveries are dropped by the unique index before the
processor runs.
## Security
- Server identity always comes from the authenticated bridge connection
(`x-arma-bridge-key` + `x-arma-server-id`), never from event payloads.
- Events are read-only inserts for the bridge; the addon cannot call
`arma-commands` directly, only pull/ack them.
- Classnames and uids are treated as opaque strings; they are length-capped and
never interpolated into queries beyond Payload's parameterized `where`.
## Mod-side behavior (garage module)
- Assign each tracked physical vehicle a `bridgeUid` on spawn; persist it on the
object (e.g. `setVariable`) and reuse it in every event.
- Emit `garage.deposit` when an instance parks in a bay zone, `garage.withdraw`
when it leaves or dies, and batch `garage.sync` after any committed change to
bay contents (same flush cadence as locker sync).
- On module load / mission start, emit `garage.request` per configured bay and
apply the returned `garage.apply` snapshot.
- Ack every executed command through the existing commands ack route; failed
executions ack with `success: false` and an error string.