/* /who-gets-paid (Marty, 2026-09-26): enter any position, read it and its matrix uplines live from /api/public/payment-path, and walk the contract's _payUpline rule one step at a time from the buyer upward. "What if" lets a viewer give a passed-over upline the level or direct they are missing and see where the payment would land instead. The rule here is the contract's: skip levelIndex uplines; pay the first with level > levelIndex and directCount >= 2; stop at the root (#1) or after 16 steps and send it to the fee wallet. */ (function () { var NAMES = ['Scintilla', 'Ascensus', 'Fabrica', 'Culmen', 'Apex', 'Fastigium', 'Vertex', 'Corona']; var $ = function (id) { return document.getElementById(id); }; var form = $('wgpForm'), idIn = $('wgpId'), sel = $('wgpLvl'), buyWrap = $('wgpBuyWrap'); var runBtn = $('wgpRun'), resetBtn = $('wgpReset'), buyerEl = $('wgpBuyer'), sumEl = $('wgpSum'); var chainEl = $('wgpChain'), legend = $('wgpLegend'); var data = null, overrides = {}, timer = null; function esc(s) { return String(s).replace(/[&<>"]/g, function (c) { return { '&': '&', '<': '<', '>': '>', '"': '"' }[c]; }); } function fmt(n) { return Number(n || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }); } function lvName(l) { return NAMES[l - 1] || ('level ' + l); } function eff(u) { var o = overrides[u.id] || {}; return { level: o.level || u.level, directCount: o.directCount != null ? o.directCount : u.directCount, what: !!(o.level || o.directCount != null) }; } function price(li) { var t = data.buyer.tier === 2 ? 2 : 1; return data.costs && data.costs.up && data.costs.up[t] ? data.costs.up[t][li] : null; } function evaluate(li) { var steps = [], paid = null, U = data.uplines; for (var i = 0; i < U.length; i++) { var e = eff(U[i]); if (i < li) { steps.push({ s: 'skip', why: 'Skipped: ' + (i + 1) + ' of ' + li + ' skipped without checking.' }); continue; } var lvOk = e.level > li, dOk = e.directCount >= 2; if (lvOk && dOk) { steps.push({ s: 'paid', why: 'Holds ' + lvName(e.level) + ', needs ' + lvName(li + 1) + ' or higher, and has ' + e.directCount + ' directs. Paid in full; the search ends.' }); paid = i; break; } var need = []; if (!lvOk) need.push('needs ' + lvName(li + 1) + ' or higher (holds ' + lvName(e.level) + ')'); if (!dOk) need.push('needs 2 directs (has ' + e.directCount + ')'); steps.push({ s: 'fail', why: 'Passed over: ' + need.join(' and ') + '.', fix: { id: U[i].id, level: lvOk ? null : li + 1, directs: dOk ? null : 2 } }); } return { steps: steps, paid: paid }; } function draw(li, upto) { var r = evaluate(li), U = data.uplines, b = data.buyer; var html = '
  • buyer#' + b.id + '' + lvName(b.level) + ' · ' + b.directCount + ' direct' + (b.directCount === 1 ? '' : 's') + ' · buying ' + lvName(li + 1) + '
  • '; for (var i = 0; i < U.length; i++) { var u = U[i], e = eff(u), st = (i < r.steps.length && i < upto) ? r.steps[i] : null; var cls = st ? st.s : 'idle', tag = st ? (st.s === 'skip' ? 'skipped' : st.s === 'fail' ? 'passed over' : 'paid') : ''; var why = ''; if (st) { why = '' + esc(st.why); if (st.fix && upto >= r.steps.length) why += ''; why += ''; } html += '
  • up ' + (i + 1) + '#' + u.id + '' + lvName(e.level) + ' · ' + e.directCount + ' direct' + (e.directCount === 1 ? '' : 's') + '' + tag + '' + why + '
  • '; } var done = upto >= r.steps.length, amt = price(li); var amtTxt = amt != null ? fmt(amt) + ' POL' : 'the upgrade'; if (done && r.paid === null) { var reason = data.stopped === 'root' && U.length < 16 ? 'The next upline is #1, the root. The search stops there.' : 'The search used all 16 steps.'; html += '
  • stopfee' + (data.stopped === 'root' && U.length < 16 ? '#1 root reached' : '16 steps used') + 'fee wallet' + reason + ' Nobody qualified, so the full ' + amtTxt + ' goes to the fee wallet.
  • '; } chainEl.innerHTML = html; if (done) { var wi = Object.keys(overrides).length ? ' (with your what-if changes)' : ''; sumEl.innerHTML = r.paid !== null ? '#' + U[r.paid].id + ' would be paid ' + amtTxt + '' + wi + '. ' + li + ' upline' + (li === 1 ? '' : 's') + ' skipped, ' + (r.paid - li) + ' passed over.' : 'Nobody in reach qualifies, so the full ' + amtTxt + ' would go to the fee wallet' + wi + '. Skipped ' + Math.min(li, U.length) + ', checked ' + Math.max(0, U.length - li) + '.'; } else sumEl.textContent = 'Checking ' + (upto + 1) + ' up…'; } function run(animate) { if (!data) return; if (timer) { clearInterval(timer); timer = null; } var li = Number(sel.value), total = evaluate(li).steps.length; var reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches; if (!animate || reduce) { draw(li, total); return; } var k = 0; draw(li, 0); timer = setInterval(function () { k++; draw(li, k); if (k >= total) { clearInterval(timer); timer = null; } }, 380); } function showBuyer() { var b = data.buyer, t = b.tier === 2 ? 'Premium' : 'Standard'; buyerEl.innerHTML = '
    Position#' + b.id + '
    ' + '
    Level' + lvName(b.level) + '
    ' + '
    Tier' + t + '
    ' + '
    Directs' + b.directCount + '
    ' + '
    Matrix parent' + (b.uplineId ? '#' + b.uplineId : '—') + '
    '; buyerEl.hidden = false; sel.innerHTML = ''; for (var L = b.level + 1; L <= 8; L++) { var li = L - 2, o = document.createElement('option'), p = price(li); o.value = li; o.textContent = NAMES[L - 1] + (p != null ? ' · ' + fmt(p) + ' POL' : '') + (L === b.level + 1 ? ' (its real next upgrade)' : ' (what if)'); sel.appendChild(o); } var canBuy = b.level < 8; buyWrap.hidden = !canBuy; runBtn.hidden = !canBuy; legend.hidden = !canBuy; if (!canBuy) { chainEl.innerHTML = ''; sumEl.innerHTML = '#' + b.id + ' already holds Corona, the top level, so it has no upgrade left to buy.'; } } function load(id) { if (!id) return; if (timer) { clearInterval(timer); timer = null; } overrides = {}; resetBtn.hidden = true; sumEl.textContent = 'Reading #' + id + ' and its uplines from the blockchain…'; chainEl.innerHTML = ''; buyerEl.hidden = true; try { history.replaceState(null, '', '/who-gets-paid?id=' + id); } catch (e) {} fetch('/api/public/payment-path?id=' + encodeURIComponent(id)).then(function (r) { return r.json().then(function (j) { if (!r.ok) throw new Error(j.error || 'Lookup failed.'); return j; }); }).then(function (j) { data = j; showBuyer(); if (data.buyer.level < 8) run(false); }).catch(function (e) { data = null; sumEl.textContent = e.message || 'Lookup failed. Try again.'; }); } form.addEventListener('submit', function (e) { e.preventDefault(); var id = parseInt(idIn.value, 10); if (id > 0) load(id); else sumEl.textContent = 'Enter a position number, like 1191.'; }); runBtn.addEventListener('click', function () { run(true); }); sel.addEventListener('change', function () { run(false); }); resetBtn.addEventListener('click', function () { overrides = {}; resetBtn.hidden = true; run(false); }); chainEl.addEventListener('click', function (e) { var bt = e.target.closest('button[data-fix]'); if (!bt) return; var id = Number(bt.dataset.fix), o = overrides[id] || {}; if (bt.dataset.level) o.level = Number(bt.dataset.level); if (bt.dataset.directs) o.directCount = Number(bt.dataset.directs); overrides[id] = o; resetBtn.hidden = false; run(false); }); var q = new URLSearchParams(location.search).get('id'); if (q && /^\d+$/.test(q)) { idIn.value = q; load(Number(q)); } })();