Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed

- **Selling back typed gear from the shop window.** A line of one of your item types, or one of your own items, could be bought but not sold back: the shop window didn't recognize it, and the sale was refused. It now sells back at the shop's buyback rate, and a new browser test sells one and checks the money.
- **A catalogue section that names nothing is now said.** A section like `[rocks]`, which is neither a shelf nor one of your item types, used to be saved quietly where no shop would ever sell it. The preview now says so, listing your types, and its lines are not saved.
- **The bank's windows now follow the theme.** BANK.EXE, PAYROLL.EXE and the GM's BANK_ADMIN.EXE drew in the classic green and grey whatever theme was picked; their boxes, numbers, chart and celebrations now take the theme's colors.
- **The bank's boxes have names a screen reader can say.** PAYROLL.EXE's amount and each player's tick box, BANK_ADMIN.EXE's Balance and Debt, and the amount you withdraw or borrow are now named, and clicking a player's name in PAYROLL.EXE ticks them.
Expand Down Expand Up @@ -104,6 +105,7 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Added

- **One-of-a-kind items sell back.** An item kept out of the shops is never on a shelf, but a player can still sell one to a shop that sells its type, at the item's own price times that shop's buyback rate. A ring sells at an Armorer, never at a Gun Shop.
- **The shop catalogue knows your items.** In a game whose items sell themselves, PREVIEW in SHOP_CATALOGUES now says how many of your items are also on sale, and which lines in your file overrule one ("Pistol: overrules the item, 200 → 260 in the shops"). A line that's exactly one of your items, same price and columns, is left out, since the item already sells itself. So is a line naming an item kept out of the shops. The preview says why for each line, and SAVE leaves out the same lines.
- **PRICE and IN SHOPS in the ITEMS page's files.** In a game with item types of its own, DOWNLOAD EXAMPLE and DOWNLOAD THIS LIST have a price column (for a type without a PRICE column of its own) and an in shops column, yes or no. IMPORT reads them: write - to clear one, and a price or a yes/no that isn't one is skipped and named by its line. A list downloaded and imported back changes nothing.
- **PRICE and IN SHOPS on the ITEMS page.** In a game with item types of its own, every item has a PRICE under its TYPE. If the type has a PRICE column, that column is shown there, once. Beside it, IN SHOPS says whether the shops sell the item: switched off, it's never sold and is tagged NOT SOLD in the list, for a one-of-a-kind ring or a quest item. A new item starts IN SHOPS and asks for its price until it has one. Moving an item to another type keeps its price. A PRICE column in the ITEMS import and downloads comes next.
Expand Down
14 changes: 7 additions & 7 deletions README.md

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion backend/__tests__/shop_items_on_sale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ describe('the lines they make', () => {
'type:armor': [{ id: 'item:jacket', name: 'Leather Jacket', price: 50, fields: { defense: 2 } }],
});
expect([...stock.off]).toEqual(['ring of might', 'rope']);
// Turned off with a price: bought back by a shop of its type, never on a shelf (8a7a3c2); the Rope, with none, not.
expect(stock.buyback).toEqual({ 'type:armor': [{ id: 'item:ring', name: 'Ring of Might', price: 5000 }] });
// Worked out once for a definition.
expect(ownStock(DEF)).toBe(stock);
});
Expand Down Expand Up @@ -139,7 +141,13 @@ describe('in the store, under the uploads', () => {
expect(store.priceOf('type:armor', 'ring_of_might')).toBeNull();
expect(store.labelOf('type:armor', 'ring_of_might')).toBeNull();
expect(store.fieldsOf('type:armor', 'ring_of_might')).toEqual({});
expect(store.findByName('Ring of Might')).toBeNull();
// Found by name only as the item it is, to sell back at its own price, never to buy (8a7a3c2).
expect(store.findByName('Ring of Might')).toEqual({ catalogue: 'type:armor', id: 'item:ring' });
expect(store.priceOf('type:armor', 'item:ring')).toBeNull();
expect(store.sellPriceOf('type:armor', 'item:ring')).toBe(5000);
expect(store.labelOf('type:armor', 'item:ring')).toBe('Ring of Might');
expect(store.sellPriceOf('type:armor', 'ring_of_might')).toBeNull();
expect(store.sellPriceOf('type:armor', 'item:jacket')).toBe(50);
// Still uploaded, for the GM's file.
expect(store.uploadedIn('type:armor').map((e) => e.name)).toEqual(['Ring of Might']);
});
Expand Down Expand Up @@ -233,6 +241,26 @@ describe('at the table', () => {
expect(await balance()).toBe(1100);
});

