Files
burrow-server/lib/commands.js
T
Burrowdev 04f468af2c feat: stats graphs, playtime, ip search against ban evasion, 150-char chat
- stats graphs for the admin and moderator panels (lib/stats.js, public/stats-view.html)
- playtime, private unless shown on the card's bio tab; set on the account page
- search accounts by ip address, only against ban evasion; mods see no addresses and each search is logged
- privacy policy for the above (terms version 2026-10-17)
- the per-second save looks only at accounts in play, with a slow sweep of the rest
- items in several dens at once; chat filter hides slurs and threats by default
- sexual usernames refused; moderators may enter full rooms; guide login allowed
- 150-character chat box (client build 55)
- fixes: gifts, trades, dens, pets in dens, den recycling, special animals' colours,
  jammer walls, settings switches; the buddy list sent at login
- the item giver, the rules screen and the art studio rework
2026-10-17 02:00:57 +03:00

529 lines
22 KiB
JavaScript

"use strict";
const accounts = require("./accounts.js");
const chatlog = require("./chatlog.js");
const defpacks = require("./defpacks.js");
const disguises = require("./disguises.js");
const dens = require("./dens.js");
const {isSilenced} = require("./moderation.js");
const {MAX_DEN_ITEMS, MAX_WORN, activeAvatar, wornItems} = require("./player.js");
// things players can do by typing "!something" in the chat box, for what
// the game has no window for. none of them touches another player: they
// look things up, or change the typist's own outfit, den or settings.
// what a command has to say comes up in a plain message box of the game's
// ("xtl", shown by the patched client), not as a note from Animal Jam HQ.
//
// the chat box only lets through letters, digits, spaces and !'.,():?-
// so that is all a command or a name given to one can be made of.
const PREFIX = "!";
const MAX_OUTFITS = 30;
const MAX_LAYOUTS = 20;
const MAX_NAME = 20;
const MAX_LISTED = 40;
const NAME_PATTERN = /^[a-z0-9]{1,20}$/;
// what each command does is listed for players at /commands
// (public/commands.html): too much to read in a popup in the game.
// short names for "!go". any room of the room list also goes by its own
// name, with dots ("sarepia.flag_shop").
const PLACES = {
township: "jamaa_township.room_main", jamaa: "jamaa_township.room_main",
sarepia: "sarepia.room_main", crystal: "crystal_sands.room_main", sands: "crystal_sands.room_main",
coral: "coral_canyons.room_main", canyons: "coral_canyons.room_main", shiveer: "mountains_of_shivveer.room_main",
appondale: "appondale.room_main", zios: "lost_temple_of_zios.room_main", temple: "lost_temple_of_zios.room_main",
kimbara: "aussie.room_main", outback: "aussie.room_main", balloosh: "balloosh.room_main",
bahari: "oceans.bahari_bay", reef: "oceans.crystal_reef", deep: "oceans.deep_sea", kani: "pirate_ship.room_main",
arcade: "jamaa_township.room_arcade", diamonds: "jamaa_township.room_diamonds", alphas: "jamaa_township.room_alphahq",
museum: "appondale.room_museum", chamber: "lost_temple_of_zios.chamber_of_knowledge", aquarium: "crystal_sands.room_tierney",
path: "canyon_path.room_main", carnival: "party.venue_carnival",
};
const PLAIN_NAME = /^[A-Za-z0-9][A-Za-z0-9 '.-]{0,19}$/; // a typed animal or pet name
const MAX_WHISPER = 200;
const MAX_AGAIN = 100;
// A hosted party's own name is three string ids, no use outside the game.
const roomTitle = room => (room.partyHost ? `${room.partyHost.username}'s party` : room.displayName);
module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideBlocked, refreshForOthers, travel, reloadRoom, buyAgain, ask}) => {
// A plain message box with an OK button.
const tell = (session, text) => session.sendXt("xtl", -1, String(text).replace(/%/g, " percent"));
const named = (list, name) => list.find(entry => entry.name === name);
const validName = (session, name) => {
if (!NAME_PATTERN.test(name || "")) {
tell(session, `Give it a name of letters and digits, up to ${MAX_NAME} long.`);
return false;
}
return true;
};
// has the animal in use wear exactly the clothes with these inventory
// ids, and remembers what it wore before for "!outfit undo". returns the
// items now worn.
const dress = (session, invs) => {
const {account} = session;
const avatar = activeAvatar(account);
const before = wornItems(account, avatar);
const wanted = account.items.filter(item => invs.includes(item.inv)).slice(0, MAX_WORN);
const takenOff = before.filter(item => !wanted.includes(item));
account.outfitUndo = {avInvId: avatar.avInvId, items: before.map(item => item.inv)};
for (const item of takenOff) {
item.on = item.on.filter(avInvId => avInvId !== avatar.avInvId);
}
for (const item of wanted) {
if (!item.on.includes(avatar.avInvId)) {
item.on.push(avatar.avInvId);
}
}
accounts.save();
// ItemXtCommManager.itemsChangedListResponse, "3" = what is worn
// changed: [3, inUse, perUserAvId, username, colour, onCount,
// offCount, (invIdx, defId)..., offInvIdx...]
session.sendXt("il", -1, 3, 1, avatar.perUserAvId, account.username, 0, wanted.length, takenOff.length,
...wanted.flatMap(item => [item.inv, item.def]), ...takenOff.map(item => item.inv));
for (const other of watchers(session)) {
sendItemList(other, session, false);
}
return wanted;
};
const outfit = (session, [action, name]) => {
const {account} = session;
const avatar = activeAvatar(account);
switch (action) {
case "off":
dress(session, []);
tell(session, "Everything is off. !outfit undo puts it back on.");
return;
// what this animal wore before the last change made here or in the
// animal editor (lib/items.js keeps it).
case "undo": {
const undo = account.outfitUndo;
if (!undo || undo.avInvId !== avatar.avInvId) {
tell(session, "There is nothing to put back for this animal.");
return;
}
dress(session, undo.items);
tell(session, "Back to what you wore before. !outfit undo again swaps back.");
return;
}
// the next free "outfit N" (the settings menu's "Save what I wear").
case "savenew": {
let number = 1;
while (named(account.outfits, `outfit${number}`)) {
number++;
}
outfit(session, ["save", `outfit${number}`]);
return;
}
case "save": {
if (!validName(session, name)) {
return;
}
const existing = named(account.outfits, name);
if (!existing && account.outfits.length >= MAX_OUTFITS) {
tell(session, `You already have ${MAX_OUTFITS} outfits. Delete one first.`);
return;
}
const items = wornItems(account, avatar).map(item => item.inv);
account.outfits = [...account.outfits.filter(entry => entry !== existing), {name, items}];
accounts.save();
tell(session, `Outfit ${name} saved (${items.length} items).`);
return;
}
case "wear": {
const saved = named(account.outfits, name);
if (!saved) {
tell(session, `You have no outfit called ${name}. Try !outfit list.`);
return;
}
const wanted = dress(session, saved.items);
const missing = saved.items.length - wanted.length;
tell(session, missing ? `Wearing ${name}. ${missing} of its items are no longer yours.` : `Wearing ${name}.`);
return;
}
case "delete":
if (!named(account.outfits, name)) {
tell(session, `You have no outfit called ${name}.`);
return;
}
account.outfits = account.outfits.filter(entry => entry.name !== name);
accounts.save();
tell(session, `Outfit ${name} deleted.`);
return;
case "list":
tell(session, account.outfits.length ? `Your outfits: ${account.outfits.map(entry => entry.name).join(", ")}` : "You have no saved outfits yet.");
return;
default:
tell(session, "Use: !outfit save NAME, !outfit wear NAME, !outfit list, !outfit delete NAME, !outfit off, !outfit undo");
}
};
const activeDenDef = account => (account.dens.find(den => den.inv === account.activeDen) || account.dens[0]).def;
const layoutOf = owner => owner.denItems
.filter(item => dens.spotOf(item, owner.activeDen))
.map(item => ({def: item.def, version: item.version, ...dens.spotOf(item, owner.activeDen)}));
// lays the account's den in use out as `layout` says, with its own items
// (whether or not they stand in other dens too). with `giveMissing`, items
// it lacks are added to its inventory first. returns how many of the
// layout's items could not be placed.
const applyLayout = (session, layout, giveMissing) => {
const {account} = session;
for (const item of account.denItems.filter(candidate => dens.spotOf(candidate, account.activeDen))) {
delete item.spots[account.activeDen];
}
const used = new Set();
let missing = 0;
for (const spot of layout) {
let item = account.denItems.find(candidate => !used.has(candidate)
&& candidate.def === spot.def && candidate.version === spot.version);
if (!item && giveMissing && account.denItems.length < MAX_DEN_ITEMS && defpacks.denItemIds().includes(spot.def)) {
item = dens.giveDenItem(account, spot.def, spot.version);
}
if (!item) {
missing++;
continue;
}
used.add(item);
item.spots = item.spots || {};
item.spots[account.activeDen] = {cat: spot.cat || 1, x: spot.x, y: spot.y, flip: spot.flip};
}
accounts.save();
dens.sendDenItems(session);
const room = session.room && session.room.denOwner === account ? session.room : null;
for (const occupant of room ? room.sessions : []) {
dens.sendDenState(occupant, room, false);
}
return missing;
};
const den = (session, [action, name]) => {
const {account} = session;
switch (action) {
case "save": {
if (!validName(session, name)) {
return;
}
const existing = named(account.denLayouts, name);
if (!existing && account.denLayouts.length >= MAX_LAYOUTS) {
tell(session, `You already have ${MAX_LAYOUTS} den layouts. Delete one first.`);
return;
}
const items = layoutOf(account);
account.denLayouts = [...account.denLayouts.filter(entry => entry !== existing), {name, den: activeDenDef(account), items}];
accounts.save();
tell(session, `Den layout ${name} saved (${items.length} items).`);
return;
}
case "load": {
const saved = named(account.denLayouts, name);
if (!saved) {
tell(session, `You have no den layout called ${name}. Try !den list.`);
return;
}
if (saved.den !== activeDenDef(account)) {
tell(session, `Layout ${name} was made in another kind of den. Switch to that den first.`);
return;
}
const missing = applyLayout(session, saved.items, false);
tell(session, missing ? `Den layout ${name} loaded. ${missing} of its items are no longer yours.` : `Den layout ${name} loaded.`);
return;
}
case "copy": {
const owner = session.room && session.room.denOwner;
if (!owner || owner === account) {
tell(session, "Stand in the den you want to copy, then type !den copy.");
return;
}
if (activeDenDef(owner) !== activeDenDef(account)) {
tell(session, "Your den in use is a different kind of den. Switch to the same kind first.");
return;
}
const missing = applyLayout(session, layoutOf(owner), true);
tell(session, missing ? `Your den now looks like this one, apart from ${missing} items.` : "Your den now looks like this one.");
return;
}
case "savenew": {
let number = 1;
while (named(account.denLayouts, `layout${number}`)) {
number++;
}
den(session, ["save", `layout${number}`]);
return;
}
// what the den's "clear" button took away (lib/dens.js clearDen keeps it).
case "undo": {
const undo = account.denUndo;
if (!undo || undo.den !== account.activeDen) {
tell(session, undo ? "That was another den. Switch to it first." : "There is nothing to put back.");
return;
}
const missing = applyLayout(session, undo.items, false);
account.denUndo = null;
accounts.save();
tell(session, missing ? `Your den is back as it was, apart from ${missing} items that are no longer yours.` : "Your den is back as it was.");
return;
}
case "delete":
if (!named(account.denLayouts, name)) {
tell(session, `You have no den layout called ${name}.`);
return;
}
account.denLayouts = account.denLayouts.filter(entry => entry.name !== name);
accounts.save();
tell(session, `Den layout ${name} deleted.`);
return;
case "list":
tell(session, account.denLayouts.length ? `Your den layouts: ${account.denLayouts.map(entry => entry.name).join(", ")}` : "You have no saved den layouts yet.");
return;
default:
tell(session, "Use: !den save NAME, !den load NAME, !den list, !den delete NAME, !den copy, !den undo");
}
};
const commands = {
online(session) {
const names = players().filter(other => !blocks.either(other.account, session.account)).map(other => other.account.username);
const shown = names.slice(0, MAX_LISTED).join(", ");
tell(session, `${names.length} playing: ${shown}${names.length > MAX_LISTED ? ", ..." : ""}`);
},
// only for buddies, as with the buddy list's own "go to".
where(session, words) {
const name = words.join(" ");
const target = findOnline(name);
if (!name) {
tell(session, "Use: !where NAME");
}
else if (!session.account.buddies.includes(name.toLowerCase())) {
tell(session, `${name} is not one of your buddies.`);
}
else if (!target || !target.room) {
tell(session, `${name} is not online.`);
}
else {
tell(session, `${target.account.username} is in ${roomTitle(target.room)}.`);
}
},
time(session) {
tell(session, `It is ${new Date().toUTCString()}.`);
},
outfit,
den,
// lib/disguises.js
become(session, [choice]) {
if (!choice || choice === "list") {
tell(session, `You can become: ${disguises.names().join(", ")}. !become off to be yourself.`);
return;
}
if (choice === "off") {
disguises.set(session, 0, refreshForOthers);
tell(session, "You are yourself again.");
return;
}
const id = disguises.find(choice);
if (!id) {
tell(session, `There is no character called ${choice}. Try !become list.`);
return;
}
const changing = disguises.of(session.account) !== 0;
disguises.set(session, id, refreshForOthers);
tell(session, changing ? "Done. It shows when you next walk into a room." : "Done. !become off to be yourself again.");
},
again(session, [choice]) {
const times = choice === undefined ? 1 : Number(choice);
if (!session.lastPurchase || !Number.isInteger(times) || times < 1 || times > MAX_AGAIN) {
tell(session, session.lastPurchase ? `Use: !again N, with N from 1 to ${MAX_AGAIN}` : "Buy something in a shop first; !again then buys more of it.");
return;
}
ask(session, times === 1 ? "Buy one more of the last thing you bought?" : `Buy ${times} more of the last thing you bought?`, () => {
for (let count = 0; count < times; count++) {
buyAgain(session);
}
});
},
go(session, [place]) {
if (!place || place === "list") {
tell(session, `Places: ${Object.keys(PLACES).join(", ")}. Any room also goes by its own name, like sarepia.flag_shop.`);
return;
}
const base = Object.hasOwn(PLACES, place) ? PLACES[place] : place;
if (!defpacks.roomDef(base) || /^(player_den|epic_dens)\./.test(base)) {
tell(session, `There is no place called ${place}. Try !go list.`);
return;
}
travel(session, base);
},
// A look for the room they stand in, a look picked afresh at every
// login, or the list of looks (config.themes).
theme(session, [action, choice]) {
const {account} = session;
const themes = require("../config.js").themes;
if (action === "random" && (choice === "on" || choice === "off")) {
account.randomTheme = choice === "on";
accounts.save();
tell(session, account.randomTheme ? "The lands get a look picked at random each time you log in." : "The lands keep the look you choose.");
}
else if (action === "here" && session.room && !session.room.denOwner && (choice === "off" || Object.hasOwn(themes, choice))) {
if (choice === "off") {
delete account.roomThemes[session.room.base];
}
else {
account.roomThemes[session.room.base] = choice;
}
accounts.save();
reloadRoom(session);
}
else if (action === "list") {
tell(session, `Looks: ${Object.keys(themes).join(", ")}`);
}
else {
tell(session, "Use: !theme list, !theme here NAME, !theme here off, !theme random on, !theme random off");
}
},
// deletes the Jam-A-Grams already read; ones with a gift not yet taken stay.
mail(session, [action]) {
const {account} = session;
if (action !== "clear") {
tell(session, "Use: !mail clear");
return;
}
const before = account.ecards.length;
account.ecards = account.ecards.filter(card => !card.read || card.gift);
accounts.save();
tell(session, `${before - account.ecards.length} read Jam-A-Grams deleted. Open your mail again to see.`);
},
// every music track, the official ones and this server's extras, as den
// items of theirs: what the den's music list offers is what they own.
music(session, [action]) {
const {account} = session;
if (action !== "all") {
tell(session, "Use: !music all");
return;
}
const owned = new Set(account.denItems.map(item => item.def));
const wanted = [...defpacks.musicIds(), ...defpacks.extraMusicIds()].filter(id => !owned.has(id));
const given = wanted.filter(id => dens.giveDenItem(account, id)).length;
dens.sendDenItems(session);
tell(session, given < wanted.length ? `${given} music tracks added. Your den items are full: recycle some for the rest.`
: wanted.length ? `${wanted.length} music tracks added. They are in your den's music list.` : "You have every music track already.");
},
hideblocked(session, [choice]) {
if (choice !== "on" && choice !== "off") {
tell(session, `Blocked players are ${session.account.hideBlocked ? "hidden" : "shown"}. Use: !hideblocked on, or !hideblocked off`);
return;
}
setHideBlocked(session, choice === "on");
tell(session, choice === "on" ? "Players you have blocked are now hidden from you." : "Players you have blocked are shown again.");
},
};
// commands whose words matter as they were typed, capitals and all.
const asTyped = {
// A whisper: only that buddy sees it, wherever they are. standing in
// the same room it is a chat bubble for the two of them; otherwise a
// message box, the one way the game has to show text from afar.
w(session, [name, ...words]) {
const {account} = session;
const said = words.join(" ").slice(0, MAX_WHISPER);
const target = name ? findOnline(name) : null;
if (!name || !said) {
tell(session, "Use: !w BUDDY message");
}
else if (!account.buddies.includes(name.toLowerCase())) {
tell(session, `${name} is not one of your buddies.`);
}
else if (!target || !target.room || blocks.either(target.account, account)
|| !accounts.featureOn(target.account, "chat") || !accounts.featureOn(account, "chat")) {
tell(session, `${name} cannot be whispered to right now.`);
}
else if (target.room === session.room) {
// "xw" makes the next chat bubble the reader's game draws a
// light green one (gui.ChatBalloon in the patched client).
for (const reader of [session, target]) {
reader.sendXt("xw", -1, 1);
reader.sendXt("uc", session.room.id, session.id, said, 0);
}
chatlog.record(account, session.room.name, said, target.account.username);
}
else {
// from afar there is no bubble to draw: a message box, in the same green.
target.sendXt("xw", -1, 0, account.username, said.replace(/%/g, " percent"));
tell(session, `Whispered to ${target.account.username}.`);
chatlog.record(account, session.room ? session.room.name : "", said, target.account.username);
}
},
whisper(session, words) {
asTyped.w(session, words);
},
// "!den name NEW NAME": what the den in use is called on the screen of
// whoever walks in. anything else after "!den" is the usual command.
den(session, [action, ...words]) {
const {account} = session;
const wanted = words.join(" ");
if (action.toLowerCase() !== "name") {
commands.den(session, [action, ...words].map(word => word.toLowerCase()));
}
else if (wanted.toLowerCase() === "off") {
delete account.denNames[account.activeDen];
accounts.save();
tell(session, "This den has its usual name again.");
}
else if (isSilenced(account)) {
tell(session, "You cannot name a den while silenced.");
}
else if (!PLAIN_NAME.test(wanted)) {
tell(session, "Use: !den name NEW NAME (letters, digits and spaces, up to 20), or !den name off");
}
else {
account.denNames[account.activeDen] = wanted;
accounts.save();
tell(session, `This den is now called ${wanted}. It shows the next time someone walks in.`);
}
},
};
return {
// runs `text` if it is a command. returns what to do with the chat line:
// null to swallow it, or the text everyone should see in its place.
run(session, text) {
if (!text.startsWith(PREFIX)) {
return text;
}
const typed = text.slice(PREFIX.length).trim().split(/\s+/);
const [name, ...words] = typed.map(word => word.toLowerCase());
if (Object.hasOwn(asTyped, name) && (name !== "den" || typed.length > 1)) {
asTyped[name](session, typed.slice(1));
return null;
}
if (name === "roll") {
// said out loud, so the room can see it was not made up.
return `(rolls a dice: ${1 + Math.floor(Math.random() * 6)})`;
}
if (!Object.hasOwn(commands, name)) {
return text; // "!!!" and the like are just chat
}
commands[name](session, words);
return null;
},
roomTitle,
};
};