| 1 | import { listAllItemPickerEntries, itemDefinition, ItemId } from "/shared/definitions/items.js"; |
| 2 | import { |
| 3 | listRecipePaperPickerEntries, |
| 4 | listPotionPickerEntries, |
| 5 | potionKeyFromListingKey, |
| 6 | potionKeyFromPotionListingKey |
| 7 | } from "/shared/market/listingKey.js"; |
| 8 | import { |
| 9 | formatGiftLetterBody, |
| 10 | QUESTWARD_GIFT_AUTHOR, |
| 11 | QUESTWARD_GIFT_ORG |
| 12 | } from "/shared/courier/giftLetters.js"; |
| 13 | import { formatLetterSignature } from "/shared/courier/systemLetters.js"; |
| 14 | |
| 15 | function giftItemPickerEntries () |
| 16 | { |
| 17 | const recipes = listRecipePaperPickerEntries(); |
| 18 | const potions = listPotionPickerEntries(); |
| 19 | const base = listAllItemPickerEntries() |
| 20 | .filter((item) => item.itemId !== ItemId.recipePaper && item.itemId !== ItemId.potion) |
| 21 | .map((item) => ({ |
| 22 | ...item, |
| 23 | listingKey: null, |
| 24 | potionKey: null |
| 25 | })); |
| 26 | return [...base, ...recipes, ...potions] |
| 27 | .sort((a, b) => a.displayName.localeCompare(b.displayName) || (a.itemId - b.itemId)); |
| 28 | } |
| 29 | |
| 30 | function giftRowKey (row) |
| 31 | { |
| 32 | const itemId = row?.itemId | 0; |
| 33 | if (itemId === ItemId.recipePaper && row?.potionKey) |
| 34 | { return `recipe:${row.potionKey}`; } |
| 35 | if (itemId === ItemId.potion && row?.potionKey) |
| 36 | { return `potion:${row.potionKey}`; } |
| 37 | if (row?.listingKey) |
| 38 | { return `listing:${row.listingKey}`; } |
| 39 | return `item:${itemId}`; |
| 40 | } |
| 41 | |
| 42 | export function initGiftsModal ({ socket, MessageType }) |
| 43 | { |
| 44 | const backdrop = document.getElementById("gifts-backdrop"); |
| 45 | const modal = document.getElementById("gifts-modal"); |
| 46 | const resultsEl = document.getElementById("gifts-results"); |
| 47 | const detailEl = document.getElementById("gifts-detail"); |
| 48 | const statusEl = document.getElementById("gifts-status"); |
| 49 | const searchInput = document.getElementById("gifts-search-input"); |
| 50 | const searchBtn = document.getElementById("gifts-search-btn"); |
| 51 | let open = false; |
| 52 | /** @type {any[]} */ |
| 53 | let characters = []; |
| 54 | /** @type {any|null} */ |
| 55 | let selected = null; |
| 56 | /** @type {Array<{ itemId: number, quantity: number, displayName: string, listingKey?: string|null, potionKey?: string|null }>} */ |
| 57 | let giftItems = []; |
| 58 | let messageText = ""; |
| 59 | /** @type {null | { resultsEl: HTMLElement, inputEl: HTMLInputElement }} */ |
| 60 | let itemPickContext = null; |
| 61 | |
| 62 | function show (visible) |
| 63 | { |
| 64 | open = visible; |
| 65 | backdrop.hidden = !visible; |
| 66 | modal.hidden = !visible; |
| 67 | if (!visible) |
| 68 | { |
| 69 | closeInlineItemSearch(); |
| 70 | return; |
| 71 | } |
| 72 | render(); |
| 73 | } |
| 74 | |
| 75 | function setStatus (text, isError = false) |
| 76 | { |
| 77 | if (!statusEl) |
| 78 | { return; } |
| 79 | statusEl.textContent = text || ""; |
| 80 | statusEl.classList.toggle("error", !!isError); |
| 81 | } |
| 82 | |
| 83 | function closeInlineItemSearch () |
| 84 | { |
| 85 | if (itemPickContext?.resultsEl) |
| 86 | { itemPickContext.resultsEl.replaceChildren(); } |
| 87 | itemPickContext = null; |
| 88 | } |
| 89 | |
| 90 | function addGiftItem (item) |
| 91 | { |
| 92 | if (!item?.itemId) |
| 93 | { return; } |
| 94 | const listingKey = item.listingKey ?? null; |
| 95 | const potionKey = item.potionKey |
| 96 | ?? potionKeyFromListingKey(listingKey) |
| 97 | ?? potionKeyFromPotionListingKey(listingKey) |
| 98 | ?? null; |
| 99 | const key = giftRowKey({ itemId: item.itemId, listingKey, potionKey }); |
| 100 | const existing = giftItems.find((row) => giftRowKey(row) === key); |
| 101 | if (existing) |
| 102 | { |
| 103 | existing.quantity = Math.min(100000, (existing.quantity | 0) + 1); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | giftItems.push({ |
| 108 | itemId: item.itemId, |
| 109 | quantity: 1, |
| 110 | displayName: item.displayName |
| 111 | || itemDefinition(item.itemId)?.displayName |
| 112 | || `Item ${item.itemId}`, |
| 113 | listingKey, |
| 114 | potionKey |
| 115 | }); |
| 116 | } |
| 117 | closeInlineItemSearch(); |
| 118 | renderDetail(); |
| 119 | } |
| 120 | |
| 121 | function filterItemMatches (query) |
| 122 | { |
| 123 | const q = String(query ?? "").trim().toLowerCase(); |
| 124 | const taken = new Set(giftItems.map((row) => giftRowKey(row))); |
| 125 | return giftItemPickerEntries().filter((item) => |
| 126 | { |
| 127 | if (taken.has(giftRowKey(item))) |
| 128 | { return false; } |
| 129 | if (!q) |
| 130 | { return true; } |
| 131 | return item.displayName.toLowerCase().includes(q) |
| 132 | || item.key.toLowerCase().includes(q) |
| 133 | || String(item.itemId).includes(q) |
| 134 | || String(item.potionKey ?? "").toLowerCase().includes(q); |
| 135 | }).slice(0, 40); |
| 136 | } |
| 137 | |
| 138 | function renderInlineItemSearch (query) |
| 139 | { |
| 140 | if (!itemPickContext) |
| 141 | { return; } |
| 142 | const { resultsEl: pickResults } = itemPickContext; |
| 143 | pickResults.replaceChildren(); |
| 144 | const matches = filterItemMatches(query); |
| 145 | if (!matches.length) |
| 146 | { |
| 147 | pickResults.textContent = "No matching items."; |
| 148 | return; |
| 149 | } |
| 150 | for (const item of matches) |
| 151 | { |
| 152 | const row = document.createElement("button"); |
| 153 | row.type = "button"; |
| 154 | row.className = "shop-item-search-row"; |
| 155 | const name = document.createElement("span"); |
| 156 | name.className = "shop-item-search-row-name"; |
| 157 | name.textContent = item.displayName; |
| 158 | const key = document.createElement("span"); |
| 159 | key.className = "shop-item-search-row-key"; |
| 160 | key.textContent = item.listingKey |
| 161 | ? `${item.key} (${item.listingKey})` |
| 162 | : `${item.key} (#${item.itemId})`; |
| 163 | row.append(name, key); |
| 164 | row.addEventListener("click", () => addGiftItem(item)); |
| 165 | pickResults.append(row); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | function characterLabel (row) |
| 170 | { |
| 171 | const name = `${row.firstName ?? ""} ${row.lastName ?? ""}`.trim(); |
| 172 | const account = row.accountUsername ? ` · @${row.accountUsername}` : ""; |
| 173 | return `${name}${account}`; |
| 174 | } |
| 175 | |
| 176 | function renderResults () |
| 177 | { |
| 178 | resultsEl.replaceChildren(); |
| 179 | if (!characters.length) |
| 180 | { |
| 181 | resultsEl.textContent = "No characters found."; |
| 182 | return; |
| 183 | } |
| 184 | for (const row of characters) |
| 185 | { |
| 186 | const btn = document.createElement("button"); |
| 187 | btn.type = "button"; |
| 188 | btn.className = selected?.characterId === row.characterId ? "active" : ""; |
| 189 | btn.textContent = characterLabel(row); |
| 190 | btn.addEventListener("click", () => |
| 191 | { |
| 192 | selected = row; |
| 193 | giftItems = []; |
| 194 | messageText = ""; |
| 195 | closeInlineItemSearch(); |
| 196 | render(); |
| 197 | }); |
| 198 | resultsEl.append(btn); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | function renderPreview () |
| 203 | { |
| 204 | const preview = detailEl.querySelector("#gifts-letter-preview"); |
| 205 | const signature = detailEl.querySelector("#gifts-letter-signature"); |
| 206 | if (!preview || !selected) |
| 207 | { return; } |
| 208 | const displayName = `${selected.firstName} ${selected.lastName}`.trim(); |
| 209 | preview.textContent = formatGiftLetterBody({ |
| 210 | characterDisplayName: displayName, |
| 211 | message: messageText, |
| 212 | items: giftItems |
| 213 | }); |
| 214 | if (signature) |
| 215 | { |
| 216 | signature.textContent = formatLetterSignature({ |
| 217 | authorDisplayName: QUESTWARD_GIFT_AUTHOR, |
| 218 | authorOrganization: QUESTWARD_GIFT_ORG |
| 219 | }); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | function renderDetail () |
| 224 | { |
| 225 | detailEl.replaceChildren(); |
| 226 | if (!selected) |
| 227 | { |
| 228 | detailEl.textContent = "Search for a character, then select one to compose a gift."; |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | const heading = document.createElement("h3"); |
| 233 | heading.textContent = `Gift to ${characterLabel(selected)}`; |
| 234 | detailEl.append(heading); |
| 235 | |
| 236 | const messageLabel = document.createElement("label"); |
| 237 | messageLabel.className = "gifts-message-label"; |
| 238 | messageLabel.textContent = "Message"; |
| 239 | const messageArea = document.createElement("textarea"); |
| 240 | messageArea.id = "gifts-message"; |
| 241 | messageArea.rows = 5; |
| 242 | messageArea.placeholder = "Write the personal note included in the letter…"; |
| 243 | messageArea.value = messageText; |
| 244 | messageArea.addEventListener("input", () => |
| 245 | { |
| 246 | messageText = messageArea.value; |
| 247 | renderPreview(); |
| 248 | }); |
| 249 | messageLabel.append(messageArea); |
| 250 | detailEl.append(messageLabel); |
| 251 | |
| 252 | const itemsHeader = document.createElement("div"); |
| 253 | itemsHeader.className = "gifts-items-header"; |
| 254 | const itemsTitle = document.createElement("strong"); |
| 255 | itemsTitle.textContent = "Items"; |
| 256 | itemsHeader.append(itemsTitle); |
| 257 | detailEl.append(itemsHeader); |
| 258 | |
| 259 | const itemSearch = document.createElement("input"); |
| 260 | itemSearch.type = "search"; |
| 261 | itemSearch.placeholder = "Search items to add…"; |
| 262 | itemSearch.autocomplete = "off"; |
| 263 | const itemResults = document.createElement("div"); |
| 264 | itemResults.className = "gifts-item-search-results"; |
| 265 | detailEl.append(itemSearch, itemResults); |
| 266 | |
| 267 | itemPickContext = { inputEl: itemSearch, resultsEl: itemResults }; |
| 268 | itemSearch.addEventListener("input", () => renderInlineItemSearch(itemSearch.value)); |
| 269 | itemSearch.addEventListener("search", () => renderInlineItemSearch(itemSearch.value)); |
| 270 | itemSearch.addEventListener("keydown", (event) => |
| 271 | { |
| 272 | if (event.key !== "Enter") |
| 273 | { return; } |
| 274 | event.preventDefault(); |
| 275 | event.stopPropagation(); |
| 276 | const matches = filterItemMatches(itemSearch.value); |
| 277 | if (matches[0]) |
| 278 | { addGiftItem(matches[0]); } |
| 279 | }); |
| 280 | renderInlineItemSearch(itemSearch.value); |
| 281 | |
| 282 | const list = document.createElement("div"); |
| 283 | list.className = "gifts-item-list"; |
| 284 | if (!giftItems.length) |
| 285 | { |
| 286 | const empty = document.createElement("p"); |
| 287 | empty.className = "hint"; |
| 288 | empty.textContent = "No items yet — search above and click a result (or press Enter)."; |
| 289 | list.append(empty); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | for (let index = 0; index < giftItems.length; index++) |
| 294 | { |
| 295 | const entry = giftItems[index]; |
| 296 | const row = document.createElement("div"); |
| 297 | row.className = "gifts-item-row"; |
| 298 | const name = document.createElement("span"); |
| 299 | name.textContent = entry.displayName |
| 300 | || itemDefinition(entry.itemId)?.displayName |
| 301 | || `Item ${entry.itemId}`; |
| 302 | const qty = document.createElement("input"); |
| 303 | qty.type = "number"; |
| 304 | qty.min = "1"; |
| 305 | qty.max = "100000"; |
| 306 | qty.value = String(entry.quantity); |
| 307 | qty.addEventListener("change", () => |
| 308 | { |
| 309 | entry.quantity = Math.max(1, Math.min(100000, qty.valueAsNumber || 1)); |
| 310 | qty.value = String(entry.quantity); |
| 311 | renderPreview(); |
| 312 | }); |
| 313 | const remove = document.createElement("button"); |
| 314 | remove.type = "button"; |
| 315 | remove.textContent = "Remove"; |
| 316 | remove.addEventListener("click", () => |
| 317 | { |
| 318 | giftItems.splice(index, 1); |
| 319 | renderDetail(); |
| 320 | }); |
| 321 | row.append(name, qty, remove); |
| 322 | list.append(row); |
| 323 | } |
| 324 | } |
| 325 | detailEl.append(list); |
| 326 | |
| 327 | const previewWrap = document.createElement("div"); |
| 328 | previewWrap.className = "gifts-preview-wrap"; |
| 329 | const previewTitle = document.createElement("strong"); |
| 330 | previewTitle.textContent = "Letter preview"; |
| 331 | const preview = document.createElement("pre"); |
| 332 | preview.id = "gifts-letter-preview"; |
| 333 | preview.className = "gifts-letter-preview"; |
| 334 | const signature = document.createElement("pre"); |
| 335 | signature.id = "gifts-letter-signature"; |
| 336 | signature.className = "gifts-letter-signature"; |
| 337 | previewWrap.append(previewTitle, preview, signature); |
| 338 | detailEl.append(previewWrap); |
| 339 | |
| 340 | const sendBtn = document.createElement("button"); |
| 341 | sendBtn.type = "button"; |
| 342 | sendBtn.id = "gifts-send"; |
| 343 | sendBtn.textContent = "Send Gift"; |
| 344 | sendBtn.addEventListener("click", () => |
| 345 | { |
| 346 | if (!selected) |
| 347 | { return; } |
| 348 | setStatus("Sending gift…"); |
| 349 | socket.send({ |
| 350 | type: MessageType.editorSendGift, |
| 351 | characterId: selected.characterId, |
| 352 | message: messageText, |
| 353 | items: giftItems.map((row) => ({ |
| 354 | itemId: row.itemId, |
| 355 | quantity: row.quantity, |
| 356 | listingKey: row.listingKey ?? null, |
| 357 | potionKey: row.potionKey ?? null |
| 358 | })) |
| 359 | }); |
| 360 | }); |
| 361 | detailEl.append(sendBtn); |
| 362 | renderPreview(); |
| 363 | } |
| 364 | |
| 365 | function render () |
| 366 | { |
| 367 | renderResults(); |
| 368 | renderDetail(); |
| 369 | } |
| 370 | |
| 371 | function runSearch () |
| 372 | { |
| 373 | const query = searchInput.value.trim(); |
| 374 | if (!query) |
| 375 | { |
| 376 | setStatus("Enter a character or account name.", true); |
| 377 | return; |
| 378 | } |
| 379 | setStatus("Searching…"); |
| 380 | socket.send({ type: MessageType.editorSearchGiftCharacters, query }); |
| 381 | } |
| 382 | |
| 383 | document.getElementById("open-gifts").addEventListener("click", () => |
| 384 | { |
| 385 | setStatus(""); |
| 386 | show(true); |
| 387 | }); |
| 388 | document.getElementById("gifts-close").addEventListener("click", () => show(false)); |
| 389 | backdrop.addEventListener("click", () => show(false)); |
| 390 | searchBtn.addEventListener("click", runSearch); |
| 391 | searchInput.addEventListener("keydown", (event) => |
| 392 | { |
| 393 | if (event.key === "Enter") |
| 394 | { |
| 395 | event.preventDefault(); |
| 396 | runSearch(); |
| 397 | } |
| 398 | }); |
| 399 | |
| 400 | return { |
| 401 | isOpen: () => open, |
| 402 | handleError (message) |
| 403 | { |
| 404 | setStatus(message || "Gift failed.", true); |
| 405 | }, |
| 406 | handleMessage (message) |
| 407 | { |
| 408 | if (message.type === MessageType.editorGiftCharacters) |
| 409 | { |
| 410 | characters = message.characters ?? []; |
| 411 | if (selected && !characters.some((row) => row.characterId === selected.characterId)) |
| 412 | { selected = null; } |
| 413 | setStatus(characters.length ? `${characters.length} character(s)` : "No matches."); |
| 414 | render(); |
| 415 | return; |
| 416 | } |
| 417 | if (message.type === MessageType.editorGiftSent) |
| 418 | { |
| 419 | const name = `${message.firstName ?? ""} ${message.lastName ?? ""}`.trim(); |
| 420 | setStatus(`Gift sent to ${name}.`); |
| 421 | giftItems = []; |
| 422 | messageText = ""; |
| 423 | renderDetail(); |
| 424 | } |
| 425 | } |
| 426 | }; |
| 427 | } |