From e6b4cb9f5816c6ee239233bc85f74ee446a161c2 Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Wed, 1 Jul 2026 16:37:45 -0400 Subject: feat: shop system overhauled, gui conversation tree editor overhauled --- internal/admin/static/talktree.js | 330 ++++++++++++++++++++------------------ 1 file changed, 170 insertions(+), 160 deletions(-) (limited to 'internal/admin/static/talktree.js') diff --git a/internal/admin/static/talktree.js b/internal/admin/static/talktree.js index e7079f5..4758918 100644 --- a/internal/admin/static/talktree.js +++ b/internal/admin/static/talktree.js @@ -86,11 +86,11 @@ } // Single clause from flat fields for (var i = 0; i < CLAUSE_TYPES.length; i++) { - if (cond[CLAUSE_TYPES[i].key] !== undefined && cond[CLAUSE_TYPES[i].key] !== '') { + if (cond[CLAUSE_TYPES[i].key] !== undefined) { var c = {}; c[CLAUSE_TYPES[i].key] = cond[CLAUSE_TYPES[i].key]; if (cond.not) c.not = cond.not; - if (cond.value !== undefined && cond.value !== '') c.value = cond.value; + if (cond.value !== undefined) c.value = cond.value; if (cond.has_item && CLAUSE_TYPES[i].key !== 'has_item') c.has_item = cond.has_item; if (cond.player_flag && CLAUSE_TYPES[i].key !== 'player_flag') c.player_flag = cond.player_flag; if (cond.flag && CLAUSE_TYPES[i].key !== 'flag') c.flag = cond.flag; @@ -100,11 +100,11 @@ // Check for has_item or min_credits as the only key for (var j = 0; j < CLAUSE_TYPES.length; j++) { var ck = CLAUSE_TYPES[j].key; - if (cond[ck] !== undefined && cond[ck] !== '') { + if (cond[ck] !== undefined) { var cc = {}; cc[ck] = cond[ck]; if (cond.not) cc.not = cond.not; - if (cond.value !== undefined && cond.value !== '') cc.value = cond.value; + if (cond.value !== undefined) cc.value = cond.value; return { mode: 'single', clauses: [cc], groupKey: null }; } } @@ -115,7 +115,7 @@ function clauseObjType(clause) { for (var i = 0; i < CLAUSE_TYPES.length; i++) { var k = CLAUSE_TYPES[i].key; - if (clause[k] !== undefined && clause[k] !== '') return k; + if (clause[k] !== undefined) return k; } return null; } @@ -126,13 +126,13 @@ if (clauses.length === 1) { var flat = {}; var c = clauses[0]; - for (var k in c) { if (c[k] !== undefined && c[k] !== '') flat[k] = c[k]; } + for (var k in c) { if (c[k] !== undefined) flat[k] = c[k]; } return flat; } var cond = {}; cond[groupKey || 'all_of'] = clauses.map(function(c) { var sub = {}; - for (var k in c) { if (c[k] !== undefined && c[k] !== '') sub[k] = c[k]; } + for (var k in c) { if (c[k] !== undefined) sub[k] = c[k]; } return sub; }); return cond; @@ -141,21 +141,14 @@ // ---- Action field definitions ---- var ACTION_FIELDS = [ - { key: 'give_item', label: 'Give Item', type: 'text' }, - { key: 'take_item', label: 'Take Item', type: 'text' }, - { key: 'teleport', label: 'Teleport Room', type: 'number' }, + { key: 'give_item', label: 'Give Item', type: 'item_search' }, + { key: 'take_item', label: 'Take Item', type: 'item_search' }, + { key: 'credits', label: 'Credits (+/-)', type: 'number' }, + { key: 'teleport', label: 'TP Room', type: 'number' }, { key: 'heal', label: 'Heal HP', type: 'number' }, - { key: 'cost', label: 'Cost (Credits)', type: 'number' }, - { key: 'reputation_cost', label: 'Rep Cost', type: 'number' }, ]; - var ACTION_BOOLS = [ - { key: 'assign_task', label: 'Assign Task' }, - { key: 'skip_task', label: 'Skip Task' }, - { key: 'extend_task', label: 'Extend Task' }, - { key: 'sawmill', label: 'Sawmill' }, - { key: 'aps_node', label: 'APS Node' }, - ]; + var ACTION_BOOLS = []; // ---- Event delegation ---- @@ -173,6 +166,7 @@ else if (type === 'number') { var n = parseFloat(el.value); val = isNaN(n) ? 0 : n; } else { val = el.value; } talkSyncPath(path, val); + if (path.endsWith('.goto')) renderTalkTree(window._talkData); } else if (tag === 'SELECT') { talkSyncPath(path, el.value); } @@ -207,11 +201,11 @@ case 'add-node': addNode(); break; - case 'add-sequence': - addSequenceLine(nodeKey); + case 'add-message': + addMessageLine(nodeKey); break; - case 'delete-sequence': - deleteSequenceLine(nodeKey, parseInt(optIdx, 10)); + case 'delete-message': + deleteMessageLine(nodeKey, parseInt(optIdx, 10)); break; case 'add-condition': toggleCondition(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null); @@ -240,12 +234,6 @@ case 'remove-kv': removeKVPair(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null, el.getAttribute('data-kvk'), subKey); break; - case 'add-shop-item': - addShopItem(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null); - break; - case 'remove-shop-item': - removeShopItem(nodeKey, optIdx !== null ? parseInt(optIdx, 10) : null, parseInt(subIdx, 10)); - break; } } @@ -255,6 +243,8 @@ var container = document.getElementById('talktree'); if (!container) return; window._talkData = data; + window._talkOpenNodes = window._talkOpenNodes || new Set(); + window._talkOpenNodes.add('start'); if (!data || !data.talk || !data.talk.nodes) { container.innerHTML = '

