feat(rust): Backpacks — staff and player views, the Bigger backpack step (PLAN_REDESIGNS §11.3)
All checks were successful
PR Checks / server-tests (pull_request) Successful in 37s
PR Checks / frozen-manifest (pull_request) Successful in 1m0s
PR Checks / client-build (pull_request) Successful in 7m23s

The site half of §11.3.2, on protocol 14:

- `rust.backpack.size` ("Bigger backpack"): a kit reward with a different
  permission (D337) — one rust_perm_run_grants row per website user with
  backpacks.size.N, no kit, no credit; the perm mirror pushes it and the run's
  revert deletes it. risk change, reversible ledger, a new `rust.backpacks`
  budget, rust.kit.entitle's recipient modes including `linked`. Refused with
  "Backpacks is not installed on this server" in the dry run too (D199), and a
  size the server does not offer is refused by name. Its message is never
  silent: empty is the take-back warning (D338).
- `rust.options.backpacksizes`: each server's sizes above its smallest, from
  the last hello, grouped by server.
- GET /player/rust/backpacks (own links only, 20 reads a minute) and
  GET /admin/users/:id/rust/backpacks (that user's links only). Live, never
  stored; a stranger's id is a 404 and never reaches the game.
- Client: a shared Backpack panel (pick server and account, open, read once)
  on the account page and the admin user page; the grid draws pages of
  pageSize, each slot named by the game (D340, no icons), blueprints by what
  they make (D341), overflow marked. Backpacks joins the server's mod line.

Server 548/548 (13 new in test/backpacks.test.js), client 79/79 (5 new),
routes.manifest regenerated against the pinned core (89 routes), swagger
fragment regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E14m6SuuY6i1vASFeGDBeY
This commit is contained in:
2026-10-12 14:35:55 -05:00
parent 69ec0f6c46
commit e4591af906
22 changed files with 1786 additions and 7 deletions

View File

@@ -10,6 +10,9 @@
// rust.announce one line in a server's chat, or every server's
// rust.coins.pay Economics coins for the people a mode picks,
// kept once paid (protocol 14, D335)
// rust.backpack.size a bigger Backpacks backpack until the run ends:
// a kit reward with `backpacks.size.N` as its
// permission and no kit (protocol 14, D337)
//
// …and the announce leg, `rust.chat`, which says a published news post in the
// chat of every server whose switch is on (D104).
@@ -43,6 +46,7 @@ const permDb = require('./model/permissions/permissions.db')
const linksDb = require('./model/links/links.db')
const emit = require('./engagement/emit')
const economics = require('./model/economics/economics.model')
const backpacks = require('./model/backpacks/backpacks.model')
const voice = require('./model/permissions/voice')
const { serverFor, transportError, pluginError, perServer, bounded } = require('./eventLeases')
const { BUDGET_MS } = require('./eventWorld')
@@ -112,6 +116,13 @@ const BUDGETS = [
description:
'Economics coins an event pays: the amount times the people paid. A mode that is not a count is priced at the most it could pay. Coins paid are kept — nothing takes them back.',
},
{
id: 'rust.backpacks',
label: 'Backpack sizes',
unit: 'backpacks',
description:
'Bigger backpacks an event lends until it ends: one per recipient. A mode that is not a count is priced at the most it could lend.',
},
]
/** A transport failure, classified. Only a missing configuration is one waiting cannot fix. */
@@ -904,6 +915,208 @@ const coinsPay = {
},
}
// ── A bigger backpack (protocol 14, PLAN_REDESIGNS.md §11.3.2, D337, D338) ──
/** Said when the author writes nothing: the take-back surprises a player nobody warned (D338). */
const BACKPACK_NOTICE =
'Your backpack holds {size} until the event ends. Anything past your normal size then moves to your inventory, or drops at your feet if it is full.'
/**
* A bigger backpack's message, checked like a kit reward's (D333): `{ ok, text,
* delivery }`. Unlike a kit reward's it is never silent: an empty message is
* the default warning above. Measured with the size in place of `{size}`.
*/
function backpackNotice(params, size) {
const written = params.message === undefined || params.message === null ? '' : String(params.message).trim()
const text = written || BACKPACK_NOTICE
const rawDelivery = params.delivery === undefined || params.delivery === null || params.delivery === '' ? 'chat' : params.delivery
const delivery = oneOf(rawDelivery, DELIVERIES)
if (!delivery) return { ok: false, error: `a bigger backpack's message goes to chat or to a popup, not to "${rawDelivery}"` }
const said = text.replace(/\{size\}/g, String(size))
if (said.length > MAX_CHAT) {
return { ok: false, error: `a bigger backpack's message is at most ${MAX_CHAT} characters with the size in it, not ${said.length}` }
}
return { ok: true, text, delivery }
}
const backpackSize = {
id: 'rust.backpack.size',
label: 'Bigger backpack',
description:
'Lends the people a mode picks from this run\'s tally — or every linked player of the server — a bigger Backpacks backpack until the run ends, and tells each of them in game. Waits for them if they are offline. Someone who already holds a size at least as big gains nothing and loses nothing. At the end, whatever is past their normal size moves to their inventory the next time they open the backpack, or drops at their feet if the inventory is full.',
// A kit reward with a different permission (D337): the same lease, the same
// revert, so the same risk.
risk: 'change',
reversible: 'ledger',
version: 1,
budgetMs: BUDGET_MS,
params: [
{ name: 'server', type: 'string', required: true, example: 'main', source: 'rust.options.servers',
description: 'The server whose backpacks grow. It needs Backpacks installed.' },
{ name: 'size', type: 'int', required: true, example: 48, source: 'rust.options.backpacksizes',
description: 'How many slots, one of the sizes the server\'s Backpacks offers (its backpacks.size.N permissions) above the smallest.' },
{ name: 'recipients', type: 'string', required: true, example: 'top', source: 'rust.options.recipientmodes',
description: 'Who gets it: everyone who scored, the top N, a score of at least X, N drawn at random, the top X per cent — or every linked player of the server, with no tally at all.' },
{ name: 'count', type: 'float', required: false, example: 3,
description: 'N for top and random, X for a minimum score, the percentage for top per cent. Not used for everyone or linked.' },
{ name: 'message', type: 'string', required: false, example: BACKPACK_NOTICE,
description: 'Said once to each player, in game: at once if they are up, otherwise when they next wake on the server. {size} is the size. Left empty, it is the example, which warns about the end.' },
{ name: 'delivery', type: 'string', required: false, example: 'chat', source: 'rust.options.delivery',
description: 'Chat, or a popup where the server has PopupNotifications (chat where it has not). Chat if left empty.' },
],
// Priced as a kit reward is: the count for a count, the recipient bound for
// every other mode.
cost: (p) => {
const mode = oneOf(p.recipients, MODES)
const n = Math.round(Number(p.count) || 0)
return { 'rust.backpacks': mode === 'top' || mode === 'random' ? Math.max(0, n) : MAX_RECIPIENTS }
},
async perform({ runId, stepId, idempotencyKey, params, verify }) {
const size = Number(params.size)
if (!Number.isInteger(size) || size < 1) return { ok: false, retry: false, error: `"${params.size}" is not a backpack size — pick one from the list` }
const mode = oneOf(params.recipients, MODES)
if (!mode) return { ok: false, retry: false, error: `recipients is one of ${MODES.join(', ')}, not "${params.recipients}"` }
const count = checkCount(mode, params.count)
if (!count.ok) return { ok: false, retry: false, error: count.error }
const notice = backpackNotice(params, size)
if (!notice.ok) return { ok: false, retry: false, error: notice.error }
const found = await serverFor(String(params.server || '').trim())
if (!found.ok) return found
const server = found.server
const name = server.name || server.id
// D199: refused with the reason where Backpacks is absent, in the dry run
// too, and a size the server does not offer is refused by name.
const report = await backpacks.forServer(server.id)
if (!report.loaded) return { ok: false, retry: false, error: `${backpacks.MISSING} (${name})` }
const offered = backpacks.leasable(report)
if (!offered.includes(size)) {
return {
ok: false,
retry: false,
error: offered.length
? `${name}'s Backpacks offers ${offered.join(', ')} slots above its smallest size, not ${size}`
: `${name}'s Backpacks offers no size above its smallest, so a bigger backpack gives nobody anything — add sizes to its "Permission sizes"`,
}
}
if ((mode === 'top' || mode === 'random') && count.value > MAX_RECIPIENTS) {
return { ok: false, retry: false, error: `${name} lends at most ${MAX_RECIPIENTS} backpacks in one step, not ${count.value}` }
}
if (verify) return { ok: true }
const permission = `backpacks.size.${size}`
const ref = entitlementRef(server.id, runId, stepId)
const resource = { kind: 'backpack-size', ref, payload: { serverId: server.id, permission } }
// As a kit reward's: the rows ARE the lease, and a repeated key finds them.
const existing = await permDb.listRunGrantsForStep(runId, stepId)
if (existing.length) {
return {
ok: true,
resources: [resource],
detail: { repeat: true, lent: new Set(existing.map((r) => r.userId)).size, note: 'answered from the first attempt; nothing new was lent' },
}
}
let picked
let bound = MAX_RECIPIENTS
let users
if (mode === 'linked') {
const linked = await linksDb.linkedOnServer(server.id, bound + 1)
picked = linked.map((r) => ({ steamId: String(r.steamId), name: r.name ? String(r.name) : String(r.steamId) }))
users = new Map(linked.map((r) => [String(r.steamId), Number(r.userId)]))
if (picked.length > bound) {
return {
ok: false,
retry: false,
error: `${name} has more than ${bound} linked players, and lends at most ${bound} backpacks in one step — lend from a tally instead`,
}
}
} else {
const tally = await readTally(server, runId)
if (!tally.ok) return tally
picked = pickRecipients(peopleOf(tally.data), mode, count.value, idempotencyKey)
bound = Number(tally.data.maxRecipients) || bound
if (picked.length > bound) {
return {
ok: false,
retry: false,
error: `${picked.length} people qualify, and ${name} lends at most ${bound} backpacks in one step — pick a count-based mode or a higher bar`,
}
}
users = await usersFor(picked.map((p) => p.steamId))
}
// One lease per website user (D103's rule). The permission reaches every
// account the user has linked, as a kit reward's does (D28).
const rows = []
const byUser = new Set()
const missed = []
for (const p of picked) {
const userId = users.get(p.steamId)
if (!userId) {
missed.push(p.name)
continue
}
if (byUser.has(userId)) continue
byUser.add(userId)
rows.push({
runId,
stepId,
idemKey: idempotencyKey,
userId,
serverId: server.id,
steamId: p.steamId,
permission,
kit: '',
credit: false,
notice: notice.text.replace(/\{size\}/g, String(size)),
noticeDelivery: notice.delivery,
})
}
await permDb.insertRunGrants(rows)
await permDb.markDirty(server.id)
log.info('backpacks lent', { server: server.id, run: runId, step: stepId, size, mode, lent: rows.length, missed: missed.length })
return {
ok: true,
resources: [resource],
detail: {
server: name,
size,
permission,
mode,
...(count.value === null ? {} : { count: count.value }),
lent: rows.length,
...(rows.length ? { told: `${rows.length} will be told in game, by ${notice.delivery}` } : {}),
...(missed.length ? { missed: missed.slice(0, 50), missedCount: missed.length, note: 'missed took part but have linked no website account' } : {}),
},
}
},
// The lease ends as a kit reward's does: the step's rows go and the next sync
// revokes the permission. Backpacks moves the overflow at the owner's next
// open (D338).
revert: (ctx) => kitEntitle.revert(ctx),
reconcile: (ctx) => kitEntitle.reconcile(ctx),
}
/**
* The body of a chat line: its delivery, and the voice's format when the line
* goes to chat and a voice is chosen (D140). A popup is not a chat line and
@@ -1141,6 +1354,21 @@ const OPTION_SOURCES = [
{ value: 'chat', label: 'Chat' },
{ value: 'popup', label: 'A popup — needs PopupNotifications on the server' },
]),
{
// From each server's last hello: the sizes its Backpacks offers above the
// smallest, grouped by server (D337). The step checks the pair it is given.
id: 'rust.options.backpacksizes',
label: 'Backpack sizes',
description: "Each server's Backpacks sizes above its smallest — its backpacks.size.N permissions.",
async resolve() {
const [list, reports] = await Promise.all([servers.listForPolling(), backpacks.byServer()])
const rows = []
for (const s of list) {
for (const n of backpacks.leasable(reports.get(s.id))) rows.push({ value: n, label: `${n} slots`, group: s.name || s.id })
}
return rows
},
},
{
id: 'rust.options.chatservers',
label: 'Chat servers',
@@ -1152,7 +1380,7 @@ const OPTION_SOURCES = [
},
]
const ACTIONS = [participationOpen, participationCollect, kitEntitle, coinsPay, announce]
const ACTIONS = [participationOpen, participationCollect, kitEntitle, coinsPay, backpackSize, announce]
module.exports = {
MAX_RECIPIENTS,
@@ -1171,6 +1399,8 @@ module.exports = {
rewardNotice,
coinsNotice,
paymentKey,
backpackNotice,
BACKPACK_NOTICE,
chatLine,
chatKey,
lineBody,

View File

@@ -0,0 +1,36 @@
// ── SQL for Backpacks (PLAN_REDESIGNS §11.3.2) ────────────────────────────
//
// What each server's last hello said about Backpacks. The backpacks themselves
// are never stored (§11.3.2: live, never stored); this file only reads the
// report the plugin makes about the plugin.
const core = require('../../core')
const STATE = 'rust_server_state'
/**
* One server's report: `{ serverId, backpacks }`, with `backpacks` as the
* driver hands JSON_EXTRACT back — text, or null for a server whose plugin
* never said (no state yet, or a plugin before protocol 14).
*/
async function reportFor(serverId) {
const rows = await core.query(
`SELECT server_id AS serverId,
JSON_EXTRACT(raw, '$.integrations.backpacks') AS backpacks
FROM ${STATE}
WHERE server_id = ?`,
[serverId],
)
return rows[0] || null
}
/** Every server's report, for the step's size list. */
async function reports() {
return core.query(
`SELECT server_id AS serverId,
JSON_EXTRACT(raw, '$.integrations.backpacks') AS backpacks
FROM ${STATE}`,
)
}
module.exports = { reportFor, reports }