it('a one-of-a-kind item kept out of the shops: sold back where its type is sold, at its price and the shop\'s rate; never at another (8a7a3c2)', async () => {
await run(db, 'UPDATE character_sheets SET data = ? WHERE username = ?', [JSON.stringify({ inventory: JSON.stringify([{ name: 'Ring of Might', qty: 1, enc: '', bundled: false, carry: 'stowed', location: '', type: 'armor' }]) }), 'kes']);
const kes = await boot('kes', false);
// Not at the General Store, which sells no ARMOR: no ring at a gun shop.
expect(await checkout(kes, goods, { buys: [], sells: [{ catalogue: 'type:armor', id: 'item:ring', qty: 1 }] })).toMatchObject({ ok: false, reason: 'not_sold' });
kes.emitted.length = 0;
// Nor bought, anywhere.
expect(await checkout(kes, arms, { buys: [{ catalogue: 'type:armor', itemId: 'item:ring', qty: 1 }] })).toMatchObject({ ok: false });
kes.emitted.length = 0;
// At the Armorer, at the app's 45%: 5000 → 2250.
expect(await checkout(kes, arms, { buys: [], sells: [{ catalogue: 'type:armor', id: 'item:ring', qty: 1 }] })).toMatchObject({ ok: true, payout: 2250 });
expect(await balance()).toBe(3250);
});

it('the window is told what a shop buys back with the catalogues (8a7a3c2)', async () => {
const kes = await boot('kes', false);
kes.handlers.requestCatalogues();
expect((await last(kes.emitted, 'catalogues')).data.buyback).toEqual({ 'type:armor': [{ id: 'item:ring', name: 'Ring of Might', price: 5000 }] });
});

