Files
Snipzil 5c9b2e0066 feat(world): expand events, rewards, and den behavior
Add owner/developer room-effect controls and chat shortcuts, staff movement that bypasses full or locked destinations, in-game moderation entry points, theatre video synchronization, and shared room effects.

Restore game and trade popups, reliably award refreshments and game rewards, support pet play outside parties, correct den placement categories, improve den item persistence, and refresh custom-item and masterpiece behavior.

Harden room, party, pet, minigame, and item state handling with matching feature and restored-system regression coverage.
2026-10-19 10:09:50 -04:00

113 lines
5.3 KiB
JavaScript

"use strict";
const defpacks = require("./defpacks.js");
const wearRules = require("./wearrules.js");
// accessors over an account's animals, pets and items (shape: accounts.js).
// SbiConstants.CURRENCY_NAMES; an item's currencyType indexes into this.
const CURRENCIES = ["gems", "tickets", "orbs", "diamonds", "straw", "bamboo", "wood", "stone", "silver", "gold", "gemstone", "ecoCredits"];
const CURRENCY_DIAMONDS = 3;
// what a diamond def's refId points at (DiamondItem.isAccessory and friends).
const DIAMOND_TYPES = {clothing: "0", denItem: "1", pet: "2", avatar: "3", denRoom: "4", customAvatar: "5"};
// shops send a diamond def id instead of the item's own def id when the item
// is priced in diamonds. resolves either form to {defId, cost, currencyType},
// with `ownPrice(defId)` giving {cost, currencyType} for an ordinary item; null
// when the diamond def does not exist or is not of the expected kind.
const resolvePurchase = (defId, isDiamond, kind, ownPrice) => {
if (!isDiamond) {
return {defId: Number(defId), ...ownPrice(defId)};
}
const diamond = defpacks.diamondDef(defId);
return diamond && diamond.type === DIAMOND_TYPES[kind]
? {defId: Number(diamond.refId), cost: Number(diamond.cost), currencyType: CURRENCY_DIAMONDS}
: null;
};
const activeAvatar = account => account.avatars.find(avatar => avatar.perUserAvId === account.activeAvatar) || account.avatars[0];
// the requested animal, or the one in use when the id is unknown.
const avatarById = (account, perUserAvId) =>
account.avatars.find(avatar => avatar.perUserAvId === Number(perUserAvId)) || activeAvatar(account);
const activePet = account => account.pets.find(pet => pet.inv === activeAvatar(account).petInvId) || null;
// The persisted state is also filtered here, not only where a player tries
// to put an item on. That keeps an old/corrupt pairing from reaching player
// cards or other people in a room while a dashboard rule is being applied.
const wornItems = (account, avatar) => account.items.filter(item => item.on.includes(avatar.avInvId)
&& wearRules.mayWear(item.def, avatar.type));
// item.EquippedAvatars: comma-separated 32-bit words, one bit per avInvId of
// an animal wearing the item.
// With an account, omit stale pairings that are no longer allowed. The full
// inventory protocol otherwise exposes those bits and a later avatar switch
// can appear to revive a prohibited item.
const equippedBits = (item, account = null) => {
const words = [0];
const wornBy = account ? item.on.filter(avInvId => {
const avatar = account.avatars.find(candidate => candidate.avInvId === avInvId);
return avatar && wearRules.mayWear(item.def, avatar.type);
}) : item.on;
for (const avInvId of wornBy) {
const word = avInvId >>> 5;
while (words.length <= word) {
words.push(0);
}
words[word] = (words[word] | (1 << (avInvId & 31))) >>> 0;
}
return words.join(",");
};
// how much an account may hold, as the patched client counts
// (shop.ShopManager), and how many pieces one animal may wear at once: the
// seven places clothes go, three to a place, and a few to spare. everyone in
// a room is sent what each animal there wears.
const MAX_ITEMS = 5000;
const MAX_DEN_ITEMS = 5000;
const MAX_WORN = 24;
// has the account room for `count` more? `kind` is "items" (clothing) or "denItems".
const hasRoom = (account, kind, count = 1) => account[kind].length + count <= (kind === "items" ? MAX_ITEMS : MAX_DEN_ITEMS);
// adds a clothing item to an account's inventory (the caller saves); null
// when the inventory is full.
const addClothing = (account, defId, color) => {
if (!hasRoom(account, "items")) {
return null;
}
const item = {inv: account.nextInv++, def: Number(defId), color: Number(color), on: []};
account.items.push(item);
return item;
};
// ItemXtCommManager.itemsChangedListResponse ("2" = inventory changed): tells
// a connected player about a new item. [type, inUse, perUserAvId, username,
// color, addedCount, removedCount, (invIdx, defId)..., removedInvIdx...]
const sendClothingAdded = (session, item) => {
if (!item) {
return; // the inventory was full
}
session.sendXt("il", -1, 2, 0, activeAvatar(session.account).perUserAvId, session.account.username, item.color, 1, 0, item.inv, item.def);
};
// the same message, for items that left the inventory (one inventory id or
// a list of them).
const sendClothingRemoved = (session, inv) => {
const gone = [].concat(inv);
session.sendXt("il", -1, 2, 0, activeAvatar(session.account).perUserAvId, session.account.username, 0, 0, gone.length, ...gone);
};
// what goes in the "days since last login" field of a player's card. the
// patched client (swf-src/BuddyCard.as) takes it as the time the player was
// last seen, in seconds, to show as a time of day and a date; -2 is "online
// now" and -1 "unknown", which the official client showed as "More than 30
// Days".
const LAST_SEEN_ONLINE = -2;
const LAST_SEEN_UNKNOWN = -1;
const lastSeenField = (account, isOnline) => (isOnline ? LAST_SEEN_ONLINE : account.lastSeen || account.lastLogin || LAST_SEEN_UNKNOWN);
const isUint = text => /^\d{1,10}$/.test(text) && Number(text) <= 0xFFFFFFFF;
module.exports = {MAX_ITEMS, MAX_DEN_ITEMS, MAX_WORN, hasRoom, CURRENCIES, DIAMOND_TYPES, resolvePurchase, activeAvatar, avatarById, activePet, wornItems, equippedBits, addClothing, sendClothingAdded, sendClothingRemoved, lastSeenField, isUint};