1 player economy authority
forgejo-actions edited this page 2026-07-11 12:38:13 +00:00

Player Economy Authority

Player economy authority owns durable inventory and currency mutation for online gameplay. It is the shared server layer used by quest rewards, NPC services, trigger rewards, combat rewards, and other gameplay authority paths that need to grant, consume, or inspect player-owned items and balances.

Purpose

Use this page when adding an economy-backed gameplay effect, debugging missing item or gold rewards, or deciding whether a feature should mutate client state directly. The answer is usually no: clients receive snapshots, while server authority persists inventory and currencies.

Durable Tables

Inventory lives in character_inventory. Currencies live in character_currencies. Idempotency records live beside the mutation surface: inventory grant applications prevent duplicate item grants, and currency event applications prevent duplicate balance changes.

Gold is canonicalized through the currency system. Treat currencies.gold and legacy gold aliases carefully; new durable grants should resolve to the canonical currency id used by the economy authority.

AuthorityEconomyTransaction

AuthorityEconomyTransaction wraps inventory, currency, and related gameplay-state persistence inside one authority-owned transaction. Gameplay authority code stages state changes, persists the gameplay state, applies rewards/costs, and finishes the transaction only after the authoritative changes are ready to commit.

This ordering matters. If a quest or service changes both gameplay condition state and inventory/currency, the state save must happen before the transaction commits the economy side effects.

Inventory Grant And Consume Rules

Inventory grants are policy-driven. Stack limits are enforced at mutation time, not trusted from client input. Consumes must verify that enough quantity exists before committing. Negative deltas are costs; positive deltas are grants.

When a feature needs an item cost, build it as an economy mutation and let the authority layer produce the durable result. Do not subtract from the session snapshot by hand.

Currency Rules

Currency mutations use canonical ids and idempotency events. A zero mutation should not create surprising state. A positive mutation grants, a negative mutation spends, and the transaction must guard against balances that would go below zero.

Snapshot Emission

player_snapshot loads inventory and currency rows into wire snapshots. Bootstrap sends the baseline; later gameplay authority paths emit updated snapshots or response packets so the UI can reconcile.

If durable state changed but clients still show old state, inspect whether the authority path emitted the updated inventory/currency snapshot after commit.

Callers And Boundaries

Economy authority is a runtime service boundary, not an authoring catalog. Catalogs define item ids, rewards, costs, and semantic effects. Runtime authority validates and applies the mutation against the database and session state.

Debugging

For duplicate rewards, inspect idempotency application ids first. For missing gold, confirm the currency id was canonicalized and that migration 0018 has run. For missing items, inspect stack caps, grant application records, and inventory snapshot loading.