BlueGit

timfa questward

Files

questward / shared / market / listingKey.js

listingKey.js

listingKey.js 13.0 KiB Download Raw
1import { ItemId, itemDefinition, ITEM_DEFINITIONS, itemDisplayName } from "../definitions/items.js";
2import { alchemyPotionDefinition, listAlchemyPotions } from "../definitions/alchemy.js";
3import { MAGIC_ELEMENT_ORDER } from "../definitions/attackTypes.js";
4import {
5 FLOWER_COLOR_ORDER,
6 FLOWER_COLOR_DISPLAY,
7 DyeItemId
8} from "../definitions/dyes.js";
9import { ItemFamilyId } from "../definitions/tierFamilies.js";
10import { canSellOnMarketplace } from "../items/marketplace.js";
11
12export const RECIPE_LISTING_PREFIX = "recipe:";
13export const POTION_LISTING_PREFIX = "potion:";
14export const FLOWER_LISTING_PREFIX = "flower:";
15export const ARROW_LISTING_PREFIX = "arrow:";
16
17/** Shared CE key for any full unstable draught (buyer gets a surprise effect). */
18export const UNSTABLE_POTION_LISTING_KEY = `${POTION_LISTING_PREFIX}unstable`;
19
20/** Stable CE sub-type key for a written recipe paper (`recipe:lesserHealthTonic`, …). */
21export function recipeListingKey (potionKey)
22{
23 const key = String(potionKey ?? "").trim();
24 if (!key)
25 { return null; }
26 return `${RECIPE_LISTING_PREFIX}${key}`;
27}
28
29/** Stable CE sub-type key for a bottled brew (`potion:lesserHealthTonic`, …). */
30export function potionListingKey (potionKey)
31{
32 const key = String(potionKey ?? "").trim();
33 if (!key)
34 { return null; }
35 return `${POTION_LISTING_PREFIX}${key}`;
36}
37
38export function flowerListingKey (colorId)
39{
40 const id = String(colorId ?? "").trim();
41 if (!FLOWER_COLOR_ORDER.includes(id))
42 { return null; }
43 return `${FLOWER_LISTING_PREFIX}${id}`;
44}
45
46export function arrowListingKey (element)
47{
48 const el = String(element ?? "").trim();
49 if (!MAGIC_ELEMENT_ORDER.includes(el))
50 { return null; }
51 return `${ARROW_LISTING_PREFIX}${el}`;
52}
53
54/** Potion key from a `recipe:…` listing key (recipe papers). */
55export function potionKeyFromListingKey (listingKey)
56{
57 const text = String(listingKey ?? "");
58 if (!text.startsWith(RECIPE_LISTING_PREFIX))
59 { return null; }
60 return text.slice(RECIPE_LISTING_PREFIX.length) || null;
61}
62
63/** Potion key from a `potion:…` listing key (bottled brews). */
64export function potionKeyFromPotionListingKey (listingKey)
65{
66 const text = String(listingKey ?? "");
67 if (!text.startsWith(POTION_LISTING_PREFIX))
68 { return null; }
69 return text.slice(POTION_LISTING_PREFIX.length) || null;
70}
71
72export function flowerColorIdFromListingKey (listingKey)
73{
74 const text = String(listingKey ?? "");
75 if (!text.startsWith(FLOWER_LISTING_PREFIX))
76 { return null; }
77 const colorId = text.slice(FLOWER_LISTING_PREFIX.length);
78 return FLOWER_COLOR_ORDER.includes(colorId) ? colorId : null;
79}
80
81export function arrowElementFromListingKey (listingKey)
82{
83 const text = String(listingKey ?? "");
84 if (!text.startsWith(ARROW_LISTING_PREFIX))
85 { return null; }
86 const element = text.slice(ARROW_LISTING_PREFIX.length);
87 return MAGIC_ELEMENT_ORDER.includes(element) ? element : null;
88}
89
90export function isArrowItemId (itemId)
91{
92 return itemDefinition(itemId)?.familyId === ItemFamilyId.arrow;
93}
94
95export function isFlowerItemId (itemId)
96{
97 return (itemId | 0) === ItemId.flower || (itemId | 0) === DyeItemId.flower;
98}
99
100/** Drinkable alchemy brew suitable for CE / gifts (not solid outputs, not unstable). */
101export function isMarketPotionBrew (potionKey)
102{
103 const potion = alchemyPotionDefinition(potionKey);
104 if (!potion || potion.outputItemId)
105 { return false; }
106 const key = String(potion.potionKey ?? "").trim();
107 return !!key && !key.startsWith("unstable:");
108}
109
110export function isUnstablePotionStack (stack)
111{
112 if ((stack?.itemId | 0) !== ItemId.potion || stack?.instance?.kind !== "potion")
113 { return false; }
114 if (stack.instance.unstable)
115 { return true; }
116 return String(stack.instance.potionKey ?? "").startsWith("unstable:");
117}
118
119/** Listing key carried on a bag stack (sell escrow) or null for normal goods. */
120export function listingKeyFromStack (stack)
121{
122 const itemId = stack?.itemId | 0;
123 if (itemId === ItemId.recipePaper)
124 {
125 if (stack.instance?.kind !== "recipe-paper")
126 { return null; }
127 return recipeListingKey(stack.instance.potionKey);
128 }
129 if (itemId === ItemId.potion)
130 {
131 if (stack.instance?.kind !== "potion")
132 { return null; }
133 if (isUnstablePotionStack(stack))
134 { return UNSTABLE_POTION_LISTING_KEY; }
135 const potionKey = String(stack.instance.potionKey ?? "").trim();
136 if (!isMarketPotionBrew(potionKey))
137 { return null; }
138 return potionListingKey(potionKey);
139 }
140 if (isFlowerItemId(itemId))
141 {
142 if (stack.instance?.kind !== "flower")
143 { return null; }
144 return flowerListingKey(stack.instance.colorId);
145 }
146 if (isArrowItemId(itemId) && stack.instance?.kind === "tipped-arrow")
147 { return arrowListingKey(stack.instance.element); }
148 // Dyeable clothing keeps listingKey null so dyed and undyed match (buyer colour surprise).
149 return null;
150}
151
152/** Resolved listing key on an open order (column or sell escrow). */
153export function listingKeyFromOrder (order)
154{
155 if (order?.listingKey)
156 { return order.listingKey; }
157 return listingKeyFromStack(order?.escrowStack) ?? null;
158}
159
160export function marketOrdersCompatible (buy, sell)
161{
162 if ((buy.itemId | 0) !== (sell.itemId | 0))
163 { return false; }
164 const buyKey = listingKeyFromOrder(buy);
165 const sellKey = listingKeyFromOrder(sell);
166 if (!buyKey && !sellKey)
167 { return true; }
168 return buyKey === sellKey;
169}
170
171export function recipePaperDisplayName (potionKey)
172{
173 const def = alchemyPotionDefinition(potionKey);
174 if (!def)
175 { return "Recipe paper"; }
176 return `${def.displayName} recipe`;
177}
178
179export function flowerListingDisplayName (colorId)
180{
181 const label = FLOWER_COLOR_DISPLAY[colorId] ?? "Flower";
182 return `${label} flower`;
183}
184
185export function tippedArrowListingDisplayName (itemId, element)
186{
187 return itemDisplayName({
188 itemId,
189 quantity: 1,
190 instance: { kind: "tipped-arrow", element }
191 });
192}
193
194/**
195 * Editor / gift picker rows: one entry per alchemy recipe (same as CE buy catalog).
196 * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>}
197 */
198export function listRecipePaperPickerEntries ()
199{
200 const entries = [];
201 for (const potion of listAlchemyPotions())
202 {
203 const listingKey = recipeListingKey(potion.potionKey);
204 if (!listingKey)
205 { continue; }
206 entries.push({
207 itemId: ItemId.recipePaper,
208 listingKey,
209 potionKey: potion.potionKey,
210 displayName: recipePaperDisplayName(potion.potionKey),
211 key: `recipe-paper:${potion.potionKey}`
212 });
213 }
214 return entries;
215}
216
217/**
218 * Gift / CE picker rows: one drinkable bottle per alchemy potion
219 * (skips solid outputs like tanning brew).
220 * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>}
221 */
222export function listPotionPickerEntries ()
223{
224 const entries = [];
225 for (const potion of listAlchemyPotions())
226 {
227 if (!isMarketPotionBrew(potion.potionKey))
228 { continue; }
229 const listingKey = potionListingKey(potion.potionKey);
230 if (!listingKey)
231 { continue; }
232 entries.push({
233 itemId: ItemId.potion,
234 listingKey,
235 potionKey: potion.potionKey,
236 displayName: potion.displayName ?? potion.potionKey,
237 key: `potion:${potion.potionKey}`
238 });
239 }
240 return entries;
241}
242
243export function listFlowerPickerEntries ()
244{
245 return FLOWER_COLOR_ORDER.map((colorId) => ({
246 itemId: ItemId.flower,
247 listingKey: flowerListingKey(colorId),
248 colorId,
249 displayName: flowerListingDisplayName(colorId),
250 key: `flower:${colorId}`
251 }));
252}
253
254export function marketOrderDisplayName (order)
255{
256 const listingKey = listingKeyFromOrder(order);
257 if (listingKey?.startsWith(RECIPE_LISTING_PREFIX))
258 {
259 return recipePaperDisplayName(potionKeyFromListingKey(listingKey));
260 }
261 if (listingKey === UNSTABLE_POTION_LISTING_KEY)
262 { return "Unstable draught"; }
263 if (listingKey?.startsWith(POTION_LISTING_PREFIX))
264 {
265 const potionKey = potionKeyFromPotionListingKey(listingKey);
266 return alchemyPotionDefinition(potionKey)?.displayName
267 ?? itemDefinition(ItemId.potion)?.displayName
268 ?? "Potion";
269 }
270 if (listingKey?.startsWith(FLOWER_LISTING_PREFIX))
271 {
272 const colorId = flowerColorIdFromListingKey(listingKey);
273 return colorId ? flowerListingDisplayName(colorId) : "Flower";
274 }
275 if (listingKey?.startsWith(ARROW_LISTING_PREFIX))
276 {
277 const element = arrowElementFromListingKey(listingKey);
278 if (element && order?.itemId)
279 { return tippedArrowListingDisplayName(order.itemId, element); }
280 }
281 if (order?.escrowStack)
282 { return itemDisplayName(order.escrowStack); }
283 return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`;
284}
285
286export function normalizeBuyListingKey (itemId, listingKey)
287{
288 const id = itemId | 0;
289 const key = String(listingKey ?? "").trim();
290 if (!key)
291 { return null; }
292 if (id === ItemId.recipePaper)
293 {
294 if (!key.startsWith(RECIPE_LISTING_PREFIX))
295 { return null; }
296 const potionKey = potionKeyFromListingKey(key);
297 if (!potionKey || !alchemyPotionDefinition(potionKey))
298 { return null; }
299 return key;
300 }
301 if (id === ItemId.potion)
302 {
303 if (key === UNSTABLE_POTION_LISTING_KEY)
304 { return UNSTABLE_POTION_LISTING_KEY; }
305 if (!key.startsWith(POTION_LISTING_PREFIX))
306 { return null; }
307 const potionKey = potionKeyFromPotionListingKey(key);
308 if (!isMarketPotionBrew(potionKey))
309 { return null; }
310 return key;
311 }
312 if (isFlowerItemId(id))
313 {
314 const colorId = flowerColorIdFromListingKey(key);
315 return colorId ? flowerListingKey(colorId) : null;
316 }
317 if (isArrowItemId(id))
318 {
319 const element = arrowElementFromListingKey(key);
320 return element ? arrowListingKey(element) : null;
321 }
322 return null;
323}
324
325/** Whether this itemId requires a specific listing key on buy orders. */
326export function marketBuyRequiresListingKey (itemId)
327{
328 const id = itemId | 0;
329 return id === ItemId.recipePaper
330 || id === ItemId.potion
331 || isFlowerItemId(id);
332}
333
334export function marketBuyListingKeyError (itemId)
335{
336 const id = itemId | 0;
337 if (id === ItemId.recipePaper)
338 { return "Choose which recipe to buy."; }
339 if (id === ItemId.potion)
340 { return "Choose which potion to buy."; }
341 if (isFlowerItemId(id))
342 { return "Choose which flower colour to buy."; }
343 return "Choose a specific listing to buy.";
344}
345
346function catalogHint (itemId, lastSaleFor, undiscountedMarketCoins, now, lerpDurationMs, settings)
347{
348 return undiscountedMarketCoins(
349 itemId,
350 lastSaleFor(itemId),
351 now,
352 lerpDurationMs,
353 settings
354 );
355}
356
357/** Buy-tab catalog rows (subtype expansions for recipes, potions, flowers, tipped arrows). */
358export function buildMarketCatalogEntries ({
359 lastSaleFor,
360 undiscountedMarketCoins,
361 now,
362 lerpDurationMs,
363 settings
364})
365{
366 const catalog = [];
367 for (const def of Object.values(ITEM_DEFINITIONS))
368 {
369 if (!canSellOnMarketplace({ itemId: def.itemId, quantity: 1 }))
370 { continue; }
371 if (def.itemId === ItemId.coins)
372 { continue; }
373 if (
374 def.itemId === ItemId.recipePaper
375 || def.itemId === ItemId.potion
376 || isFlowerItemId(def.itemId)
377 )
378 { continue; }
379
380 const hint = catalogHint(
381 def.itemId,
382 lastSaleFor,
383 undiscountedMarketCoins,
384 now,
385 lerpDurationMs,
386 settings
387 );
388 if (isArrowItemId(def.itemId))
389 {
390 catalog.push({
391 itemId: def.itemId,
392 listingKey: null,
393 displayName: def.displayName,
394 hintPrice: hint
395 });
396 for (const element of MAGIC_ELEMENT_ORDER)
397 {
398 catalog.push({
399 itemId: def.itemId,
400 listingKey: arrowListingKey(element),
401 displayName: tippedArrowListingDisplayName(def.itemId, element),
402 hintPrice: hint
403 });
404 }
405 continue;
406 }
407
408 catalog.push({
409 itemId: def.itemId,
410 listingKey: null,
411 displayName: def.displayName,
412 hintPrice: hint
413 });
414 }
415 const recipeDef = ITEM_DEFINITIONS[ItemId.recipePaper];
416 if (recipeDef && canSellOnMarketplace({ itemId: ItemId.recipePaper, quantity: 1 }))
417 {
418 const recipeHint = catalogHint(
419 ItemId.recipePaper,
420 lastSaleFor,
421 undiscountedMarketCoins,
422 now,
423 lerpDurationMs,
424 settings
425 );
426 for (const potion of listAlchemyPotions())
427 {
428 const listingKey = recipeListingKey(potion.potionKey);
429 if (!listingKey)
430 { continue; }
431 catalog.push({
432 itemId: ItemId.recipePaper,
433 listingKey,
434 displayName: recipePaperDisplayName(potion.potionKey),
435 hintPrice: recipeHint
436 });
437 }
438 }
439 const potionDef = ITEM_DEFINITIONS[ItemId.potion];
440 if (potionDef && canSellOnMarketplace({ itemId: ItemId.potion, quantity: 1 }))
441 {
442 const potionHint = catalogHint(
443 ItemId.potion,
444 lastSaleFor,
445 undiscountedMarketCoins,
446 now,
447 lerpDurationMs,
448 settings
449 );
450 for (const entry of listPotionPickerEntries())
451 {
452 catalog.push({
453 itemId: ItemId.potion,
454 listingKey: entry.listingKey,
455 displayName: entry.displayName,
456 hintPrice: potionHint
457 });
458 }
459 catalog.push({
460 itemId: ItemId.potion,
461 listingKey: UNSTABLE_POTION_LISTING_KEY,
462 displayName: "Unstable draught",
463 hintPrice: potionHint
464 });
465 }
466 const flowerDef = ITEM_DEFINITIONS[ItemId.flower];
467 if (flowerDef && canSellOnMarketplace({ itemId: ItemId.flower, quantity: 1 }))
468 {
469 const flowerHint = catalogHint(
470 ItemId.flower,
471 lastSaleFor,
472 undiscountedMarketCoins,
473 now,
474 lerpDurationMs,
475 settings
476 );
477 for (const entry of listFlowerPickerEntries())
478 {
479 catalog.push({
480 itemId: ItemId.flower,
481 listingKey: entry.listingKey,
482 displayName: entry.displayName,
483 hintPrice: flowerHint
484 });
485 }
486 }
487 catalog.sort((a, b) => a.displayName.localeCompare(b.displayName));
488 return catalog;
489}