From aa455f52d0ca2618e9438c839b341e4d6a75a45c Mon Sep 17 00:00:00 2001 From: Burrowdev Date: Sat, 17 Oct 2026 11:57:40 +0300 Subject: [PATCH] perf: save only changed accounts, cache missing assets, lighter logging and backup Ping on prod jumped to seconds because node's main thread was busy most of every second. - accounts.save(account, ...) names the changed accounts, which are written within a second; the rest are looked at a few at a time (10 ms a save for those in play, 5 ms for the others). One save under prod-like load: 289 ms before, 16 ms after. - an asset the CDN does not have gets a cacheable 404 and is logged once - the proxy's own connections no longer count against the web cap, which had node reset requests nginx had already let in - a repeated client error is written once in ten minutes; the panel's log buffer trims in batches - the daily backup uses sqlite's online backup off the main thread --- lib/accounts.js | 227 +++++++++++++++++++++++++++++--------------- lib/achievements.js | 2 +- lib/admin.js | 6 +- lib/adopt.js | 2 +- lib/adventures.js | 4 +- lib/avatars.js | 8 +- lib/buddies.js | 12 +-- lib/cdn.js | 6 +- lib/commands.js | 24 ++--- lib/dens.js | 18 ++-- lib/disguises.js | 2 +- lib/ecards.js | 14 +-- lib/http.js | 62 +++++++++--- lib/items.js | 8 +- lib/log.js | 5 +- lib/manage.js | 34 +++---- lib/minigames.js | 14 +-- lib/moderation.js | 14 +-- lib/modpanel.js | 8 +- lib/panel.js | 16 ++-- lib/parties.js | 2 +- lib/pets.js | 12 +-- lib/trades.js | 12 +-- lib/walls.js | 16 ++-- lib/world.js | 24 ++--- lib/worlditems.js | 2 +- 26 files changed, 334 insertions(+), 220 deletions(-) diff --git a/lib/accounts.js b/lib/accounts.js index 50387dc..5f045da 100644 --- a/lib/accounts.js +++ b/lib/accounts.js @@ -3,7 +3,7 @@ const crypto = require("crypto"); const fs = require("fs"); const path = require("path"); -const {DatabaseSync} = require("node:sqlite"); +const {DatabaseSync, backup: copyDatabase} = require("node:sqlite"); const config = require("../config.js"); const defpacks = require("./defpacks.js"); const names = require("./names.js"); @@ -13,9 +13,9 @@ const names = require("./names.js"); // JSON document per account in `state`. // // every account is held in memory while the server runs. game code changes -// those objects directly and then calls save(), which has every account -// whose state changed written back, a moment later: however many times it -// is called in that moment, the accounts are gone through once (flush). +// those objects directly and then calls save(account, ...), which has the +// accounts named written back a moment later, and the others looked at a +// few at a time (flush, below). const SCHEMA = ` CREATE TABLE IF NOT EXISTS accounts ( @@ -415,21 +415,30 @@ const load = () => { console.log(`[accounts] ${byKey.size} accounts in ${path.join(config.dataDir, "game.sqlite")}`); }; -// Which accounts the save after a message looks at. turning an account into -// JSON to see whether it changed costs, and doing it for every account there -// is, a second after nearly every message, held the whole server up for -// longer the more accounts there were: a second and more, for everyone. so -// it looks at the accounts in play: online ones (hold, from lib/world.js), -// and ones looked up lately (by name, id, token, password: how the admin -// panel, buddies and the like get at an offline account), until a save -// finds them written and WARM_MS has gone by. the rest are looked at a few -// at a time behind those (SWEEP_BUDGET_MS a save), round and round, for -// what reached an account some other way (accounts.all()). a stop of the -// server, and every flush() called by name, looks at all of them. +// Which accounts a save looks at. turning an account into JSON to see +// whether it changed costs (up to a millisecond for a big one), and doing it +// a second after nearly every message for every account in play (online +// ones, and the buddies, dens and cards they had looked up lately) held the +// whole server up for most of every second once there were enough players. +// so game code says which accounts it changed, save(account, ...), and those +// are written within SAVE_DELAY_MS. behind them, for whatever changed an +// account without saying so, the accounts in play are looked at a few at a +// time (PLAY_BUDGET_MS a save), round and round: online ones (hold, from +// lib/world.js) and ones looked up lately (by name, id, token, password: how +// the admin panel, buddies and the like get at an offline account), for +// WARM_MS. behind those every other account is looked at the same way +// (SWEEP_BUDGET_MS), for what reached an account some other way +// (accounts.all()). a stop of the server, and every flush() called by name, +// looks at all of them. const WARM_MS = 10 * 60 * 1000; +const PLAY_BUDGET_MS = 10; const SWEEP_BUDGET_MS = 5; const held = new Set(); // accounts online const warm = new Map(); // account -> when last looked up +const dirty = new Set(); // accounts said to have changed since the last save +let playOrder = []; +let playAt = 0; +let playLeft = 0; // accounts in play still to be looked at since the last save() let sweepOrder = []; let sweepAt = 0; let sweepLeft = 0; // accounts the sweep still has to look at since the last save() @@ -457,9 +466,60 @@ const changedState = (account, changed) => { return false; }; +const elapsedMs = started => Number(process.hrtime.bigint() - started) / 1e6; + +// the next accounts in play, while there is time: the ones online and the +// ones looked up lately. one looked up longer ago than WARM_MS is looked at +// once more, then left to the sweep. +const lookAtPlay = (changed, looked) => { + const started = process.hrtime.bigint(); + while (playLeft > 0 && elapsedMs(started) < PLAY_BUDGET_MS) { + if (playAt >= playOrder.length) { + const now = Date.now(); + playOrder = [...new Set([...held, ...warm.keys()])]; + playAt = 0; + for (const [account, at] of warm) { + if (now - at > WARM_MS && !held.has(account)) { + warm.delete(account); + } + } + if (playOrder.length === 0) { + playLeft = 0; + break; + } + } + const account = playOrder[playAt++]; + playLeft--; + if (!looked.has(account) && byId.get(account.id) === account) { + looked.add(account); + changedState(account, changed); + } + } +}; + +const lookAtRest = (changed, looked) => { + const started = process.hrtime.bigint(); + while (sweepLeft > 0 && elapsedMs(started) < SWEEP_BUDGET_MS) { + if (sweepAt >= sweepOrder.length) { + sweepOrder = [...byId.values()]; + sweepAt = 0; + if (sweepOrder.length === 0) { + sweepLeft = 0; + break; + } + } + const account = sweepOrder[sweepAt++]; + sweepLeft--; + if (!looked.has(account) && byId.get(account.id) === account) { + looked.add(account); + changedState(account, changed); + } + } +}; + // writes every account whose state changed since it was last written, in -// one transaction; with {some: true}, of the accounts in play and the -// sweep's next few (above). +// one transaction; with {some: true}, of the accounts said to have changed +// and the next few of the others (above). const flush = ({some = false} = {}) => { clearTimeout(saveTimer); saveTimer = null; @@ -467,49 +527,28 @@ const flush = ({some = false} = {}) => { return; } const changed = []; + const named = [...dirty]; + dirty.clear(); if (some) { - const now = Date.now(); const looked = new Set(); - for (const account of held) { - looked.add(account); - changedState(account, changed); - } - for (const [account, at] of warm) { - if (looked.has(account)) { - continue; - } - looked.add(account); - if (byId.get(account.id) !== account) { - warm.delete(account); // removed - } - else if (!changedState(account, changed) && now - at > WARM_MS && !held.has(account)) { - warm.delete(account); - } - } - const started = process.hrtime.bigint(); - while (sweepLeft > 0 && Number(process.hrtime.bigint() - started) / 1e6 < SWEEP_BUDGET_MS) { - if (sweepAt >= sweepOrder.length) { - sweepOrder = [...byId.values()]; - sweepAt = 0; - if (sweepOrder.length === 0) { - break; - } - } - const account = sweepOrder[sweepAt++]; - sweepLeft--; - if (!looked.has(account) && byId.get(account.id) === account) { + for (const account of named) { + if (byId.get(account.id) === account) { + looked.add(account); changedState(account, changed); } } + lookAtPlay(changed, looked); + lookAtRest(changed, looked); } else { for (const account of byId.values()) { changedState(account, changed); } + playLeft = 0; sweepLeft = 0; } - if (sweepLeft > 0) { - save(); // the sweep goes on in a moment + if (playLeft > 0 || sweepLeft > 0) { + later(CONTINUE_MS); // the looking goes on in a moment } if (changed.length === 0) { return; @@ -524,6 +563,7 @@ const flush = ({some = false} = {}) => { } catch (err) { db.exec("ROLLBACK"); + named.forEach(account => dirty.add(account)); throw err; } for (const [id, json] of changed) { @@ -531,16 +571,17 @@ const flush = ({some = false} = {}) => { } }; -// "Something changed": the accounts are written within SAVE_DELAY_MS. going -// through every account costs more the more there are, and game code calls -// this after nearly every message (and some of it a hundred times in a -// row), so the going-through is done once for all the calls of a moment. -// what a crash can lose is that moment; a stop of the server writes first +// "Something changed": the accounts named are written within SAVE_DELAY_MS, +// and the looking-through above starts again. game code calls this after +// nearly every message (and some of it a hundred times in a row), so it is +// done once for all the calls of a moment. what a crash can lose is that +// moment, for an account named, and the time a round of the looking takes, +// for one changed without saying so; a stop of the server writes first // (server.js). const SAVE_DELAY_MS = 1000; +const CONTINUE_MS = 250; let saveTimer = null; -const save = () => { - sweepLeft = byId.size; +const later = delay => { if (saveTimer === null) { saveTimer = setTimeout(() => { try { @@ -550,25 +591,47 @@ const save = () => { console.error(`[accounts] could not save: ${err.message}`); save(); // tried again in a moment: nothing is lost while the server runs } - }, SAVE_DELAY_MS); + }, delay); } }; +const save = (...changedAccounts) => { + for (const account of changedAccounts) { + if (account) { + dirty.add(account); + } + } + playLeft = held.size + warm.size; + sweepLeft = byId.size; + later(SAVE_DELAY_MS); +}; // A copy of the whole database, made once a day into data/backups/ and the -// latest BACKUPS_KEPT kept. it is a whole file of its own (VACUUM INTO), to -// be copied somewhere else by whoever runs the server: a copy on the same -// disk is no help when the disk goes. +// latest BACKUPS_KEPT kept. it is a whole file of its own, to be copied +// somewhere else by whoever runs the server: a copy on the same disk is no +// help when the disk goes. it is copied a batch of pages at a time off the +// main thread (sqlite's online backup): made in one go (VACUUM INTO, after +// writing every account) it held the game up for seconds. const BACKUPS_KEPT = 14; -const backup = () => { +let backingUp = false; +const backup = async () => { const folder = path.join(config.dataDir, "backups"); const file = path.join(folder, `game-${new Date().toISOString().slice(0, 10)}.sqlite`); - if (fs.existsSync(file)) { + if (backingUp || fs.existsSync(file)) { return; } - fs.mkdirSync(folder, {recursive: true, mode: 0o700}); - flush(); - db.prepare("VACUUM INTO ?").run(file); - fs.chmodSync(file, 0o600); + backingUp = true; + const part = `${file}.part`; + try { + fs.mkdirSync(folder, {recursive: true, mode: 0o700}); + fs.rmSync(part, {force: true}); + await copyDatabase(db, part); + fs.chmodSync(part, 0o600); + fs.renameSync(part, file); // only a whole copy has the day's name + } + finally { + backingUp = false; + fs.rmSync(part, {force: true}); + } const old = fs.readdirSync(folder).filter(name => /^game-\d{4}-\d{2}-\d{2}\.sqlite$/.test(name)).sort().slice(0, -BACKUPS_KEPT); for (const name of old) { fs.unlinkSync(path.join(folder, name)); @@ -577,12 +640,9 @@ const backup = () => { }; const startBackups = () => { const attempt = () => { - try { - backup(); - } - catch (err) { + backup().catch(err => { console.error(`[accounts] backup failed: ${err.message}`); - } + }); }; attempt(); setInterval(attempt, 3600000).unref(); @@ -733,12 +793,16 @@ const rename = (account, username, anyName = false) => { byKey.delete(oldKey); account.username = username; byKey.set(newKey, account); + const named = [account]; for (const other of byId.values()) { - other.buddies = other.buddies.map(name => (name === oldKey ? newKey : name)); - other.blocked = other.blocked.map(name => (name === oldKey ? newKey : name)); - other.buddyRequests = other.buddyRequests.map(name => (name === oldKey ? newKey : name)); + if ([other.buddies, other.blocked, other.buddyRequests].some(list => list.includes(oldKey))) { + other.buddies = other.buddies.map(name => (name === oldKey ? newKey : name)); + other.blocked = other.blocked.map(name => (name === oldKey ? newKey : name)); + other.buddyRequests = other.buddyRequests.map(name => (name === oldKey ? newKey : name)); + named.push(other); + } } - save(); + save(...named); return null; }; @@ -772,13 +836,18 @@ const remove = account => { byId.delete(account.id); savedState.delete(account.id); retire(key); + const named = []; for (const other of byId.values()) { - other.tradeOffers = other.tradeOffers.filter(offer => offer.fromId !== account.id); - other.buddies = other.buddies.filter(name => name !== key); - other.blocked = other.blocked.filter(name => name !== key); - other.buddyRequests = other.buddyRequests.filter(name => name !== key); + if (other.tradeOffers.some(offer => offer.fromId === account.id) + || [other.buddies, other.blocked, other.buddyRequests].some(list => list.includes(key))) { + other.tradeOffers = other.tradeOffers.filter(offer => offer.fromId !== account.id); + other.buddies = other.buddies.filter(name => name !== key); + other.blocked = other.blocked.filter(name => name !== key); + other.buddyRequests = other.buddyRequests.filter(name => name !== key); + named.push(other); + } } - save(); + save(...named); console.log(`[accounts] deleted "${account.username}" (id ${account.id})`); }; diff --git a/lib/achievements.js b/lib/achievements.js index 7316c16..9c27490 100644 --- a/lib/achievements.js +++ b/lib/achievements.js @@ -105,7 +105,7 @@ const grant = (account, defId) => { } const achievement = {inv: account.nextInv++, def: def.id}; account.achievements.push(achievement); - accounts.save(); + accounts.save(account); const session = findOnline(account.username); if (session) { if (account.userVars[def.userVarRef] === undefined) { diff --git a/lib/admin.js b/lib/admin.js index 7361ba7..e61a224 100644 --- a/lib/admin.js +++ b/lib/admin.js @@ -95,7 +95,7 @@ const give = body => { if (!item) { return {status: 409, body: {error: "That inventory is full"}}; } - accounts.save(); + accounts.save(account); if (session) { sendClothingAdded(session, item); } @@ -119,7 +119,7 @@ const give = body => { account.finderGiven[key] = {count: ((account.finderGiven[key] || {}).count || 0) + 1, at: Math.floor(Date.now() / 1000)}; const kept = Object.entries(account.finderGiven).sort((one, other) => other[1].at - one[1].at).slice(0, MAX_FAVOURITES); account.finderGiven = Object.fromEntries(kept); - accounts.save(); + accounts.save(account); return {status: 200, body: {ok: true, online: Boolean(session)}}; }; @@ -236,7 +236,7 @@ const handle = async (route, params, body, from) => { const result = accounts.register(username, password, {type: avatarType}); if (result.account) { result.account.agreed = {version: config.termsVersion, at: Math.floor(Date.now() / 1000)}; - accounts.save(); + accounts.save(result.account); } // the client signs in straight after, and is not asked again. return result.error ? {status: 400, body: {error: result.error}} diff --git a/lib/adopt.js b/lib/adopt.js index 086c42e..33ac90d 100644 --- a/lib/adopt.js +++ b/lib/adopt.js @@ -59,7 +59,7 @@ const handlers = { for (const found of session.account.adoptAPets) { found.seen = found.seen || seen.has(found.def); } - accounts.save(); + accounts.save(session.account); }, }; diff --git a/lib/adventures.js b/lib/adventures.js index ca3ee5d..deccda4 100644 --- a/lib/adventures.js +++ b/lib/adventures.js @@ -82,7 +82,7 @@ module.exports = ({enterRoom, joinRoom, tell}) => { avatar.questXp = before + amount; avatar.questLevel = levelFor(avatar.questXp); if (amount > 0) { - accounts.save(); + accounts.save(session.account); } q.send("quxp", amount, avatar.questXp, level, avatar.questLevel, percent(avatar.questXp), actor); if (avatar.questLevel > level) { @@ -121,7 +121,7 @@ module.exports = ({enterRoom, joinRoom, tell}) => { const {account} = session; account.currency.gems = (account.currency.gems || 0) + amount; achievements.currencyChanged(account, "gems", amount); - accounts.save(); + accounts.save(account); session.sendXt("cu", -1, CURRENCY_GEMS, account.currency.gems, 0); }, }; diff --git a/lib/avatars.js b/lib/avatars.js index dad5ae4..6e841e1 100644 --- a/lib/avatars.js +++ b/lib/avatars.js @@ -78,7 +78,7 @@ module.exports = ({others, findOnline, findBySfsId, refreshForOthers, petFields} // (accounts.freeAvatarSlot). const avatar = accounts.newAvatar({perUserAvId: account.nextInv++, avInvId: accounts.freeAvatarSlot(account)}, `${name1}|${name2}|${name3}`, type, customAvId); account.avatars.push(avatar); - accounts.save(); + accounts.save(account); session.sendXt("aa", -1, 1, avatar.perUserAvId, avatar.avInvId); }, @@ -93,7 +93,7 @@ module.exports = ({others, findOnline, findBySfsId, refreshForOthers, petFields} return; } account.activeAvatar = avatar.perUserAvId; - accounts.save(); + accounts.save(account); const messageRoom = session.room ? session.room.id : -1; const common = [1, session.id, avatar.perUserAvId, avatar.avInvId, avatar.customAvId, avatar.type, ...avatar.colors, avatar.name]; session.sendXt("as", messageRoom, ...common, avatar.petInvId, avatar.questLevel || 0); @@ -116,7 +116,7 @@ module.exports = ({others, findOnline, findBySfsId, refreshForOthers, petFields} for (const item of account.items) { item.on = item.on.filter(avInvId => avInvId !== avatar.avInvId); } - accounts.save(); + accounts.save(account); session.sendXt("ak", -1, 1, 0); }, @@ -134,7 +134,7 @@ module.exports = ({others, findOnline, findBySfsId, refreshForOthers, petFields} if (tagChanged) { session.account.nameBarData = nameBarData; } - accounts.save(); + accounts.save(session.account); session.sendXt("ap", -1, 1); refreshForOthers(session); if (tagChanged) { diff --git a/lib/buddies.js b/lib/buddies.js index a8fde33..e5661eb 100644 --- a/lib/buddies.js +++ b/lib/buddies.js @@ -105,7 +105,7 @@ module.exports = ({findOnline, onBlocked, onUnblocked, partyTimeLeft}) => { else if (!account.buddies.includes(key(target))) { if (!target.buddyRequests.includes(key(account))) { target.buddyRequests.push(key(account)); - accounts.save(); + accounts.save(account, target); } if (targetSession) { targetSession.sendXt("ba", -1, REQUEST_INCOMING, account.username, USERNAME_SHOWN); @@ -122,7 +122,7 @@ module.exports = ({findOnline, onBlocked, onUnblocked, partyTimeLeft}) => { return; } account.buddyRequests = account.buddyRequests.filter(name => name !== key(requester)); - accounts.save(); + accounts.save(account); const requesterSession = findOnline(requester.username); if (accept !== "1" || account.buddies.includes(key(requester)) || account.buddies.length >= MAX_BUDDIES || requester.buddies.length >= MAX_BUDDIES) { @@ -135,7 +135,7 @@ module.exports = ({findOnline, onBlocked, onUnblocked, partyTimeLeft}) => { requester.buddies.push(key(account)); achievements.add(account, achievements.VARS.buddiesAdded); achievements.add(requester, achievements.VARS.buddiesAdded); - accounts.save(); + accounts.save(account, requester); sendStatus(session, requester, STATUS_UPDATE); if (requesterSession) { sendStatus(requesterSession, account, STATUS_ACCEPTED_YOUR_REQUEST); @@ -149,7 +149,7 @@ module.exports = ({findOnline, onBlocked, onUnblocked, partyTimeLeft}) => { if (other) { account.buddies = account.buddies.filter(name => name !== key(other)); other.buddies = other.buddies.filter(name => name !== key(account)); - accounts.save(); + accounts.save(account, other); const otherSession = findOnline(other.username); if (otherSession) { otherSession.sendXt("bd", -1, account.username); @@ -165,7 +165,7 @@ module.exports = ({findOnline, onBlocked, onUnblocked, partyTimeLeft}) => { const added = other && other !== account && !account.blocked.includes(key(other)) && account.blocked.length < MAX_BLOCKED; if (added) { account.blocked.push(key(other)); - accounts.save(); + accounts.save(account); } session.sendXt("bb", -1, username, other && other !== account && account.blocked.includes(key(other)) ? 1 : 0); if (added) { @@ -178,7 +178,7 @@ module.exports = ({findOnline, onBlocked, onUnblocked, partyTimeLeft}) => { const other = accounts.byName(username); const removed = other && account.blocked.includes(key(other)); account.blocked = account.blocked.filter(name => name !== String(username).toLowerCase()); - accounts.save(); + accounts.save(account); session.sendXt("bu", -1, username, 1); if (removed) { onUnblocked(session, other); diff --git a/lib/cdn.js b/lib/cdn.js index a66c6a6..2599a12 100644 --- a/lib/cdn.js +++ b/lib/cdn.js @@ -171,4 +171,8 @@ const cached = async asked => { } }; -module.exports = {get, cached, hashName, hashedPath}; +// whether the official CDN said it has no such file (a 404 or 403 when it +// was asked), as against one not asked for yet or not fetched just now. +const knownMissing = asked => missing.has(clean(asked)); + +module.exports = {get, cached, knownMissing, hashName, hashedPath}; diff --git a/lib/commands.js b/lib/commands.js index 46e2122..39d977d 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -79,7 +79,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB item.on.push(avatar.avInvId); } } - accounts.save(); + accounts.save(account); // ItemXtCommManager.itemsChangedListResponse, "3" = what is worn // changed: [3, inUse, perUserAvId, username, colour, onCount, // offCount, (invIdx, defId)..., offInvIdx...] @@ -131,7 +131,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB } const items = wornItems(account, avatar).map(item => item.inv); account.outfits = [...account.outfits.filter(entry => entry !== existing), {name, items}]; - accounts.save(); + accounts.save(account); tell(session, `Outfit ${name} saved (${items.length} items).`); return; } @@ -152,7 +152,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB return; } account.outfits = account.outfits.filter(entry => entry.name !== name); - accounts.save(); + accounts.save(account); tell(session, `Outfit ${name} deleted.`); return; case "list": @@ -194,7 +194,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB item.spots = item.spots || {}; item.spots[account.activeDen] = {cat: spot.cat || 1, x: spot.x, y: spot.y, flip: spot.flip}; } - accounts.save(); + accounts.save(account); dens.sendDenItems(session); const room = session.room && session.room.denOwner === account ? session.room : null; for (const occupant of room ? room.sessions : []) { @@ -217,7 +217,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB } const items = layoutOf(account); account.denLayouts = [...account.denLayouts.filter(entry => entry !== existing), {name, den: activeDenDef(account), items}]; - accounts.save(); + accounts.save(account); tell(session, `Den layout ${name} saved (${items.length} items).`); return; } @@ -266,7 +266,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB } const missing = applyLayout(session, undo.items, false); account.denUndo = null; - accounts.save(); + accounts.save(account); tell(session, missing ? `Your den is back as it was, apart from ${missing} items that are no longer yours.` : "Your den is back as it was."); return; } @@ -276,7 +276,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB return; } account.denLayouts = account.denLayouts.filter(entry => entry.name !== name); - accounts.save(); + accounts.save(account); tell(session, `Den layout ${name} deleted.`); return; case "list": @@ -373,7 +373,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB const themes = require("../config.js").themes; if (action === "random" && (choice === "on" || choice === "off")) { account.randomTheme = choice === "on"; - accounts.save(); + accounts.save(account); tell(session, account.randomTheme ? "The lands get a look picked at random each time you log in." : "The lands keep the look you choose."); } else if (action === "here" && session.room && !session.room.denOwner && (choice === "off" || Object.hasOwn(themes, choice))) { @@ -383,7 +383,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB else { account.roomThemes[session.room.base] = choice; } - accounts.save(); + accounts.save(account); reloadRoom(session); } else if (action === "list") { @@ -403,7 +403,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB } const before = account.ecards.length; account.ecards = account.ecards.filter(card => !card.read || card.gift); - accounts.save(); + accounts.save(account); tell(session, `${before - account.ecards.length} read Jam-A-Grams deleted. Open your mail again to see.`); }, @@ -482,7 +482,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB } else if (wanted.toLowerCase() === "off") { delete account.denNames[account.activeDen]; - accounts.save(); + accounts.save(account); tell(session, "This den has its usual name again."); } else if (isSilenced(account)) { @@ -493,7 +493,7 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB } else { account.denNames[account.activeDen] = wanted; - accounts.save(); + accounts.save(account); tell(session, `This den is now called ${wanted}. It shows the next time someone walks in.`); } }, diff --git a/lib/dens.js b/lib/dens.js index ddd50c8..dd44161 100644 --- a/lib/dens.js +++ b/lib/dens.js @@ -149,7 +149,7 @@ const giveDenItem = (account, defId, version = 0) => { } const item = {inv: account.nextInv++, def: Number(defId), version: Number(version) || 0, spots: {}}; account.denItems.push(item); - accounts.save(); + accounts.save(account); return item; }; @@ -167,7 +167,7 @@ const clearDen = (owner, findOnline) => { for (const pet of petsInDen(owner)) { delete pet.inDen; } - accounts.save(); + accounts.save(owner); const ownerSession = findOnline(owner.username); const room = ownerSession && ownerSession.room && ownerSession.room.denOwner === owner ? ownerSession.room : null; for (const session of room ? room.sessions : []) { @@ -231,7 +231,7 @@ const handlers = ({others, findOnline}) => { account.currency[currency] -= purchase.cost; achievements.currencyChanged(account, currency, -purchase.cost); account.dens.push({inv: freeSlot(account, /^\d{1,3}$/.test(String(slot)) ? Number(slot) : NaN), def: purchase.defId}); - accounts.save(); + accounts.save(account); session.sendXt("db", -1, 1, account.currency[currency], 0, purchase.currencyType, 0); sendDenList(session); }; @@ -259,7 +259,7 @@ const handlers = ({others, findOnline}) => { masterpieces.claim(paintingId, frame); const item = giveDenItem(account, masterpieces.DEN_ITEM, frame); item.image = String(paintingId); - accounts.save(); + accounts.save(account); console.log(`[world] ${account.username} made masterpiece ${item.image}${masterpieces.itemApproved(item) ? "" : " (waiting for review in the admin panel)"}`); session.sendXt("db", -1, 1, account.currency.diamonds || 0, 0, CURRENCY_DIAMONDS, item.inv); sendDenItems(session); @@ -289,7 +289,7 @@ const handlers = ({others, findOnline}) => { achievements.currencyChanged(account, "gems", refund); recycled.push(den.inv); } - accounts.save(); + accounts.save(account); session.sendXt("dr", -1, recycled.length, account.currency.gems, ...recycled); // the switcher empties the slot itself; the items that come back need // the inventory's list. @@ -418,7 +418,7 @@ const handlers = ({others, findOnline}) => { } applied.push(item.inv); } - accounts.save(); + accounts.save(account); session.sendXt("dss", -1, ...applied); if (inOwnDen(session)) { for (const other of others(session)) { @@ -452,7 +452,7 @@ const handlers = ({others, findOnline}) => { account.denItems.splice(index, 1); recycled.push(invIdx); } - accounts.save(); + accounts.save(account); session.sendXt("dr", -1, recycled.length, account.currency.gems, ...recycled); if (recycled.length) { sendDenItems(session); @@ -486,7 +486,7 @@ const handlers = ({others, findOnline}) => { return; } account.activeDen = den.inv; - accounts.save(); + accounts.save(account); const avatar = activeAvatar(account); session.sendXt("dc", -1, den.def, avatar.perUserAvId, avatar.petInvId); // what is free and what is placed differs from den to den. @@ -520,7 +520,7 @@ const handlers = ({others, findOnline}) => { } else if ([DEN_LOCKED, DEN_BUDDIES_ONLY, DEN_OPEN].includes(Number(value))) { session.account.denPrivacy = Number(value); - accounts.save(); + accounts.save(session.account); } }, }; diff --git a/lib/disguises.js b/lib/disguises.js index 2ab3a15..15dfc8c 100644 --- a/lib/disguises.js +++ b/lib/disguises.js @@ -70,7 +70,7 @@ const show = (to, subject) => { // afresh, which brings the disguise with it (`show`). const set = (session, id, redraw) => { session.account.disguise = id; - accounts.save(); + accounts.save(session.account); session.sendXt("dph", -1, 1, session.id, id ? 1 : 0, id || NPC_PHANTOM); redraw(session); }; diff --git a/lib/ecards.js b/lib/ecards.js index 2082126..dc059e9 100644 --- a/lib/ecards.js +++ b/lib/ecards.js @@ -154,7 +154,7 @@ module.exports = ({findOnline, blocks}) => { // a localization string (StartupPopups.setupGiftPopup). const message = clean(text).replace(/^(?=[\s+-]*\d)/, "Gift: "); deliver(to, {from: HQ_NAME, hq: true, card: art.cards[0], stamp: 0, msg: message, gift: attached}); - accounts.save(); + accounts.save(to); return null; }; @@ -165,7 +165,7 @@ module.exports = ({findOnline, blocks}) => { return false; } deliver(to, {from: HQ_NAME, hq: true, card: art.cards[0], stamp: 0, msg: clean(text).replace(/^(?=[\s+-]*\d)/, "Note: "), gift: null}); - accounts.save(); + accounts.save(to); return true; }; @@ -268,7 +268,7 @@ module.exports = ({findOnline, blocks}) => { if (gift) { achievements.add(account, achievements.VARS.giftsSent); } - accounts.save(); + accounts.save(account, to); reply(SENT); }, @@ -277,7 +277,7 @@ module.exports = ({findOnline, blocks}) => { const card = session.account.ecards.find(candidate => candidate.id === Number(msgId)); if (card) { card.read = true; - accounts.save(); + accounts.save(session.account); } session.sendXt("er", -1, msgId, card ? 1 : 0); }, @@ -288,7 +288,7 @@ module.exports = ({findOnline, blocks}) => { const {account} = session; const before = account.ecards.length; account.ecards = account.ecards.filter(card => card.id !== Number(msgId)); - accounts.save(); + accounts.save(account); session.sendXt("ed", -1, 1, before - account.ecards.length, ...(before === account.ecards.length ? [] : [msgId])); }, @@ -314,7 +314,7 @@ module.exports = ({findOnline, blocks}) => { if (accept === "1") { giveGift(session, gift); } - accounts.save(); + accounts.save(session.account); session.sendXt("eg", -1, msgId, 1); }, @@ -324,7 +324,7 @@ module.exports = ({findOnline, blocks}) => { const valid = [FROM_EVERYONE, FROM_BUDDIES, FROM_NOBODY].includes(value); if (valid) { session.account.ecardPrivacy = value; - accounts.save(); + accounts.save(session.account); } session.sendXt("ei", -1, valid ? 1 : 0); }, diff --git a/lib/http.js b/lib/http.js index c6d8591..04d9fea 100644 --- a/lib/http.js +++ b/lib/http.js @@ -151,6 +151,7 @@ const send = (res, status, body, type = "application/json") => { const TEN_MINUTES_MS = 600000; const failuresByAddress = limits.limiter(config.failedLoginsPerTenMinutes, TEN_MINUTES_MS); const searches = limits.limiter(120, 60000); // item finder searches, per address +const MISSING_ASSET_SECONDS = 600; // how long a client may keep the CDN's "no such file" const cacheMisses = limits.limiter(config.cacheMissesPerTenMinutes, TEN_MINUTES_MS); // files fetched from the official CDN for one address const errorReports = limits.limiter(config.errorReportsPerMinute, 60000); const paintingPosts = limits.limiter(60, TEN_MINUTES_MS); // per address; each also needs a token from the game @@ -169,6 +170,27 @@ const keepClientError = text => { .then(size => (size < MAX_CLIENT_ERROR_FILE ? fs.promises.appendFile(CLIENT_ERROR_FILE, text) : null)) .catch(() => {}); }; +// the same error from many clients (one broken item, met by everyone who +// sees it) is written once in REPEAT_WINDOW_MS, and the next time after that +// with how many there were between: a log, and a thread that waits for it, +// had a stack of twenty lines several times a second. null for one to leave +// out; else what to add to its line. +const REPEAT_WINDOW_MS = 600000; +const MAX_REMEMBERED_ERRORS = 1000; +const recentErrors = new Map(); // section and message -> {at, more} +const repeatedError = text => { + const seen = recentErrors.get(text); + if (seen && Date.now() - seen.at < REPEAT_WINDOW_MS) { + seen.more++; + return null; + } + recentErrors.delete(text); + if (recentErrors.size >= MAX_REMEMBERED_ERRORS) { + recentErrors.delete(recentErrors.keys().next().value); + } + recentErrors.set(text, {at: Date.now(), more: 0}); + return seen && seen.more > 0 ? `\n\t(and ${seen.more} more like it in the ${Math.round(REPEAT_WINDOW_MS / 60000)} minutes before)` : ""; +}; const THEMED_ROOM = /^(\d+)\/roomDefs\/theme_(\d+)\/(.+)$/; // the web pages, as against what the game and the client load @@ -348,7 +370,7 @@ const renameAndSignIn = (res, body, from) => { delete account.nameRejected; manage.forget(account); accounts.recordLogin(account, "launcher", from.address); - accounts.save(); + accounts.save(account); console.log(`[accounts] a rejected username was replaced by "${account.username}"`); accounts.setLoginChoices(account, body.login_choices); const token = accounts.issueToken(account); @@ -384,7 +406,7 @@ const authenticate = async (req, res, from) => { const tried = accounts.byName(accountKey); if (tried) { accounts.recordLogin(tried, "launcher", from.address, false); - accounts.save(); + accounts.save(tried); } } // error_code 100 = expired refresh token, 101 = wrong credentials @@ -429,7 +451,7 @@ const authenticate = async (req, res, from) => { if (!totp.verify(account, String(body.totp))) { failuresByAddress.record(from.key); accounts.recordLogin(account, "launcher", from.address, false); - accounts.save(); + accounts.save(account); send(res, 401, {error_code: 101, error: "totp_wrong", captcha_pass: captcha.issuePass(account, from)}); return; } @@ -440,13 +462,13 @@ const authenticate = async (req, res, from) => { // its username was rejected: a new one first (above). the official // launcher shows this as a wrong password; the account page asks too. if (account.nameRejected) { - accounts.save(); // a two-step code just used stays used + accounts.save(account); // a two-step code just used stays used send(res, 401, {error_code: 101, error: "name_rejected", rename_ticket: renameTicket(account, deviceToken)}); return; } if (!byRefresh) { accounts.recordLogin(account, "launcher", from.address); - accounts.save(); + accounts.save(account); } // the custom client's advanced settings; the official launcher sends none. accounts.setLoginChoices(account, body.login_choices); @@ -687,9 +709,12 @@ const handle = async (req, res) => { const section = String(report.section).slice(0, 20).replace(/[^\w-]/g, "?"); const trace = message.startsWith("media "); if (trace ? config.logPackets : from.local || errorReports.allow(from.key)) { - console.log(`[${trace ? "client-trace" : "client-error"}] (${section}) ${message}`); - if (!trace) { - keepClientError(`${new Date().toISOString()} (${section}) ${message}\n`); + const again = trace ? "" : repeatedError(`${section} ${message}`); + if (again !== null) { + console.log(`[${trace ? "client-trace" : "client-error"}] (${section}) ${message}${again}`); + if (!trace) { + keepClientError(`${new Date().toISOString()} (${section}) ${message}${again}\n`); + } } } send(res, 200, {}); @@ -790,7 +815,15 @@ const handle = async (req, res) => { const body = (themed && Object.values(config.themes).some(theme => theme.build === Number(themed[2])) && await cdn.get(`${themed[2]}/roomDefs/${themed[3]}`, mayFetch)) || await cdn.get(themed ? `${themed[1]}/roomDefs/${themed[3]}` : relPath, mayFetch); if (!body) { - console.log(`[http] 404 ${url.pathname}`); + // one the CDN does not have is asked for again and again (the same few + // hundred, by every client that meets them): its client may keep the + // answer a while, and it was logged when the CDN said so ([cdn] 404). + if (cdn.knownMissing(themed ? `${themed[1]}/roomDefs/${themed[3]}` : relPath)) { + res.setHeader("Cache-Control", `public, max-age=${MISSING_ASSET_SECONDS}`); + } + else { + console.log(`[http] 404 ${url.pathname}`); + } send(res, 404, "Not found", "text/plain"); return; } @@ -815,12 +848,15 @@ const start = () => new Promise((resolve, reject) => { : http.createServer(onRequest); // nobody gets to hold a request open by trickling it in, nor a reply by // never reading it; and there is an end to how many connections there - // may be, in all and from one address. + // may be, in all and from one address. a proxy in front (nginx) keeps + // its own count of those; its connections here are not counted, since + // turning them away only had it fail the requests of players it had + // already let in, who then asked again. server.requestTimeout = 30000; server.headersTimeout = 15000; server.timeout = config.httpSocketTimeoutMs; - server.maxConnections = config.maxHttpConnections; const open = new Map(); // address, as limits count it -> connections + let openInAll = 0; server.on("connection", socket => { const address = limits.key(socket.remoteAddress); // behind a proxy every connection is the proxy's own. @@ -828,12 +864,14 @@ const start = () => new Promise((resolve, reject) => { return; } const count = (open.get(address) || 0) + 1; - if (count > config.maxHttpConnectionsPerAddress) { + if (count > config.maxHttpConnectionsPerAddress || openInAll >= config.maxHttpConnections) { socket.destroy(); return; } open.set(address, count); + openInAll++; socket.once("close", () => { + openInAll--; const left = open.get(address) - 1; if (left > 0) { open.set(address, left); diff --git a/lib/items.js b/lib/items.js index a3972e6..86d5cd5 100644 --- a/lib/items.js +++ b/lib/items.js @@ -48,7 +48,7 @@ module.exports = ({others, sendItemList}) => { achievements.currencyChanged(account, currency, -purchase.cost); achievements.add(account, achievements.VARS.clothesBought); const item = addClothing(account, def.id, colors[colorIndex]); - accounts.save(); + accounts.save(account); sendClothingAdded(session, item); session.sendXt("ib", -1, 1, shopId, account.currency[currency], purchase.currencyType); }, @@ -95,7 +95,7 @@ module.exports = ({others, sendItemList}) => { }; const off = ids.slice(onCount).filter(invIdx => setWorn(invIdx, false) !== null); const on = ids.slice(0, onCount).map(invIdx => [invIdx, setWorn(invIdx, true)]).filter(([, def]) => def !== null); - accounts.save(); + accounts.save(account); // ItemXtCommManager.itemUseResponse session.sendXt("iu", -1, 1); // the reply only closes the editor. the animal in the room is redrawn @@ -127,7 +127,7 @@ module.exports = ({others, sendItemList}) => { account.currency[received] = (account.currency[received] || 0) + def.result; achievements.currencyChanged(account, paid, -def.cost); achievements.currencyChanged(account, received, def.result); - accounts.save(); + accounts.save(account); // ItemXtCommManager.currencyUpdateResponse: [currencyType, newAmount, reason] session.sendXt("cu", -1, def.resultType, account.currency[received], 0); session.sendXt("ce", -1, 1, account.currency[paid]); @@ -154,7 +154,7 @@ module.exports = ({others, sendItemList}) => { account.items.splice(index, 1); recycled.push(invIdx); } - accounts.save(); + accounts.save(account); session.sendXt("ir", -1, recycled.length, account.currency.gems || 0, ...recycled); if (recycled.length) { sendClothingRemoved(session, recycled); diff --git a/lib/log.js b/lib/log.js index 21c5417..c16dfa8 100644 --- a/lib/log.js +++ b/lib/log.js @@ -5,6 +5,7 @@ const MAX_LINES = 5000; const MAX_LINE_LENGTH = 2000; +const TRIM_BATCH = 1000; const lines = []; // {id, time, text} let nextId = 1; @@ -15,7 +16,9 @@ const capture = original => (...args) => { for (const line of text.split("\n")) { lines.push({id: nextId++, time: Date.now(), text: line.slice(0, MAX_LINE_LENGTH)}); } - if (lines.length > MAX_LINES) { + // trimmed a batch at a time: trimming a line at a time moved all the + // others along for every line printed. + if (lines.length > MAX_LINES + TRIM_BATCH) { lines.splice(0, lines.length - MAX_LINES); } }; diff --git a/lib/manage.js b/lib/manage.js index cda4436..fd9c5b5 100644 --- a/lib/manage.js +++ b/lib/manage.js @@ -248,7 +248,7 @@ const act = (account, body, req, from, res) => { return {error: NAME_ERRORS[problem] || NAME_ERRORS.invalid_username}; } delete account.nameRejected; - accounts.save(); + accounts.save(account); console.log(`[account] a rejected username was replaced by "${account.username}" from ${from.address}`); return {ok: "Saved."}; } @@ -291,7 +291,7 @@ const act = (account, body, req, from, res) => { accounts.revokeTokens(account); world.moderation.actions.kick(account); } - accounts.save(); + accounts.save(account); console.log(`[account] "${account.username}" ${account.locked ? "locked" : "unlocked"} from ${from.address}`); return {ok: account.locked ? "Locked." : "Unlocked."}; } @@ -300,7 +300,7 @@ const act = (account, body, req, from, res) => { return {error: "Unknown setting."}; } account.features[body.feature] = body.on; - accounts.save(); + accounts.save(account); return {ok: "Saved."}; } // two-step sign-in (lib/totp.js). turning it on: the password again, @@ -314,17 +314,17 @@ const act = (account, body, req, from, res) => { return {error: "Wrong password."}; } const setup = totp.begin(account); - accounts.save(); + accounts.save(account); return {ok: "Scan the code.", setup}; } case "twoStepConfirm": { const recovery = totp.confirm(account, body.code); if (!recovery) { - accounts.save(); + accounts.save(account); return {error: "Wrong code."}; } accounts.revokeTokens(account); - accounts.save(); + accounts.save(account); console.log(`[account] "${account.username}" turned two-step sign-in on from ${from.address}`); return {ok: "2FA is on.", recovery}; } @@ -337,18 +337,18 @@ const act = (account, body, req, from, res) => { return {error: "Wrong password."}; } if (!totp.verify(account, String(body.code || ""))) { - accounts.save(); + accounts.save(account); return {error: "Wrong code."}; } totp.turnOff(account); - accounts.save(); + accounts.save(account); res.setHeader("Set-Cookie", cookieHeader("", 0, DEVICE_COOKIE)); console.log(`[account] "${account.username}" turned two-step sign-in off from ${from.address}`); return {ok: "2FA is off."}; } case "forgetDevices": totp.forgetDevices(account); - accounts.save(); + accounts.save(account); return {ok: "Trusted devices forgotten."}; // their bio, for the Bio tab of their card in the game. empty takes it off. case "bio": { @@ -361,13 +361,13 @@ const act = (account, body, req, from, res) => { return {error: `At most ${badges.MAX_BIO} characters on ${badges.MAX_BIO_LINES} lines.`}; } account.bio = bio; - accounts.save(); + accounts.save(account); return {ok: bio ? "Saved." : "Removed."}; } // whether their card in the game shows how long they have played. case "playtimePublic": account.playtimePublic = body.on === true; - accounts.save(); + accounts.save(account); return {ok: account.playtimePublic ? "Others can see your playtime on your card." : "Your playtime is private."}; // their Discord username, for their card in the game. empty takes it off. case "discord": { @@ -376,7 +376,7 @@ const act = (account, body, req, from, res) => { return {error: "Not a Discord username."}; } account.discord = name; - accounts.save(); + accounts.save(account); return {ok: name ? "Saved." : "Removed."}; } // the player's own words about a buddy; only they see them. @@ -392,7 +392,7 @@ const act = (account, body, req, from, res) => { else { delete account.buddyNotes[key]; } - accounts.save(); + accounts.save(account); return {ok: "Saved."}; } // blocking and unblocking, as the game's own buttons do it. @@ -422,7 +422,7 @@ const act = (account, body, req, from, res) => { case "signOutEverywhere": accounts.revokeTokens(account); totp.forgetDevices(account); - accounts.save(); + accounts.save(account); world.moderation.actions.kick(account); signOutOthers(account, req); return {ok: "Signed out everywhere."}; @@ -495,7 +495,7 @@ const handle = async (req, res, url, from, web) => { failuresByAddress.record(from.key); if (known) { accounts.recordLogin(known, "web", from.address, false); - accounts.save(); + accounts.save(known); } reply(401, {error: "Wrong username or password."}); return; @@ -511,7 +511,7 @@ const handle = async (req, res, url, from, web) => { if (!totp.verify(account, String(body.code))) { failuresByAddress.record(from.key); accounts.recordLogin(account, "web", from.address, false); - accounts.save(); + accounts.save(account); reply(401, {error: "Wrong code.", twoStep: true, captchaPass: captcha.issuePass(account, from)}); return; } @@ -520,7 +520,7 @@ const handle = async (req, res, url, from, web) => { } } accounts.recordLogin(account, "web", from.address); - accounts.save(); + accounts.save(account); signIn(res, account, cookies); reply(200, {ok: true}); return; diff --git a/lib/minigames.js b/lib/minigames.js index 0123582..e475af2 100644 --- a/lib/minigames.js +++ b/lib/minigames.js @@ -271,7 +271,7 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { } account.currency.gems -= CARNIVAL_COST; achievements.currencyChanged(account, "gems", -CARNIVAL_COST); - accounts.save(); + accounts.save(account); session.sendXt("cu", -1, CURRENCY_GEMS, account.currency.gems, 0); game.serial2 = serial(); reply("pg", "1", hideSerial(game.serial2)); @@ -288,7 +288,7 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { if (tickets > 0) { account.currency.tickets = (account.currency.tickets || 0) + tickets; achievements.currencyChanged(account, "tickets", tickets); - accounts.save(); + accounts.save(account); session.sendXt("cu", -1, CURRENCY_TICKETS, account.currency.tickets, 0); } reply("gr", tickets); @@ -315,7 +315,7 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { } account.currency.gems -= CLAW_COST; achievements.currencyChanged(account, "gems", -CLAW_COST); - accounts.save(); + accounts.save(account); session.sendXt("cu", -1, CURRENCY_GEMS, account.currency.gems, 0); const prizes = clawPrizes(game.def); const won = prizes.length > 0 && Math.random() < CLAW_WIN_CHANCE; @@ -343,7 +343,7 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { gd(session, game, [keep]) { if (keep === "1" && session.gamePrize) { sendClothingAdded(session, addClothing(session.account, session.gamePrize.def, session.gamePrize.color)); - accounts.save(); + accounts.save(session.account); } session.gamePrize = null; }, @@ -401,7 +401,7 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { } account.currency.gems -= INVASION_COST; achievements.currencyChanged(account, "gems", -INVASION_COST); - accounts.save(); + accounts.save(account); session.sendXt("cu", -1, CURRENCY_GEMS, account.currency.gems, 0); const prizes = invasionPrizes(); session.prizeSerial = serial(); @@ -528,7 +528,7 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { if (valid) { account.currency.gems = (account.currency.gems || 0) + amount; achievements.currencyChanged(account, "gems", amount); - accounts.save(); + accounts.save(account); } session.gemSerial = serial(); session.sendXt("mg", -1, session.gemSerial, 0); @@ -547,7 +547,7 @@ module.exports = ({tell, leaveRoom, findOnline, blocks}) => { if (valid && (pet.mastery || 0) < MASTERY_FULL) { pet.mastery = Math.min(MASTERY_FULL, (pet.mastery || 0) + amount); nowFull = pet.mastery === MASTERY_FULL; - accounts.save(); + accounts.save(account); } session.masterySerial = serial(); session.sendXt("ma", -1, session.masterySerial, nowFull ? 1 : 0); diff --git a/lib/moderation.js b/lib/moderation.js index ca6772f..56f4321 100644 --- a/lib/moderation.js +++ b/lib/moderation.js @@ -71,7 +71,7 @@ module.exports = ({findOnline}) => { target.banReason = bans.reasonText(reason); target.bannedBy = by; target.bannedAt = now(); - accounts.save(); + accounts.save(target); bans.record({account: target, action: "ban", seconds: duration, reason: target.banReason, by}); const session = findOnline(target.username); if (session) { @@ -87,7 +87,7 @@ module.exports = ({findOnline}) => { delete target.banReason; delete target.bannedBy; delete target.bannedAt; - accounts.save(); + accounts.save(target); if (was) { bans.record({account: target, action: "unban", by}); } @@ -100,7 +100,7 @@ module.exports = ({findOnline}) => { return false; } target.banReason = bans.reasonText(reason); - accounts.save(); + accounts.save(target); bans.record({account: target, action: "reason", reason: target.banReason, by}); return true; }, @@ -108,7 +108,7 @@ module.exports = ({findOnline}) => { // duration in seconds, PERMANENT, or 0 to lift silence(target, duration) { target.silencedUntil = duration === PERMANENT ? PERMANENT : (duration > 0 ? now() + duration : 0); - accounts.save(); + accounts.save(target); const session = findOnline(target.username); if (session) { // minutes, negative for "until further notice", 0 for "you may type again" @@ -123,7 +123,7 @@ module.exports = ({findOnline}) => { rejectName(target, by = "") { target.nameRejected = {by, at: now()}; accounts.revokeTokens(target); - accounts.save(); + accounts.save(target); actions.kick(target); return true; }, @@ -131,7 +131,7 @@ module.exports = ({findOnline}) => { // taken back: the name will do after all. acceptName(target) { delete target.nameRejected; - accounts.save(); + accounts.save(target); return true; }, @@ -154,7 +154,7 @@ module.exports = ({findOnline}) => { const before = target.currency[name] || 0; target.currency[name] = Math.max(0, before + amount); achievements.currencyChanged(target, name, target.currency[name] - before); - accounts.save(); + accounts.save(target); const session = findOnline(target.username); if (session) { // ItemXtCommManager.currencyUpdateResponse: [currencyType, newAmount, reason] diff --git a/lib/modpanel.js b/lib/modpanel.js index 7cb2c34..2ec0741 100644 --- a/lib/modpanel.js +++ b/lib/modpanel.js @@ -198,7 +198,7 @@ const act = (mod, target, body) => { return {ok: `${name} keeps the name.`}; case "clearBio": target.bio = ""; - accounts.save(); + accounts.save(target); return {ok: "Bio cleared."}; // the address they last signed in from, for good. which address that // is, the moderator is not told. @@ -271,7 +271,7 @@ const handle = async (req, res, url, from, web) => { failuresByAddress.record(from.key); if (known && !account) { accounts.recordLogin(known, "mod", from.address, false); - accounts.save(); + accounts.save(known); } reply(401, {error: "Wrong username or password."}); return; @@ -287,12 +287,12 @@ const handle = async (req, res, url, from, web) => { if (!totp.verify(account, String(body.code || ""))) { failuresByAddress.record(from.key); accounts.recordLogin(account, "mod", from.address, false); - accounts.save(); + accounts.save(account); reply(401, {error: "Wrong code."}); return; } accounts.recordLogin(account, "mod", from.address); - accounts.save(); + accounts.save(account); for (const [hash, session] of sessions) { if (session.expiry < Date.now()) { sessions.delete(hash); diff --git a/lib/panel.js b/lib/panel.js index 1d769a8..15847e0 100644 --- a/lib/panel.js +++ b/lib/panel.js @@ -348,7 +348,7 @@ const gift = (account, body) => { if (body.kind === "adopt") { const ids = body.id === "all" ? defpacks.adoptAPetIds() : [body.id]; const given = ids.filter(id => adopt.give(account, id)).length; - accounts.save(); + accounts.save(account); return given ? {ok: `${name} has ${given} more on their Adopt-A-Pet checklist (it shows after their next login).`} : {error: "No such Adopt-A-Pet, or they already have it."}; } @@ -381,7 +381,7 @@ const act = (account, body) => { // they cannot. case "unlock": account.locked = false; - accounts.save(); + accounts.save(account); return {ok: `${name} is unlocked.`}; // for a player who lost their phone and their recovery codes: their // password alone signs them in again. @@ -390,7 +390,7 @@ const act = (account, body) => { return {error: `${name} does not use 2FA.`}; } totp.turnOff(account); - accounts.save(); + accounts.save(account); return {ok: `2FA is off for ${name}.`}; case "rename": { world.disconnect(account); @@ -413,7 +413,7 @@ const act = (account, body) => { return {error: "Pick a date and time in the past."}; } account.lastSeen = when; - accounts.save(); + accounts.save(account); return {ok: `${name} was last seen ${new Date(when * 1000).toISOString().replace("T", " ").slice(0, 16)} UTC, as far as the game knows.`}; } // the adventure level of the animal they are using. @@ -431,7 +431,7 @@ const act = (account, body) => { return {error: "Unknown badge."}; } badges.set(account, body.badges); - accounts.save(); + accounts.save(account); world.checkNameTag(account); return {ok: account.badges.length ? `${name} has: ${badges.of(account).map(badge => badge.label).join(", ")}.` : `${name} has no badges.`}; } @@ -443,7 +443,7 @@ const act = (account, body) => { return {error: "Not a Discord username."}; } account.discord = discord; - accounts.save(); + accounts.save(account); return {ok: discord ? `Discord set to "${discord}".` : "Discord cleared."}; } // their bio, which they otherwise write on the account page; empty @@ -454,7 +454,7 @@ const act = (account, body) => { return {error: `At most ${badges.MAX_BIO} characters on ${badges.MAX_BIO_LINES} lines.`}; } account.bio = bio; - accounts.save(); + accounts.save(account); return {ok: bio ? "Bio set." : "Bio cleared."}; } case "ban": { @@ -496,7 +496,7 @@ const act = (account, body) => { delete account.moderator; modpanel.forget(account); } - accounts.save(); + accounts.save(account); return {ok: body.on === true ? `${name} is a moderator.${totp.isOn(account) ? "" : " They have no 2FA yet and cannot sign in to the moderator panel until they turn it on."}` : `${name} is no longer a moderator.`}; diff --git a/lib/parties.js b/lib/parties.js index 819359e..bfeee9a 100644 --- a/lib/parties.js +++ b/lib/parties.js @@ -187,7 +187,7 @@ module.exports = ({enterRoom, findOnline, blocks}) => { return; } account.currency.diamonds -= HOST_COST; - accounts.save(); + accounts.save(account); const party = {host: account, defId: def.id, name: `${name1}|${name2}|${name3}`, music: Number(music), endsAt: now() + config.partyMinutes * 60}; hosted.set(account.username.toLowerCase(), party); console.log(`[world] ${account.username} is hosting a party (def ${def.id})`); diff --git a/lib/pets.js b/lib/pets.js index 6c5d2d8..0bc6465 100644 --- a/lib/pets.js +++ b/lib/pets.js @@ -100,7 +100,7 @@ module.exports = ({others, findOnline, petFields}) => { giveCharacter(pet); account.pets.push(pet); achievements.set(account, achievements.VARS.pets, Math.max(Number(account.userVars[achievements.VARS.pets]) || 0, account.pets.length)); - accounts.save(); + accounts.save(account); session.sendXt("pc", -1, 1, account.currency.gems || 0, 0, pet.inv, ...petFields(pet), 0); }; @@ -147,7 +147,7 @@ module.exports = ({others, findOnline, petFields}) => { return; } activeAvatar(account).petInvId = pet ? pet.inv : 0; - accounts.save(); + accounts.save(account); const messageRoom = session.room ? session.room.id : -1; session.sendXt("ps", messageRoom, 1, session.id, pet ? pet.inv : 0); if (session.pos) { @@ -171,7 +171,7 @@ module.exports = ({others, findOnline, petFields}) => { avatar.petInvId = 0; } } - accounts.save(); + accounts.save(account); session.sendXt("pd", -1, 1, pet.inv); }, @@ -187,7 +187,7 @@ module.exports = ({others, findOnline, petFields}) => { return; } pet.uBits = ((pet.uBits & ~MASTERY_BITS) | (item << 8)) >>> 0; - accounts.save(); + accounts.save(account); session.sendXt("pm", -1, 1, pet.inv, pet.lBits, pet.uBits, pet.eBits); if (pet.inv === activeAvatar(account).petInvId && session.pos) { for (const other of others(session)) { @@ -207,7 +207,7 @@ module.exports = ({others, findOnline, petFields}) => { return; } pet.name = `${name1}|${name2}`; - accounts.save(); + accounts.save(account); session.sendXt("pn", -1, 1, pet.inv, pet.name); }, @@ -224,7 +224,7 @@ module.exports = ({others, findOnline, petFields}) => { return; } pet.uBits = ((pet.uBits & 0xFFF) | (parts[0] << 12) | (parts[1] << 16) | (parts[2] << 20)) >>> 0; - accounts.save(); + accounts.save(account); session.sendXt("pi", -1, 1, account.currency.gems || 0, pet.inv, pet.lBits, pet.uBits, pet.eBits); if (session.pos) { for (const other of others(session)) { diff --git a/lib/trades.js b/lib/trades.js index a2e5c5e..d2af03e 100644 --- a/lib/trades.js +++ b/lib/trades.js @@ -267,7 +267,7 @@ module.exports = ({findOnline, blocks}) => { const offeredNames = toOwner.map(item => nameOf(item.type, item.entry)); note(requester.account, owner.account, wantedName, offeredNames); note(owner.account, requester.account, offeredNames, wantedName); - accounts.save(); + accounts.save(requester.account, owner.account); requester.sendXt("ti", -1, TRADE_PROGRESS, COMPLETED, 1, ...doneFields(wantedType, toRequester), offered.length, ...offered.flatMap(item => [doneType(item.type), item.entry.inv])); owner.sendXt("ti", -1, TRADE_PROGRESS, COMPLETED, toOwner.length, ...toOwner.flatMap(item => doneFields(item.type, item.entry)), @@ -308,7 +308,7 @@ module.exports = ({findOnline, blocks}) => { account.tradeList.push({type, inv}); } } - accounts.save(); + accounts.save(account); sendTradeList(session, account); }, @@ -388,7 +388,7 @@ module.exports = ({findOnline, blocks}) => { return "too_many"; } to.tradeOffers.push({id: to.nextInv++, fromId: from.id, at: Math.floor(Date.now() / 1000), wanted: want, offered: give}); - accounts.save(); + accounts.save(to); return null; }, @@ -406,7 +406,7 @@ module.exports = ({findOnline, blocks}) => { const given = from ? offer.offered.map(item => ({type: item.type, entry: find(from, item.type, item.inv)})) : []; if (accept && (!from || !wanted || given.some(item => !item.entry) || blocks.either(from, account) || !fits(from, [offer.wanted], given) || !fits(account, given, [offer.wanted]))) { - accounts.save(); + accounts.save(account); return "gone"; } if (accept) { @@ -419,7 +419,7 @@ module.exports = ({findOnline, blocks}) => { // whoever is in the game sees their inventories change. [account, from].forEach(refresh); } - accounts.save(); + accounts.save(account, from); return null; }, @@ -428,7 +428,7 @@ module.exports = ({findOnline, blocks}) => { const before = to ? to.tradeOffers.length : 0; if (to) { to.tradeOffers = to.tradeOffers.filter(offer => !(offer.id === Number(id) && offer.fromId === from.id)); - accounts.save(); + accounts.save(to); } return Boolean(to) && to.tradeOffers.length < before; }, diff --git a/lib/walls.js b/lib/walls.js index d0707fb..5d8ff34 100644 --- a/lib/walls.js +++ b/lib/walls.js @@ -177,7 +177,7 @@ module.exports = ({blocks}) => { owner.wall.posts.push(post); owner.wall.posts = owner.wall.posts.slice(-MAX_POSTS); owner.wall.modified = post.t; - accounts.save(); + accounts.save(owner); return {PUT: {s: true}, x: post.x, t: post.t, GET: getReply(owner, viewer)}; }; @@ -207,7 +207,7 @@ module.exports = ({blocks}) => { } wall.posts = wall.posts.filter(candidate => candidate !== post && candidate.pp !== post.x); wall.modified = Date.now(); - accounts.save(); + accounts.save(owner); return {s: true}; } case "GMB": // posts the owner chose to hide @@ -217,7 +217,7 @@ module.exports = ({blocks}) => { return {s: false}; } wall.hidden = [...new Set([...wall.hidden, body.x])].slice(-MAX_HIDDEN); - accounts.save(); + accounts.save(owner); return {s: true}; case "CLW": if (!mine) { @@ -226,7 +226,7 @@ module.exports = ({blocks}) => { wall.posts = []; wall.hidden = []; wall.modified = Date.now(); - accounts.save(); + accounts.save(owner); return {s: true}; case "RMP": // masterpieces taken off the wall; there are none case "ACK": // notifications marked as read; there are none @@ -264,7 +264,7 @@ module.exports = ({blocks}) => { && mayOpen(owner, session.account) === TOKEN_OK; if (allowed) { owner.wall.likes = [...likes(owner), session.account.id]; - accounts.save(); + accounts.save(owner); } session.sendXt("crp", -1, allowed ? 1 : 0); }, @@ -286,7 +286,7 @@ module.exports = ({blocks}) => { const valid = [NOBODY, BUDDIES, EVERYONE].includes(value); if (valid) { session.account.wall.privacy = value; - accounts.save(); + accounts.save(session.account); } session.sendXt("wss", -1, valid ? 1 : 0); }, @@ -324,7 +324,7 @@ module.exports = ({blocks}) => { } wall.params = merged; wall.modified = Date.now(); - accounts.save(); + accounts.save(session.account); session.sendXt("wps", -1, 1); }, @@ -333,7 +333,7 @@ module.exports = ({blocks}) => { const owner = accounts.byName(username); if (owner && owner !== session.account && mayOpen(owner, session.account) === TOKEN_OK) { owner.wall.visits++; - accounts.save(); + accounts.save(owner); } session.sendXt("wci", -1, owner ? 1 : 0); }, diff --git a/lib/world.js b/lib/world.js index 4c334dc..104cd89 100644 --- a/lib/world.js +++ b/lib/world.js @@ -299,7 +299,7 @@ const login = (session, msg) => { // before the session is findable, so that nothing is sent ahead of the // login reply. achievements.refresh(account); - accounts.save(); + accounts.save(account); online.set(key, session); console.log(`[world] ${account.username} logged in as sfs user ${session.id}`); session.sendXtJson(-1, {_cmd: "login", status: "1", params: loginParams(session)}); @@ -461,7 +461,7 @@ const makeColour = (account, rgbs, asker = null) => { } if (entry.created) { account.coloursMade = {day: today, count: made + 1}; - accounts.save(); + accounts.save(account); } for (const recipient of entry.created ? online.values() : asker ? [asker] : []) { recipient.sendXt("xcc", -1, 1, entry.id, ...entry.rgbs); @@ -497,7 +497,7 @@ const handlers = { .filter(Boolean).slice(0, MAX_FILTER_WORDS); session.account.filter = {level: [0, 1, 2].includes(Number(level)) ? Number(level) : 0, mode: Number(mode) === 1 ? 1 : 0, words: [...new Set(own)].join(","), chosen: true}; - accounts.save(); + accounts.save(session.account); sendFilter(session); }, @@ -769,7 +769,7 @@ const handlers = { vars[id] = 0; } const earned = achievements.award(account, id); - accounts.save(); + accounts.save(account); achievements.sendVar(session, id, earned); }, @@ -818,7 +818,7 @@ const handlers = { const ok = Boolean(thing) && TYPED_NAME.test(wanted) && !moderation.isSilenced(account); if (ok) { thing.name = wanted; - accounts.save(); + accounts.save(account); if (kind === "a" && thing === activeAvatar(account)) { sendAvatarData(session, session, thing.perUserAvId); } @@ -1004,7 +1004,7 @@ const leaveWorld = session => { countPlaytime(session); stats.session(session.account, (Date.now() - session.playStarted) / 1000); accounts.release(session.account); - accounts.save(); + accounts.save(session.account); buddies.announce(session.account); } }; @@ -1055,7 +1055,7 @@ const setTheme = (account, theme) => { return false; } account.theme = theme; - accounts.save(); + accounts.save(account); const session = findOnline(account.username); if (session) { reloadRoom(session); @@ -1163,7 +1163,7 @@ const setHideBlocked = (session, hide) => { } account.hideBlocked = hide; account.userVars[USER_VAR_HIDE_BLOCKED] = hide ? 1 : 0; - accounts.save(); + accounts.save(account); if (!hide) { for (const other of blocked) { sendAvatarCreate(session, other); @@ -1420,7 +1420,7 @@ module.exports = { return false; } account.blocked = block ? [...account.blocked, key] : account.blocked.filter(name => name !== key); - accounts.save(); + accounts.save(account); const session = findOnline(account.username); if (session) { (block ? onBlocked : onUnblocked)(session, target); @@ -1439,7 +1439,7 @@ module.exports = { return; } account.nameBarData &= ~0xFF00; - accounts.save(); + accounts.save(account); const session = findOnline(account.username); if (session && session.room) { // AvatarXtCommManager "an": [sfsUserId, nameBarData] @@ -1455,7 +1455,7 @@ module.exports = { const avatar = activeAvatar(account); avatar.questLevel = level; avatar.questXp = adventureModule.levelXp(level); - accounts.save(); + accounts.save(account); const session = findOnline(account.username); if (session && session.room) { for (const viewer of session.room.sessions) { @@ -1542,7 +1542,7 @@ module.exports = { dens.sendDenState(visitor, room, false); } } - accounts.save(); + accounts.save(...changed, maker); return true; }, diff --git a/lib/worlditems.js b/lib/worlditems.js index c61a744..a40e2f2 100644 --- a/lib/worlditems.js +++ b/lib/worlditems.js @@ -67,7 +67,7 @@ const handlers = { } // the client reads the bit straight from its copy of the user var. achievements.set(account, def.userVarRefId, ((Number(account.userVars[def.userVarRefId]) || 0) | (1 << Number(def.uvIndex))) >>> 0); - accounts.save(); + accounts.save(account); session.sendXt("wig", -1, 1, amount); }, };