f27f2894d0
Marty: let anyone enter a position and watch who the contract pays when it upgrades, so members can simulate helping someone. chain.paymentPath reads the buyer and up to 16 matrix uplines live from the contract (not the snapshot) and stops where _payUpline stops (root #1, empty, 16). GET /api/public/payment-path is rate-limited like the member lookup, cached 60 s. The page walks the rule from the buyer upward, one generation per step; the real next upgrade is labelled, higher levels are what-ifs, and any passed-over upline gets a "what if they did?" to give them the missing level or direct and re-run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
8.6 KiB
JavaScript
129 lines
8.6 KiB
JavaScript
/* /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 = '<li class="hop buyer idle"><span class="n">buyer</span><span class="id">#' + b.id + '</span><span class="meta">' + lvName(b.level) + ' · ' + b.directCount + ' direct' + (b.directCount === 1 ? '' : 's') + ' · buying ' + lvName(li + 1) + '</span><span class="tag"></span></li>';
|
|
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 = '<span class="why">' + esc(st.why);
|
|
if (st.fix && upto >= r.steps.length) why += '<button type="button" data-fix="' + st.fix.id + '" data-level="' + (st.fix.level || '') + '" data-directs="' + (st.fix.directs || '') + '">What if they did?</button>';
|
|
why += '</span>';
|
|
}
|
|
html += '<li class="hop ' + cls + (e.what ? ' whatif' : '') + '"><span class="n">up ' + (i + 1) + '</span><span class="id">#' + u.id + '</span><span class="meta">' + lvName(e.level) + ' · ' + e.directCount + ' direct' + (e.directCount === 1 ? '' : 's') + '</span><span class="tag">' + tag + '</span>' + why + '</li>';
|
|
}
|
|
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 += '<li class="hop fee"><span class="n">stop</span><span class="id">fee</span><span class="meta">' + (data.stopped === 'root' && U.length < 16 ? '#1 root reached' : '16 steps used') + '</span><span class="tag">fee wallet</span><span class="why">' + reason + ' Nobody qualified, so the full ' + amtTxt + ' goes to the fee wallet.</span></li>';
|
|
}
|
|
chainEl.innerHTML = html;
|
|
if (done) {
|
|
var wi = Object.keys(overrides).length ? ' <span style="color:var(--gold)">(with your what-if changes)</span>' : '';
|
|
sumEl.innerHTML = r.paid !== null
|
|
? '<b>#' + U[r.paid].id + ' would be paid ' + amtTxt + '</b>' + wi + '. ' + li + ' upline' + (li === 1 ? '' : 's') + ' skipped, ' + (r.paid - li) + ' passed over.'
|
|
: '<b>Nobody in reach qualifies, so the full ' + amtTxt + ' would go to the fee wallet</b>' + 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 =
|
|
'<div class="fact"><small>Position</small><strong>#' + b.id + '</strong></div>' +
|
|
'<div class="fact"><small>Level</small><strong>' + lvName(b.level) + '</strong></div>' +
|
|
'<div class="fact"><small>Tier</small><strong>' + t + '</strong></div>' +
|
|
'<div class="fact"><small>Directs</small><strong>' + b.directCount + '</strong></div>' +
|
|
'<div class="fact"><small>Matrix parent</small><strong>' + (b.uplineId ? '#' + b.uplineId : '—') + '</strong></div>';
|
|
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>#' + b.id + ' already holds Corona</b>, 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)); }
|
|
})();
|