diff --git a/config.js b/config.js index 882f37b..92b4408 100644 --- a/config.js +++ b/config.js @@ -155,7 +155,7 @@ module.exports = { // 49 and 50: its link to a colour picker. // 61: the Owner, Moderator and Verified name tag badges seen by everyone. // 62: "Custom pictures" in the settings menu, to hide players' item pictures. - buildVersion: env.AJ_BUILD_VERSION || "71", + buildVersion: env.AJ_BUILD_VERSION || "78", // what the official CDN is asked for; not ours to change. upstreamBuildVersion: "3", @@ -327,6 +327,14 @@ module.exports = { cacheMissesPerTenMinutes: 1500, // a first visit with an empty cache fetches several hundred files upstreamTimeoutMs: 20000, maxCacheBytes: Number(env.AJ_MAX_CACHE_MB || 8192) * 1024 * 1024, + // how often at most the packs with custom items in are made again after + // one changes (lib/defpacks.js clientPack); the tests use 0, made at once. + packRemakeMs: env.AJ_PACK_REMAKE_MS === undefined ? 15000 : Number(env.AJ_PACK_REMAKE_MS), + // masterpieces (lib/masterpieces.js): seconds an account must have had + // before it paints one (AJ_MASTERPIECE_MIN_AGE; the tests use 0), and new + // pictures an account may post in any 24 hours. + masterpieceMinAccountAge: env.AJ_MASTERPIECE_MIN_AGE === undefined ? 86400 : Number(env.AJ_MASTERPIECE_MIN_AGE), + masterpiecesPerDay: 10, maxConnections: 2000, maxHttpConnections: 2000, // open at once, in all maxHttpConnectionsPerAddress: 40, // the game opens a good many at once while it loads diff --git a/lib/accounts.js b/lib/accounts.js index 50d679d..48f7480 100644 --- a/lib/accounts.js +++ b/lib/accounts.js @@ -46,7 +46,7 @@ const SCHEMA = ` // fields stored in their own columns rather than in `state`. const COLUMN_FIELDS = ["id", "username", "salt", "hash", "createdAt"]; // how the game treats the account this session; set again at every login. -const SESSION_FIELDS = ["accountType", "isGuide", "isModerator", "membershipDays", "startInDen", "startRoom"]; +const SESSION_FIELDS = ["accountType", "isGuide", "guideRefused", "isModerator", "membershipDays", "startInDen", "startRoom"]; const DEFAULT_MEMBERSHIP_DAYS = 9999; const MAX_ACCOUNT_TYPE = 5; const MEMBERSHIP_TYPES = [0, 2]; // non-member, member @@ -78,7 +78,10 @@ const LOGIN_DAYS_KEPT = 90; // and none older than this: they hold addresses let db = null; const byKey = new Map(); // lowercased username -> account const byId = new Map(); -const savedState = new Map(); // account id -> state JSON as last written +// account id -> digest of the state JSON as last written: enough to tell a +// change, without a second copy of every account's state (~13 KB each). +const savedState = new Map(); +const stateDigest = json => crypto.createHash("sha256").update(json).digest("base64"); // A name somebody gave up (a rename, a deleted account) is nobody's to take // for a while: whoever had it is still known by it to the players they met. const NAME_HELD_DAYS = 30; @@ -299,13 +302,17 @@ const MAX_START_ROOM = 80; function setLoginChoices(account, choices) { const asked = config.loginChoices && choices && typeof choices === "object" ? choices : {}; const whole = (value, max) => Number.isInteger(value) && value >= 0 && value <= max; - // member or not, and guide or not, are the player's own choice on any - // server; the rest only with config.loginChoices: another account type - // shows to everyone. + // member or not is the player's own choice on any server; the rest only + // with config.loginChoices: another account type shows to everyone. const own = choices && typeof choices === "object" ? choices : {}; const type = MEMBERSHIP_TYPES.includes(own.accountType) ? own.accountType : asked.accountType; account.accountType = whole(type, MAX_ACCOUNT_TYPE) ? type : config.defaultAccountType; - account.isGuide = own.isGuide === true; + // guide (the name tag, the chat bubble, what others see) only for an + // account the admin made one (lib/panel.js); asked for without that, it is + // refused. a guide's own client may leave it off; the official launcher + // asks nothing, and a guide is one. + account.isGuide = account.guideGiven === true && (choices && typeof choices === "object" ? own.isGuide === true : true); + account.guideRefused = own.isGuide === true && account.guideGiven !== true; account.isModerator = asked.isModerator === true; account.membershipDays = whole(asked.membershipDays, MAX_MEMBERSHIP_DAYS) ? asked.membershipDays : DEFAULT_MEMBERSHIP_DAYS; // where to start is the player's to say on any server: their own den in @@ -331,7 +338,7 @@ const insert = (username, salt, hash, createdAt, state) => { const result = db.prepare("INSERT INTO accounts (username, username_key, password_salt, password_hash, created_at, state) VALUES (?, ?, ?, ?, ?, ?)") .run(username, username.toLowerCase(), salt, hash, createdAt, json); account.id = Number(result.lastInsertRowid); - savedState.set(account.id, json); + savedState.set(account.id, stateDigest(json)); remember(account); touch(account); return account; @@ -381,7 +388,7 @@ const load = () => { createdAt: row.created_at, ...JSON.parse(row.state), }); - savedState.set(account.id, row.state); + savedState.set(account.id, stateDigest(row.state)); remember(account); } importLegacyFile(); @@ -475,7 +482,7 @@ const release = account => { const changedState = (account, changed) => { const json = stateJson(account); - if (savedState.get(account.id) !== json) { + if (savedState.get(account.id) !== stateDigest(json)) { changed.push([account.id, json]); return true; } @@ -583,7 +590,7 @@ const flush = ({some = false} = {}) => { throw err; } for (const [id, json] of changed) { - savedState.set(id, json); + savedState.set(id, stateDigest(json)); } }; diff --git a/lib/amf3.js b/lib/amf3.js index 13b0cab..4c1c412 100644 --- a/lib/amf3.js +++ b/lib/amf3.js @@ -177,40 +177,89 @@ class Amf3Reader { // writer for what the reader above produces from a defPack: plain objects, // arrays (with or without string keys), strings, numbers, booleans and null. // nothing is written as a reference except repeated strings. +// written into one buffer that grows: a pack is megabytes of small values, +// and a Buffer for each of them took seconds for the item pack. class Amf3Writer { constructor() { - this.parts = []; + this.buf = Buffer.allocUnsafe(1 << 16); + this.pos = 0; this.strings = new Map(); } + room(bytes) { + if (this.pos + bytes > this.buf.length) { + let size = this.buf.length * 2; + while (size < this.pos + bytes) { + size *= 2; + } + const bigger = Buffer.allocUnsafe(size); + this.buf.copy(bigger, 0, 0, this.pos); + this.buf = bigger; + } + } + + byte(value) { + this.room(1); + this.buf[this.pos++] = value; + } + u29(value) { + this.room(4); + const {buf} = this; if (value < 0x80) { - this.parts.push(Buffer.from([value])); + buf[this.pos++] = value; } else if (value < 0x4000) { - this.parts.push(Buffer.from([(value >> 7) | 0x80, value & 0x7f])); + buf[this.pos++] = (value >> 7) | 0x80; + buf[this.pos++] = value & 0x7f; } else if (value < 0x200000) { - this.parts.push(Buffer.from([(value >> 14) | 0x80, ((value >> 7) & 0x7f) | 0x80, value & 0x7f])); + buf[this.pos++] = (value >> 14) | 0x80; + buf[this.pos++] = ((value >> 7) & 0x7f) | 0x80; + buf[this.pos++] = value & 0x7f; } else { - this.parts.push(Buffer.from([(value >> 22) | 0x80, ((value >> 15) & 0x7f) | 0x80, ((value >> 8) & 0x7f) | 0x80, value & 0xff])); + buf[this.pos++] = (value >> 22) | 0x80; + buf[this.pos++] = ((value >> 15) & 0x7f) | 0x80; + buf[this.pos++] = ((value >> 8) & 0x7f) | 0x80; + buf[this.pos++] = value & 0xff; } } string(text) { if (text === "") { this.u29(1); + return; } - else if (this.strings.has(text)) { - this.u29(this.strings.get(text) << 1); + const known = this.strings.get(text); + if (known !== undefined) { + this.u29(known << 1); + return; } - else { - this.strings.set(text, this.strings.size); - const bytes = Buffer.from(text, "utf8"); - this.u29((bytes.length << 1) | 1); - this.parts.push(bytes); + this.strings.set(text, this.strings.size); + // nearly every string of a pack is short and ASCII: its bytes are its + // characters, copied here rather than through Buffer's utf8 writer. + if (text.length < 64) { + let ascii = true; + for (let i = 0; i < text.length; i++) { + if (text.charCodeAt(i) > 0x7f) { + ascii = false; + break; + } + } + if (ascii) { + this.u29((text.length << 1) | 1); + this.room(text.length); + for (let i = 0; i < text.length; i++) { + this.buf[this.pos++] = text.charCodeAt(i); + } + return; + } } + const length = Buffer.byteLength(text, "utf8"); + this.u29((length << 1) | 1); + this.room(length); + this.pos += this.buf.write(text, this.pos, "utf8"); } pairs(entries) { @@ -222,45 +271,55 @@ class Amf3Writer { } value(value) { - if (value === undefined) { - this.parts.push(Buffer.from([0x00])); + // most values of a pack are strings: asked first + if (typeof value === "string") { + this.byte(0x06); + this.string(value); + } + else if (value === undefined) { + this.byte(0x00); } else if (value === null) { - this.parts.push(Buffer.from([0x01])); + this.byte(0x01); } else if (typeof value === "boolean") { - this.parts.push(Buffer.from([value ? 0x03 : 0x02])); + this.byte(value ? 0x03 : 0x02); } else if (typeof value === "number") { if (Number.isInteger(value) && value >= -0x10000000 && value < 0x10000000) { - this.parts.push(Buffer.from([0x04])); + this.byte(0x04); this.u29(value & 0x1fffffff); } else { - const bytes = Buffer.alloc(9); - bytes[0] = 0x05; - bytes.writeDoubleBE(value, 1); - this.parts.push(bytes); + this.byte(0x05); + this.room(8); + this.pos = this.buf.writeDoubleBE(value, this.pos); } } - else if (typeof value === "string") { - this.parts.push(Buffer.from([0x06])); - this.string(value); - } else if (Array.isArray(value)) { - this.parts.push(Buffer.from([0x09])); + this.byte(0x09); this.u29((value.length << 1) | 1); - // string keys set on the array beside its numbered elements. - this.pairs(Object.entries(value).filter(([key]) => !/^\d+$/.test(key))); + // string keys set on the array beside its numbered elements (an array + // with as many keys as elements has none). + const keys = Object.keys(value); + this.pairs(keys.length === value.length ? [] : keys.filter(key => !/^\d+$/.test(key)).map(key => [key, value[key]])); for (const element of value) { this.value(element); } } else if (typeof value === "object" && !Buffer.isBuffer(value)) { // A dynamic object of no class, with its traits written in place. - this.parts.push(Buffer.from([0x0a, 0x0b])); + this.byte(0x0a); + this.byte(0x0b); + this.string(""); + // its own keys in Object.keys order, without an array of entries for each object + for (const key in value) { + if (Object.hasOwn(value, key)) { + this.string(key); + this.value(value[key]); + } + } this.string(""); - this.pairs(Object.entries(value)); } else { throw new Error("Cannot write this value as AMF3"); @@ -273,6 +332,6 @@ module.exports = { encode: value => { const writer = new Amf3Writer(); writer.value(value); - return Buffer.concat(writer.parts); + return writer.buf.subarray(0, writer.pos); }, }; diff --git a/lib/customitems.js b/lib/customitems.js index c7b26fb..5f148b3 100644 --- a/lib/customitems.js +++ b/lib/customitems.js @@ -3,14 +3,15 @@ const {logText} = require("./logtext.js"); const accounts = require("./accounts.js"); const defpacks = require("./defpacks.js"); +const limits = require("./limits.js"); // items the staff make while the server runs, with no restart of the server // or of anybody's game. each is an existing item's art (the clothing piece // or den item it is made from) under an id, name, price and colours of its // own. the server's tables and the packs new games fetch have it at once // (defpacks.setCustomItem); games already playing are sent its def row -// ("xdi", which the patched client adds to its tables), and so is every game -// at login, in case it was started before the item was made. +// ("xdi", which the patched client adds to its tables), and so is a game at +// login if its packs may be older than the item (sendAll). // // an item can be taken out of the shop, renamed and repriced, or deleted: // then it goes from every inventory too (lib/itemcontrol.js). @@ -29,6 +30,21 @@ const CHUNK = 40; // def rows per "xdi" message const TYPE_CLOTHING = "1"; const OFFERED_SPECIAL_TYPES = ["0", "4"]; // den items that can be copied: plain, mannequin const CURRENCY_GEMS = "0"; +// the fields of a def row the patched client reads from "xdi" +// (ItemXtCommManager.addItemDef, DenXtCommManager.addDenItemDef): the rest of +// the base's row is only weight, ~600 bytes a row and 5000 rows at login. +const CLIENT_FIELDS = { + clothing: ["id", "layerDefId", "layerId", "type", "value", "titleStrId", "isGiftable", "membersOnly", "itemStatus", "enviroType", + "avatarUseFlags", "recycleValue", "currencyType", "defense", "attack", "attackMediaRefId", "combatType", "modifierType", + "modifierValue", "criticalHit", "combinedCost", "colors", "availabilityStartTime", "availabilityEndTime"], + den: ["id", "abbrName", "combinedCost", "cost", "currencyType", "enviroType", "flag", "gameDefId", "itemStatus", "layer", + "mannequinAvatarDefId", "mannequinCatId", "membersOnly", "nameStrId", "parentArrayId", "promoType", "recycleValue", + "scalePercent", "sortCat", "specialType", "typeCat", "availabilityStartTime", "availabilityEndTime", "ecoPower"], +}; +// how long a fetch of the packs is taken to describe a game of that address +// that logs in (sendAll). a game logs in minutes after it fetched them. +const PACKS_SEEN_MS = 2 * 3600000; +const MAX_PACKS_SEEN = 100000; // addresses const SCHEMA = `CREATE TABLE IF NOT EXISTS custom_items ( kind TEXT NOT NULL, id INTEGER NOT NULL, base INTEGER NOT NULL, name TEXT NOT NULL, @@ -38,12 +54,22 @@ const SCHEMA = `CREATE TABLE IF NOT EXISTS custom_items ( const items = new Map(KINDS.map(kind => [kind, new Map()])); // kind -> id -> entry let made = 0; // entries in the order they were made (`seq`), for lists let broadcast = () => {}; // (cmd, ...params) to every game playing; set by lib/world.js +// address key -> {at, clothing, den, names}: the version (defpacks +// customVersion) of the packs with custom items in that address last +// fetched, the oldest within PACKS_SEEN_MS (players behind one address +// fetch at different times). an entry's `changed` is the version of its last change. +const packsSeen = new Map(); const db = () => accounts.database(); const now = () => Math.floor(Date.now() / 1000); const baseDef = (kind, id) => (kind === "den" ? defpacks.denItemDef(id) : defpacks.itemDef(id)); +// the file name an uploaded music track is asked for by (lib/custommusic.js): +// a new upload is a new name, so no game plays an old copy from its cache. +const audioName = entry => `CustomMusic${entry.id}_${entry.audio}`; +let audioGone = () => {}; // (entry): an uploaded track's item was deleted; set by lib/custommusic.js + // the def row of an entry: its base's row with its own id, name, price and // colours, always on sale and marked new. const rowOf = entry => { @@ -59,12 +85,34 @@ const rowOf = entry => { combinedCost: "", }; if (entry.kind === "den") { + // a music track uploaded by staff (lib/custommusic.js): the game plays + // "audio/.mp3", which the server answers with its file + if (entry.audio) { + own.abbrName = audioName(entry); + } return {...base, ...own, nameStrId: String(STRING_BASE.den + entry.id - FIRST_ID), cost: String(entry.cost)}; } return {...base, ...own, titleStrId: String(STRING_BASE.clothing + entry.id - FIRST_ID), value: String(entry.cost), colors: entry.colors.join(","), isGiftable: "1"}; }; -const apply = entry => defpacks.setCustomItem(entry.kind, rowOf(entry), entry.name, entry.inShop); +const apply = entry => { + defpacks.setCustomItem(entry.kind, rowOf(entry), entry.name, entry.inShop); + entry.changed = defpacks.customVersion(); +}; + +// what "xdi" carries of an entry: the fields the client reads, its name and +// picture. clothing goes through the clients' pack transform, so the wear +// rules reach custom items too. +const clientRow = entry => { + const row = entry.kind === "clothing" ? defpacks.clientItemRow(rowOf(entry)) : rowOf(entry); + const out = {}; + for (const field of CLIENT_FIELDS[entry.kind]) { + if (row[field] !== undefined) { + out[field] = row[field]; + } + } + return {...out, name: entry.name, art: entry.artPending ? "" : entry.art || "", artMode: entry.artMode || "sticker"}; +}; // "xdi" [.., .., kind, json]: def rows with their names, as JSON. a field // may not hold "%" (lib/sfs.js drops it), so a "%" in a name is written as @@ -75,9 +123,7 @@ const messages = entries => { const out = []; for (const kind of KINDS) { // a picture waiting for a moderator is not shown yet - const rows = entries.filter(entry => entry.kind === kind && !entry.pending) - .map(entry => ({...(kind === "clothing" ? defpacks.clientItemRow(rowOf(entry)) : rowOf(entry)), name: entry.name, - art: entry.artPending ? "" : entry.art || "", artMode: entry.artMode || "sticker"})); + const rows = entries.filter(entry => entry.kind === kind && !entry.pending).map(clientRow); for (let at = 0; at < rows.length; at += CHUNK) { out.push([kind, xtRows(rows.slice(at, at + CHUNK))]); } @@ -119,6 +165,7 @@ const fromRow = row => ({ artMode: LOOKS.includes(row.art_mode) ? row.art_mode : "sticker", pending: row.pending === 1, artPending: row.art_pending === 1, + audio: /^[0-9a-f]{8,64}$/.test(row.audio || "") ? row.audio : null, }); // a player-made item (lib/playeritems.js) has the maker's account id; @@ -143,6 +190,10 @@ const addOwnerColumn = () => { if (!columns.includes("art_pending")) { db().exec("ALTER TABLE custom_items ADD COLUMN art_pending INTEGER"); } + // a music track uploaded by staff (lib/custommusic.js): its file's version + if (!columns.includes("audio")) { + db().exec("ALTER TABLE custom_items ADD COLUMN audio TEXT"); + } }; const load = () => { @@ -233,10 +284,12 @@ const create = (kind, fields, by, ownerId = null, pending = false) => { return values; } const id = Math.max(FIRST_ID - 1, ...items.get(kind).keys()) + 1; + // `fields.audio`: an uploaded music track's version (lib/custommusic.js) + const audio = kind === "den" && /^[0-9a-f]{8,64}$/.test(String(fields.audio || "")) ? String(fields.audio) : null; const entry = {kind, id, base: baseId, colors: [], ...values, createdBy: String(by), createdAt: now(), ownerId, artMode: "sticker", pending, artPending: false, - seq: made++}; - db().prepare("INSERT INTO custom_items (kind, id, base, name, cost, members, colors, in_shop, created_by, created_at, owner_id, pending) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") - .run(kind, id, baseId, entry.name, entry.cost, entry.members ? 1 : 0, entry.colors.join(","), entry.inShop ? 1 : 0, entry.createdBy, entry.createdAt, ownerId, pending ? 1 : 0); + audio, seq: made++}; + db().prepare("INSERT INTO custom_items (kind, id, base, name, cost, members, colors, in_shop, created_by, created_at, owner_id, pending, audio) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") + .run(kind, id, baseId, entry.name, entry.cost, entry.members ? 1 : 0, entry.colors.join(","), entry.inShop ? 1 : 0, entry.createdBy, entry.createdAt, ownerId, pending ? 1 : 0, audio); items.get(kind).set(id, entry); if (!pending) { apply(entry); @@ -281,6 +334,7 @@ const setArtMode = (kind, id, mode) => { } entry.artMode = mode; db().prepare("UPDATE custom_items SET art_mode = ? WHERE kind = ? AND id = ?").run(mode, kind, entry.id); + entry.changed = defpacks.customChanged(); announce(entry); return {item: entry}; }; @@ -295,6 +349,7 @@ const setArt = (kind, id, art, pending = false) => { entry.art = art || null; entry.artPending = Boolean(art && pending); db().prepare("UPDATE custom_items SET art = ?, art_pending = ? WHERE kind = ? AND id = ?").run(entry.art, entry.artPending ? 1 : 0, kind, entry.id); + entry.changed = defpacks.customChanged(); announce(entry); }; @@ -324,6 +379,9 @@ const remove = (kind, id) => { db().prepare("DELETE FROM custom_items WHERE kind = ? AND id = ?").run(kind, entry.id); items.get(kind).delete(entry.id); defpacks.unsetCustomItem(kind, entry.id); + if (entry.audio) { + audioGone(entry); + } return {item: entry}; }; @@ -333,6 +391,10 @@ const list = () => KINDS.flatMap(kind => [...items.get(kind).values()]) module.exports = { KINDS, FIRST_ID, + audioName, + onAudioGone: callback => { + audioGone = callback; + }, load, create, update, @@ -362,9 +424,36 @@ module.exports = { const def = baseDef(entry.kind, entry.base); return def ? defpacks.string(entry.kind === "den" ? def.nameStrId : def.titleStrId) || String(def.abbrName || entry.base) : String(entry.base); }, - // every custom item, for a game logging in. + // an address was sent a pack with custom items in: "clothing", "den" or + // "names" (lib/defpacks.js customPackKind), made at `version`. + sawPack(key, packKind, version) { + const time = Date.now(); + const seen = packsSeen.get(key); + if (seen && time - seen.at < PACKS_SEEN_MS) { + if (seen[packKind] === undefined) { + seen[packKind] = version; + } + return; + } + packsSeen.delete(key); + packsSeen.set(key, {at: time, [packKind]: version}); + // the oldest are first + for (const [oldKey, old] of packsSeen) { + if (packsSeen.size <= MAX_PACKS_SEEN && time - old.at < PACKS_SEEN_MS) { + break; + } + packsSeen.delete(oldKey); + } + }, + // the custom items a game logging in may not have: those changed since its + // address fetched the packs, and those with a picture (which the packs do + // not carry); every one when the address fetched none lately. sendAll(session) { - for (const params of messages(list().reverse().filter(entry => !entry.pending))) { + const seen = packsSeen.get(limits.key(session.address)); + const fresh = seen && Date.now() - seen.at < PACKS_SEEN_MS && seen.names !== undefined ? seen : null; + const since = kind => (fresh && fresh[kind] !== undefined ? Math.min(fresh[kind], fresh.names) : -1); + const needed = entry => !entry.pending && (entry.changed > since(entry.kind) || (entry.art && !entry.artPending)); + for (const params of messages(list().reverse().filter(needed))) { session.sendXt("xdi", -1, ...params); } }, diff --git a/lib/custommusic.js b/lib/custommusic.js new file mode 100644 index 0000000..69bf1c2 --- /dev/null +++ b/lib/custommusic.js @@ -0,0 +1,174 @@ +"use strict"; + +const crypto = require("crypto"); +const fs = require("fs"); +const path = require("path"); +const {logText} = require("./logtext.js"); +const cdn = require("./cdn.js"); +const config = require("../config.js"); +const customItems = require("./customitems.js"); +const extraMusic = require("./extramusic.js"); + +// music tracks staff upload in the panels (permission "music.upload", +// lib/roles.js): an MP3 made a den music item while the server runs, as the +// extra tracks of lib/extramusic.js are. each is a custom den item +// (lib/customitems.js) made from the game's "Jam On", with an `audio` +// version of its own: games are sent its def row at once, players get it +// from the item finder (or the shop, if staff put it on sale), and deleting +// it takes it from everyone, as with any custom item. +// +// a den music item plays "audio/.mp3" from the content server +// (DenItemHolder); for these the server answers with the file uploaded, kept +// in data/custom-music/-.mp3 (the data folder is the one +// place the server writes to). + +const MAX_BYTES = 15 * 1024 * 1024; +const MIN_BYTES = 4 * 1024; +const VERSION_LENGTH = 12; +const FOLDER = () => path.join(config.dataDir, "custom-music"); +const fileOf = entry => path.join(FOLDER(), `${entry.id}-${entry.audio}.mp3`); + +// MPEG audio layer III, as the game's Flash plays it: an ID3v2 tag may come +// first; then a frame header whose next frame is where its length says. +const BITRATES = { + 1: [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320], + 2: [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160], +}; +const SAMPLE_RATES = {3: [44100, 48000, 32000], 2: [22050, 24000, 16000], 0: [11025, 12000, 8000]}; + +// the frame at `at`: {length, sampleRate} or null. +const frameAt = (data, at) => { + if (at + 4 > data.length || data[at] !== 0xFF || (data[at + 1] & 0xE0) !== 0xE0) { + return null; + } + const version = (data[at + 1] >> 3) & 3; // 3 MPEG-1, 2 MPEG-2, 0 MPEG-2.5 + const layer = (data[at + 1] >> 1) & 3; // 1 is layer III + const bitrateIndex = data[at + 2] >> 4; + const rateIndex = (data[at + 2] >> 2) & 3; + const padding = (data[at + 2] >> 1) & 1; + if (version === 1 || layer !== 1 || bitrateIndex === 0 || bitrateIndex === 15 || rateIndex === 3) { + return null; + } + const bitrate = BITRATES[version === 3 ? 1 : 2][bitrateIndex] * 1000; + const sampleRate = SAMPLE_RATES[version][rateIndex]; + return {length: Math.floor(((version === 3 ? 144 : 72) * bitrate) / sampleRate) + padding, sampleRate}; +}; + +// null for an MP3 the game can play, or what is wrong with it. +const problem = data => { + if (!Buffer.isBuffer(data) || data.length < MIN_BYTES) { + return "That file is too small to be a song."; + } + if (data.length > MAX_BYTES) { + return `That file is too big: ${MAX_BYTES / 1024 / 1024} MB at most.`; + } + let at = 0; + if (data.toString("latin1", 0, 3) === "ID3" && data.length > 10) { + at = 10 + (((data[6] & 0x7F) << 21) | ((data[7] & 0x7F) << 14) | ((data[8] & 0x7F) << 7) | (data[9] & 0x7F)) + (data[5] & 0x10 ? 10 : 0); + } + // the first frame: the header, then two more where their lengths say + for (const end = Math.min(data.length, at + 64 * 1024); at < end; at++) { + const first = frameAt(data, at); + const second = first && frameAt(data, at + first.length); + const third = second && frameAt(data, at + first.length + second.length); + if (third) { + return null; + } + } + return "That is not an MP3 file (MPEG audio layer III)."; +}; + +const tracks = () => customItems.list().filter(entry => entry.kind === "den" && entry.audio); + +// content path ("1830/audio/") -> entry, made again when the tracks change. +let paths = null; +const pathMap = () => { + if (!paths) { + paths = new Map(tracks().map(entry => [cdn.hashedPath(`audio/${customItems.audioName(entry)}.mp3`), entry])); + } + return paths; +}; + +// what to answer a content request with when it is for one of these +// tracks: {body, type}; undefined when it is not. +const asset = async relPath => { + const entry = pathMap().get(relPath); + if (!entry) { + return undefined; + } + const body = await fs.promises.readFile(fileOf(entry)).catch(() => null); + return {body, type: "audio/mpeg"}; +}; + +// an upload made a track: {item} or {error}. `fields`: {name, cost, inShop, members}. +const add = (data, fields, by) => { + const wrong = problem(data); + if (wrong) { + return {error: wrong}; + } + const audio = crypto.createHash("sha256").update(data).digest("hex").slice(0, VERSION_LENGTH); + fs.mkdirSync(FOLDER(), {recursive: true, mode: 0o700}); + // written before the item exists, so no game asks for a file not there yet + const waiting = path.join(FOLDER(), `new-${audio}-${process.pid}.mp3`); + fs.writeFileSync(waiting, data, {mode: 0o600}); + const result = customItems.create("den", {name: fields.name, cost: fields.cost, members: fields.members, + inShop: fields.inShop === true, base: extraMusic.TEMPLATE, audio}, by); + if (result.error) { + fs.rmSync(waiting, {force: true}); + return result; + } + fs.renameSync(waiting, fileOf(result.item)); + paths = null; + console.log(`[custom music] ${logText(by)} uploaded "${logText(result.item.name)}" (den item ${logText(result.item.id)}, ${logText(data.length)} bytes)`); + return result; +}; + +// the file of a track, for staff to listen to before or after: {data} or null. +const file = id => { + const entry = tracks().find(track => track.id === Number(id)); + if (!entry) { + return null; + } + const data = (() => { + try { + return fs.readFileSync(fileOf(entry)); + } + catch (err) { + return null; + } + })(); + return data ? {data, entry} : null; +}; + +// for the panels: newest first +const list = () => tracks().map(entry => ({id: entry.id, name: entry.name, cost: entry.cost, inShop: entry.inShop, + createdBy: entry.createdBy, createdAt: entry.createdAt, + bytes: (() => { + try { + return fs.statSync(fileOf(entry)).size; + } + catch (err) { + return 0; + } + })()})); + +// a track's item was deleted (lib/itemcontrol.js deleteCustom): its file goes too. +customItems.onAudioGone(entry => { + fs.rmSync(fileOf(entry), {force: true}); + paths = null; +}); + +// at start: files of tracks no longer there (and of uploads cut short) go. +const load = () => { + const kept = new Set(tracks().map(entry => path.basename(fileOf(entry)))); + let gone = 0; + for (const name of fs.existsSync(FOLDER()) ? fs.readdirSync(FOLDER()) : []) { + if (!kept.has(name)) { + fs.rmSync(path.join(FOLDER(), name), {force: true}); + gone++; + } + } + console.log(`[custom music] ${logText(kept.size)} uploaded tracks${gone ? `, ${logText(gone)} stray files removed` : ""}`); +}; + +module.exports = {MAX_BYTES, problem, asset, add, file, list, load}; diff --git a/lib/defpacks.js b/lib/defpacks.js index 7107186..4ff8cf6 100644 --- a/lib/defpacks.js +++ b/lib/defpacks.js @@ -370,24 +370,51 @@ const CLIENT_PACKS = new Map([ [DEF_NAMEBAR_BADGE, () => {}], ]); const clientPackPaths = new Map([...CLIENT_PACKS.keys()].map(id => [cdn.hashedPath(`defPacks/${id}`), id])); -const clientPackBodies = new Map(); // id -> {day, allOnSale, body} +const clientPackBodies = new Map(); // id -> {day, allOnSale, body, version, madeAt} +const clientPacksChanged = new Set(); // ids whose body is older than a custom item change +const clientPacksMaking = new Map(); // id -> the promise of the body being made +// a pack is made again after a custom item change at most every +// config.packRemakeMs: the item pack takes ~70 ms of the one thread at 5000 +// items (~300 ms at 50000), and players make items many times a minute. +// until then the body made before goes out, with the version it has; a +// game logging in is sent what changed after it (lib/customitems.js sendAll). +const deflateRaw = require("util").promisify(zlib.deflateRaw); +// the packs custom items are in: their rows, and their names +const CUSTOM_PACK_KINDS = new Map([[DEF_ITEM, "clothing"], [DEF_DEN_ITEM, "den"], [DEF_LOCALIZATION_EN, "names"]]); -// the pack at `relPath` as clients should get it, or null when it is not -// one of the above (and comes straight from the CDN). +const makeClientPack = async id => { + // the version before the rows are read: a change while it is made counts + // as after, and is sent at login (more than needed, never less) + const version = customVersion; + const pack = await loadChanged(id); + CLIENT_PACKS.get(id)(pack); + // deflated on libuv's threads, not this one + const body = await deflateRaw(amf3.encode(pack)); + const made = {day: sales.today(), allOnSale: sales.everythingOnSale(), body, version, madeAt: Date.now()}; + clientPackBodies.set(id, made); + if (customVersion === version) { + clientPacksChanged.delete(id); + } + return made; +}; + +// the pack at `relPath` as clients should get it, {body, version}, or null +// when it is not one of the above (and comes straight from the CDN). +// `version`: customVersion() of the custom items in it. const clientPack = async relPath => { const id = clientPackPaths.get(relPath); if (id === undefined) { return null; } const made = clientPackBodies.get(id); - if (made && made.day === sales.today() && made.allOnSale === sales.everythingOnSale()) { - return made.body; + if (made && made.day === sales.today() && made.allOnSale === sales.everythingOnSale() + && (!clientPacksChanged.has(id) || Date.now() - made.madeAt < config.packRemakeMs)) { + return made; } - const pack = await loadChanged(id); - CLIENT_PACKS.get(id)(pack); - const body = zlib.deflateRawSync(amf3.encode(pack)); - clientPackBodies.set(id, {day: sales.today(), allOnSale: sales.everythingOnSale(), body}); - return body; + if (!clientPacksMaking.has(id)) { + clientPacksMaking.set(id, makeClientPack(id).finally(() => clientPacksMaking.delete(id))); + } + return clientPacksMaking.get(id); }; // most packs are tables: an object of rows keyed by row index. @@ -535,8 +562,8 @@ const setCustomItem = (kind, row, name, inShop = true) => { clothingIds = clothingIds.includes(id) ? clothingIds : [...clothingIds, id]; } customInShop[kind][inShop ? "add" : "delete"](id); - clientPackBodies.delete(pack); - clientPackBodies.delete(DEF_LOCALIZATION_EN); + clientPacksChanged.add(pack); + clientPacksChanged.add(DEF_LOCALIZATION_EN); customVersion++; }; @@ -558,7 +585,7 @@ const unsetCustomItem = (kind, id) => { clothingIds = clothingIds.filter(one => one !== number); } customInShop[kind].delete(number); - clientPackBodies.delete(pack); + clientPacksChanged.add(pack); customVersion++; }; @@ -569,6 +596,13 @@ module.exports = { // the custom items on sale, the newest first. customShopIds: kind => [...customInShop[kind]].reverse(), customVersion: () => customVersion, + // a change of a custom item that is not in the packs (its picture): one + // more version, for a game logging in to be told of it + customChanged: () => ++customVersion, + // the avatarUseFlags every clothing row has in the clients' pack (any animal) + EVERY_AVATAR, + // "clothing", "den" or "names" for a pack path with custom items in (lib/customitems.js sawPack), else null. + customPackKind: relPath => CUSTOM_PACK_KINDS.get(clientPackPaths.get(relPath)) || null, load, itemDef: id => itemDefs.get(Number(id)), clothingIds: () => clothingIds, diff --git a/lib/http.js b/lib/http.js index b9a983c..7e9ab4f 100644 --- a/lib/http.js +++ b/lib/http.js @@ -24,6 +24,8 @@ const manage = require("./manage.js"); const modpanel = require("./modpanel.js"); const badges = require("./badges.js"); const extraMusic = require("./extramusic.js"); +const customMusic = require("./custommusic.js"); +const itemControl = require("./itemcontrol.js"); const masterpieces = require("./masterpieces.js"); const panel = require("./panel.js"); const totp = require("./totp.js"); @@ -230,6 +232,75 @@ const itemArtUpload = async (req, res, url, from, signedIn = null, adminSignedIn send(res, 200, result); }; +// music tracks uploaded in the panels (lib/custommusic.js), at +// /mod/api/music (staff with "music.upload") and /admin/api/music: +// GET: the tracks; GET ?play=: one's file, to listen to; +// POST ?name=&cost=&inShop=1, the MP3 as the body: a new track; +// POST ?delete=: the track deleted, and taken from every player. +// `signedIn` (req) -> account: the moderator panel's session; +// `adminSignedIn` (req) -> boolean: the admin panel's, written down as "admin". +const musicUploads = limits.limiter(30, 3600000); // tracks an hour, per staff member +const musicRoute = async (req, res, url, from, signedIn = null, adminSignedIn = null) => { + res.setHeader("Cache-Control", "no-store"); + const asAdmin = Boolean(adminSignedIn && adminSignedIn(req)); + const account = asAdmin ? null : signedIn ? signedIn(req) : null; + if (!asAdmin && !(account && roles.can(account, "music.upload"))) { + send(res, 403, {error: "Your role cannot upload music."}); + return; + } + const by = asAdmin ? "admin" : account.username; + if (req.method === "GET" || req.method === "HEAD") { + if (url.searchParams.has("play")) { + const track = customMusic.file(url.searchParams.get("play")); + if (!track) { + send(res, 404, "Not found", "text/plain"); + return; + } + res.setHeader("Content-Security-Policy", "default-src 'none'; sandbox"); + res.setHeader("X-Content-Type-Options", "nosniff"); + send(res, 200, track.data, "audio/mpeg"); + return; + } + send(res, 200, {tracks: customMusic.list(), maxBytes: customMusic.MAX_BYTES}); + return; + } + if (req.method !== "POST" || !limits.originAllowed(req)) { + send(res, 403, {error: "forbidden"}); + return; + } + if (url.searchParams.has("delete")) { + const entry = customItems.get("den", url.searchParams.get("delete")); + if (!entry || !entry.audio) { + send(res, 404, {error: "No such uploaded track."}); + return; + } + const result = itemControl.deleteCustom("den", entry.id, world); + if (result.ok) { + modlog.record(by, "music deleted", "", `den ${entry.id} ${entry.name}`); + } + send(res, result.ok ? 200 : 400, result); + return; + } + if (!asAdmin && !musicUploads.allow(account.id)) { + send(res, 429, {error: "Too many uploads. Try again later."}); + return; + } + const data = await readBody(req, customMusic.MAX_BYTES + 1).catch(() => null); + if (!data) { + send(res, 413, {error: `That file is too big: ${customMusic.MAX_BYTES / 1024 / 1024} MB at most.`}); + return; + } + const result = customMusic.add(data, {name: url.searchParams.get("name"), cost: url.searchParams.get("cost") || 0, + inShop: url.searchParams.get("inShop") === "1"}, by); + if (result.error) { + send(res, 400, result); + return; + } + modlog.record(by, "music uploaded", "", `den ${result.item.id} ${result.item.name}`); + send(res, 200, {ok: `"${result.item.name}" is den item ${result.item.id}. Players can get it from the item finder now${result.item.inShop ? ", and the shop" : ""}.`, + item: {id: result.item.id, name: result.item.name}}); +}; + // a picture still waiting for a moderator, which /itemart/ does not serve: // to its maker (the account page) and to staff who review or make items. const itemArtView = (req, res, url, signedIn, adminSignedIn) => { @@ -361,6 +432,7 @@ const CAPTCHA_PAGE_HEADERS = {...PAGE_HEADERS, "Content-Security-Policy": captch // write, from config.js. const LEGAL_VALUES = {legalEmail: config.legalEmail, adminEmail: config.adminEmail, operator: config.operator, hostedIn: config.hostedIn, version: config.termsVersion, site: new URL(config.siteUrl).host}; +const MAX_RULES_LENGTH = 65536; // characters of data/rules.txt the game is sent const escapeHtml = text => String(text).replace(/[&<>"]/g, sign => ({"&": "&", "<": "<", ">": ">", "\"": """})[sign]); const sendLegalPage = async (res, file) => { for (const [name, value] of Object.entries(PAGE_HEADERS)) { @@ -731,6 +803,10 @@ const handle = async (req, res) => { await itemArtUpload(req, res, url, from, null, panel.signedIn); return; } + if (url.pathname === "/admin/api/music") { + await musicRoute(req, res, url, from, null, panel.signedIn); + return; + } await panel.handle(req, res, url, from, {send, readJson}); return; } @@ -757,6 +833,10 @@ const handle = async (req, res) => { await itemArtUpload(req, res, url, from, modpanel.signedInAs); return; } + if (isMod && url.pathname === "/mod/api/music") { + await musicRoute(req, res, url, from, modpanel.signedInAs); + return; + } if (isMod) { await modpanel.handle(req, res, url, from, {send, readJson}); return; @@ -981,13 +1061,38 @@ const handle = async (req, res) => { return; } const parts = formParts(req.headers["content-type"], await readBody(req, masterpieces.MAX_IMAGE_BYTES + 16 * 1024)); - const uuid = await masterpieces.upload(String(parts.get("authToken") || ""), parts.get("image")); + const {uuid, refusal, maker} = await masterpieces.upload(String(parts.get("authToken") || ""), parts.get("image")); + // the game only says it could not be sent: HQ says why + if (refusal && maker) { + world.tellAccount(maker, refusal); + } send(res, 200, uuid ? {success: true, uuid} : {success: false}); return; } case "GET /crossdomain.xml": send(res, 200, CROSS_DOMAIN, "text/xml"); return; + // the game's rules window (AJClient.showRules) shows this file, read + // again on every ask so it can be changed on a running server; made + // with a placeholder when there is none. + case "GET /rules.txt": { + const file = path.join(config.dataDir, "rules.txt"); + let text; + try { + text = await fs.promises.readFile(file, "utf8"); + } + catch (err) { + if (err.code !== "ENOENT") { + throw err; + } + text = "Example\n"; + await fs.promises.writeFile(file, text, {flag: "wx"}).catch(() => {}); + } + res.setHeader("Cache-Control", "no-cache"); + res.setHeader("X-Content-Type-Options", "nosniff"); + send(res, 200, text.slice(0, MAX_RULES_LENGTH), "text/plain; charset=utf-8"); + return; + } case "GET /gameClient/game/index.html": // not to be put in a frame of anyone's; the client shows it in a view of its own. res.setHeader("X-Frame-Options", "DENY"); @@ -1043,15 +1148,21 @@ const handle = async (req, res) => { send(res, body ? 200 : 404, body || "Not found", body ? MIME[".swf"] : "text/plain"); return; } - // the extra den music tracks, which the CDN does not have as files. - const extra = await extraMusic.asset(relPath); + // the extra den music tracks, which the CDN does not have as files, and + // those uploaded by staff. + const extra = await extraMusic.asset(relPath) || await customMusic.asset(relPath); if (extra) { send(res, extra.body ? 200 : 404, extra.body || "Not found", extra.body ? extra.type : "text/plain"); return; } const clientPack = await defpacks.clientPack(relPath); if (clientPack) { - send(res, 200, clientPack, "application/octet-stream"); + // what custom items this game has from its packs (customItems.sendAll) + const packKind = defpacks.customPackKind(relPath); + if (packKind) { + customItems.sawPack(from.key, packKind, clientPack.version); + } + send(res, 200, clientPack.body, "application/octet-stream"); return; } // A room file in a seasonal theme ("/roomDefs/theme_ { + const minCode = new Int32Array(17); + const maxCode = new Int32Array(18).fill(-1); + const valPtr = new Int32Array(17); + let code = 0; + let k = 0; + for (let length = 1; length <= 16; length++) { + valPtr[length] = k; + minCode[length] = code; + code += counts[length - 1]; + k += counts[length - 1]; + maxCode[length] = counts[length - 1] ? code - 1 : -1; + code <<= 1; + } + maxCode[17] = 0x7FFFFFFF; + return {minCode, maxCode, valPtr, values}; +}; + +// the fingerprint of a JPEG, or null. +const fingerprint = buf => { + try { + return read(buf); + } + catch (err) { + return null; + } +}; + +const read = buf => { + const tables = {}; + const quant = {}; + let frame = null; + let scan = null; + let at = 2; + while (!scan) { + if (at + 4 > buf.length || buf[at] !== 0xFF) { + return null; + } + const marker = buf[at + 1]; + const length = buf.readUInt16BE(at + 2); + const body = buf.subarray(at + 4, at + 2 + length); + if (marker === 0xDB) { + for (let p = 0; p + 65 <= body.length; p += 65) { + if (body[p] >> 4) { + return null; // 16-bit tables: not the Art Studio's + } + quant[body[p] & 15] = body[p + 1]; + } + } + else if (marker === 0xC4) { + for (let p = 0; p + 17 <= body.length;) { + const counts = body.subarray(p + 1, p + 17); + const total = counts.reduce((sum, count) => sum + count, 0); + tables[(body[p] >> 4 ? "ac" : "dc") + (body[p] & 15)] = huffmanTable(counts, body.subarray(p + 17, p + 17 + total)); + p += 17 + total; + } + } + else if (marker === 0xC0 || marker === 0xC1) { + const comps = body[5]; + frame = {height: body.readUInt16BE(1), width: body.readUInt16BE(3), comps: []}; + for (let c = 0; c < comps; c++) { + if (body[7 + c * 3] !== 0x11) { + return null; // subsampled + } + frame.comps.push(body[8 + c * 3]); + } + } + else if (marker === 0xDD || (marker >= 0xC2 && marker <= 0xCF && marker !== 0xC4 && marker !== 0xC8 && marker !== 0xCC)) { + return null; // restart markers, progressive, lossless or arithmetic + } + else if (marker === 0xDA) { + scan = []; + for (let c = 0; c < body[0]; c++) { + scan.push({dc: tables[`dc${body[2 + c * 2] >> 4}`], ac: tables[`ac${body[2 + c * 2] & 15}`]}); + } + } + at += 2 + length; + } + if (!frame || frame.width < 1 || frame.height < 1 || scan.length !== frame.comps.length || scan.length < 1 || scan.length > 3 + || scan.some(table => !table.dc || !table.ac) || frame.comps.some(id => quant[id] === undefined)) { + return null; + } + + // the entropy-coded data, its 0xFF bytes followed by a stuffed 0x00 + let pos = at; + let bits = 0; + let left = 0; + const bit = () => { + if (left === 0) { + if (pos >= buf.length) { + throw new Error("ran out"); + } + bits = buf[pos++]; + if (bits === 0xFF) { + if (buf[pos] !== 0) { + throw new Error("marker in the data"); + } + pos++; + } + left = 8; + } + left--; + return (bits >> left) & 1; + }; + const decode = table => { + let code = bit(); + let length = 1; + while (code > table.maxCode[length]) { + code = (code << 1) | bit(); + if (++length > 16) { + throw new Error("bad code"); + } + } + return table.values[table.valPtr[length] + code - table.minCode[length]]; + }; + const receive = size => { + let value = 0; + for (let i = 0; i < size; i++) { + value = (value << 1) | bit(); + } + return value < 1 << (size - 1) ? value - (1 << size) + 1 : value; + }; + + const across = Math.ceil(frame.width / 8); + const down = Math.ceil(frame.height / 8); + const comps = scan.length; + const out = Buffer.alloc(HEADER + across * down * comps); + out.writeUInt16BE(frame.width, 0); + out.writeUInt16BE(frame.height, 2); + out[4] = comps; + const predicted = new Int32Array(comps); + for (let block = 0; block < across * down; block++) { + for (let c = 0; c < comps; c++) { + const size = decode(scan[c].dc); + predicted[c] += size ? receive(size) : 0; + // the DC coefficient is eight times the block's average, less 128 + const average = Math.round(predicted[c] * quant[frame.comps[c]] / 8 + 128); + out[HEADER + block * comps + c] = Math.max(0, Math.min(255, average)); + for (let k = 1; k < 64;) { + const runSize = decode(scan[c].ac); + if ((runSize & 15) === 0) { + if (runSize >> 4 !== 15) { + break; // end of block + } + k += 16; + continue; + } + k += runSize >> 4; + receive(runSize & 15); + k++; + } + } + } + return out; +}; + +// the first component (brightness) in THUMB by THUMB, to find candidates +// among many fingerprints before comparing whole ones. +const thumbnail = print => { + const across = Math.ceil(print.readUInt16BE(0) / 8); + const down = Math.ceil(print.readUInt16BE(2) / 8); + const comps = print[4]; + const sums = new Float64Array(THUMB * THUMB); + const counts = new Float64Array(THUMB * THUMB); + for (let y = 0; y < down; y++) { + for (let x = 0; x < across; x++) { + const cell = Math.floor(y * THUMB / down) * THUMB + Math.floor(x * THUMB / across); + sums[cell] += print[HEADER + (y * across + x) * comps]; + counts[cell]++; + } + } + return Buffer.from(sums.map((sum, cell) => (counts[cell] ? Math.round(sum / counts[cell]) : 0))); +}; + +// could these two be the same picture? (thumbnails: a cheap first look) +const thumbnailsClose = (one, other) => one.length === other.length && one.every((value, i) => Math.abs(value - other[i]) <= SAME_WITHIN); + +// the same picture: the same size, and no block further apart than a step or two. +const samePicture = (one, other) => { + if (!one || !other || one.length !== other.length || one.compare(other, 0, HEADER, 0, HEADER) !== 0) { + return false; + } + for (let i = HEADER; i < one.length; i++) { + if (Math.abs(one[i] - other[i]) > SAME_WITHIN) { + return false; + } + } + return true; +}; + +module.exports = {fingerprint, thumbnail, thumbnailsClose, samePicture}; diff --git a/lib/masterpieces.js b/lib/masterpieces.js index 1ad1a03..b7498ab 100644 --- a/lib/masterpieces.js +++ b/lib/masterpieces.js @@ -7,6 +7,7 @@ const config = require("../config.js"); const accounts = require("./accounts.js"); const limits = require("./limits.js"); const imageModeration = require("./image-moderation.js"); +const jpegPrint = require("./jpegprint.js"); // masterpieces: paintings from the Art Studio, framed and hung in a den // (client side: game.artStudioPainting.ArtStudioPainting, the same in @@ -45,6 +46,23 @@ const SCHEMA = ` likes TEXT NOT NULL, image BLOB NOT NULL ); + -- every picture taken in, for the daily limit (pictures thrown away + -- unframed count too) + CREATE TABLE IF NOT EXISTS masterpiece_uploads ( + creator_id INTEGER NOT NULL, + created_at INTEGER NOT NULL + ); + CREATE INDEX IF NOT EXISTS masterpiece_uploads_by ON masterpiece_uploads (creator_id, created_at); + -- pictures a moderator or the admin approved once: the same picture + -- again, the same file (SHA-256) or the same painting saved and painted + -- again (its fingerprint, lib/jpegprint.js), is approved as it is framed + CREATE TABLE IF NOT EXISTS masterpiece_approved_pictures ( + hash TEXT PRIMARY KEY, + approved_at INTEGER NOT NULL, + approved_by TEXT NOT NULL, + fingerprint BLOB, + thumb BLOB + ); `; const UPLOADED = 0; // posted, not made into an item yet @@ -79,19 +97,35 @@ const fileOf = id => path.join(folder(), `${id}.jpg`); const GALLERY_SPOTS = 12; // PartyManager.setupMasterpiecesInRoom const uploadsPerHour = limits.limiter(30, 3600000); +const DAY = 86400; +const AUTO_APPROVER = "auto"; // reviewedBy of a painting approved for a picture approved before const now = () => Math.floor(Date.now() / 1000); const db = () => accounts.database(); -const known = new Map(); // id -> {id, creator, createdAt, status, frame, reviewedAt, reviewedBy, likes: [account ids]} +const known = new Map(); // id -> {id, creator, createdAt, status, frame, reviewedAt, reviewedBy, likes: [account ids], hash} +const approvedPictures = new Map(); // hash -> thumbnail of its fingerprint, or null: masterpiece_approved_pictures const tokens = new Map(); // upload token -> {account id, expires (ms)} const load = () => { db().exec(SCHEMA); // who approved it: a moderator's username, "admin", or null when nobody // looked (review was off, or it was approved before this was kept). - if (!db().prepare("PRAGMA table_info(masterpieces)").all().some(column => column.name === "reviewed_by")) { + const columns = db().prepare("PRAGMA table_info(masterpieces)").all().map(column => column.name); + if (!columns.includes("reviewed_by")) { db().exec("ALTER TABLE masterpieces ADD COLUMN reviewed_by TEXT"); } + // the picture's SHA-256 (hashOf) and fingerprint (lib/jpegprint.js), to know it again + if (!columns.includes("hash")) { + db().exec("ALTER TABLE masterpieces ADD COLUMN hash TEXT"); + } + if (!columns.includes("fingerprint")) { + db().exec("ALTER TABLE masterpieces ADD COLUMN fingerprint BLOB"); + } + const pictureColumns = db().prepare("PRAGMA table_info(masterpiece_approved_pictures)").all().map(column => column.name); + if (!pictureColumns.includes("fingerprint")) { + db().exec("ALTER TABLE masterpiece_approved_pictures ADD COLUMN fingerprint BLOB"); + db().exec("ALTER TABLE masterpiece_approved_pictures ADD COLUMN thumb BLOB"); + } fs.mkdirSync(folder(), {recursive: true}); // pictures still in the table move out into files, once. for (const row of db().prepare("SELECT id FROM masterpieces WHERE length(image) > 0").all()) { @@ -99,16 +133,138 @@ const load = () => { fs.writeFileSync(fileOf(row.id), Buffer.from(picture)); db().prepare("UPDATE masterpieces SET image = ? WHERE id = ?").run(NO_IMAGE, row.id); } - for (const row of db().prepare("SELECT id, creator_id, created_at, status, frame, reviewed_at, reviewed_by, likes FROM masterpieces").all()) { + for (const row of db().prepare("SELECT id, creator_id, created_at, status, frame, reviewed_at, reviewed_by, likes, hash FROM masterpieces").all()) { known.set(row.id, {id: row.id, creator: row.creator_id, createdAt: row.created_at, status: row.status, frame: row.frame, - reviewedAt: row.reviewed_at, reviewedBy: row.reviewed_by, likes: JSON.parse(row.likes)}); + reviewedAt: row.reviewed_at, reviewedBy: row.reviewed_by, likes: JSON.parse(row.likes), hash: row.hash || null}); } + for (const row of db().prepare("SELECT hash, thumb FROM masterpiece_approved_pictures").all()) { + approvedPictures.set(row.hash, row.thumb ? Buffer.from(row.thumb) : null); + } + backfill(); +}; + +const hashOf = image => crypto.createHash("sha256").update(image).digest("hex"); + +const printOf = painting => { + const row = db().prepare("SELECT fingerprint FROM masterpieces WHERE id = ?").get(painting.id); + return row && row.fingerprint ? Buffer.from(row.fingerprint) : null; +}; + +// the approved pictures (their hashes) that this picture is: the same file, +// or the same painting by its fingerprint. +const approvedMatches = (hash, print) => { + const found = approvedPictures.has(hash) ? [hash] : []; + if (print) { + const thumb = jpegPrint.thumbnail(print); + for (const [other, otherThumb] of approvedPictures) { + if (other !== hash && otherThumb && jpegPrint.thumbnailsClose(thumb, otherThumb)) { + const row = db().prepare("SELECT fingerprint FROM masterpiece_approved_pictures WHERE hash = ?").get(other); + if (row && row.fingerprint && jpegPrint.samePicture(print, Buffer.from(row.fingerprint))) { + found.push(other); + } + } + } + } + return found; +}; + +const pictureApproved = (hash, print) => Boolean(hash) && approvedMatches(hash, print).length > 0; + +const notePictureApproved = (painting, print) => { + const thumb = print ? jpegPrint.thumbnail(print) : null; + db().prepare("INSERT OR IGNORE INTO masterpiece_approved_pictures (hash, approved_at, approved_by, fingerprint, thumb) VALUES (?, ?, ?, ?, ?)") + .run(painting.hash, painting.reviewedAt || now(), painting.reviewedBy, print, thumb); + if (!approvedPictures.has(painting.hash)) { + approvedPictures.set(painting.hash, thumb); + } +}; + +// paintings from before pictures were known again get their hash and +// fingerprint, a few at a time after start (each is a file read and some +// milliseconds of decoding, on the thread the game shares); those a person +// approved make their pictures approved ones. +const BACKFILL_BATCH = 20; +const backfill = () => { + const missing = db().prepare("SELECT id FROM masterpieces WHERE fingerprint IS NULL OR hash IS NULL").all().map(row => row.id); + const notedAgain = db().prepare("SELECT hash FROM masterpiece_approved_pictures WHERE fingerprint IS NULL").all().map(row => row.hash); + const step = () => { + for (const id of missing.splice(0, BACKFILL_BATCH)) { + const painting = known.get(id); + let image = null; + try { + image = painting ? fs.readFileSync(fileOf(id)) : null; + } + catch (err) { + image = null; + } + if (!image) { + continue; + } + painting.hash = hashOf(image); + const print = jpegPrint.fingerprint(image); + db().prepare("UPDATE masterpieces SET hash = ?, fingerprint = ? WHERE id = ?").run(painting.hash, print, id); + if (painting.status === APPROVED && painting.reviewedBy && painting.reviewedBy !== AUTO_APPROVER) { + notePictureApproved(painting, print); + } + } + // pictures approved before fingerprints were kept: from a painting of + // theirs, once every painting has its own + for (const hash of missing.length ? [] : notedAgain.splice(0, BACKFILL_BATCH)) { + const row = db().prepare("SELECT fingerprint FROM masterpieces WHERE hash = ? AND fingerprint IS NOT NULL").get(hash); + if (row) { + const print = Buffer.from(row.fingerprint); + const thumb = jpegPrint.thumbnail(print); + db().prepare("UPDATE masterpiece_approved_pictures SET fingerprint = ?, thumb = ? WHERE hash = ?").run(print, thumb, hash); + approvedPictures.set(hash, thumb); + } + } + if (missing.length || notedAgain.length) { + setTimeout(step, 200).unref(); + } + }; + if (missing.length || notedAgain.length) { + setTimeout(step, 1000).unref(); + } +}; + +// why this account may not paint for a frame now, as words for the player, +// or null when it may: too new an account (asked at the easel, step 1), or +// as many new pictures in the last 24 hours as a day allows (asked of a +// picture not approved before, step 2). `whenFree` speaks of the wait. +const whenFree = seconds => { + const hours = Math.ceil(seconds / 3600); + return hours > 1 ? `in ${hours} hours` : `in ${Math.max(1, Math.ceil(seconds / 60))} minutes`; +}; +const tooNew = account => { + const age = now() - (Number(account.createdAt) || 0); + if (age < config.masterpieceMinAccountAge) { + return `Accounts can make Masterpieces once they are a day old. Try again ${whenFree(config.masterpieceMinAccountAge - age)}.`; + } + return null; +}; +const tooMany = account => { + const recent = db().prepare("SELECT created_at FROM masterpiece_uploads WHERE creator_id = ? AND created_at > ? ORDER BY created_at") + .all(account.id, now() - DAY); + const perDay = config.masterpiecesPerDay; + if (recent.length >= perDay) { + return `You can make ${perDay} new Masterpieces a day. Try again ${whenFree(recent[recent.length - perDay].created_at + DAY - now())}.`; + } + return null; }; const needsReview = () => accounts.setting(REVIEW_SETTING) !== "0"; const setNeedsReview = on => accounts.setSetting(REVIEW_SETTING, on ? "1" : "0"); -const forget = id => { +// `unapprove`: it was taken back by staff, so its picture is no longer one +// approved before (a painting thrown away unframed leaves it alone). +const forget = (id, unapprove = false) => { + const painting = known.get(id); + if (unapprove && painting && painting.hash) { + for (const hash of approvedMatches(painting.hash, printOf(painting))) { + db().prepare("DELETE FROM masterpiece_approved_pictures WHERE hash = ?").run(hash); + approvedPictures.delete(hash); + } + } known.delete(id); db().prepare("DELETE FROM masterpieces WHERE id = ?").run(id); fs.rmSync(fileOf(id), {force: true}); @@ -126,6 +282,7 @@ const sweep = () => { forget(painting.id); } } + db().prepare("DELETE FROM masterpiece_uploads WHERE created_at <= ?").run(now() - DAY); }; setInterval(() => { try { @@ -136,8 +293,12 @@ setInterval(() => { } }, 600000).unref(); -// A token for one upload by this account (step 1). +// A token for one upload by this account (step 1), or null when it is +// `tooNew`, which says why. const issueToken = account => { + if (tooNew(account)) { + return null; + } for (const [token, entry] of tokens) { if (entry.account === account.id) { tokens.delete(token); @@ -189,20 +350,33 @@ const looksLikeJpeg = image => { return false; }; -// step 2. resolves to the new painting's id, or null when the token is not -// good, the picture is not a JPEG of a sane size, or the account has posted -// too many. +// step 2. resolves to {uuid}, the new painting's id, or {uuid: null} when +// the token is not good, the picture is not a JPEG of a sane size, or the +// account has posted too many; with `refusal` and `maker` when the player +// should be told why. const upload = async (token, image) => { const entry = typeof token === "string" ? tokens.get(token) : null; tokens.delete(token); if (!entry || entry.expires < Date.now() || !Buffer.isBuffer(image) || image.length > MAX_IMAGE_BYTES || !looksLikeJpeg(image) || !uploadsPerHour.allow(entry.account)) { - return null; + return {uuid: null}; + } + // a picture approved before is not a new one and does not count + const hash = hashOf(image); + const print = jpegPrint.fingerprint(image); + const approvedBefore = pictureApproved(hash, print); + const maker = accounts.byId(entry.account); + const refusal = maker && !approvedBefore ? tooNew(maker) || tooMany(maker) : null; + if (!maker || refusal) { + return {uuid: null, refusal, maker}; } const safety = await imageModeration.scan(image); if (safety.blocked) { console.log(`[masterpieces] rejected unsafe upload from account ${entry.account}`); - return null; + return {uuid: null}; + } + if (!approvedBefore) { + db().prepare("INSERT INTO masterpiece_uploads (creator_id, created_at) VALUES (?, ?)").run(entry.account, now()); } // one painting at a time is between the easel and the frame. for (const painting of [...known.values()]) { @@ -210,12 +384,12 @@ const upload = async (token, image) => { forget(painting.id); } } - const painting = {id: crypto.randomUUID(), creator: entry.account, createdAt: now(), status: UPLOADED, frame: 0, reviewedAt: null, reviewedBy: null, likes: []}; + const painting = {id: crypto.randomUUID(), creator: entry.account, createdAt: now(), status: UPLOADED, frame: 0, reviewedAt: null, reviewedBy: null, likes: [], hash}; fs.writeFileSync(fileOf(painting.id), image); - db().prepare("INSERT INTO masterpieces (id, creator_id, created_at, status, frame, likes, image) VALUES (?, ?, ?, ?, ?, '[]', ?)") - .run(painting.id, painting.creator, painting.createdAt, painting.status, painting.frame, NO_IMAGE); + db().prepare("INSERT INTO masterpieces (id, creator_id, created_at, status, frame, likes, image, hash, fingerprint) VALUES (?, ?, ?, ?, ?, '[]', ?, ?, ?)") + .run(painting.id, painting.creator, painting.createdAt, painting.status, painting.frame, NO_IMAGE, hash, print); known.set(painting.id, painting); - return painting.id; + return {uuid: painting.id}; }; // `by`: who approved it, when someone did. @@ -239,11 +413,19 @@ const claimable = (account, id, frame) => { }; // step 3, paid for: the painting is an item now. true when it is approved -// on the spot. +// on the spot: review is off, or its picture was approved before. const claim = (id, frame) => { const painting = known.get(String(id)); painting.frame = frame; - setStatus(painting, needsReview() ? PENDING : APPROVED); + if (!needsReview()) { + setStatus(painting, APPROVED); + } + else if (pictureApproved(painting.hash, printOf(painting))) { + setStatus(painting, APPROVED, AUTO_APPROVER); + } + else { + setStatus(painting, PENDING); + } return painting.status === APPROVED; }; @@ -278,14 +460,26 @@ const mayView = (id, address, addressOf) => { return painting.status === APPROVED || Boolean(maker && addressOf(maker) && addressOf(maker) === address); }; -// `by`: the moderator's username, or "admin". +// `by`: the moderator's username, or "admin". its picture is approved from +// now on, and the same picture waiting in other paintings is approved with +// it. the paintings approved, none when `id` was not waiting. const approve = (id, by) => { const painting = get(id); if (!painting || painting.status !== PENDING) { - return false; + return []; } setStatus(painting, APPROVED, by); - return true; + if (!painting.hash) { + return [painting]; + } + const print = printOf(painting); + notePictureApproved(painting, print); + const twins = [...known.values()].filter(other => other.status === PENDING + && (other.hash === painting.hash || (print && jpegPrint.samePicture(print, printOf(other))))); + for (const twin of twins) { + setStatus(twin, APPROVED, AUTO_APPROVER); + } + return [painting, ...twins]; }; // is a den item fit to be shown, traded and recycled? anything that is not @@ -362,7 +556,7 @@ const frameList = listId => ([LIST_FRAMES_OFFERED, LIST_FRAMES_IN_ORDER].includes(Number(listId)) ? FRAMES.flat() : null); module.exports = { - load, issueToken, upload, claimable, claim, get, image, mayView, approve, forget, itemApproved, itemFields, creatorOf, like, gallery, list, listsFor, recentlyApproved, frameList, + load, issueToken, tooNew, upload, claimable, claim, get, image, mayView, approve, forget, itemApproved, itemFields, creatorOf, like, gallery, list, listsFor, recentlyApproved, frameList, needsReview, setNeedsReview, madeBy, PENDING, APPROVED, DEN_ITEM, TOKEN_DEN_ITEM, DIAMOND_DEF, MAX_IMAGE_BYTES, }; diff --git a/lib/minigames.js b/lib/minigames.js index 732a9d2..2ab43bc 100644 --- a/lib/minigames.js +++ b/lib/minigames.js @@ -464,8 +464,15 @@ module.exports = ({tell, leaveRoom, findOnline, blocks, watchers}) => { }, }; + // refused to an account under a day old: the game shows its "could not + // send" popup, and a plain message box ("xtl") says why. const uploadToken = (session, game, [, purpose], reply) => { - reply("tkn", 1, 0, masterpieces.issueToken(session.account), 0, purpose === undefined ? 1 : Number(purpose) || 1); + const token = masterpieces.issueToken(session.account); + if (!token) { + const why = masterpieces.tooNew(session.account) || "Your Masterpiece could not be sent. Try again later."; + session.sendXt("xtl", -1, why.replace(/%/g, " percent")); + } + reply("tkn", token ? 1 : 0, 0, token || "", 0, purpose === undefined ? 1 : Number(purpose) || 1); }; // what a game asks of the server while it runs, by game class and then by diff --git a/lib/modpanel.js b/lib/modpanel.js index 04d46d7..fd6bb26 100644 --- a/lib/modpanel.js +++ b/lib/modpanel.js @@ -395,7 +395,7 @@ const PAGE_HEADERS = { "X-Frame-Options": "DENY", "X-Content-Type-Options": "nosniff", "Referrer-Policy": "no-referrer", - "Content-Security-Policy": captcha.policy("default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; " + "Content-Security-Policy": captcha.policy("default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; media-src 'self'; font-src 'self'; " + "form-action 'none'; frame-ancestors 'none'; base-uri 'none'"), }; @@ -670,6 +670,17 @@ const handle = async (req, res, url, from, web) => { return; } + // the mod log (lib/modlog.js): what staff did, as the admin panel's + // Moderators page shows it. + if (route === "GET /mod/api/modlog") { + if (!roles.can(mod, "staff.log")) { + reply(403, {error: "Your role does not allow that."}); + return; + } + reply(200, {log: modlog.recent()}); + return; + } + // everything else shows players and what they did: not for a role that // only makes things. if (!roles.can(mod, "player.view")) { diff --git a/lib/panel.js b/lib/panel.js index 21a783f..cf27532 100644 --- a/lib/panel.js +++ b/lib/panel.js @@ -149,6 +149,7 @@ const accountRow = (account, playing) => { twoStep: totp.isOn(account), discord: account.discord, adminDashboardAccess: Boolean(account.adminDashboardAccess), + guide: account.guideGiven === true, bio: account.bio, badges: account.badges, featuresOff: accounts.FEATURES.filter(name => !accounts.featureOn(account, name)), @@ -382,6 +383,13 @@ const gift = (account, body) => { const MAX_QUEST_LEVEL = 100; +// a password for a player who lost theirs: read out or pasted to them, so +// nothing that looks like something else (0 O, 1 l I). +const TEMP_PASSWORD_LETTERS = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"; +const TEMP_PASSWORD_LENGTH = 12; +const randomPassword = () => Array.from({length: TEMP_PASSWORD_LENGTH}, + () => TEMP_PASSWORD_LETTERS[crypto.randomInt(TEMP_PASSWORD_LETTERS.length)]).join(""); + const wholeNumber = (value, min, max) => (Number.isInteger(value) && value >= min && value <= max ? value : null); // returns {ok: text} or {error: text}. @@ -534,6 +542,36 @@ const act = (account, body) => { `+${(account.permGrants || []).join(",") || "none"} -${(account.permDenies || []).join(",") || "none"}`); return {ok: `${name}'s permissions are saved.`}; } + // guide: lets the player log in as one (lib/accounts.js setLoginChoices). + // it shows from their next login. + // lost access: a new random password, shown here once. it signs them out + // everywhere (lib/accounts.js setPassword); with `twoStepOff` their 2FA + // goes as well, for a lost authenticator. + case "tempPassword": { + let password = randomPassword(); + // the account rules want a capital, a small letter and a digit, and no + // common word or the username in it: draw again until it has them. + while (accounts.setPassword(account, password)) { + password = randomPassword(); + } + const twoStepOff = body.twoStepOff === true && totp.isOn(account); + if (twoStepOff) { + totp.turnOff(account); + } + accounts.save(account); + world.disconnect(account); + manage.forget(account); + modpanel.forget(account); + modlog.record("admin", "temporary password set", account.username, twoStepOff ? "2FA turned off" : ""); + return {ok: `${name}'s password is now ${password}${twoStepOff ? " and their 2FA is off" : ""}. Ask them to change it on their account page.`, password}; + } + case "guide": { + const on = body.on === true; + account.guideGiven = on; + accounts.save(account); + modlog.record("admin", on ? "made a guide" : "no longer a guide", account.username, ""); + return {ok: on ? `${name} is a guide from their next login.` : `${name} is no longer a guide from their next login.`}; + } case "adminDashboardAccess": { const on = body.on === true; if (on && !account.discordId) { @@ -600,7 +638,7 @@ const PAGE_HEADERS = { "X-Frame-Options": "DENY", "X-Content-Type-Options": "nosniff", "Referrer-Policy": "no-referrer", - "Content-Security-Policy": "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; " + "Content-Security-Policy": "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; media-src 'self'; font-src 'self'; " + "form-action 'none'; frame-ancestors 'none'; base-uri 'none'", }; diff --git a/lib/pets.js b/lib/pets.js index 113e0a4..b91a09c 100644 --- a/lib/pets.js +++ b/lib/pets.js @@ -3,7 +3,7 @@ const accounts = require("./accounts.js"); const achievements = require("./achievements.js"); const defpacks = require("./defpacks.js"); -const {activeAvatar, isUint} = require("./player.js"); +const {activeAvatar, activePet, isUint} = require("./player.js"); // pets (client side: PetManager, PetXtCommManager). A pet's species and look // are packed into three integers by PetManager.packPetBits; the species is @@ -30,6 +30,13 @@ const MASTERY_FULL = 100; const MASTERY_PER_ROOM = 5; const MASTERY_BITS = 0xF00; // the part of uBits PetInventory writes const SECONDS_TO_HATCH = 259200; // PetManager.hasHatched +// not one of the game's: the pet chooser's "play as your pet" switch +// (gui.PetInventory), kept with the user vars so it reaches the client at +// login. on, whichever pet is out stands in for the avatar, in any room, as +// at a Play-As-Your-Pet party. +const USER_VAR_PET_FORM = 9008; + +const inPetForm = account => Number(account.userVars[USER_VAR_PET_FORM]) === 1 && Boolean(activePet(account)); const random = (min, max) => min + Math.floor(Math.random() * (max - min + 1)); @@ -106,6 +113,24 @@ module.exports = ({others, findOnline, petFields}) => { const ownPet = (account, invIdx) => account.pets.find(candidate => candidate.inv === Number(invIdx)); + // tells the player and everyone who sees them whether the pet stands in + // for the avatar (UserCommXtCommManager, user communication type 13). + const sendPetForm = session => { + if (session.pos) { + const state = inPetForm(session.account) ? "on" : "off"; + for (const other of [session, ...others(session)]) { + other.sendXt("uc", session.room.id, session.id, state, "13"); + } + } + }; + + // the pet chooser's switch. on with no pet out is kept for the next pet. + const setPetForm = (session, on) => { + achievements.set(session.account, USER_VAR_PET_FORM, on ? 1 : 0); + accounts.save(session.account); + sendPetForm(session); + }; + const handlers = { // PetManager.onPetListResponse: [found, username, count, (invIdx, // createdTs, name, lBits, uBits, eBits, mastery, personality, toy, food, @@ -146,15 +171,8 @@ module.exports = ({others, findOnline, petFields}) => { session.sendXt("ps", -1, 0); return; } + const wasPetForm = inPetForm(account); activeAvatar(account).petInvId = pet ? pet.inv : 0; - if (!pet && session.petPlayerMode) { - session.petPlayerMode = false; - if (session.pos) { - for (const other of [session, ...others(session)]) { - other.sendXt("uc", session.room.id, session.id, "off", "13"); - } - } - } accounts.save(account); const messageRoom = session.room ? session.room.id : -1; session.sendXt("ps", messageRoom, 1, session.id, pet ? pet.inv : 0); @@ -163,6 +181,11 @@ module.exports = ({others, findOnline, petFields}) => { other.sendXt("ps", messageRoom, 1, session.id, ...petFields(pet)); } } + // with no pet out the avatar comes back; the next pet out takes its + // place again while the switch is on. + if (wasPetForm !== inPetForm(account)) { + sendPetForm(session); + } }, // PetManager.onPetDismissResponse: [success, invIdx] @@ -173,14 +196,10 @@ module.exports = ({others, findOnline, petFields}) => { session.sendXt("pd", -1, 0, invIdx); return; } + const wasPetForm = inPetForm(account); account.pets = account.pets.filter(candidate => candidate !== pet); - if (session.petPlayerMode && activeAvatar(account).petInvId === pet.inv) { - session.petPlayerMode = false; - if (session.pos) { - for (const other of [session, ...others(session)]) { - other.sendXt("uc", session.room.id, session.id, "off", "13"); - } - } + if (wasPetForm && !inPetForm(account)) { + sendPetForm(session); } for (const avatar of account.avatars) { if (avatar.petInvId === pet.inv) { @@ -215,7 +234,8 @@ module.exports = ({others, findOnline, petFields}) => { // EggPetHatchedPopup: [petInvIdx, nameId1, nameId2] names a pet hatched // from an egg. PetManager.onPetEggNameResponse: [success, invIdx, name]. pn(session, roomId, [invIdx, name1, name2]) { - const pet = ownPet(session.account, invIdx); + const {account} = session; + const pet = ownPet(account, invIdx); const def = pet && defpacks.petDef(pet.lBits & 0xFF); if (!pet || ![name1, name2].every(isUint) || pet.name || !def || def.isEgg !== "1" || pet.createdAt + SECONDS_TO_HATCH > Math.floor(Date.now() / 1000)) { @@ -250,5 +270,5 @@ module.exports = ({others, findOnline, petFields}) => { }, }; - return {handlers, walked, giveCharacter, favouritesList, personalityList, PERSONALITY_LIST}; + return {handlers, walked, giveCharacter, favouritesList, personalityList, PERSONALITY_LIST, inPetForm, setPetForm}; }; diff --git a/lib/roles.js b/lib/roles.js index 74b37a7..ac33c27 100644 --- a/lib/roles.js +++ b/lib/roles.js @@ -32,11 +32,13 @@ const PERMISSIONS = { "items.create": "make new items from the game's art, change and delete them, lock items out of the item finder and take items from players (!item, !lock, !unlock, !locked, !take)", "room.effects": "run temporary room-wide size and colour effects (!roomfx)", "theater.control": "play or stop a YouTube video for everyone in the theatre", + "music.upload": "upload MP3s as den music tracks, and delete those uploaded (the Music page)", + "staff.log": "read the mod log: what staff did, in the panels and the game (the Mod log page)", }; const HELPER = ["server.view", "player.view", "staff.list", "player.warn", "player.goto", "chat.silence"]; -const MODERATOR = [...HELPER, "player.kick", "player.summon", "staff.invisible", "player.ban", "player.names", "address.ban", "items.approve", "items.restrict", "theater.control"]; -const ADMIN = [...MODERATOR, "items.create", "room.effects"]; +const MODERATOR = [...HELPER, "player.kick", "player.summon", "staff.invisible", "player.ban", "player.names", "address.ban", "items.approve", "items.restrict", "theater.control", "staff.log"]; +const ADMIN = [...MODERATOR, "items.create", "room.effects", "music.upload"]; // a developer makes things, and moderates nobody. const DEVELOPER = ["server.view", "staff.list", "items.create", "room.effects", "theater.control"]; diff --git a/lib/world.js b/lib/world.js index 670f04f..8c68593 100644 --- a/lib/world.js +++ b/lib/world.js @@ -459,17 +459,20 @@ const sendAvatarCreate = (to, target) => { avatar.questLevel || 0, 1, // userNameModeratedFlag lastSeenField(account, Boolean(findOnline(account.username))), // daysSinceLastLogin: see lib/player.js - // optional-field flags: bit 0 accountType, bit 1 nameBarData, bit 2 pet - pet ? 7 : 3, + // optional-field flags: bit 0 accountType, bit 1 nameBarData, bit 2 pet, + // bit 3 a blend colour (none: 0), bit 4 guide (the name tag and bubble + // every client draws for a guide) + 3 | (pet ? 4 : 0) | (account.isGuide ? 24 : 0), account.accountType, account.nameBarData, - ...(pet ? petFields(pet) : [])); + ...(pet ? petFields(pet) : []), + ...(account.isGuide ? [0, 1] : [])); sendItemList(to, target, false); disguises.show(to, target); if (target.invisible) { to.sendXt("xiv", -1, target.id, 1); } - if (target.petPlayerMode) { + if (pets.inPetForm(account)) { to.sendXt("uc", room.id, target.id, "on", "13"); } // `ac` only creates the avatar. Actions normally travel as live `uc` @@ -692,6 +695,9 @@ const handlers = { if (motd) { tell(session, motd); } + if (session.account.guideRefused) { + session.sendXt("xtl", -1, "Guide is only for players the Burrow team has made guides."); + } buddies.greet(session); minigames.greet(session); moderation.greet(session); @@ -1772,15 +1778,12 @@ module.exports = { minigames.bubble(session, fields[0], fields[2]); return; } - // Per-player pet form. It is deliberately a user-communication - // message so every client already in the room updates the same - // avatar, while the normal pet-out state stays unchanged. + // Per-player pet form (the pet chooser's switch, lib/pets.js). It + // is deliberately a user-communication message so every client + // already in the room updates the same avatar, while the normal + // pet-out state stays unchanged. if (fields[1] === "13") { - const enabled = fields[0] === "on" && Boolean(activePet(session.account)); - session.petPlayerMode = enabled; - for (const other of [session, ...watchers(session)]) { - other.sendXt("uc", session.room.id, session.id, enabled ? "on" : "off", "13"); - } + pets.setPetForm(session, fields[0] === "on"); return; } // Type 3 is the action packet. It used to be sent only to people @@ -2016,20 +2019,21 @@ module.exports = { // the panel's Masterpieces page. A painting waiting for review is an // item its maker cannot use yet; approving it tells them so, by a // Jam-A-Gram from Animal Jam HQ and at once if they are playing. `by` - // is who approved it: a moderator's username, or "admin". + // is who approved it: a moderator's username, or "admin". the same + // picture waiting elsewhere is approved with it, and its makers told too. approveMasterpiece(id, by) { - const maker = masterpieces.creatorOf(id); - if (!masterpieces.approve(id, by)) { - return false; - } - if (maker) { - ecards.noteFromHq(maker, "Your Masterpiece has been approved! It is with your den items."); - const session = findOnline(maker.username); - if (session) { - dens.sendDenItems(session); + const approved = masterpieces.approve(id, by); + for (const painting of approved) { + const maker = masterpieces.creatorOf(painting.id); + if (maker) { + ecards.noteFromHq(maker, "Your Masterpiece has been approved! It is with your den items."); + const session = findOnline(maker.username); + if (session) { + dens.sendDenItems(session); + } } } - return true; + return approved.length > 0; }, // takes a painting away for good, from whoever has it by now: one that // was not approved, or one that should not have been. its maker gets a @@ -2054,7 +2058,7 @@ module.exports = { account.denItems = account.denItems.filter(item => item.image !== painting.id); account.tradeList = account.tradeList.filter(listed => !(listed.type === 1 && held.some(item => item.inv === listed.inv))); } - masterpieces.forget(painting.id); + masterpieces.forget(painting.id, true); if (maker) { dens.giveDenItem(maker, masterpieces.TOKEN_DEN_ITEM); ecards.noteFromHq(maker, "Your Masterpiece was not approved. Here is a Masterpiece Token to frame another one with."); diff --git a/public/admin.html b/public/admin.html index b337ab4..5a7d10b 100644 --- a/public/admin.html +++ b/public/admin.html @@ -413,6 +413,7 @@ + @@ -501,6 +502,15 @@
+
+

Lost access

+

A new random password, shown once. They are signed out everywhere and should change it on their account page.

+
+
+ +

Adventure level

@@ -525,6 +535,8 @@

Badges

+
+
@@ -735,6 +747,31 @@
+