Files
burrow-server/lib/items.js
T
Burrowdev aa455f52d0 perf: save only changed accounts, cache missing assets, lighter logging and backup
Ping on prod jumped to seconds because node's main thread was busy most
of every second.

- accounts.save(account, ...) names the changed accounts, which are
  written within a second; the rest are looked at a few at a time
  (10 ms a save for those in play, 5 ms for the others). One save under
  prod-like load: 289 ms before, 16 ms after.
- an asset the CDN does not have gets a cacheable 404 and is logged once
- the proxy's own connections no longer count against the web cap, which
  had node reset requests nginx had already let in
- a repeated client error is written once in ten minutes; the panel's
  log buffer trims in batches
- the daily backup uses sqlite's online backup off the main thread
2026-10-17 11:57:40 +03:00

168 lines
8.0 KiB
JavaScript

"use strict";
const accounts = require("./accounts.js");
const achievements = require("./achievements.js");
const defpacks = require("./defpacks.js");
const {CURRENCIES, MAX_ITEMS, MAX_WORN, activeAvatar, addClothing, sendClothingAdded, sendClothingRemoved, resolvePurchase} = require("./player.js");
// clothing: buying, wearing and recycling (client side: ItemXtCommManager).
const DEF_ITEM = 1000;
const DEF_DIAMOND = 1054;
const DEF_COMBINED_LIST = 1060;
const DEF_CURRENCY_EXCHANGE = 1051;
const SHOP_TYPES = [DEF_ITEM, DEF_DIAMOND, DEF_COMBINED_LIST]; // kinds of shop that sell clothing
const TYPE_CLOTHING = "1";
const LAYER_PATTERN = "2";
const LAYER_EYES = "3";
const MAX_RECYCLED_AT_ONCE = 500;
const MAX_USED_AT_ONCE = 200; // pieces put on or taken off in one message
const RECYCLE_FRACTION = 0.3; // matches recyclePercentage sent at login
module.exports = ({others, sendItemList}) => {
// ItemXtCommManager.itemBuyResponse: [status, shopId, newAmount, currencyType]
const buyFailed = (session, shopId) => session.sendXt("ib", -1, 0, shopId, "", 0);
return {
// shop.requestItemBuy: [shopId, 1, defId, colorIndex, isDiamondItem,
// denItemInvIdx, username]. for an item priced in diamonds defId is its
// diamond def's id.
ib(session, roomId, [shopId, , defId, colorIndex, isDiamondItem]) {
const {account} = session;
const purchase = resolvePurchase(defId, isDiamondItem === "1", "clothing", id => {
const own = defpacks.itemDef(id);
return {cost: own ? Number(own.value) : 0, currencyType: own ? Number(own.currencyType) : -1};
});
const def = purchase && defpacks.itemDef(purchase.defId);
const colors = def ? String(def.colors).split(",") : [];
const currency = purchase && CURRENCIES[purchase.currencyType];
// only clothing bought with a single currency is supported; combined
// costs are refused.
if (!SHOP_TYPES.includes(defpacks.listType(shopId)) || !def || def.type !== TYPE_CLOTHING
|| !currency || !/^\d{1,3}$/.test(colorIndex) || colors[colorIndex] === undefined || account.items.length >= MAX_ITEMS
|| (account.currency[currency] || 0) < purchase.cost) {
buyFailed(session, shopId);
return;
}
account.currency[currency] -= purchase.cost;
achievements.currencyChanged(account, currency, -purchase.cost);
achievements.add(account, achievements.VARS.clothesBought);
const item = addClothing(account, def.id, colors[colorIndex]);
accounts.save(account);
sendClothingAdded(session, item);
session.sendXt("ib", -1, 1, shopId, account.currency[currency], purchase.currencyType);
},
// ItemXtCommManager.requestItemUse: [onCount, offCount, ...invIdxOn, ...invIdxOff]
// for the animal in use. eyes and patterns are sent as minus their def id.
iu(session, roomId, params) {
const {account} = session;
const onCount = Number(params[0]);
const offCount = Number(params[1]);
const ids = params.slice(2).map(Number);
if (!Number.isInteger(onCount) || !Number.isInteger(offCount) || onCount < 0 || offCount < 0
|| ids.length !== onCount + offCount || ids.length > MAX_USED_AT_ONCE || !ids.every(Number.isInteger)) {
session.sendXt("iu", -1, 0);
return;
}
const avatar = activeAvatar(account);
// for "!outfit undo" (lib/commands.js).
account.outfitUndo = {avInvId: avatar.avInvId, items: account.items.filter(item => item.on.includes(avatar.avInvId)).map(item => item.inv)};
// returns the item's def id when the change was one this player can make.
const setWorn = (invIdx, worn) => {
if (invIdx < 0) {
const def = defpacks.itemDef(-invIdx);
if (def && def.layerId === LAYER_EYES && worn) {
avatar.eyes = -invIdx;
}
else if (def && def.layerId === LAYER_PATTERN && (worn || avatar.pattern === -invIdx)) {
avatar.pattern = worn ? -invIdx : 0;
}
return def ? -invIdx : null;
}
const item = account.items.find(candidate => candidate.inv === invIdx);
if (item) {
item.on = item.on.filter(avInvId => avInvId !== avatar.avInvId);
// no more than MAX_WORN on one animal: what is over stays off.
if (worn && account.items.filter(candidate => candidate.on.includes(avatar.avInvId)).length >= MAX_WORN) {
return null;
}
if (worn) {
item.on.push(avatar.avInvId);
}
}
return item ? item.def : null;
};
const off = ids.slice(onCount).filter(invIdx => setWorn(invIdx, false) !== null);
const on = ids.slice(0, onCount).map(invIdx => [invIdx, setWorn(invIdx, true)]).filter(([, def]) => def !== null);
accounts.save(account);
// ItemXtCommManager.itemUseResponse
session.sendXt("iu", -1, 1);
// the reply only closes the editor. the animal in the room is redrawn
// from this: itemsChangedListResponse, "3" = what is worn changed,
// [3, inUse, perUserAvId, username, colour, onCount, offCount,
// (invIdx, defId)..., offInvIdx...]. without it the player goes on
// seeing the old outfit until the next room.
session.sendXt("il", -1, 3, 1, avatar.perUserAvId, account.username, 0, on.length, off.length, ...on.flat(), ...off);
for (const other of others(session)) {
sendItemList(other, session, false);
}
},
// shop.requestCurrencyExchange: [shopId, exchangeDefId], one of the deals
// in the currency exchange defPack (gems for tickets, diamonds for gems).
// the reply (currencyExchangeResponse -> Shop.confirmPurchase) is
// [status, new amount of the currency paid with]; the currency received
// gets its own update.
ce(session, roomId, [shopId, defId]) {
const {account} = session;
const def = defpacks.exchangeDef(defId);
const paid = def && CURRENCIES[def.costType];
const received = def && CURRENCIES[def.resultType];
if (defpacks.listType(shopId) !== DEF_CURRENCY_EXCHANGE || !paid || !received || (account.currency[paid] || 0) < def.cost) {
session.sendXt("ce", -1, 0, 0);
return;
}
account.currency[paid] -= def.cost;
account.currency[received] = (account.currency[received] || 0) + def.result;
achievements.currencyChanged(account, paid, -def.cost);
achievements.currencyChanged(account, received, def.result);
accounts.save(account);
// ItemXtCommManager.currencyUpdateResponse: [currencyType, newAmount, reason]
session.sendXt("cu", -1, def.resultType, account.currency[received], 0);
session.sendXt("ce", -1, 1, account.currency[paid]);
},
// ItemXtCommManager.requestItemRecycle: [invIdx, ...]. the reply
// (itemRecycleResponse) is [count, newGemTotal, ...recycledInvIdx], and
// the recycling window then waits, behind its spinner, for the item
// list to change (ItemXtCommManager.checkIlCallbacks).
ir(session, roomId, params) {
const {account} = session;
const recycled = [];
for (const invIdx of new Set(params.slice(0, MAX_RECYCLED_AT_ONCE).map(Number))) {
const index = account.items.findIndex(item => item.inv === invIdx);
if (index === -1) {
continue;
}
const def = defpacks.itemDef(account.items[index].def);
const refund = def ? Number(def.recycleValue) || Math.floor(Number(def.value) * RECYCLE_FRACTION) : 0;
if (def && def.currencyType === "0") {
account.currency.gems = (account.currency.gems || 0) + refund;
achievements.currencyChanged(account, "gems", refund);
}
account.items.splice(index, 1);
recycled.push(invIdx);
}
accounts.save(account);
session.sendXt("ir", -1, recycled.length, account.currency.gems || 0, ...recycled);
if (recycled.length) {
sendClothingRemoved(session, recycled);
for (const other of others(session)) {
sendItemList(other, session, false);
}
}
},
};
};