feat(rust): chat titles rank twenty-three conditions, and admins name the categories (PLAN_REDESIGNS §5, D172-D175, D209)

- Schema: fourteen title columns on rust_player_wipe_stats (the two
  best_* distances move by GREATEST, the rest are sums), rust_weapon_kills
  (kills by weapon prefab name) and rust_title_categories (the admin's
  title per category). purge.sql drops the two tables.
- Ingest: player.tally's new fields, in one statement per frame, none
  for an older plugin's frame.
- Titles: 23 categories plus playtime, each naming where it is read
  from (a sum, a MAX, npc_kills - animal_kills in signed arithmetic, or
  a named list). The bow/melee/blade/revolver/wood/ore/plants lists are
  one file, applied when a title is read. "NPC kills" ranks human NPCs
  only (D209).
- A rule's text may be empty, meaning the category's title: the rule's
  own, then the admin's, then the default. Typed-only-markup is still
  refused. A rename forgets every server's cached answer.
- Admin API: GET /admin/rust/title-categories and
  PUT /admin/rust/title-categories/:stat (empty text resets); the server
  list returns them too. Swagger fragment and routes.manifest.json
  regenerated (63 routes, against the pinned core).
- Screen: every category in the rule form, the category's title as a
  placeholder, and a Category titles section with Save and Reset.

The weapon lists are unverified until the rig probe runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E14m6SuuY6i1vASFeGDBeY
This commit is contained in:
2026-09-28 23:32:59 -05:00
parent afb16112b0
commit aea94b5d7a
18 changed files with 1150 additions and 65 deletions

View File

@@ -5,9 +5,9 @@
// the app's — so it is remembered briefly: the leaderboard is polled by every
// open page, and each rule is a leaderboard query of its own.
const eventsDb = require('../events/events.db')
const serversDb = require('../servers/servers.db')
const db = require('./titles.db')
const lists = require('./lists')
const titles = require('./titles')
/** How long one server's answer is reused. The same as the push loop's tick. */
@@ -47,8 +47,40 @@ async function saveSettings(serverId, value) {
forget(serverId)
}
function forget(serverId) {
for (const key of memo.keys()) if (key.startsWith(`${serverId} `)) memo.delete(key)
/** Forgets one server's answer, or every server's when a category was renamed. */
function forget(serverId = null) {
for (const key of memo.keys()) if (serverId === null || key.startsWith(`${serverId} `)) memo.delete(key)
}
/**
* The categories as the admin screen shows them (D175): each one's label, the
* shipped default, the admin's own title if any, and what rules show now.
*/
async function categories() {
const own = new Map((await db.listCategories()).map((r) => [r.stat, r]))
return titles.CATEGORY_IDS.map((stat) => {
const row = own.get(stat)
const def = titles.STATS[stat]
return {
stat,
label: def.label,
default: def.title,
text: row ? row.text : null,
current: row ? row.text : def.title,
updatedAt: row ? row.updatedAt : null,
}
})
}
/**
* Sets or resets one category's title. An empty text is the Reset. Every
* server's answer is forgotten, because every server's rules may use it, and
* the next push carries the new name (§4.6: "from the next push").
*/
async function setCategory(stat, text, userId = null) {
if (text) await db.setCategory(stat, text, userId)
else await db.clearCategory(stat)
forget()
}
/**
@@ -64,43 +96,42 @@ async function heldFor(serverId, { wipeId, now = Date.now() } = {}) {
const hit = memo.get(key)
if (hit && now - hit.at < MEMO_MS) return hit.held
const settings = await settingsFor(serverId)
const [settings, overrides] = await Promise.all([settingsFor(serverId), db.categoryTitles()])
const rules = titles.resolveRules(settings.rules, overrides)
const standings = {}
// One query per STAT, at the deepest top N any rule on it asks for, rather
// than one per rule: two rules on kills read the same rows.
const depth = new Map()
for (const r of settings.rules) depth.set(r.stat, Math.max(depth.get(r.stat) || 0, r.topN))
for (const r of rules) if (titles.STATS[r.stat]) depth.set(r.stat, Math.max(depth.get(r.stat) || 0, r.topN))
await Promise.all(
[...depth.entries()].map(async ([stat, limit]) => {
standings[stat] = await eventsDb.leaderboard({ serverId, wipeId, sort: titles.STATS[stat].sort, limit })
const rows = await db.standings({ serverId, wipeId, source: titles.STATS[stat].source, lists, limit })
// SUM() and MAX() come back as strings; the rules compare numbers.
standings[stat] = rows.map((r) => ({ steamId: r.steamId, value: Number(r.value) || 0 }))
}),
)
const held = titles.evaluate(settings.rules, normalise(standings), settings)
const held = titles.evaluate(rules, standings, settings)
memo.set(key, { at: now, held })
return held
}
/** The leaderboard's rows carry numbers as strings from SUM(); the rules compare numbers. */
function normalise(standings) {
const out = {}
for (const [stat, rows] of Object.entries(standings)) {
out[stat] = rows.map((r) => ({
steamId: r.steamId,
kills: Number(r.kills) || 0,
npcKills: Number(r.npcKills) || 0,
playtimeSec: Number(r.playtimeSec) || 0,
}))
}
return out
}
/** The same, for a server by id, reading its current wipe from its last report. */
async function currentFor(serverId) {
const state = await serversDb.getState(serverId)
return heldFor(serverId, { wipeId: state && state.wipeId ? state.wipeId : null })
}
module.exports = { MEMO_MS, settingsFor, settingsByServer, saveSettings, heldFor, currentFor, forget }
module.exports = {
MEMO_MS,
settingsFor,
settingsByServer,
saveSettings,
heldFor,
currentFor,
forget,
categories,
setCategory,
}