View File

@@ -0,0 +1,129 @@
// ── Backpacks, as the site sees it (PLAN_REDESIGNS §11.3.2, D199, D337, D338) ─
//
// WhiteThunder's Backpacks is optional (D199): a server reports it in hello's
// `integrations.backpacks`, with the sizes its `backpacks.size.N` permissions
// offer, and every surface that needs it refuses with a reason where it is
// absent. Two readers share this file:
//
// • the step (`rust.backpack.size`, in eventRewards.js), which leases one of
// those sizes for a run (D337) and needs the list to check its param;
// • the staff view and the player's own page, which read one backpack live.
//
// **Live, never stored.** Each view is one `backpacks.read`, and nothing about
// a backpack is written to the database. The plugin only reads (R2).
const client = require('../../sidecarClient')
const db = require('./backpacks.db')
const servers = require('../servers/servers.model')
const { pluginError, transportError } = require('../../eventLeases')
/** The sentence every surface says where a server has no Backpacks (D199). */
const MISSING = 'Backpacks is not installed on this server'
/** JSON_EXTRACT comes back as text from the driver; an object passes through. */
function parse(value) {
if (value === null || value === undefined) return null
if (typeof value === 'object') return value
try {
return JSON.parse(value)
} catch {
return null
}
}
/**
* A server's Backpacks as its last hello said: `{ loaded: false }`, or loaded
* with its version and its sizes, ascending and whole.
*/
function shape(raw) {
const b = parse(raw)
if (!b || b.loaded !== true) return { loaded: false, sizes: [] }
const sizes = [...new Set((Array.isArray(b.sizes) ? b.sizes : []).map(Number).filter((n) => Number.isInteger(n) && n > 0))]
sizes.sort((x, y) => x - y)
return { loaded: true, version: typeof b.version === 'string' ? b.version : null, sizes }
}
/** One server's report. `{ loaded: false }` for a server never heard from. */
async function forServer(serverId) {
const row = await db.reportFor(serverId)
return shape(row && row.backpacks)
}
/** Every server's report, keyed by id. */
async function byServer() {
const rows = await db.reports()
return new Map(rows.map((r) => [r.serverId, shape(r.backpacks)]))
}
/**
* The sizes a step may lease on a server: every size but the smallest, which
* is the default everybody holds and so gives nobody anything.
*/
function leasable(report) {
return report && report.loaded ? report.sizes.slice(1) : []
}
/** One stack as the page draws it; nested `contents` keep the shape one level deep. */
function stack(raw, nest) {
const row = {
slot: Number(raw.slot) || 0,
shortname: typeof raw.shortname === 'string' ? raw.shortname : null,
display: typeof raw.display === 'string' ? raw.display : null,
amount: Number(raw.amount) || 0,
}
if (raw.itemId !== undefined) row.itemId = Number(raw.itemId)
if (typeof raw.blueprint === 'string') row.blueprint = raw.blueprint
// A skin id is past 2^53 more often than not; it is a label here, never a number to do sums with.
if (raw.skin !== undefined && raw.skin !== null) row.skin = String(raw.skin)
if (typeof raw.name === 'string' && raw.name) row.name = raw.name
if (raw.overflow === true) row.overflow = true
if (nest && Array.isArray(raw.contents) && raw.contents.length) row.contents = raw.contents.filter(Boolean).map((c) => stack(c, false))
return row
}
/**
* One player's backpack on one server, read live.
*
* Resolves `{ ok: true, backpack }` — `{ serverId, server, steamId, exists,
* capacity, pageSize, items }`, the last three only when it exists — or
* `{ ok: false, status, message, reason? }`: 404 for no such server, 409 for a
* server without Backpacks, 503 for one that could not be asked.
*/
async function read({ serverId, steamId }) {
const server = await servers.getForCalling(serverId)
if (!server) return { ok: false, status: 404, message: 'No such server' }
const name = server.name || server.id
const result = await client.backpacksRead(server, String(steamId))
if (!result.ok) return { ok: false, status: 503, message: transportError(server, result, 'backpacks') }
const data = result.data || {}
if (data.kind === 'backpacks.contents') {
const base = { serverId: server.id, server: name, steamId: String(steamId) }
if (data.exists !== true) return { ok: true, backpack: { ...base, exists: false } }
return {
ok: true,
backpack: {
...base,
exists: true,
capacity: Number(data.capacity) || 0,
pageSize: Number(data.pageSize) || 48,
items: (Array.isArray(data.items) ? data.items : []).filter(Boolean).map((s) => stack(s, true)),
},
}
}
if (data.reason === 'backpacks-missing') return { ok: false, status: 409, reason: data.reason, message: `${MISSING} (${name})` }
return {
ok: false,
status: data.reason === 'bad-id' ? 400 : 502,
reason: data.reason || null,
message: pluginError(data, `${name} did not answer with a backpack`),
}
}
module.exports = { MISSING, shape, forServer, byServer, leasable, stack, read }