No conversation tree defined.

'; return; @@ -285,6 +275,9 @@ html += renderAddNodeRow(); html += ''; container.innerHTML = html; + if (typeof ced_setupSearchFields === 'function') { + setTimeout(function() { ced_setupSearchFields(); }, 10); + } }; // ---- Add/Delete Node ---- @@ -302,7 +295,7 @@ var key = input.value.trim(); if (!key) return; if (talkNodes()[key]) { notify('Node "' + key + '" already exists.', 'error'); return; } - talkNodes()[key] = { message: '', options: [] }; + talkNodes()[key] = { messages: [], options: [] }; input.value = ''; renderTalkTree(window._talkData); talkSync(); @@ -328,7 +321,7 @@ function renderOrphanNode(key, node, nodes) { var h = '
'; - h += '
'; + h += '
'; h += ''; h += '' + esc(key) + ''; h += '' + esc((getNodePreview(node) || '').substring(0, 60)) + ''; @@ -349,16 +342,17 @@ var node = nodes[rootKey]; if (!node) return ''; if (seen[rootKey]) { - return '
' + esc(rootKey) + ' (already shown)
'; + return '
' + esc(rootKey) + ' (see above)
'; } seen[rootKey] = true; var previewText = getNodePreview(node); + var isOpen = window._talkOpenNodes.has(rootKey); - var html = '
'; + var html = '
'; // Header — fix: check for data-action before toggling - html += '
'; + html += '
'; html += ''; html += '' + esc(rootKey) + ''; html += '' + esc((previewText || '').substring(0, 60)) + ''; @@ -390,16 +384,19 @@ } function getNodePreview(node) { - if (node.message) return typeof node.message === 'string' ? node.message : (Array.isArray(node.message) ? node.message[0] : ''); - if (node.sequence && node.sequence.length) return node.sequence[0]; + if (node.messages && node.messages.length) return node.messages[0]; return ''; } // ---- Node body: condition-gated content ---- + // Track open node state across re-renders + window._talkOpenNodes = window._talkOpenNodes || new Set(); + function renderNodeBody(nodeKey, node, nodes) { var ec = extractClauses(node.condition); - var hasCond = ec.mode !== 'none'; + var hasCond = ec.mode !== 'none' || (node.condition && typeof node.condition === 'object' && !Array.isArray(node.condition)); + var hasOpts = node.options && node.options.length > 0; var html = ''; @@ -412,23 +409,38 @@ html += '
'; html += renderConditionClauses(ec, nodeKey, null); html += '
'; - html += renderMessageSection(nodeKey, node); - html += renderSequenceSection(nodeKey, node); - html += renderActionSection(nodeKey, null, node.action); + html += renderMessagesSection(nodeKey, node); html += renderOptionsSection(nodeKey, node); + html += '
'; + + var nodeAction = node.action; + var actionPrefix = 'nodes.' + escAttr(nodeKey) + '.action'; + if (nodeAction && typeof nodeAction === 'object' && !Array.isArray(nodeAction)) { + html += '
'; + html += '
' + + 'Action' + + '
'; + html += '
'; + html += renderActionForm(nodeAction, actionPrefix, nodeKey, null); + html += '
'; + html += '
'; + } else { + html += ''; + } + + html += '
'; + html += renderGotoFallback(nodeKey, node, 'If condition fails, go to'); html += '
'; // .talk-cond-card-body html += '
'; // .talk-cond-card } else { html += ''; - html += renderMessageSection(nodeKey, node); - html += renderSequenceSection(nodeKey, node); - html += renderActionSection(nodeKey, null, node.action); + html += renderMessagesSection(nodeKey, node); html += renderOptionsSection(nodeKey, node); - } - - // Goto fallback — only when no options - if (!node.options || node.options.length === 0) { - html += renderGotoFallback(nodeKey, node); + html += '
'; + html += renderActionSection(nodeKey, null, node.action); + if (!hasOpts) { + html += renderGotoFallback(nodeKey, node, 'Auto-advance to'); + } } return html; @@ -436,39 +448,31 @@ // ---- Goto fallback ---- - function renderGotoFallback(nodeKey, node) { + function renderGotoFallback(nodeKey, node, label) { + label = label || 'Auto-advance to'; return '
' + - 'Auto-advance to' + - '' + + '' + esc(label) + '' + + '' + '
'; } - // ---- Message / Sequence ---- - - function renderMessageSection(nodeKey, node) { - var msg = node.message || ''; - if (Array.isArray(msg)) msg = msg.join('\n'); - return '
' + - '
Message
' + - '' + - '
'; - } + // ---- Messages section ---- - function renderSequenceSection(nodeKey, node) { - var seq = node.sequence || []; + function renderMessagesSection(nodeKey, node) { + var msgs = node.messages || []; var html = '
'; - html += '
Sequence
'; - seq.forEach(function(line, i) { + html += '
Messages
'; + msgs.forEach(function(line, i) { html += '
' + - '' + (i + 1) + '' + - '' + - '' + + '' + + '' + + '' + '
'; }); html += '
' + - '' + (seq.length + 1) + '' + - '' + - '' + + '' + + '' + + '' + '
'; html += '
'; return html; @@ -518,15 +522,16 @@ html += '' + esc(clauseTypeLabel(typeKey)) + ''; if (typeKey === 'flag' || typeKey === 'player_flag') { + var notLabel = (clause.value !== undefined && clause.value !== '') ? 'Not' : 'Not Exists'; html += 'key:'; html += ''; - html += ''; + html += ''; html += 'val:'; html += ''; } else if (typeKey === 'has_item') { html += 'item:'; html += ''; - html += ''; + html += ''; } else if (typeKey === 'min_credits') { html += 'cr:'; html += ''; @@ -620,6 +625,10 @@ delete target.condition; } else { target.condition = {}; + if (optIdx !== null) { + window._talkAutoOpenDetails = new Set(); + window._talkAutoOpenDetails.add('cond_' + optIdx + '_' + escAttr(nodeKey)); + } } renderTalkTree(window._talkData); talkSync(); @@ -661,10 +670,21 @@ ACTION_FIELDS.forEach(function(f) { var fpath = prefix + '.' + f.key; var val = (action && action[f.key] !== undefined) ? action[f.key] : ''; - html += '
' + - '' + esc(f.label) + '' + - '' + - '
'; + if (f.type === 'item_search') { + html += '
' + + '' + esc(f.label) + '' + + '
' + + '' + + '' + + '
' + + '
'; + } else { + var narrowStyle = (f.key === 'teleport' || f.key === 'heal') ? ' style="max-width:70px"' : ''; + html += '
' + + '' + esc(f.label) + '' + + '' + + '
'; + } }); html += '
'; @@ -678,7 +698,6 @@ html += renderKVEditor(action, prefix, 'set_flags', 'Set Flags', nodeKey, optIdx); html += renderKVEditor(action, prefix, 'set_player_flags', 'Set Player Flags', nodeKey, optIdx); - html += renderShopEditor(action, prefix, nodeKey, optIdx); html += '
'; // .talk-action-block return html; @@ -697,14 +716,14 @@ html += '
'; keys.forEach(function(k) { html += '
' + + '' + '' + - 'key: ' + esc(k) + '' + '' + '
'; }); html += '
' + - '' + - '' + + '' + + '' + '' + '' + '
'; @@ -712,62 +731,30 @@ return html; } - function renderShopEditor(action, prefix, nodeKey, optIdx) { - var shop = action && action.shop; - var hasShop = shop && typeof shop === 'object' && !Array.isArray(shop); - var html = '
'; - html += '
Shop
'; - - if (hasShop) { - html += '
' + - 'Shop Message' + - '' + - '
'; - - var items = shop.items || []; - html += '
Items
'; - items.forEach(function(item, si) { - var ip = prefix + '.shop.items.' + si; - html += '
' + - '' + - '' + - '' + - '' + - '
'; - }); - html += ''; - } else { - html += ''; - } - - html += '
'; - return html; - } - // ---- Options section ---- function renderOptionsSection(nodeKey, node) { var options = node.options || []; var html = '
'; - html += 'Options'; + html += 'Options'; if (options.length > 0) { options.forEach(function(opt, idx) { html += '
'; - html += ''; - html += ''; - html += ''; + html += '' + (idx + 1) + ''; + html += ''; + html += ''; html += '
'; if (opt.condition && typeof opt.condition === 'object' && !Array.isArray(opt.condition)) { html += 'cond'; } else { - html += ''; + html += ''; } if (opt.action) { html += 'act'; } else { - html += ''; + html += ''; } html += '
'; @@ -775,7 +762,9 @@ html += '
'; if (opt.condition && typeof opt.condition === 'object' && !Array.isArray(opt.condition)) { - html += '