// ── SQL for chat titles (phase 17) ──────────────────────────────────────── // // The rules an operator writes, per server, and the two columns on // `rust_servers` that say how many titles a player shows. The titles themselves // are never stored — see `schema.sql` and `titles.js`. const core = require('../../core') const RULES = 'rust_title_rules' const SERVERS = 'rust_servers' /** One server's rules, in precedence order. */ async function listRules(serverId) { return core.query( `SELECT id, stat, top_n AS topN, text, color FROM ${RULES} WHERE server_id = ? ORDER BY position ASC, id ASC`, [serverId], ) } /** Every server's rules, for the admin list — one read rather than one per server. */ async function listAllRules() { return core.query( `SELECT server_id AS serverId, stat, top_n AS topN, text, color FROM ${RULES} ORDER BY server_id ASC, position ASC, id ASC`, ) } async function getMode(serverId) { const rows = await core.query( `SELECT title_mode AS mode, title_max AS max FROM ${SERVERS} WHERE id = ?`, [serverId], ) return rows[0] || null } async function listModes() { return core.query(`SELECT id AS serverId, title_mode AS mode, title_max AS max FROM ${SERVERS}`) } /** * Replace one server's rules and mode whole. The form edits a list, so the * write is a list — and a rule's position is its place in that list. */ async function saveSettings(serverId, { mode, max, rules }) { await core.query(`UPDATE ${SERVERS} SET title_mode = ?, title_max = ? WHERE id = ?`, [mode, max, serverId]) await core.query(`DELETE FROM ${RULES} WHERE server_id = ?`, [serverId]) if (!rules.length) return await core.query( `INSERT INTO ${RULES} (server_id, position, stat, top_n, text, color) VALUES ${rules.map(() => '(?, ?, ?, ?, ?, ?)').join(',')}`, rules.flatMap((r, i) => [serverId, i, r.stat, r.topN, r.text, r.color]), ) } module.exports = { RULES, listRules, listAllRules, getMode, listModes, saveSettings }