mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-18 17:27:50 -05:00
feat(account): web item giver with colour pickers, den variants, tints and previews
/account/items, linked from the account page and using its sign-in, gives items to the account signed in: - clothing: the item's own colours, or four colours of your own as a picker, #RRGGBB, or AJ's numbers (palette places, or the whole item's colour id signed or unsigned), converted as you type; hex colours not in the palette become an exact custom colour - den items: the variants their art has (lib/den-variants.json, made by tools/den-variants.sh) and a whole-item tint with a strength, kept in the item's version above the variant and drawn by DenItemHelper (build 83) - a preview of each: clothing icons coloured on the page as the game colours them, den items played by Ruffle in a sandboxed frame through public/item-preview.swf (tools/preview-host) a give with a custom colour id that does not exist is refused.
This commit is contained in:
1 parent
44af83f1c5
commit
7cfef493fa
18 files changed
+1958
-7
No files matched your search
+139
-3
@@ -2,6 +2,8 @@
|
||||
|
||||
const accounts = require("./accounts.js");
|
||||
const bans = require("./bans.js");
|
||||
const cdn = require("./cdn.js");
|
||||
const colours = require("./colours.js");
|
||||
const captcha = require("./captcha.js");
|
||||
const defpacks = require("./defpacks.js");
|
||||
const devices = require("./devices.js");
|
||||
@@ -10,6 +12,7 @@ const roles = require("./roles.js");
|
||||
const dens = require("./dens.js");
|
||||
const itemControl = require("./itemcontrol.js");
|
||||
const customItems = require("./customitems.js");
|
||||
const itemArt = require("./itemart.js");
|
||||
const limits = require("./limits.js");
|
||||
const world = require("./world.js");
|
||||
const {addClothing, sendClothingAdded, isUint} = require("./player.js");
|
||||
@@ -71,6 +74,24 @@ const search = (params, favourites = null, seesLocked = false, askerId = null) =
|
||||
};
|
||||
};
|
||||
|
||||
// the variants each den item's art has (tools/den-variants.sh):
|
||||
// {versions: [...], colours?: [[hex, ...] per variant]}, or null.
|
||||
let denVariants = null;
|
||||
const variantsOf = id => {
|
||||
if (!denVariants) {
|
||||
try {
|
||||
denVariants = require("./den-variants.json");
|
||||
}
|
||||
catch (err) {
|
||||
denVariants = {};
|
||||
}
|
||||
}
|
||||
return denVariants[Number(id)] || null;
|
||||
};
|
||||
const SPECIAL_MANNEQUIN = "4";
|
||||
const MAX_TINT_SLOT = 0x7FFFFF; // what fits above a den item's variant (8 bits) in its version, kept positive
|
||||
const MAX_PRESETS = 40;
|
||||
|
||||
// one to four "#RRGGBB", spaces or commas between. the # is wanted: six
|
||||
// digits alone are a colour id
|
||||
const HEX_COLOURS = /^#[0-9a-f]{6}([\s,]+#[0-9a-f]{6}){0,3}$/i;
|
||||
@@ -89,6 +110,7 @@ const give = (body, seesLocked = false) => {
|
||||
return {status: 403, body: {error: "That item is locked out of the item finder."}};
|
||||
}
|
||||
const session = world.findOnline(account.username);
|
||||
const given = {}; // what the item was given as: its colour, or its version
|
||||
if (body.kind === "clothing") {
|
||||
const def = defpacks.itemDef(body.id);
|
||||
const colors = def ? String(def.colors).split(",") : [];
|
||||
@@ -119,6 +141,9 @@ const give = (body, seesLocked = false) => {
|
||||
if (custom && !isUint(custom)) {
|
||||
return {status: 400, body: {error: "Colour: an id, or #RRGGBB"}};
|
||||
}
|
||||
if (custom && !colours.known(Number(custom))) {
|
||||
return {status: 400, body: {error: "No such custom colour"}};
|
||||
}
|
||||
const color = custom || colors[Number(body.colorIndex) || 0];
|
||||
if (!def || !defpacks.clothingIds().includes(Number(def.id)) || color === undefined) {
|
||||
return {status: 400, body: {error: "No such clothing item or colour"}};
|
||||
@@ -127,6 +152,7 @@ const give = (body, seesLocked = false) => {
|
||||
if (!item) {
|
||||
return {status: 409, body: {error: "That inventory is full"}};
|
||||
}
|
||||
given.color = Number(color);
|
||||
accounts.save(account);
|
||||
if (session) {
|
||||
sendClothingAdded(session, item);
|
||||
@@ -136,12 +162,48 @@ const give = (body, seesLocked = false) => {
|
||||
if (!defpacks.denItemIds().includes(Number(body.id))) {
|
||||
return {status: 400, body: {error: "No such den item"}};
|
||||
}
|
||||
if (!dens.giveDenItem(account, body.id)) {
|
||||
// `version`: one of the variants its art has (above), 0 by default.
|
||||
// `tint` ("#RRGGBB") and `tintStrength` (1-100, 100 by default): the
|
||||
// whole item in shades of that colour, which the patched client draws
|
||||
// (DenItemHelper.applyTint). the tint is a custom colour entry
|
||||
// (lib/colours.js, [rgb, strength, 0, 0]), and 1 + its number goes in
|
||||
// the version above the variant's 8 bits, so it goes wherever the item
|
||||
// goes: dens, trades, gifts.
|
||||
const variants = variantsOf(body.id);
|
||||
const variant = body.version === undefined || body.version === "" ? 0 : Number(body.version);
|
||||
if (!Number.isInteger(variant) || (variant !== 0 && !(variants && variants.versions.includes(variant)))) {
|
||||
return {status: 400, body: {error: "That item has no such variant"}};
|
||||
}
|
||||
let version = variant;
|
||||
const tint = typeof body.tint === "string" ? body.tint.trim() : "";
|
||||
if (tint) {
|
||||
const strength = body.tintStrength === undefined || body.tintStrength === "" ? 100 : Number(body.tintStrength);
|
||||
if (!/^#[0-9a-f]{6}$/i.test(tint) || !Number.isInteger(strength) || strength < 1 || strength > 100) {
|
||||
return {status: 400, body: {error: "Tint: #RRGGBB, strength 1 to 100"}};
|
||||
}
|
||||
if ((defpacks.denItemDef(body.id) || {}).specialType === SPECIAL_MANNEQUIN) {
|
||||
return {status: 400, body: {error: "Mannequins cannot be tinted"}};
|
||||
}
|
||||
const entry = world.makeColour(account, [parseInt(tint.slice(1), 16), strength, 0, 0], session);
|
||||
if (!entry) {
|
||||
return {status: 400, body: {error: "Bad colour"}};
|
||||
}
|
||||
if (entry.full) {
|
||||
return {status: 429, body: {error: "No more new colours today. Colours made before can still be used."}};
|
||||
}
|
||||
const slot = entry.id - colours.CUSTOM_BASE + 1;
|
||||
if (slot > MAX_TINT_SLOT) {
|
||||
return {status: 503, body: {error: "No more tints can be made"}};
|
||||
}
|
||||
version |= slot << 8;
|
||||
}
|
||||
if (!dens.giveDenItem(account, body.id, version)) {
|
||||
return {status: 409, body: {error: "That inventory is full"}};
|
||||
}
|
||||
if (session) {
|
||||
dens.sendDenItems(session);
|
||||
}
|
||||
given.version = version;
|
||||
}
|
||||
else {
|
||||
return {status: 400, body: {error: "Unknown item kind"}};
|
||||
@@ -152,7 +214,81 @@ const give = (body, seesLocked = false) => {
|
||||
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(account);
|
||||
return {status: 200, body: {ok: true, online: Boolean(session)}};
|
||||
return {status: 200, body: {ok: true, online: Boolean(session), ...given}};
|
||||
};
|
||||
|
||||
// the game's 256 colours (the CDN's paletteData; PaletteHelper.setGamePalette),
|
||||
// as "rrggbb", once read. null while the file cannot be had.
|
||||
let palette = null;
|
||||
const loadPalette = async () => {
|
||||
if (!palette) {
|
||||
const file = await cdn.get(cdn.hashedPath("paletteData"));
|
||||
if (file && file.length >= 4 && file.readInt32BE(0) === 1024) {
|
||||
palette = Array.from({length: 256}, (unused, index) => (file.readUInt32BE(4 + index * 4) & 0xFFFFFF).toString(16).padStart(6, "0"));
|
||||
}
|
||||
}
|
||||
return palette;
|
||||
};
|
||||
// a packed colour's four, as "rrggbb" (a custom colour's own, or the
|
||||
// palette's at each of its four bytes, colour 1 the top one).
|
||||
const coloursOf = id => {
|
||||
const custom = colours.rgbsOf(id);
|
||||
if (custom) {
|
||||
return custom.map(rgb => rgb.toString(16).padStart(6, "0"));
|
||||
}
|
||||
return palette ? [24, 16, 8, 0].map(shift => palette[(id >>> shift) & 0xFF]) : null;
|
||||
};
|
||||
|
||||
// the web item giver (/account/items, lib/manage.js): a search as the
|
||||
// in-game one, with what each item may be given as: a clothing item's own
|
||||
// colours, a den item's variants and whether it takes a tint.
|
||||
const searchFor = async (account, params) => {
|
||||
await loadPalette();
|
||||
const found = search(params, account.finderGiven, roles.can(account, "items.create"), account.id);
|
||||
return {...found, results: found.results.map(result => {
|
||||
// a custom item's own picture, which the game shows in place of its art
|
||||
const custom = customItems.get(result.kind, result.id);
|
||||
const entry = {...result, art: custom ? itemArt.urlOf(custom, "/account/api/itemart") : null};
|
||||
if (entry.kind === "clothing") {
|
||||
const def = defpacks.itemDef(entry.id);
|
||||
const ids = def ? String(def.colors).split(",").map(Number).filter(Number.isInteger).slice(0, MAX_PRESETS) : [];
|
||||
return {...entry, presets: ids.map(id => ({id: id >>> 0, colours: coloursOf(id >>> 0)}))};
|
||||
}
|
||||
const variants = variantsOf(entry.id);
|
||||
return {...entry, variants: variants ? variants.versions : [], variantColours: variants && variants.colours ? variants.colours : null,
|
||||
tintable: (defpacks.denItemDef(entry.id) || {}).specialType !== SPECIAL_MANNEQUIN};
|
||||
})};
|
||||
};
|
||||
|
||||
// ... and its colour boxes, which turn colours to the game's numbers and
|
||||
// back as they are typed: with `id`, a custom colour's four ("rrggbb"); with
|
||||
// `rgbs` (four "rrggbb", commas between), the custom colour already made of
|
||||
// those four, or null (Give would make it); with neither, the game's 256.
|
||||
const colourLookup = async params => {
|
||||
if (params.has("id")) {
|
||||
const text = params.get("id");
|
||||
const id = /^-?\d{1,10}$/.test(text) && Number(text) >= -0x80000000 && Number(text) <= 0xFFFFFFFF ? Number(text) >>> 0 : null;
|
||||
const rgbs = id === null ? null : colours.rgbsOf(id);
|
||||
return rgbs ? {status: 200, body: {id, colours: rgbs.map(rgb => rgb.toString(16).padStart(6, "0"))}}
|
||||
: {status: 404, body: {error: "No such custom colour"}};
|
||||
}
|
||||
if (params.has("rgbs")) {
|
||||
const rgbs = params.get("rgbs").split(",").map(hex => (/^[0-9a-f]{6}$/i.test(hex) ? parseInt(hex, 16) : NaN));
|
||||
const entry = colours.allocate(rgbs, false);
|
||||
if (!entry) {
|
||||
return {status: 400, body: {error: "Four colours: rrggbb,rrggbb,rrggbb,rrggbb"}};
|
||||
}
|
||||
return {status: 200, body: {id: entry.full ? null : entry.id}};
|
||||
}
|
||||
return {status: 200, body: {palette: await loadPalette()}};
|
||||
};
|
||||
|
||||
// ... and its Give, for the account signed in on that page.
|
||||
const giveAs = (account, body) => {
|
||||
if (!givesPerMinute.allow(account.id)) {
|
||||
return {status: 429, body: {error: "Too many at once. Try again in a minute."}};
|
||||
}
|
||||
return give({...body, username: account.username}, roles.can(account, "items.create"));
|
||||
};
|
||||
|
||||
const FORBIDDEN = {status: 403, body: {error: "forbidden"}};
|
||||
@@ -296,4 +432,4 @@ const handle = async (route, params, body, from) => {
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {handle, search};
|
||||
module.exports = {handle, search, searchFor, colourLookup, giveAs};
|
||||
+4
-1
@@ -71,4 +71,7 @@ const chunks = (size = 200) => {
|
||||
// the custom range), or an entry of the table?
|
||||
const known = id => id < CUSTOM_BASE || id > MAX_ID || entries.has(id);
|
||||
|
||||
module.exports = {load, allocate, chunks, known, count: () => entries.size};
|
||||
// the four RGB values of an entry, or null.
|
||||
const rgbsOf = id => entries.get(id) || null;
|
||||
|
||||
module.exports = {CUSTOM_BASE, load, allocate, chunks, known, rgbsOf, count: () => entries.size};
|
||||
@@ -0,0 +1 @@
|
||||
{"1":{"versions":[0,1,2,3,4,5,6,7],"colours":[["56c3d8","1078ff","4acaff","4876ba"],["8f48ff","3d00a7","5e1dff","5d0089"],["ff99f0","e150ff","ff6bf7","ff3dba"],["ac0912","ea004f","c40000","a10000"],["ff732b","dd3c4d","ff742d","e84138"],["f3ff86","d0ee19","e6ff3b","dfff05"],["63ff66","007917","23b700","035000"],["63565d","201219","551f4c","2f151f"]]},"9":{"versions":[0,1]},"13":{"versions":[0,1,2,3]},"19":{"versions":[0,1,2,3,4,5,6,7,8,9],"colours":[["7cbbff","2388ff","b9fffc","6bc1ff"],["be5bff","9348ff","42195b","ac43ff"],["e689c2","e45dad","fbfbfb","ea65c4"],["c8a0cd","cc434c","887e8e","ff4b4b"],["efcd34","e57712","ffffff","7bfff2"],["fff81a","fff81a","835c40","ff6bb3"],["4cbf00","43910b","ade53b","57de00"],["9e9e9e","5c5c7f","727c72","ffbf15"],["9e1616","3a3a3a","0f0f0f","ff1d1d"],["644b18","503000","e0b61f","7b4100"]]},"21":{"versions":[0,1,2]},"27":{"versions":[0,1,2,3]},"36":{"versions":[0,1,2,3,4,5,6,7,8,9],"colours":[["2b5eff","ffd646","56b8ff","b7ecff"],["ee58ff","5e0096","ffcf84","e3bcff"],["ff50d8","fbfbfb","ffbc1d","ffffef"],["ff3418","050505","ff9e53","ffffff"],["ff7323","ffa640","ffe42d","c7fcff"],["f8ff12","150f00","f3ff1a","050500"],["4caf00","adff3b","fff87e","c4ffe2"],["969696","ffdb5b","e0e5ff","809ca2"],["3d3d3d","830004","fafafa","0d0d0d"],["644b18","2b0c00","b9ab00","314800"]]},"37":{"versions":[0,1,3,4,5,6,7],"colours":[["a1daff","7bbfe8","b7eaff","4893ff"],["ceafff","a550ff","5d00b7","9c50ff"],["ff9999","990000","e95353","e11515"],["ffca94","f4a500","d07200","e37d48"],["dbe700","ecd743","ffff02","fff20a"],["afffa4","0a9100","75ff5e","30ff0d"],["727272","909090","f6f6f6","dedede"]]},"45":{"versions":[0,1,2,3,4,5,6,7]},"48":{"versions":[0,1,2,3,4,5,6,7]},"49":{"versions":[0,1,2,3,4,5,6,7]},"53":{"versions":[0,1,2,3,4,5,6,7]},"54":{"versions":[0,1,2,3,4,5,6,7]},"56":{"versions":[0,1,2,3,4,5,6,7,8,9],"colours":[["3e89d0","0d3673","b17400"],["9361ff","42195b","ff8ce4"],["f771a8","ff33a9","ffffff"],["d24743","ae3d59","434343"],["cf6227","e78100","ffe63d"],["ffea30","ff6d1a","bc7900"],["bde745","ff9400","dfaa00"],["969696","525252","ffffff"],["303030","f1f1f1","7d7d7d"],["91602d","ffe463","acb43e"]]},"83":{"versions":[0,1,2,3,4,5,6,7,8,9],"colours":[["c7f3ff","408aff","6bdaff"],["c976ff","5e0096","dcd4ff"],["ff89f3","ff4cac","f5d7ff"],["e43437","ffffff","ffffff"],["fd7500","ffcd63","fffbea"],["ffff3d","150f00","e2fff4"],["4caf00","adff3b","ffffff"],["969696","6b7c6b","dedede"],["212121","6b4496","030303"],["713700","ffffff","fffff2"]]},"91":{"versions":[0,1,2,3,4,5,6,7,8,9],"colours":[["3d5977","c47821","bc8000","ab6412"],["906fa5","705091","af8957","56486c"],["e5768c","f1f1da","e9b3c7","e2c1ce"],["bd483e","373736","7f5437","30312e"],["c37138","3a3539","c75524","474446"],["b46d00","ffe042","787878","525252"],["6b7e48","433422","a16951","58462c"],["8ca0a6","7b8f90","cc9d5d","dcdaa6"],["434342","4b4641","2b3331","212121"],["8a6341","78502a","dbab64","c3b39a"]]},"131":{"versions":[0,1,2,3,4,5]},"147":{"versions":[0,1,2,3,4,5,6,7],"colours":[["7bb8f7","fffedc","dcfcff","ffffff"],["9b4eff","ffeff5","eacaff","aa69ff"],["ff9efa","f33de9","ffd2fb","ca00c4"],["d20000","636363","e62525","616161"],["ef8454","e0ffdf","efe4ce","ffffff"],["fff802","af6f2b","faff58","1ecf00"],["3dc43d","7b7800","35ee45","31cc00"],["505050","ffffff","686868","494949"]]},"152":{"versions":[0,1,2,3,4,5,6,7],"colours":[["7bb8f7","ade0e6","e0db00"],["af37d7","3b0038","f0e579"],["e825e1","ff99f7","cfcfcf"],["d20000","ffacac","eddc6b"],["ff732b","ffda76","cecece"],["ede80a","a2a700","d1d1d1"],["3dc43d","afffa4","ffd938"],["484848","757575","fffaa4"]]},"226":{"versions":[0,1,2,3,4,5,6,7],"colours":[["060043","c4dbff","0015b4","3548ff"],["f5f5f5","42195b","8d00f2","ae74eb"],["ff71e3","ffeaf1","d400c9","ff3dba"],["ac0912","fffee7","fbffa7","ea0000"],["ede3a0","d8822e","fbdf9e","be4e33"],["fff812","639ac6","949400","dfd300"],["fffbca","008903","faffbc","008e3e"],["ffffff","505050","ffffff","343434"]]},"295":{"versions":[0,1,2,3,4,5,6,7],"colours":[["3458dd","4696ff"],["9a47d8","b875ff"],["ff8193","ffc4c6"],["ff2d2d","ff9c9c"],["f57a00","ffd47b"],["e7e700","ffffbb"],["05af00","daffa3"],["7d7d7d","d5d5d5"]]},"348":{"versions":[0,1,2,3,4,5,6,7,8,9],"colours":[["4589ff","fffbac","4086ff","7bc744"],["a640ff","fff986","6300c2","cbe26a"],["ffffff","00b999","ff35e5","bc910e"],["01000a","ff3333","bf0000","ffffff"],["ff7a00","d6d6af","ffffff","3a5000"],["fff800","73e3e0","f7f000","b1ea8c"],["4caf00","adff3b","7dd700","c4ffe2"],["727272","bebebe","e0e5ff","a6a6a6"],["404040","3d3d3d","828282","5e0000"],["644b18","d6c72b","664d00","506a16"]]},"350":{"versions":[0,1,2,3,4,5,6,7,8,9],"colours":[["48c5ff","ffff18","5bff35","0dff1e"],["a846ff","ff650d","ffff33","43f1ff"],["ff73d9","ff30c2","ffffff","00f2eb"],["ff2d2d","828282","ffe925","fdea00"],["e76a00","35f700","ffed10","fff930"],["ffff00","4bfaff","23f700","13ff02"],["76ff20","21ea00","ff7210","ffff02"],["ffffff","cfecff","0affff","fff928"],["717171","3c3c3c","00fdf6","02fff2"],["92663a","ffcfLine truncated
|
||||
@@ -0,0 +1,76 @@
|
||||
"use strict";
|
||||
|
||||
// pictures of items for the web item giver (public/items.html), from the
|
||||
// game's own files on the CDN:
|
||||
//
|
||||
// - clothing: the icon the game shows in a closet (item.SimpleIcon), a
|
||||
// paletted bitmap the page colours itself as the game does
|
||||
// (LayerBitmap.paintFrame), so any four colours show without asking again.
|
||||
// - den items: their art, a SWF, which public/item-preview.html plays with
|
||||
// Ruffle through public/item-preview.swf (tools/preview-host).
|
||||
//
|
||||
// both are the same for everyone, so no sign-in is asked for them.
|
||||
|
||||
const zlib = require("zlib");
|
||||
const amf3 = require("./amf3.js");
|
||||
const cdn = require("./cdn.js");
|
||||
const customItems = require("./customitems.js");
|
||||
const defpacks = require("./defpacks.js");
|
||||
|
||||
const ICON_AVATAR = 1; // the avatar every closet icon is drawn for (Item.makeSmallIcon)
|
||||
const ICON_LARGE = 25; // SimpleIcon.AN_ICON_L
|
||||
const ICON_SMALL = 26; // SimpleIcon.AN_ICON_S
|
||||
const IMAGE_VERSION = 0x7080900; // what ImageServerURL accepts
|
||||
|
||||
// DenItem.getIconId: which of an item's pictures the den shows, by its sort category.
|
||||
const iconId = sortCat => ([0, 99, 6, 5].includes(sortCat) ? 1 : sortCat === 1 ? 2 : [2, 3, 4].includes(sortCat) ? 3 : sortCat + 1);
|
||||
|
||||
// a custom item is drawn as the game's item it was made from.
|
||||
const baseIdOf = (kind, id) => {
|
||||
const entry = customItems.get(kind, id);
|
||||
return entry ? entry.base : Number(id);
|
||||
};
|
||||
|
||||
// a clothing item's closet icon, large if there is one: {w, h, b} with b the
|
||||
// first frame's run-length bytes, base64 (LayerBitmap.paintFrame reads them).
|
||||
// null when it has none.
|
||||
const clothingIcon = async id => {
|
||||
const def = defpacks.itemDef(id);
|
||||
if (!def || !Number.isInteger(Number(def.layerDefId))) {
|
||||
return null;
|
||||
}
|
||||
for (const size of [ICON_LARGE, ICON_SMALL]) {
|
||||
const file = await cdn.get(cdn.hashedPath(`imageArrays/${((ICON_AVATAR << 24) | (size << 16) | Number(def.layerDefId)) >>> 0}`));
|
||||
try {
|
||||
const image = file && amf3.decode(zlib.inflateSync(file));
|
||||
const frame = image && image.v === IMAGE_VERSION && Array.isArray(image.f) && image.f[0];
|
||||
if (frame && Buffer.isBuffer(frame.b) && frame.w > 0 && frame.h > 0) {
|
||||
return {w: frame.w, h: frame.h, b: frame.b.toString("base64")};
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
// not an icon this can read: the next size, or none.
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
// a den item's art as the SWF it holds, or null.
|
||||
const denArt = async id => {
|
||||
const def = defpacks.denItemDef(id);
|
||||
const baseId = baseIdOf("den", id);
|
||||
const baseDef = defpacks.denItemDef(baseId);
|
||||
if (!def || !baseDef) {
|
||||
return null;
|
||||
}
|
||||
const file = await cdn.get(cdn.hashedPath(`items/${((baseId << 16) | iconId(Number(baseDef.sortCat))) >>> 0}`));
|
||||
try {
|
||||
const wrapped = file && amf3.decode(file);
|
||||
return wrapped && wrapped.ba ? Buffer.from(wrapped.ba) : null;
|
||||
}
|
||||
catch (err) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {clothingIcon, denArt};
|
||||
@@ -26,6 +26,8 @@ const recoveryMail = require("./recoverymail.js");
|
||||
const world = require("./world.js");
|
||||
const chatStyles = require("./chatstyles.js");
|
||||
const roles = require("./roles.js");
|
||||
const admin = require("./admin.js");
|
||||
const itemPreview = require("./itempreview.js");
|
||||
|
||||
// the account page at /account: what a player can do about their own
|
||||
// account from a browser. they sign in with the game's username and
|
||||
@@ -566,6 +568,16 @@ const PAGE_HEADERS = {
|
||||
+ "form-action 'none'; frame-ancestors 'none'; base-uri 'none'"),
|
||||
};
|
||||
|
||||
// the web item giver shows den items in frames of public/item-preview.html,
|
||||
// which plays them with Ruffle (a Flash player, from jsDelivr).
|
||||
const ITEMS_PAGE_POLICY = "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; "
|
||||
+ "frame-src 'self'; form-action 'none'; frame-ancestors 'none'; base-uri 'none'";
|
||||
const RUFFLE = "https://cdn.jsdelivr.net/npm/@ruffle-rs/[email protected]/";
|
||||
const PREVIEW_POLICY = `default-src 'none'; script-src 'unsafe-inline' 'wasm-unsafe-eval' ${RUFFLE}; connect-src 'self' ${RUFFLE}; `
|
||||
+ "style-src 'unsafe-inline'; img-src 'self' data: blob:; media-src 'self' data: blob:; worker-src blob:; "
|
||||
+ "form-action 'none'; frame-ancestors 'self'; base-uri 'none'; sandbox allow-scripts";
|
||||
const artFetches = limits.limiter(600, 600000); // item pictures for the web item giver, per address in ten minutes
|
||||
|
||||
// A request that changes something must come from this server's own page.
|
||||
const sameOrigin = limits.originAllowed;
|
||||
|
||||
@@ -581,6 +593,41 @@ const handle = async (req, res, url, from, web) => {
|
||||
web.send(res, 200, captcha.withScriptVersion(await fs.promises.readFile(path.join(config.publicDir, "account.html"))), "text/html; charset=utf-8");
|
||||
return;
|
||||
}
|
||||
// the item giver on the web (public/items.html), with this page's sign-in.
|
||||
if (route === "GET /account/items") {
|
||||
res.setHeader("Content-Security-Policy", ITEMS_PAGE_POLICY);
|
||||
web.send(res, 200, await fs.promises.readFile(path.join(config.publicDir, "items.html")), "text/html; charset=utf-8");
|
||||
return;
|
||||
}
|
||||
// its den item previews (lib/itempreview.js): a page of their own in a
|
||||
// frame, sandboxed, as they run a Flash player from elsewhere (Ruffle).
|
||||
if (route === "GET /account/item-preview") {
|
||||
res.setHeader("Content-Security-Policy", PREVIEW_POLICY);
|
||||
res.setHeader("X-Frame-Options", "SAMEORIGIN");
|
||||
web.send(res, 200, await fs.promises.readFile(path.join(config.publicDir, "item-preview.html")), "text/html; charset=utf-8");
|
||||
return;
|
||||
}
|
||||
const art = /^GET \/account\/(item-preview\.swf|art\/(clothing|den)\/(\d{1,9}))$/.exec(route);
|
||||
if (art) {
|
||||
// fetched from the preview's sandbox, which has no origin of its own
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
if (art[1] === "item-preview.swf") {
|
||||
web.send(res, 200, await fs.promises.readFile(path.join(config.publicDir, "item-preview.swf")), "application/x-shockwave-flash");
|
||||
return;
|
||||
}
|
||||
if (!artFetches.allow(from.address)) {
|
||||
reply(429, {error: "Too many at once. Try again in a minute."});
|
||||
return;
|
||||
}
|
||||
const found = art[2] === "den" ? await itemPreview.denArt(Number(art[3])) : await itemPreview.clothingIcon(Number(art[3]));
|
||||
if (!found) {
|
||||
reply(404, {error: "No picture."});
|
||||
return;
|
||||
}
|
||||
res.setHeader("Cache-Control", "public, max-age=3600");
|
||||
web.send(res, 200, found, art[2] === "den" ? "application/x-shockwave-flash" : "application/json");
|
||||
return;
|
||||
}
|
||||
if (route === "GET /account/recover") {
|
||||
web.send(res, 200, captcha.withScriptVersion(await fs.promises.readFile(path.join(config.publicDir, "recover.html"))), "text/html; charset=utf-8");
|
||||
return;
|
||||
@@ -854,6 +901,28 @@ const handle = async (req, res, url, from, web) => {
|
||||
case "GET /account/api/me":
|
||||
reply(200, profile(account, from));
|
||||
return;
|
||||
// the item giver (above, and lib/admin.js): for the account signed in
|
||||
// here only, as the in-game one is for the one playing; not while banned.
|
||||
case "GET /account/api/giver":
|
||||
case "GET /account/api/colour":
|
||||
case "POST /account/api/give": {
|
||||
if (banFor(account, from) || account.nameRejected) {
|
||||
reply(403, {error: "Not now."});
|
||||
return;
|
||||
}
|
||||
if (route === "GET /account/api/colour") {
|
||||
const result = await admin.colourLookup(url.searchParams);
|
||||
reply(result.status, result.body);
|
||||
return;
|
||||
}
|
||||
if (req.method === "GET") {
|
||||
reply(200, await admin.searchFor(account, url.searchParams));
|
||||
return;
|
||||
}
|
||||
const result = admin.giveAs(account, body);
|
||||
reply(result.status, result.body);
|
||||
return;
|
||||
}
|
||||
// for the trade offer form: what the player could give, and what
|
||||
// another player has up for trade.
|
||||
case "GET /account/api/tradable": {
|
||||
|
||||
Reference in new issue
Block a user