View File

@@ -9,6 +9,7 @@ const crypto = require('node:crypto')
const core = require('../../core')
const backpacks = require('../../model/backpacks/backpacks.model')
const economics = require('../../model/economics/economics.model')
const links = require('../../model/links/links.model')
const permissionsDb = require('../../model/permissions/permissions.db')
@@ -275,4 +276,26 @@ async function adjustEconomics(req, res) {
}
}
module.exports = { listLinks, removeLink, listPermissions, addGrant, removeGrant, listEconomics, adjustEconomics }
/**
* GET /admin/users/:id/rust/backpacks — one of this user's backpacks
* (§11.3.2), read live. Scoped by the user like every route in this panel: a
* Steam id they have not linked is a 404, never a stranger's backpack.
*/
async function readBackpack(req, res) {
const userId = Number(req.params.id)
const { serverId, steamId } = req.query
try {
const held = await links.listForUser(userId)
if (!held.some((l) => String(l.steamId) === String(steamId))) {
return res.status(404).json({ message: 'That Steam account is not linked to this user' })
}
const result = await backpacks.read({ serverId, steamId })
if (!result.ok) return res.status(result.status).json({ message: result.message, ...(result.reason ? { reason: result.reason } : {}) })
return res.json(result.backpack)
} catch (err) {
log.error('failed to read a backpack', { userId, serverId, error: err.message })
return res.status(500).json({ message: 'Failed to read that backpack' })
}
}
module.exports = { listLinks, removeLink, listPermissions, addGrant, removeGrant, listEconomics, adjustEconomics, readBackpack }

