Files
recflare/apps/econ/src/inventory-db.ts
T
devin 178d3b5b0e support for 202507 endpoints (#37)
* [auth][api] accept the 20250424.01 client

* [2025] unstable

* 20250718.0

* correct one this time

* stubs

* more stubs

* more stubs

* [lists] add worker

* [ai] route stubs

* [api] player photo setting

* [econ] add roomEconConfig route

* [infra] update worker generators

* [worker] add cards/moderation/platformnotification workers

* [lists] updates to some endpoints

* [clubs] stub out announcement endpoint, for now

* [econ] stub out season endpoints for now

* [chat] apps/chat stub out party endpoint not sure the shape yet

* [api] stub out statsig and lockeditems

* [doc] new services

* [lists] stub the bulk endpoint

* [datacollection] add placeholder service until we can kill it

* [api] set gifting to lvl5

* update lock

* [cdn] enable cache

* [match] matchmake v2

* [lists] stub some lists

* [ai] stubs

* [rooms] new subroom save endpoint

* [econ] add bulk purchase endpoint

* [discovery] update featured creator to 1 for fun

* [api] add photo settings flag

* [chat] fixup chat permissions (sorta)

* [auth] restrictions endpoint

* [rooms] contributed endpoint

* [api] fix outfit endpoint

* [discovery] attempt to fix store

* [chat] privacy endpoints

* [api] cheered images

* [rooms] add xp endpoint (disbaled)

* [rooms] add xp endpoint (disabled)

* update images-db for cheers

* [rooms] add autocomplete endpoint

* [cdn/img] increase cache ttl for statics

* [api] bulk route for images

* [accounts] add banner image

* [api] add misc missing endpoints

* [discovery] remove AI tab

* [platformnotifications] stub some endpoints

* [lists] add some more lists

* [rooms] additional endpoints

* [chat] stub a few privacy endpoints

* [econ] stub some endpoints

* misc db fixes

* [api] tweak shape for images v6

* [rooms] dont show trending RROs
2026-08-21 15:15:55 -04:00

108 lines
4.1 KiB
TypeScript

/**
* Owned avatar items on the shared `recflare` D1 database — the items a player has
* bought from a storefront. One row per (account, item): the item is granted at
* purchase time (`POST /api/storefronts/v2/buyItem`) and read back by
* `GET /api/avatar/v4/items`, which concatenates it with the default catalog.
*
* The item is keyed by its full `AvatarItemDesc` — the comma-delimited descriptor exactly
* as sent, trailing `,,,` and all — so re-buying the same item upserts rather than piling
* up duplicate rows. The descriptor is stored verbatim (not normalized): the client expects
* the commas back and fails without them. `data` is the rendered avatar-item DTO, stored
* opaquely and served back untouched; it matches the shape of the entries in
* default-avatar-items.json.
*
* This worker (`econ`) owns the table and its migration — see apps/econ/migrations/
* 0004_inventory.sql. The gift box the purchase also creates lives in a separate table
* (@repo/domain's received_gift); ownership does not depend on the box being opened.
*/
/** Schema DDL (mirror of migrations 0004_inventory.sql) — also builds the table in tests. */
export const INVENTORY_SCHEMA_DDL: string[] = [
`CREATE TABLE IF NOT EXISTS inventory (
account_id INTEGER NOT NULL,
avatar_item_desc TEXT NOT NULL,
data TEXT NOT NULL,
PRIMARY KEY (account_id, avatar_item_desc)
)`,
]
/**
* A rendered avatar item, as `/api/avatar/v4/items` serves it (same shape as the
* entries in default-avatar-items.json). `AvatarItemDesc` is the item's guid string
* and the row's key.
*/
export interface AvatarItem extends Record<string, unknown> {
AvatarItemType: number | null
AvatarItemDesc: string
PlatformMask: number
FriendlyName: string
Tooltip: string
Rarity: number
}
/**
* The camelCase DTO `GET /api/avatar/v4/items` serves. Distinct from the PascalCase
* `AvatarItem` we store and from what the sibling item endpoints (`defaultunlocked`,
* `defaultbaseavataritems`) serve — those hand back their stored/bundled records raw.
*/
export interface AvatarItemV4 {
avatarItemId: number
avatarItemDesc: string
friendlyName: string
tooltip: string
tagList: string
avatarItemType: number
rarity: number
isBaseAvatarItem: boolean
}
/**
* Project a stored or bundled avatar item into the v4 DTO. Neither source carries an
* `AvatarItemId`, a `TagList` or an `IsBaseAvatarItem` flag — the storefront gift-drops
* we grant from have none and the default catalog has none either — so those default to
* 0 / "" / false rather than being invented.
*/
export function toAvatarItemV4(item: Record<string, unknown>): AvatarItemV4 {
const str = (v: unknown): string => (typeof v === 'string' ? v : '')
const num = (v: unknown): number => (typeof v === 'number' ? v : 0)
return {
avatarItemId: num(item.AvatarItemId),
avatarItemDesc: str(item.AvatarItemDesc),
friendlyName: str(item.FriendlyName),
tooltip: str(item.Tooltip),
tagList: str(item.TagList),
avatarItemType: num(item.AvatarItemType),
rarity: num(item.Rarity),
isBaseAvatarItem: item.IsBaseAvatarItem === true,
}
}
/**
* Grant an item into a player's inventory. Upserts on (account_id, avatar_item_desc):
* owning an item is boolean, so re-buying it refreshes the stored DTO rather than
* adding a second copy. The descriptor is stored verbatim, commas included — the client
* expects the full comma-delimited form back.
*/
export async function grantItem(
db: D1Database,
accountId: number,
item: AvatarItem
): Promise<void> {
await db
.prepare(
`INSERT INTO inventory (account_id, avatar_item_desc, data) VALUES (?1, ?2, ?3)
ON CONFLICT (account_id, avatar_item_desc) DO UPDATE SET data = ?3`
)
.bind(accountId, item.AvatarItemDesc, JSON.stringify(item))
.run()
}
/** Every avatar item a player owns, ordered by item guid for a stable listing. */
export async function getInventory(db: D1Database, accountId: number): Promise<AvatarItem[]> {
const { results } = await db
.prepare('SELECT data FROM inventory WHERE account_id = ?1 ORDER BY avatar_item_desc')
.bind(accountId)
.all<{ data: string }>()
return results.map((r) => JSON.parse(r.data) as AvatarItem)
}