Merge branch 'feature/gift-specific-potions'
@@ -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 @@1
import { listAllItemPickerEntries, itemDefinition, ItemId } from "/shared/definitions/items.js";1
import { listAllItemPickerEntries, itemDefinition, ItemId } from "/shared/definitions/items.js";2
import {2
import {3
listRecipePaperPickerEntries,3
listRecipePaperPickerEntries,4
listPotionPickerEntries,4
potionKeyFromListingKey5
potionKeyFromListingKey5
} from "/shared/market/listingKey.js";6
} from "/shared/market/listingKey.js";6
import {7
import {@@ -13,24 +14,28 @@ import { formatLetterSignature } from "/shared/courier/systemLetters.js";13
function giftItemPickerEntries ()14
function 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: null23
potionKey: null22
}));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
27
function giftRowKey (row)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
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
36
export function initGiftsModal ({ socket, MessageType })41
export function initGiftsModal ({ socket, MessageType })@@ -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 @@1
import { randomUUID } from "node:crypto";1
import { randomUUID } from "node:crypto";2
import { ItemId, itemDefinition, LETTER_MAX_BODY_CHARS } from "../../../shared/definitions/items.js";2
import { ItemId, itemDefinition, LETTER_MAX_BODY_CHARS } from "../../../shared/definitions/items.js";3
import { alchemyPotionDefinition } from "../../../shared/definitions/alchemy.js";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
import {5
import {6
normalizeBuyListingKey,6
normalizeBuyListingKey,7
potionKeyFromListingKey,7
potionKeyFromListingKey,@@ -111,6 +111,37 @@ export class GiftService111
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 ?? 1128
});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.potionKey141
});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
else302
{303
merged.set(key, {304
itemId,305
quantity,306
potionKey307
});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)@@ -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
finally266
finally267
{ harness.close(); }267
{ harness.close(); }268
});268
});269
270
test("admin gift can credit a specific potion brew", () =>271
{272
const harness = makeSimulation({ workerCount: 1 });273
try274
{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.potion299
&& 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
finally312
{ harness.close(); }313
});314
315
test("admin gift rejects blank potion without a potion key", () =>316
{317
const harness = makeSimulation({ workerCount: 1 });318
try319
{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/i328
);329
}330
finally331
{ harness.close(); }332
});