it('a line uploaded with its name wins: one Pistol, at the file\'s price', async () => {
const gm = await boot('gm', true);
gm.handlers.saveCatalogue({ text: '[WEAPONS]\nname, price, damage\nPistol, 260, 2d6\n' });
Expand Down
4 changes: 3 additions & 1 deletion backend/__tests__/shop_typed_lines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ describe('at the table', () => {
const out = await checkout(kes, store_, { buys: [], sells: [{ catalogue: 'type:guns', id: 'pistol', qty: 1 }] });
expect(out).toMatchObject({ ok: true });
expect(await inventoryOf('kes')).toEqual([]);
expect((await get(db, 'SELECT balance FROM bank_accounts WHERE username = ?', ['kes'])).balance).toBeGreaterThan(1000);
// Exactly the app's 45% of its 250 (8a7a3c2; it said only "more than 1000").
expect(out).toMatchObject({ payout: 112 });
expect((await get(db, 'SELECT balance FROM bank_accounts WHERE username = ?', ['kes'])).balance).toBe(1112);
});

it('GIVE: every type offered, sold anywhere or not; given, a line of its type with its values', async () => {
Expand Down
28 changes: 25 additions & 3 deletions backend/shops/catalogueStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const bookApplies = () => loadedSystem === BOOK_SYSTEM;
* so a published change sells at once. Under the uploaded lines, as they are over the book: a line uploaded with an item's
* name is the GM's latest word and wins; an item turned off keeps a line of its name off the shelves.
*/
const NO_OWN = { catalogues: {}, off: new Set() };
const NO_OWN = { catalogues: {}, off: new Set(), buyback: {} };
let ownSource = () => NO_OWN;
const setOwnSource = (fn) => { ownSource = typeof fn === 'function' ? fn : () => NO_OWN; };
/** Whatever is set, so a test that sets its own can put it back. */
Expand Down Expand Up @@ -95,6 +95,9 @@ const ownIn = (catalogue) => {
return (own().catalogues[String(catalogue || '')] || []).filter((e) => !named.has(builtIn.normaliseName(e.name)));
};

/** The game's items kept out of the shops, by id, that a shop of their type buys back (8a7a3c2): never sold, never priced to buy. */
const buybackOf = (catalogue, itemId) => ((own().buyback || {})[String(catalogue || '')] || []).find((e) => e.id === String(itemId || ''));

/** One entry a shop sells, uploaded or the game's own, by id; undefined for neither (the book is asked apart). */
const sold = (catalogue, itemId) => {
const mine = (uploaded[String(catalogue || '')] || {})[String(itemId || '')];
Expand All @@ -119,10 +122,23 @@ const priceOf = (catalogue, itemId) => {
return bookApplies() ? builtIn.priceOf(catalogue, itemId) : null;
};

/** What a catalogue entry is called. Uploaded first, then the book. */
/**
* What a shop pays for one, before its rate (8a7a3c2): what it sells it for, else an item kept out of the shops at its own
* price, as one-of-a-kind things still sell back. Null for neither.
*/
const sellPriceOf = (catalogue, itemId) => {
const price = priceOf(catalogue, itemId);
if (price !== null) return price;
const kept = buybackOf(catalogue, itemId);
return kept ? kept.price : null;
};

/** What a catalogue entry is called. Uploaded first, then the book; an item kept out of the shops by its own name. */
const labelOf = (catalogue, itemId) => {
const mine = sold(catalogue, itemId);
if (mine) return mine.entry ? mine.entry.name : null;
const kept = buybackOf(catalogue, itemId);
if (kept) return kept.name;
return bookApplies() ? builtIn.labelOf(catalogue, itemId) : null;
};

Expand Down Expand Up @@ -159,6 +175,12 @@ const findByName = (name) => {
if (builtIn.normaliseName(entry.name) === wanted) return { catalogue, id: entry.id };
}
}
// And one kept out of the shops (8a7a3c2), to sell back where its type is sold.
for (const [catalogue, entries] of Object.entries(own().buyback || {})) {
for (const entry of entries) {
if (builtIn.normaliseName(entry.name) === wanted) return { catalogue, id: entry.id };
}
}
return null;
};

Expand Down Expand Up @@ -191,6 +213,6 @@ const overridesIn = (catalogue, entries) => {
module.exports = {
BOOK_SYSTEM, bookApplies, setOwnSource, ownSourceNow,
load, clear, systemLoaded, uploadedIn, hasUploads, entriesIn, overridesIn,
priceOf, labelOf, fieldsOf, findByName,
priceOf, sellPriceOf, labelOf, fieldsOf, findByName,
normaliseName: builtIn.normaliseName,
};
22 changes: 15 additions & 7 deletions backend/shops/ownItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,41 @@ const { normaliseName } = require('./prices');

/** An item's line id: apart from an uploaded line's (a slug of its name), so the two never share one. */
const ITEM_PREFIX = 'item:';
const NONE = Object.freeze({ catalogues: Object.freeze({}), off: new Set() });
const NONE = Object.freeze({ catalogues: Object.freeze({}), off: new Set(), buyback: Object.freeze({}) });
/** Dice read whatever names they read: the builder checked them. */
const ANY_NAME = { known: () => true, refused: (n) => `@${n}` };

const cache = new WeakMap();

/**
* { catalogues: { 'type:<id>': [{ id, name, price, fields }] }, off: Set of names }: each item IN SHOPS with a price, a line
* of its type, its columns' values and its words as its fields, as an uploaded typed line has them; and the names of those
* turned off. Nothing for a system without types of its own, whose shops have no type shelves.
* { catalogues: { 'type:<id>': [{ id, name, price, fields }] }, off: Set of names, buyback: { 'type:<id>': [...] } }: each item
* IN SHOPS with a price, a line of its type, its columns' values and its words as its fields, as an uploaded typed line has
* them; the names of those turned off; and those turned off with a price, which no shelf shows but a shop of their type buys
* back at its rate (8a7a3c2; the user: "a unique item should still be sellable for its set price"). Nothing for a system
* without types of its own, whose shops have no type shelves.
*/
const ownStock = (definition) => {
if (!definition || typeof definition !== 'object' || !ownTypes(definition)) return NONE;
if (cache.has(definition)) return cache.get(definition);
const catalogues = {};
const off = new Set();
const buyback = {};
for (const item of itemsOf(definition)) {
const name = typeof item.label === 'string' ? item.label.trim() : '';
if (!name || typeof item.id !== 'string') continue;
if (!inShops(definition, item)) { off.add(normaliseName(name)); continue; }
const price = itemPrice(definition, item);
const shelf = `type:${typeNamed(definition, item.type).id}`;
if (!inShops(definition, item)) {
off.add(normaliseName(name));
if (isPrice(price)) (buyback[shelf] ||= []).push({ id: ITEM_PREFIX + item.id, name, price });
continue;
}
if (!isPrice(price)) continue;
const description = typeof item.description === 'string' ? item.description.trim() : '';
const fields = { ...itemValues(definition, item, ANY_NAME), ...(description ? { description } : {}) };
(catalogues[`type:${typeNamed(definition, item.type).id}`] ||= []).push({ id: ITEM_PREFIX + item.id, name, price, fields });
(catalogues[shelf] ||= []).push({ id: ITEM_PREFIX + item.id, name, price, fields });
}
const out = { catalogues, off };
const out = { catalogues, off, buyback };
cache.set(definition, out);
return out;
};
Expand Down
2 changes: 1 addition & 1 deletion backend/shops/owned.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const tally = (into, name, source, extra) => {
id: hit ? hit.id : null,
label: hit ? prices.labelOf(hit.catalogue, hit.id) : label,
// What one is worth on a shelf. Null for anything no catalogue carries.
unitPrice: hit ? prices.priceOf(hit.catalogue, hit.id) : null,
unitPrice: hit ? prices.sellPriceOf(hit.catalogue, hit.id) : null,
qty: 0,
/** Every place one of these is sitting, so a sale knows what to empty. */
at: [],
Expand Down
3 changes: 2 additions & 1 deletion backend/shops/sell.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ const planSale = ({ data, items, catalogues, locationPct, globalPct, system }) =
takenSoFar.set(key, already + want);

// Priced from the book table on this side, never from anything the client sent.
const unit = catalogue && id ? prices.priceOf(catalogue, id) : null;
// One-of-a-kind items too (8a7a3c2): kept out of the shops, bought back at their own price.
const unit = catalogue && id ? prices.sellPriceOf(catalogue, id) : null;
const each = buyback.buybackValue(unit, pct);
payout += each * want;
sold.push({ catalogue, id, label: line.label, qty: want, each });
Expand Down
3 changes: 2 additions & 1 deletion backend/sockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2569,7 +2569,8 @@ module.exports = (io, db, { elevatedUsers, emitUpdate, recordAction }) => {
for (const c of buildingTypes.CATALOGUES) out[c.id] = shopPrices.entriesIn(c.id);
// Each of the game's own item types too (8a7a1), its lines with their columns, for DOWNLOAD CURRENT and the shops.
for (const t of ownTypesIn(system)) out[`type:${t.id}`] = shopPrices.entriesIn(`type:${t.id}`);
socket.emit('catalogues', { entries: out });
// And the game's items kept out of the shops, which no shelf shows but a shop of their type buys back (8a7a3c2).
socket.emit('catalogues', { entries: out, buyback: ownStock(customSystems.definitionOf(system)).buyback });
});
};

Expand Down
2 changes: 1 addition & 1 deletion e2e/COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ panel), so a control that looks fine but does nothing is caught for all of them.
| GIVE ITEM and shop catalogues | a catalogue typed in, an item given with a note | `shops_give`, `shop_catalogue_*` | `player-journey`, `shop-cart-items` (a catalogue saved while a player is online, bought by them through the cart, 8n1), `shop-cart` (CWN's book: bought and sold back, 8n1), `give-item` (a ring and two ropes given, on the sheet as bought ones are, in GIVEN, 8n2) | **AT THE TABLE** |
| Shop catalogues by item type | a section per item type (8a7a1), its columns checked, blanks kept | `shop_catalogue_types`, `catalogueTypes` | `catalogue-types`, `shop-typed-lines` (bought at the General Store and given, landing typed, 8a7a2) | **AT THE TABLE** |
| An item type's SOLD AT | the kinds of shop that sell its lines (8a7a2), the General Store where unsaid; set in the builder in 8a7b | `shop_typed_lines`, `typedShelves` | `shop-typed-lines` (unsaid: the General Store) | **AT THE TABLE** unsaid; a kind of shop picked waits for 8a7b's box |
| An item's PRICE and IN SHOPS | priced and IN SHOPS, it sells itself at its type's shops (8a7a3a); off, never sold, not even from a file; a type's PRICE column read as its price; set on ITEMS under TYPE, NOT SOLD in the list, a new item's price asked for (8a7a3b1); in the ITEMS import and downloads, price and in shops columns (8a7a3b2) | `shop_items_on_sale`, `itemPrice`, `ItemsPage`, `system_builder_items_import`, `itemsImport` | `shop-items-on-sale` (a type's PRICE column; a ring turned off), `builder-item-price` (set by clicks), `builder-items-import` (imported priced and IN SHOPS) | **AT THE TABLE** |
| An item's PRICE and IN SHOPS | priced and IN SHOPS, it sells itself at its type's shops (8a7a3a); off, never sold, not even from a file, but bought back at its price by a shop of its type (8a7a3c2); a type's PRICE column read as its price; set on ITEMS under TYPE, NOT SOLD in the list, a new item's price asked for (8a7a3b1); in the ITEMS import and downloads, price and in shops columns (8a7a3b2) | `shop_items_on_sale`, `itemPrice`, `ItemsPage`, `system_builder_items_import`, `itemsImport` | `shop-items-on-sale` (a type's PRICE column; a ring turned off; sold back from the window at 50%), `builder-item-price` (set by clicks), `builder-items-import` (imported priced and IN SHOPS) | **AT THE TABLE** |
| A catalogue file held against the game's items | an exact copy of an item left out, an item kept out of the shops left out, one of an item's name overruling it, ALSO ON SALE FROM ITEMS (8a7a3c1) | `shop_items_on_sale`, `CatalogueWindow` | `catalogue-and-items` | **AT THE TABLE** |
| Building types and catalogues renamed or off | | `buildings` | | **GAP** |
| Currencies | several, decimals, a catalogue's currency, the cart per currency, PAYROLL | `currencies`, `currency_icons`, `catalogue_currency`, `bank_currencies`, `shop_checkout_currencies` | | **GAP** |
Expand Down
Loading
Loading