View File

@@ -30,7 +30,7 @@
const core = require('../../core')
const express = core.express
const { body, param } = core.validator
const { body, param, query } = core.validator
const usersRust = require('./usersRust.controller')
const { validate } = core.middleware
@@ -171,4 +171,29 @@ usersRustRouter.post(
usersRust.adjustEconomics,
)
// ── Backpacks (PLAN_REDESIGNS §11.3.2): what a person carries, read live ──
//
// Read only (R2): there is no write beside it, here or in the game. Each open
// of the panel is one read; nothing is stored.
usersRustRouter.get(
'/rust/backpacks',
// #swagger.tags = ['Admin · Users']
// #swagger.summary = 'A user’s backpack on one server (admin only)'
// #swagger.description = 'One of this user’s linked Steam accounts’ Backpacks backpack on one server, read live from the game and never stored. The same shape as GET /player/rust/backpacks. Staff see the overflow too; only the owner’s own open of the backpack moves it.'
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
// #swagger.parameters['id'] = { in: 'path', required: true, schema: { type: 'integer' }, description: 'User id.' }
// #swagger.parameters['serverId'] = { in: 'query', required: true, schema: { type: 'string' }, description: 'The server.' }
// #swagger.parameters['steamId'] = { in: 'query', required: true, schema: { type: 'string' }, description: 'One of this user’s linked Steam ids.' }
/* #swagger.responses[200] = { description: 'The backpack', content: { "application/json": { schema: { $ref: "#/components/schemas/RustBackpack" } } } } */
/* #swagger.responses[404] = { description: 'No such server, or that Steam account is not linked to this user', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
/* #swagger.responses[409] = { description: 'Backpacks is not installed on that server', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
/* #swagger.responses[503] = { description: 'The server could not be reached', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
param('id').isInt(),
query('serverId').isString().isLength({ min: 1, max: 64 }),
query('steamId').isString().matches(STEAM_ID_RE),
validate,
usersRust.readBackpack,
)
module.exports = usersRustRouter

View File

@@ -21,6 +21,7 @@
const core = require('../../core')
const backpacks = require('../../model/backpacks/backpacks.model')
const economics = require('../../model/economics/economics.model')
const links = require('../../model/links/links.model')
const npcs = require('../../model/npcs/npcs.model')
@@ -70,6 +71,28 @@ async function ownEconomics(req, res) {
}
}
/**
* GET /player/rust/backpacks — one of the caller's own backpacks (§11.3.2),
* read live. Only an account the caller has linked: anybody else's is a 404,
* the same answer as an id nobody holds, so the route says nothing about
* whose ids exist.
*/
async function ownBackpack(req, res) {
const { serverId, steamId } = req.query
try {
const held = await links.listForUser(req.user.id)
if (!held.some((l) => String(l.steamId) === String(steamId))) {
return res.status(404).json({ message: 'That Steam account is not linked to you' })
}
const result = await backpacks.read({ serverId, steamId })
if (!result.ok) return res.status(result.status).json({ message: result.message, ...(result.reason ? { reason: result.reason } : {}) })
return res.json(result.backpack)
} catch (err) {
log.error('failed to read a player’s backpack', { serverId, error: err.message })
return res.status(500).json({ message: 'Failed to read your backpack' })
}
}
/** GET /player/rust/links — the Steam accounts the caller holds. */
async function listLinks(req, res) {
try {
@@ -202,4 +225,4 @@ async function listPermissions(req, res) {
}
}
module.exports = { listServers, listLinks, confirmLink, removeLink, listPermissions, ownNpcKills, ownEconomics }
module.exports = { listServers, listLinks, confirmLink, removeLink, listPermissions, ownNpcKills, ownEconomics, ownBackpack }

View File

@@ -24,7 +24,7 @@
const core = require('../../core')
const express = core.express
const { body, param } = core.validator
const { body, param, query } = core.validator
const rust = require('./rust.controller')
const { validate, rateLimit } = core.middleware
@@ -67,6 +67,37 @@ playerRustRouter.get(
rust.ownNpcKills,
)
/**
* A backpack read runs on the game's main thread (55–117 ms the first time
* after a boot, §11.3.1), so a page held open on refresh is not allowed to
* become a load on the server. Generous for a person, tight for a loop.
*/
const backpackLimiter = rateLimit({
windowMs: 60 * 1000,
max: 20,
label: 'rust-backpack-read',
message: 'Too many backpack reads. Please wait a minute.',
})
playerRustRouter.get(
'/backpacks',
// #swagger.tags = ['Player · Rust']
// #swagger.summary = 'Your backpack on one server'
// #swagger.description = 'One of the caller’s own linked Steam accounts’ Backpacks backpack on one server, read live from the game and never stored (PLAN_REDESIGNS §11.3.2). `exists: false` for an account that has never had one. Each stack has its slot, shortname, the game’s English name (`display`), amount, and skin, custom name, blueprint target and nested contents where set; `overflow` marks a stack past the capacity, which moves to the owner’s inventory the next time they open the backpack. 404 for a Steam account the caller has not linked; 409 where the server has no Backpacks. Requires a session; 20 reads a minute.'
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
// #swagger.parameters['serverId'] = { in: 'query', required: true, schema: { type: 'string' }, description: 'The server.' }
// #swagger.parameters['steamId'] = { in: 'query', required: true, schema: { type: 'string' }, description: 'One of the caller’s linked Steam ids.' }
/* #swagger.responses[200] = { description: 'The backpack', content: { "application/json": { schema: { $ref: "#/components/schemas/RustBackpack" } } } } */
/* #swagger.responses[404] = { description: 'No such server, or not one of your linked Steam accounts', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
/* #swagger.responses[409] = { description: 'Backpacks is not installed on that server', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
/* #swagger.responses[503] = { description: 'The server could not be reached', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
backpackLimiter,
query('serverId').isString().isLength({ min: 1, max: 64 }),
query('steamId').isString().matches(STEAM_ID_RE),
validate,
rust.ownBackpack,
)
playerRustRouter.get(
'/economics',
// #swagger.tags = ['Player · Rust']

View File

@@ -474,6 +474,13 @@ const economicsBalances = (server, steamIds) =>
/** Staff: `{ key, steamId, op, amount }`, op deposit · withdraw · set. `economics.adjusted` has before and after. */
const economicsAdjust = (server, body) => request(server, '/economics/adjust', { method: 'POST', body })
/**
* One player's backpack (protocol 14, PLAN_REDESIGNS.md §11.3.2), read live.
* `backpacks.contents` has `exists`, and with a backpack its capacity, page
* size and stacks; `backpacks.error` has a reason. Nothing is written.
*/
const backpacksRead = (server, steamId) => request(server, '/backpacks/read', { method: 'POST', body: { steamId } })
/**
* Say one line in the server's chat. `chat.ok` carries `said: false` when the
* key was said in the last ten minutes — a retry, answered and not repeated.
@@ -566,6 +573,7 @@ module.exports = {
economicsPay,
economicsBalances,
economicsAdjust,
backpacksRead,
chat,
titles,
mapInfo,

View File

@@ -244,6 +244,15 @@ module.exports = {
type: 'object',
properties: { loaded: { type: 'boolean', example: false } },
},
backpacks: {
type: 'object',
description: 'Protocol 14 (PLAN_REDESIGNS §11.3.2): `sizes` is every registered backpacks.size.N, ascending; a Bigger backpack step may lease any above the first.',
properties: {
loaded: { type: 'boolean', example: true },
version: { type: 'string', example: '3.17.8' },
sizes: { type: 'array', items: { type: 'integer' }, example: [6, 12, 18, 24, 30, 36, 42, 48, 96, 144] },
},
},
},
},
},
@@ -1331,6 +1340,35 @@ module.exports = {
repeat: { type: 'boolean', example: false },
},
},
RustBackpackStack: {
type: 'object',
description: 'One stack in a backpack (PLAN_REDESIGNS §11.3.2). `display` is the game’s English name (D340); for a blueprint it is the name of what it makes, and `blueprint` that item’s shortname (D341). `shortname` is null, with `itemId`, for an item the game does not know.',
properties: {
slot: { type: 'integer', example: 2, description: 'Runs across pages: the first slot of page two is pageSize.' },
shortname: { type: 'string', nullable: true, example: 'rifle.ak' },
display: { type: 'string', nullable: true, example: 'Assault Rifle' },
amount: { type: 'integer', example: 1 },
itemId: { type: 'integer', example: 1545779598 },
blueprint: { type: 'string', example: 'rifle.ak' },
skin: { type: 'string', example: '10129', description: 'A workshop skin id, as text.' },
name: { type: 'string', example: 'spike-a', description: 'A renamed item’s name.' },
overflow: { type: 'boolean', example: true, description: 'Past the capacity: it moves to the owner’s inventory, or the ground, at their next open (D338).' },
contents: { type: 'array', description: 'A weapon’s attachments or a container’s items, one level deep, without `contents` or `overflow` of their own.', items: { type: 'object' } },
},
},
RustBackpack: {
type: 'object',
description: 'One Steam account’s backpack on one server, read live and never stored. `capacity`, `pageSize` and `items` are present only when `exists`.',
properties: {
serverId: { type: 'string', example: 'main' },
server: { type: 'string', example: 'Main' },
steamId: { type: 'string', example: '76561198000000001' },
exists: { type: 'boolean', example: true, description: 'False for an account that has never had a backpack.' },
capacity: { type: 'integer', example: 12 },
pageSize: { type: 'integer', example: 48 },
items: { type: 'array', items: { $ref: '#/components/schemas/RustBackpackStack' } },
},
},
RustVisibilityUpdate: {
type: 'object',
description: 'A change to who may see who is online. Either part may be omitted; a server set to null follows the fleet default again.',

View File

@@ -0,0 +1,325 @@
// ── Backpacks (PLAN_REDESIGNS §11.3.2, protocol 14, D337, D338, D340, D341) ─
//
// Each test is one way the feature could look right and be wrong:
//
// the sizes a step may lease skip the smallest, which everybody holds
// a server without Backpacks is refused with the reason, in the dry run too (D199)
// a size the server does not offer is refused by name
// the lease is a kit reward's row: the size's permission, no kit, no credit (D337)
// an empty message is the take-back warning, never silence (D338)
// the revert deletes the step's rows and marks the server for a push
// a read answers what the game said, live; a stranger's id never reaches it
// a stack keeps its name, blueprint, skin as text, and one level of contents
const test = require('node:test')
const assert = require('node:assert')
const { fakeCtx } = require('./_fakes')
function withRecorder(rows = []) {
const statements = []
const ctx = fakeCtx({
db: {
query: (sql, params = []) => {
statements.push({ sql, params })
return Promise.resolve(typeof rows === 'function' ? rows(sql, params) : rows)
},
pool: {},
},
})
require('../core')._reset()
require('../core').init(ctx)
return { statements }
}
withRecorder()
const client = require('../sidecarClient')
const serversDb = require('../model/servers/servers.db')
const servers = require('../model/servers/servers.model')
const linksDb = require('../model/links/links.db')
const permDb = require('../model/permissions/permissions.db')
const backpacksDb = require('../model/backpacks/backpacks.db')
const backpacks = require('../model/backpacks/backpacks.model')
const rewards = require('../eventRewards')
const ok = (data) => ({ ok: true, status: 'ok', data })
const A = '76561198000000001'
const B = '76561198000000002'
const C = '76561198000000003'
const SIZES = [6, 12, 18, 24, 30, 36, 42, 48, 96, 144]
// ── What a server reports ──────────────────────────────────────────────────
test('a server’s sizes are whole, ascending and unique; the smallest is never leased', () => {
assert.deepStrictEqual(backpacks.shape(null), { loaded: false, sizes: [] })
assert.deepStrictEqual(backpacks.shape(JSON.stringify({ loaded: false })), { loaded: false, sizes: [] })
const report = backpacks.shape(JSON.stringify({ loaded: true, version: '3.17.8', sizes: [48, 6, 12, 12, 'x', -3, 2.5] }))
assert.deepStrictEqual(report, { loaded: true, version: '3.17.8', sizes: [6, 12, 48] })
assert.deepStrictEqual(backpacks.leasable(report), [12, 48])
assert.deepStrictEqual(backpacks.leasable({ loaded: false, sizes: [] }), [])
})
// ── The step ───────────────────────────────────────────────────────────────
const ROW = { id: 'main', name: 'Main', sidecarBaseUrl: 'http://main:1', sidecarTokenEnc: null, enabled: 1 }
const lend = rewards.ACTIONS.find((a) => a.id === 'rust.backpack.size')
function stub(t, { loaded = true, sizes = SIZES, people = [], links = {}, existing = [] } = {}) {
const calls = { inserted: [], dirty: [], deleted: [] }
const saved = {
getServer: serversDb.getServer,
reportFor: backpacksDb.reportFor,
tallySnapshot: client.tallySnapshot,
userIdsForSteamIds: linksDb.userIdsForSteamIds,
listRunGrantsForStep: permDb.listRunGrantsForStep,
insertRunGrants: permDb.insertRunGrants,
markDirty: permDb.markDirty,
deleteRunGrantsForStep: permDb.deleteRunGrantsForStep,
}
serversDb.getServer = async (id) => (id === 'main' ? ROW : null)
backpacksDb.reportFor = async () => ({ serverId: 'main', backpacks: loaded ? JSON.stringify({ loaded: true, version: '3.17.8', sizes }) : null })
client.tallySnapshot = async (server, runId) => ok({ kind: 'tally.snapshot', runId, people, maxRecipients: 100 })
linksDb.userIdsForSteamIds = async (ids) => ids.filter((id) => links[id]).map((id) => ({ steamId: id, userId: links[id] }))
permDb.listRunGrantsForStep = async () => existing
permDb.insertRunGrants = async (rows) => {
calls.inserted.push(...rows)
return rows.length
}
permDb.markDirty = async (serverId) => calls.dirty.push(serverId)
permDb.deleteRunGrantsForStep = async (runId, stepId) => {
calls.deleted.push(`${runId}:${stepId}`)
return ['main']
}
t.after(() => Object.assign(serversDb, { getServer: saved.getServer }))
t.after(() => Object.assign(backpacksDb, { reportFor: saved.reportFor }))
t.after(() => Object.assign(client, { tallySnapshot: saved.tallySnapshot }))
t.after(() => Object.assign(linksDb, { userIdsForSteamIds: saved.userIdsForSteamIds }))
t.after(() =>
Object.assign(permDb, {
listRunGrantsForStep: saved.listRunGrantsForStep,
insertRunGrants: saved.insertRunGrants,
markDirty: saved.markDirty,
deleteRunGrantsForStep: saved.deleteRunGrantsForStep,
}),
)
return calls
}
const PARAMS = { server: 'main', size: 48, recipients: 'everyone' }
const person = (steamId, score) => ({ steamId, name: `p${steamId.slice(-1)}`, seconds: 60, kills: 0, score, joinedAt: 1 })
test('a bigger backpack is a ledgered change, priced per recipient on its own budget', () => {
assert.strictEqual(lend.risk, 'change')
assert.strictEqual(lend.reversible, 'ledger')
assert.deepStrictEqual(lend.cost({ recipients: 'top', count: 3 }), { 'rust.backpacks': 3 })
assert.deepStrictEqual(lend.cost({ recipients: 'linked' }), { 'rust.backpacks': rewards.MAX_RECIPIENTS })
})
test('a server without Backpacks is refused in the dry run, with the reason (D199)', async (t) => {
stub(t, { loaded: false })
const result = await lend.perform({ runId: 7, stepId: 4, idempotencyKey: 'k-1', params: PARAMS, verify: true })
assert.strictEqual(result.ok, false)
assert.strictEqual(result.retry, false)
assert.match(result.error, /Backpacks is not installed on this server \(Main\)/)
})
test('a size the server does not offer, or its smallest, is refused by name', async (t) => {
stub(t, { sizes: [6, 12, 48] })
for (const size of [6, 96, 0, 'big']) {
const result = await lend.perform({ runId: 7, stepId: 4, idempotencyKey: 'k', params: { ...PARAMS, size }, verify: true })
assert.strictEqual(result.ok, false, String(size))
assert.strictEqual(result.retry, false)
}
const named = await lend.perform({ runId: 7, stepId: 4, idempotencyKey: 'k', params: { ...PARAMS, size: 96 }, verify: true })
assert.match(named.error, /offers 12, 48 slots/)
const ok48 = await lend.perform({ runId: 7, stepId: 4, idempotencyKey: 'k', params: PARAMS, verify: true })
assert.deepStrictEqual(ok48, { ok: true })
})
test('the lease is one row per website user: the size’s permission, no kit, no credit (D337)', async (t) => {
const calls = stub(t, {
// A and B are one person (user 1); C has linked nothing.
people: [person(A, 9), person(B, 5), person(C, 3)],
links: { [A]: 1, [B]: 1 },
})
const result = await lend.perform({ runId: 7, stepId: 4, idempotencyKey: 'k-1', params: PARAMS, verify: false })
assert.strictEqual(result.ok, true)
assert.strictEqual(calls.inserted.length, 1)
const [row] = calls.inserted
assert.strictEqual(row.permission, 'backpacks.size.48')
assert.strictEqual(row.kit, '')
assert.strictEqual(row.credit, false)
assert.strictEqual(row.steamId, A)
assert.strictEqual(row.userId, 1)
assert.deepStrictEqual(calls.dirty, ['main'])
assert.deepStrictEqual(result.resources, [{ kind: 'backpack-size', ref: 'main:7:4', payload: { serverId: 'main', permission: 'backpacks.size.48' } }])
assert.strictEqual(result.detail.lent, 1)
assert.deepStrictEqual(result.detail.missed, ['p3'])
})
test('an empty message is the take-back warning, with the size in it (D338)', async (t) => {
const calls = stub(t, { people: [person(A, 1)], links: { [A]: 1 } })
await lend.perform({ runId: 7, stepId: 4, idempotencyKey: 'k-1', params: { ...PARAMS, message: ' ' }, verify: false })
assert.match(calls.inserted[0].notice, /^Your backpack holds 48 until the event ends\./)
assert.strictEqual(calls.inserted[0].noticeDelivery, 'chat')
assert.strictEqual(rewards.backpackNotice({}, 48).text, rewards.BACKPACK_NOTICE)
assert.strictEqual(rewards.backpackNotice({ message: 'Big bag: {size}', delivery: 'popup' }, 96).delivery, 'popup')
assert.strictEqual(rewards.backpackNotice({ message: 'x', delivery: 'carrier pigeon' }, 96).ok, false)
assert.strictEqual(rewards.backpackNotice({ message: `${'x'.repeat(254)}{size}` }, 144).ok, false)
})
test('a repeated key finds its rows and lends nothing new', async (t) => {
const calls = stub(t, { existing: [{ userId: 1 }, { userId: 2 }], people: [person(A, 1)], links: { [A]: 1 } })
const result = await lend.perform({ runId: 7, stepId: 4, idempotencyKey: 'k-1', params: PARAMS, verify: false })
assert.strictEqual(result.detail.repeat, true)
assert.strictEqual(result.detail.lent, 2)
assert.strictEqual(calls.inserted.length, 0)
})
test('the revert deletes the step’s rows, and the next sync takes the size back', async (t) => {
const calls = stub(t)
const result = await lend.revert({ runId: 7, resources: [{ kind: 'backpack-size', ref: 'main:7:4' }], idempotencyKey: 'k-1' })
assert.deepStrictEqual(result, { ok: true })
assert.deepStrictEqual(calls.deleted, ['7:4'])
assert.deepStrictEqual(calls.dirty, ['main'])
})
test('the size list offers each server’s sizes above its smallest, grouped by server', async (t) => {
const saved = { listForPolling: servers.listForPolling, reports: backpacksDb.reports }
t.after(() => Object.assign(servers, { listForPolling: saved.listForPolling }) && Object.assign(backpacksDb, { reports: saved.reports }))
servers.listForPolling = async () => [{ id: 'main', name: 'Main' }, { id: 'pve', name: 'PvE' }]
backpacksDb.reports = async () => [
{ serverId: 'main', backpacks: JSON.stringify({ loaded: true, sizes: [6, 48] }) },
{ serverId: 'pve', backpacks: null },
]
const source = rewards.OPTION_SOURCES.find((s) => s.id === 'rust.options.backpacksizes')
assert.deepStrictEqual(await source.resolve(), [{ value: 48, label: '48 slots', group: 'Main' }])
})
// ── Reading one ────────────────────────────────────────────────────────────
function stubRead(t, answer) {
const sent = []
const saved = { getForCalling: servers.getForCalling, backpacksRead: client.backpacksRead }
t.after(() => Object.assign(servers, { getForCalling: saved.getForCalling }) && Object.assign(client, { backpacksRead: saved.backpacksRead }))
servers.getForCalling = async (id) => (id === 'main' ? { id: 'main', name: 'Main', baseUrl: 'http://main:1', token: 't' } : null)
client.backpacksRead = async (server, steamId) => {
sent.push(steamId)
return typeof answer === 'function' ? answer(steamId) : answer
}
return sent
}
test('a backpack is read live and reduced to what the grid draws', async (t) => {
const sent = stubRead(
t,
ok({
kind: 'backpacks.contents',
steamId: A,
exists: true,
capacity: 12,
pageSize: 48,
items: [
{ slot: 0, shortname: 'wood', display: 'Wood', amount: 1000 },
{ slot: 2, shortname: 'rifle.ak', display: 'Assault Rifle', amount: 1, contents: [{ slot: 0, shortname: 'weapon.mod.lasersight', display: 'Weapon Lasersight', amount: 1, contents: [{ slot: 0 }] }] },
{ slot: 3, shortname: 'hoodie', display: 'Hoodie', amount: 1, skin: 10129 },
{ slot: 5, shortname: 'blueprintbase', display: 'Assault Rifle', blueprint: 'rifle.ak', amount: 1 },
{ slot: 14, shortname: 'note', display: 'Note', amount: 1, name: 'spike-a', overflow: true },
{ slot: 15, shortname: null, itemId: 999, amount: 1, overflow: true },
],
}),
)
const result = await backpacks.read({ serverId: 'main', steamId: A })
assert.deepStrictEqual(sent, [A])
assert.strictEqual(result.ok, true)
const bp = result.backpack
assert.strictEqual(bp.server, 'Main')
assert.strictEqual(bp.capacity, 12)
assert.strictEqual(bp.pageSize, 48)
assert.strictEqual(bp.items.length, 6)
assert.strictEqual(bp.items[1].contents.length, 1)
assert.strictEqual(bp.items[1].contents[0].contents, undefined, 'one level of contents, no deeper')
assert.strictEqual(bp.items[2].skin, '10129', 'a skin id is text')
assert.strictEqual(bp.items[3].blueprint, 'rifle.ak')
assert.strictEqual(bp.items[4].name, 'spike-a')
assert.strictEqual(bp.items[4].overflow, true)
assert.strictEqual(bp.items[5].itemId, 999)
})
test('a player who never had a backpack is exists: false, with no capacity', async (t) => {
stubRead(t, ok({ kind: 'backpacks.contents', steamId: A, exists: false }))
const result = await backpacks.read({ serverId: 'main', steamId: A })
assert.deepStrictEqual(result.backpack, { serverId: 'main', server: 'Main', steamId: A, exists: false })
})
test('a read says why it has nothing: no server, no Backpacks, or no answer', async (t) => {
stubRead(t, ok({ kind: 'backpacks.error', reason: 'backpacks-missing', message: 'Backpacks is not installed on this server' }))
const missing = await backpacks.read({ serverId: 'main', steamId: A })
assert.strictEqual(missing.status, 409)
assert.strictEqual(missing.message, 'Backpacks is not installed on this server (Main)')
assert.strictEqual((await backpacks.read({ serverId: 'nope', steamId: A })).status, 404)
client.backpacksRead = async () => ({ ok: false, status: 'http-503' })
const down = await backpacks.read({ serverId: 'main', steamId: A })
assert.strictEqual(down.status, 503)
assert.match(down.message, /Main has no game connected/)
})
// ── The routes ─────────────────────────────────────────────────────────────
function res() {
return {
statusCode: 200,
body: null,
status(code) {
this.statusCode = code
return this
},
json(body) {
this.body = body
return this
},
}
}
test('a backpack is read only for an account its user has linked', async (t) => {
const links = require('../model/links/links.model')
const saved = { listForUser: links.listForUser }
t.after(() => Object.assign(links, saved))
links.listForUser = async (userId) => (userId === 1 ? [{ steamId: A, name: 'pa' }] : [])
const sent = stubRead(t, ok({ kind: 'backpacks.contents', steamId: A, exists: false }))
const player = require('../router/player/rust.controller')
const staff = require('../router/admin/usersRust.controller')
const own = res()
await player.ownBackpack({ user: { id: 1 }, query: { serverId: 'main', steamId: A } }, own)
assert.strictEqual(own.statusCode, 200)
assert.strictEqual(own.body.exists, false)
const stranger = res()
await player.ownBackpack({ user: { id: 1 }, query: { serverId: 'main', steamId: B } }, stranger)
assert.strictEqual(stranger.statusCode, 404)
const wrongUser = res()
await staff.readBackpack({ params: { id: '2' }, query: { serverId: 'main', steamId: A } }, wrongUser)
assert.strictEqual(wrongUser.statusCode, 404)
const viaStaff = res()
await staff.readBackpack({ params: { id: '1' }, query: { serverId: 'main', steamId: A } }, viaStaff)
assert.strictEqual(viaStaff.statusCode, 200)
assert.deepStrictEqual(sent, [A, A], 'a stranger’s id never reaches the game')
})

View File

@@ -156,6 +156,7 @@ test('the event verbs are registered, and every budget has a verb that spends it
const budgets = api.record.eventBudgets
assert.deepStrictEqual(actions.map((a) => a.id).sort(), [
'rust.announce',
'rust.backpack.size',
'rust.coins.pay',
'rust.crate.place',
'rust.kit.entitle',
@@ -166,6 +167,7 @@ test('the event verbs are registered, and every budget has a verb that spends it
])
assert.deepStrictEqual(budgets.map((b) => b.id).sort(), [
'rust.announcements',
'rust.backpacks',
'rust.coins',
'rust.grants',
'rust.npcs',

View File

@@ -45,6 +45,8 @@ test('the player tier serves the identity routes, the entitlement read, and noth
routes.map((r) => `${r.method} ${r.path}`).sort(),
[
'DELETE /links/:steamId',
// Protocol 14 (§11.3.2): the caller's own backpack, read live. Read-only.
'GET /backpacks',
// Protocol 14 (§11.2.2): the caller's own coin balances. Read-only.
'GET /economics',
'GET /links',
@@ -90,6 +92,8 @@ test('the admin.users.detail router merges the parent’s params and keeps its o
'DELETE /rust/links/:steamId',
// Phase 7 filled the same panel with what this person may do in game.
'DELETE /rust/permissions/grants/:grantId',
// Protocol 14 (§11.3.2): what they carry, read live. There is no write.
'GET /rust/backpacks',
// Protocol 14 (§11.2.2): their coins, and staff's hand on them.
'GET /rust/economics',
'GET /rust/links',