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