BlueGit

timfa questward

Files

questward / shared / market / listingKey.js

listingKey.js

listingKey.js 7.6 KiB Download Raw
1import { ItemId, itemDefinition, ITEM_DEFINITIONS } from "../definitions/items.js";
2import { alchemyPotionDefinition, listAlchemyPotions } from "../definitions/alchemy.js";
3import { canSellOnMarketplace } from "../items/marketplace.js";
4
5export const RECIPE_LISTING_PREFIX = "recipe:";
6export const POTION_LISTING_PREFIX = "potion:";
7
8/** Stable CE sub-type key for a written recipe paper (`recipe:lesserHealthTonic`, …). */
9export function recipeListingKey (potionKey)
10{
11 const key = String(potionKey ?? "").trim();
12 if (!key)
13 { return null; }
14 return `${RECIPE_LISTING_PREFIX}${key}`;
15}
16
17/** Stable CE sub-type key for a bottled brew (`potion:lesserHealthTonic`, …). */
18export function potionListingKey (potionKey)
19{
20 const key = String(potionKey ?? "").trim();
21 if (!key)
22 { return null; }
23 return `${POTION_LISTING_PREFIX}${key}`;
24}
25
26/** Potion key from a `recipe:…` listing key (recipe papers). */
27export function potionKeyFromListingKey (listingKey)
28{
29 const text = String(listingKey ?? "");
30 if (!text.startsWith(RECIPE_LISTING_PREFIX))
31 { return null; }
32 return text.slice(RECIPE_LISTING_PREFIX.length) || null;
33}
34
35/** Potion key from a `potion:…` listing key (bottled brews). */
36export function potionKeyFromPotionListingKey (listingKey)
37{
38 const text = String(listingKey ?? "");
39 if (!text.startsWith(POTION_LISTING_PREFIX))
40 { return null; }
41 return text.slice(POTION_LISTING_PREFIX.length) || null;
42}
43
44/** Drinkable alchemy brew suitable for CE / gifts (not solid outputs). */
45export function isMarketPotionBrew (potionKey)
46{
47 const potion = alchemyPotionDefinition(potionKey);
48 if (!potion || potion.outputItemId)
49 { return false; }
50 const key = String(potion.potionKey ?? "").trim();
51 return !!key && !key.startsWith("unstable:");
52}
53
54/** Listing key carried on a bag stack (sell escrow) or null for normal goods. */
55export function listingKeyFromStack (stack)
56{
57 const itemId = stack?.itemId | 0;
58 if (itemId === ItemId.recipePaper)
59 {
60 if (stack.instance?.kind !== "recipe-paper")
61 { return null; }
62 return recipeListingKey(stack.instance.potionKey);
63 }
64 if (itemId === ItemId.potion)
65 {
66 if (stack.instance?.kind !== "potion")
67 { return null; }
68 if (stack.instance.unstable)
69 { return null; }
70 const potionKey = String(stack.instance.potionKey ?? "").trim();
71 if (!isMarketPotionBrew(potionKey))
72 { return null; }
73 return potionListingKey(potionKey);
74 }
75 return null;
76}
77
78/** Resolved listing key on an open order (column or sell escrow). */
79export function listingKeyFromOrder (order)
80{
81 if (order?.listingKey)
82 { return order.listingKey; }
83 return listingKeyFromStack(order?.escrowStack) ?? null;
84}
85
86export function marketOrdersCompatible (buy, sell)
87{
88 if ((buy.itemId | 0) !== (sell.itemId | 0))
89 { return false; }
90 const buyKey = listingKeyFromOrder(buy);
91 const sellKey = listingKeyFromOrder(sell);
92 if (!buyKey && !sellKey)
93 { return true; }
94 return buyKey === sellKey;
95}
96
97export function recipePaperDisplayName (potionKey)
98{
99 const def = alchemyPotionDefinition(potionKey);
100 if (!def)
101 { return "Recipe paper"; }
102 return `${def.displayName} recipe`;
103}
104
105/**
106 * Editor / gift picker rows: one entry per alchemy recipe (same as CE buy catalog).
107 * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>}
108 */
109export function listRecipePaperPickerEntries ()
110{
111 const entries = [];
112 for (const potion of listAlchemyPotions())
113 {
114 const listingKey = recipeListingKey(potion.potionKey);
115 if (!listingKey)
116 { continue; }
117 entries.push({
118 itemId: ItemId.recipePaper,
119 listingKey,
120 potionKey: potion.potionKey,
121 displayName: recipePaperDisplayName(potion.potionKey),
122 key: `recipe-paper:${potion.potionKey}`
123 });
124 }
125 return entries;
126}
127
128/**
129 * Gift / CE picker rows: one drinkable bottle per alchemy potion
130 * (skips solid outputs like tanning brew).
131 * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>}
132 */
133export function listPotionPickerEntries ()
134{
135 const entries = [];
136 for (const potion of listAlchemyPotions())
137 {
138 if (!isMarketPotionBrew(potion.potionKey))
139 { continue; }
140 const listingKey = potionListingKey(potion.potionKey);
141 if (!listingKey)
142 { continue; }
143 entries.push({
144 itemId: ItemId.potion,
145 listingKey,
146 potionKey: potion.potionKey,
147 displayName: potion.displayName ?? potion.potionKey,
148 key: `potion:${potion.potionKey}`
149 });
150 }
151 return entries;
152}
153
154export function marketOrderDisplayName (order)
155{
156 const listingKey = listingKeyFromOrder(order);
157 if (listingKey?.startsWith(RECIPE_LISTING_PREFIX))
158 {
159 return recipePaperDisplayName(potionKeyFromListingKey(listingKey));
160 }
161 if (listingKey?.startsWith(POTION_LISTING_PREFIX))
162 {
163 const potionKey = potionKeyFromPotionListingKey(listingKey);
164 return alchemyPotionDefinition(potionKey)?.displayName
165 ?? itemDefinition(ItemId.potion)?.displayName
166 ?? "Potion";
167 }
168 return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`;
169}
170
171export function normalizeBuyListingKey (itemId, listingKey)
172{
173 const id = itemId | 0;
174 const key = String(listingKey ?? "").trim();
175 if (id === ItemId.recipePaper)
176 {
177 if (!key.startsWith(RECIPE_LISTING_PREFIX))
178 { return null; }
179 const potionKey = potionKeyFromListingKey(key);
180 if (!potionKey || !alchemyPotionDefinition(potionKey))
181 { return null; }
182 return key;
183 }
184 if (id === ItemId.potion)
185 {
186 if (!key.startsWith(POTION_LISTING_PREFIX))
187 { return null; }
188 const potionKey = potionKeyFromPotionListingKey(key);
189 if (!isMarketPotionBrew(potionKey))
190 { return null; }
191 return key;
192 }
193 return null;
194}
195
196/** Whether this itemId requires a specific listing key on buy orders. */
197export function marketBuyRequiresListingKey (itemId)
198{
199 const id = itemId | 0;
200 return id === ItemId.recipePaper || id === ItemId.potion;
201}
202
203/** Buy-tab catalog rows (recipe papers / potions expand per known subtype). */
204export function buildMarketCatalogEntries ({
205 lastSaleFor,
206 undiscountedMarketCoins,
207 now,
208 lerpDurationMs,
209 settings
210})
211{
212 const catalog = [];
213 for (const def of Object.values(ITEM_DEFINITIONS))
214 {
215 if (!canSellOnMarketplace({ itemId: def.itemId, quantity: 1 }))
216 { continue; }
217 if (def.itemId === ItemId.coins)
218 { continue; }
219 if (def.itemId === ItemId.recipePaper || def.itemId === ItemId.potion)
220 { continue; }
221 catalog.push({
222 itemId: def.itemId,
223 listingKey: null,
224 displayName: def.displayName,
225 hintPrice: undiscountedMarketCoins(
226 def.itemId,
227 lastSaleFor(def.itemId),
228 now,
229 lerpDurationMs,
230 settings
231 )
232 });
233 }
234 const recipeDef = ITEM_DEFINITIONS[ItemId.recipePaper];
235 if (recipeDef && canSellOnMarketplace({ itemId: ItemId.recipePaper, quantity: 1 }))
236 {
237 const recipeHint = undiscountedMarketCoins(
238 ItemId.recipePaper,
239 lastSaleFor(ItemId.recipePaper),
240 now,
241 lerpDurationMs,
242 settings
243 );
244 for (const potion of listAlchemyPotions())
245 {
246 const listingKey = recipeListingKey(potion.potionKey);
247 if (!listingKey)
248 { continue; }
249 catalog.push({
250 itemId: ItemId.recipePaper,
251 listingKey,
252 displayName: recipePaperDisplayName(potion.potionKey),
253 hintPrice: recipeHint
254 });
255 }
256 }
257 const potionDef = ITEM_DEFINITIONS[ItemId.potion];
258 if (potionDef && canSellOnMarketplace({ itemId: ItemId.potion, quantity: 1 }))
259 {
260 const potionHint = undiscountedMarketCoins(
261 ItemId.potion,
262 lastSaleFor(ItemId.potion),
263 now,
264 lerpDurationMs,
265 settings
266 );
267 for (const entry of listPotionPickerEntries())
268 {
269 catalog.push({
270 itemId: ItemId.potion,
271 listingKey: entry.listingKey,
272 displayName: entry.displayName,
273 hintPrice: potionHint
274 });
275 }
276 }
277 catalog.sort((a, b) => a.displayName.localeCompare(b.displayName));
278 return catalog;
279}