BlueGit

timfa questward

Merge branch 'feature/gift-specific-potions'

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
087584f1e1ea7e813c12554b63d90168632f80d1
4 files changed +152 −6
client/src/editor/giftsModal.js +10 −5
@@ -1,6 +1,7 @@
1 1 import { listAllItemPickerEntries, itemDefinition, ItemId } from "/shared/definitions/items.js";
2 2 import {
3 3 listRecipePaperPickerEntries,
4 listPotionPickerEntries,
4 5 potionKeyFromListingKey
5 6 } from "/shared/market/listingKey.js";
6 7 import {
@@ -13,24 +14,28 @@ import { formatLetterSignature } from "/shared/courier/systemLetters.js";
13 14 function giftItemPickerEntries ()
14 15 {
15 16 const recipes = listRecipePaperPickerEntries();
17 const potions = listPotionPickerEntries();
16 18 const base = listAllItemPickerEntries()
17 .filter((item) => item.itemId !== ItemId.recipePaper)
19 .filter((item) => item.itemId !== ItemId.recipePaper && item.itemId !== ItemId.potion)
18 20 .map((item) => ({
19 21 ...item,
20 22 listingKey: null,
21 23 potionKey: null
22 24 }));
23 return [...base, ...recipes]
25 return [...base, ...recipes, ...potions]
24 26 .sort((a, b) => a.displayName.localeCompare(b.displayName) || (a.itemId - b.itemId));
25 27 }
26 28
27 29 function giftRowKey (row)
28 30 {
31 const itemId = row?.itemId | 0;
32 if (itemId === ItemId.recipePaper && row?.potionKey)
33 { return `recipe:${row.potionKey}`; }
34 if (itemId === ItemId.potion && row?.potionKey)
35 { return `potion:${row.potionKey}`; }
29 36 if (row?.listingKey)
30 37 { return `listing:${row.listingKey}`; }
31 if (row?.potionKey)
32 { return `recipe:${row.potionKey}`; }
33 return `item:${row?.itemId | 0}`;
38 return `item:${itemId}`;
34 39 }
35 40
36 41 export function initGiftsModal ({ socket, MessageType })
@@ -1,6 +1,7 @@
1import { listAllItemPickerEntries, itemDefinition, ItemId } from "/shared/definitions/items.js";
1import { listAllItemPickerEntries, itemDefinition, ItemId } from "/shared/definitions/items.js";
2import {
2import {
3 listRecipePaperPickerEntries,
3 listRecipePaperPickerEntries,
4 listPotionPickerEntries,
4 potionKeyFromListingKey
5 potionKeyFromListingKey
5} from "/shared/market/listingKey.js";
6} from "/shared/market/listingKey.js";
6import {
7import {
@@ -13,24 +14,28 @@ import { formatLetterSignature } from "/shared/courier/systemLetters.js";
13function giftItemPickerEntries ()
14function giftItemPickerEntries ()
14{
15{
15 const recipes = listRecipePaperPickerEntries();
16 const recipes = listRecipePaperPickerEntries();
17 const potions = listPotionPickerEntries();
16 const base = listAllItemPickerEntries()
18 const base = listAllItemPickerEntries()
17 .filter((item) => item.itemId !== ItemId.recipePaper)
19 .filter((item) => item.itemId !== ItemId.recipePaper && item.itemId !== ItemId.potion)
18 .map((item) => ({
20 .map((item) => ({
19 ...item,
21 ...item,
20 listingKey: null,
22 listingKey: null,
21 potionKey: null
23 potionKey: null
22 }));
24 }));
23 return [...base, ...recipes]
25 return [...base, ...recipes, ...potions]
24 .sort((a, b) => a.displayName.localeCompare(b.displayName) || (a.itemId - b.itemId));
26 .sort((a, b) => a.displayName.localeCompare(b.displayName) || (a.itemId - b.itemId));
25}
27}
26
28
27function giftRowKey (row)
29function giftRowKey (row)
28{
30{
31 const itemId = row?.itemId | 0;
32 if (itemId === ItemId.recipePaper && row?.potionKey)
33 { return `recipe:${row.potionKey}`; }
34 if (itemId === ItemId.potion && row?.potionKey)
35 { return `potion:${row.potionKey}`; }
29 if (row?.listingKey)
36 if (row?.listingKey)
30 { return `listing:${row.listingKey}`; }
37 { return `listing:${row.listingKey}`; }
31 if (row?.potionKey)
38 return `item:${itemId}`;
32 { return `recipe:${row.potionKey}`; }
33 return `item:${row?.itemId | 0}`;
34}
39}
35
40
36export function initGiftsModal ({ socket, MessageType })
41export function initGiftsModal ({ socket, MessageType })
server/src/gifts/service.js +52 −1
@@ -1,7 +1,7 @@
1 1 import { randomUUID } from "node:crypto";
2 2 import { ItemId, itemDefinition, LETTER_MAX_BODY_CHARS } from "../../../shared/definitions/items.js";
3 3 import { alchemyPotionDefinition } from "../../../shared/definitions/alchemy.js";
4 import { recipePaperBody } from "../../../shared/alchemy/recipes.js";
4 import { recipePaperBody, potionItemInstance } from "../../../shared/alchemy/recipes.js";
5 5 import {
6 6 normalizeBuyListingKey,
7 7 potionKeyFromListingKey,
@@ -111,6 +111,37 @@ export class GiftService
111 111 continue;
112 112 }
113 113
114 if (entry.itemId === ItemId.potion)
115 {
116 const potion = alchemyPotionDefinition(entry.potionKey);
117 if (!potion || potion.outputItemId)
118 { throw userError("Pick a specific potion brew, not a blank potion bottle."); }
119 const instance = potionItemInstance({
120 potionKey: potion.potionKey,
121 unstable: false,
122 displayName: potion.displayName,
123 color: potion.color,
124 fillLevel: potion.fillLevel,
125 effect: potion.effect,
126 effectKey: potion.effect?.type ?? potion.potionKey,
127 minAlchemyLevel: potion.minAlchemyLevel ?? 1
128 });
129 const stack = attachOrigins({
130 itemId: ItemId.potion,
131 quantity: entry.quantity,
132 instance,
133 origins: [{ origin: giftItemOrigin(ItemId.potion), quantity: entry.quantity }]
134 });
135 this.#creditBank(characterId, stack, def);
136 credited.push({
137 itemId: entry.itemId,
138 quantity: entry.quantity,
139 displayName: potion.displayName,
140 potionKey: entry.potionKey
141 });
142 continue;
143 }
144
114 145 const rules = stackRulesFor(def, { inVault: true });
115 146 if (rules.uniqueInstance || !rules.stackable)
116 147 {
@@ -257,6 +288,26 @@ function normalizeGiftItems (raw)
257 288 }
258 289 continue;
259 290 }
291 if (itemId === ItemId.potion)
292 {
293 const potionKey = String(row?.potionKey ?? "").trim();
294 const potion = potionKey ? alchemyPotionDefinition(potionKey) : null;
295 if (!potion || potion.outputItemId)
296 { continue; }
297 const key = `potion:${potionKey}`;
298 const prev = merged.get(key);
299 if (prev)
300 { prev.quantity += quantity; }
301 else
302 {
303 merged.set(key, {
304 itemId,
305 quantity,
306 potionKey
307 });
308 }
309 continue;
310 }
260 311 const key = `item:${itemId}`;
261 312 const prev = merged.get(key);
262 313 if (prev)
@@ -1,7 +1,7 @@
1import { randomUUID } from "node:crypto";
1import { randomUUID } from "node:crypto";
2import { ItemId, itemDefinition, LETTER_MAX_BODY_CHARS } from "../../../shared/definitions/items.js";
2import { ItemId, itemDefinition, LETTER_MAX_BODY_CHARS } from "../../../shared/definitions/items.js";
3import { alchemyPotionDefinition } from "../../../shared/definitions/alchemy.js";
3import { alchemyPotionDefinition } from "../../../shared/definitions/alchemy.js";
4import { recipePaperBody } from "../../../shared/alchemy/recipes.js";
4import { recipePaperBody, potionItemInstance } from "../../../shared/alchemy/recipes.js";
5import {
5import {
6 normalizeBuyListingKey,
6 normalizeBuyListingKey,
7 potionKeyFromListingKey,
7 potionKeyFromListingKey,
@@ -111,6 +111,37 @@ export class GiftService
111 continue;
111 continue;
112 }
112 }
113
113
114 if (entry.itemId === ItemId.potion)
115 {
116 const potion = alchemyPotionDefinition(entry.potionKey);
117 if (!potion || potion.outputItemId)
118 { throw userError("Pick a specific potion brew, not a blank potion bottle."); }
119 const instance = potionItemInstance({
120 potionKey: potion.potionKey,
121 unstable: false,
122 displayName: potion.displayName,
123 color: potion.color,
124 fillLevel: potion.fillLevel,
125 effect: potion.effect,
126 effectKey: potion.effect?.type ?? potion.potionKey,
127 minAlchemyLevel: potion.minAlchemyLevel ?? 1
128 });
129 const stack = attachOrigins({
130 itemId: ItemId.potion,
131 quantity: entry.quantity,
132 instance,
133 origins: [{ origin: giftItemOrigin(ItemId.potion), quantity: entry.quantity }]
134 });
135 this.#creditBank(characterId, stack, def);
136 credited.push({
137 itemId: entry.itemId,
138 quantity: entry.quantity,
139 displayName: potion.displayName,
140 potionKey: entry.potionKey
141 });
142 continue;
143 }
144
114 const rules = stackRulesFor(def, { inVault: true });
145 const rules = stackRulesFor(def, { inVault: true });
115 if (rules.uniqueInstance || !rules.stackable)
146 if (rules.uniqueInstance || !rules.stackable)
116 {
147 {
@@ -257,6 +288,26 @@ function normalizeGiftItems (raw)
257 }
288 }
258 continue;
289 continue;
259 }
290 }
291 if (itemId === ItemId.potion)
292 {
293 const potionKey = String(row?.potionKey ?? "").trim();
294 const potion = potionKey ? alchemyPotionDefinition(potionKey) : null;
295 if (!potion || potion.outputItemId)
296 { continue; }
297 const key = `potion:${potionKey}`;
298 const prev = merged.get(key);
299 if (prev)
300 { prev.quantity += quantity; }
301 else
302 {
303 merged.set(key, {
304 itemId,
305 quantity,
306 potionKey
307 });
308 }
309 continue;
310 }
260 const key = `item:${itemId}`;
311 const key = `item:${itemId}`;
261 const prev = merged.get(key);
312 const prev = merged.get(key);
262 if (prev)
313 if (prev)
server/tests/gifts.test.js +64 −0
@@ -266,3 +266,67 @@ test("admin gift rejects blank recipe paper without a potion key", () =>
266 266 finally
267 267 { harness.close(); }
268 268 });
269
270 test("admin gift can credit a specific potion brew", () =>
271 {
272 const harness = makeSimulation({ workerCount: 1 });
273 try
274 {
275 placeBank(harness.worldDb, 8, 8);
276 harness.simulation.courier.refreshBankIndex();
277 const character = makeCharacter(harness.stateDb, {
278 tileX: 8,
279 tileY: 8,
280 firstName: "Sip",
281 lastName: "Tester"
282 });
283 const gifts = new GiftService(harness.stateDb, harness.simulation);
284 const result = gifts.sendGift({
285 characterId: character.character_id,
286 message: "Drink responsibly.",
287 items: [{
288 itemId: ItemId.potion,
289 quantity: 3,
290 potionKey: "lesserHealthTonic"
291 }]
292 });
293 assert.equal(result.ok, true);
294 assert.equal(result.items[0].displayName, "Lesser health tonic");
295
296 const vault = harness.stateDb.getBag(character.character_id).vault ?? [];
297 const stack = vault.find((slot) =>
298 slot?.itemId === ItemId.potion
299 && slot.instance?.potionKey === "lesserHealthTonic"
300 );
301 assert.ok(stack);
302 assert.equal(stack.quantity, 3);
303 assert.equal(stack.instance.kind, "potion");
304 assert.equal(stack.instance.displayName, "Lesser health tonic");
305 assert.equal(stack.instance.effect?.type, "health-regen");
306 assert.equal(
307 originLabel(stack.origins?.[0]?.origin),
308 "Brewed by John Questward"
309 );
310 }
311 finally
312 { harness.close(); }
313 });
314
315 test("admin gift rejects blank potion without a potion key", () =>
316 {
317 const harness = makeSimulation({ workerCount: 1 });
318 try
319 {
320 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
321 const gifts = new GiftService(harness.stateDb, harness.simulation);
322 assert.throws(
323 () => gifts.sendGift({
324 characterId: character.character_id,
325 items: [{ itemId: ItemId.potion, quantity: 1 }]
326 }),
327 /at least one item/i
328 );
329 }
330 finally
331 { harness.close(); }
332 });
@@ -266,3 +266,67 @@ test("admin gift rejects blank recipe paper without a potion key", () =>
266 finally
266 finally
267 { harness.close(); }
267 { harness.close(); }
268});
268});
269
270test("admin gift can credit a specific potion brew", () =>
271{
272 const harness = makeSimulation({ workerCount: 1 });
273 try
274 {
275 placeBank(harness.worldDb, 8, 8);
276 harness.simulation.courier.refreshBankIndex();
277 const character = makeCharacter(harness.stateDb, {
278 tileX: 8,
279 tileY: 8,
280 firstName: "Sip",
281 lastName: "Tester"
282 });
283 const gifts = new GiftService(harness.stateDb, harness.simulation);
284 const result = gifts.sendGift({
285 characterId: character.character_id,
286 message: "Drink responsibly.",
287 items: [{
288 itemId: ItemId.potion,
289 quantity: 3,
290 potionKey: "lesserHealthTonic"
291 }]
292 });
293 assert.equal(result.ok, true);
294 assert.equal(result.items[0].displayName, "Lesser health tonic");
295
296 const vault = harness.stateDb.getBag(character.character_id).vault ?? [];
297 const stack = vault.find((slot) =>
298 slot?.itemId === ItemId.potion
299 && slot.instance?.potionKey === "lesserHealthTonic"
300 );
301 assert.ok(stack);
302 assert.equal(stack.quantity, 3);
303 assert.equal(stack.instance.kind, "potion");
304 assert.equal(stack.instance.displayName, "Lesser health tonic");
305 assert.equal(stack.instance.effect?.type, "health-regen");
306 assert.equal(
307 originLabel(stack.origins?.[0]?.origin),
308 "Brewed by John Questward"
309 );
310 }
311 finally
312 { harness.close(); }
313});
314
315test("admin gift rejects blank potion without a potion key", () =>
316{
317 const harness = makeSimulation({ workerCount: 1 });
318 try
319 {
320 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
321 const gifts = new GiftService(harness.stateDb, harness.simulation);
322 assert.throws(
323 () => gifts.sendGift({
324 characterId: character.character_id,
325 items: [{ itemId: ItemId.potion, quantity: 1 }]
326 }),
327 /at least one item/i
328 );
329 }
330 finally
331 { harness.close(); }
332});
shared/market/listingKey.js +26 −0
@@ -81,6 +81,32 @@ export function listRecipePaperPickerEntries ()
81 81 return entries;
82 82 }
83 83
84 /**
85 * Gift / editor picker rows: one drinkable bottle per alchemy potion
86 * (skips solid outputs like tanning brew).
87 * @returns {Array<{ itemId: number, listingKey: null, potionKey: string, displayName: string, key: string }>}
88 */
89 export function listPotionPickerEntries ()
90 {
91 const entries = [];
92 for (const potion of listAlchemyPotions())
93 {
94 if (potion.outputItemId)
95 { continue; }
96 const potionKey = String(potion.potionKey ?? "").trim();
97 if (!potionKey)
98 { continue; }
99 entries.push({
100 itemId: ItemId.potion,
101 listingKey: null,
102 potionKey,
103 displayName: potion.displayName ?? potionKey,
104 key: `potion:${potionKey}`
105 });
106 }
107 return entries;
108 }
109
84 110 export function marketOrderDisplayName (order)
85 111 {
86 112 const listingKey = listingKeyFromOrder(order);
@@ -81,6 +81,32 @@ export function listRecipePaperPickerEntries ()
81 return entries;
81 return entries;
82}
82}
83
83
84/**
85 * Gift / editor picker rows: one drinkable bottle per alchemy potion
86 * (skips solid outputs like tanning brew).
87 * @returns {Array<{ itemId: number, listingKey: null, potionKey: string, displayName: string, key: string }>}
88 */
89export function listPotionPickerEntries ()
90{
91 const entries = [];
92 for (const potion of listAlchemyPotions())
93 {
94 if (potion.outputItemId)
95 { continue; }
96 const potionKey = String(potion.potionKey ?? "").trim();
97 if (!potionKey)
98 { continue; }
99 entries.push({
100 itemId: ItemId.potion,
101 listingKey: null,
102 potionKey,
103 displayName: potion.displayName ?? potionKey,
104 key: `potion:${potionKey}`
105 });
106 }
107 return entries;
108}
109
84export function marketOrderDisplayName (order)
110export function marketOrderDisplayName (order)
85{
111{
86 const listingKey = listingKeyFromOrder(order);
112 const listingKey = listingKeyFromOrder(order);