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:
Burrowdev committed 2026-10-23 19:14:49 +03:00
1 parent 44af83f1c5
commit 7cfef493fa
18 files changed
+1958 -7

No files matched your search

+139 -3
View File
@@ -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};