' + (nxt <= PLAN.maxL ? 'Buying level ' + nxt + ' (' + $$(price(nxt)) + ') ' + where + how + skipped + '.' : 'At the top of the ladder.') + '
'
+ (nxt <= PLAN.maxL ? '' : '')
+ '
Directs: ' + n.directs + '
'
+ [0, 1, 2].filter(k => k !== n.directs).map(k => '').join('');
pop.hidden = false;
const w = 250, h = pop.offsetHeight || 140;
let left = r.left + r.width / 2 - w / 2, top = r.bottom + 8;
left = Math.max(8, Math.min(left, window.innerWidth - w - 8));
if (top + h > window.innerHeight - 8) top = r.top - h - 8;
pop.style.left = left + 'px'; pop.style.top = top + 'px';
const pb = document.getElementById('popBuy'); if (pb) pb.onclick = () => { closePop(); buy(n); };
pop.querySelectorAll('[data-dir]').forEach(b => b.onclick = () => {
closePop(); const k = Number(b.dataset.dir);
if (k >= 1 && n.directs < 1) n.cashFromHalf = nextId; // the first direct arrives now: depths 1-2 pay from here on
if (k >= 2 && n.directs < 2) n.cashFrom = nextId; // the second: full reach from here on
n.directs = k; refreshNode(n); paintPays();
const older = cnt(nodes.filter(x => x.id < nextId && x !== n).length);
const msg = k === 2 ? name(n) + ' now has two directs: full reach, cash from its own directs and from positions that join from now on. The ' + older + ' existing positions keep passing it.'
: k === 1 ? name(n) + ' now has one direct: ' + (PLAN.half ? 'cash from depths 1-2 (levels 2-3), from its own directs and from positions that join from now on' : 'no cash until the second (half step is off)') + (PLAN.unq === 'licence' ? '; licences still reach it.' : '.')
: name(n) + ' now has no directs: cash passes over it' + (PLAN.unq === 'licence' ? ', licences still reach it.' : '.');
renderLast(null, null, null, msg, k ? 'cash' : 'none');
addLog([{ kind: k ? 'cash' : 'skip', text: msg }], name(n));
});
}
document.addEventListener('click', e => { if (!pop.hidden && !pop.contains(e.target) && !e.target.closest('.node')) closePop(); });
document.addEventListener('keydown', e => { if (e.key === 'Escape') closePop(); });
function renderLast(n, L, steps, override, cls){
const el = document.getElementById('last');
if (override) { el.className = 'last ' + (cls || ''); el.innerHTML = override; return; }
const lic = steps.filter(s => s.kind === 'lic');
const cash = steps.filter(s => s.kind === 'cash');
const youLic = lic.some(s => /^YOU held/.test(s.text));
const youCash = cash.some(s => /^YOU already/.test(s.text));
let text = '' + name(n) + ' bought level ' + L + ' (' + product(L) + ') for ' + $$(price(L)) + '. ';
if (youLic) text += 'That payment was your licence: you were upgraded in the same transaction and the cost went on above you.';
else if (youCash) text += 'It reached you as cash' + (lic.length ? ', after issuing ' + lic.length + ' licence' + (lic.length > 1 ? 's' : '') + ' on the way up' : '') + '.';
else if (lic.length) text += 'It became a licence for ' + lic[0].text.split(' held')[0] + ' and the money climbed on from there.';
else if (cash.length && !/above you/.test(cash[0].text)) text += 'It paid ' + cash[0].text.split(' already')[0] + ' as cash.';
else text += 'It went above you, to your own upline.';
const why = youLic || youCash ? '' : 'Not yours: a depth-' + n.depth + ' position pays you only when it buys level ' + (n.depth + 1) + '. It holds level ' + L + ' now' + (L < n.depth + 1 ? ', so ' + (n.depth + 1 - L) + ' more purchase' + (n.depth + 1 - L > 1 ? 's' : '') + ' until its next one reaches you.' : '.') + '';
el.className = 'last ' + (youLic ? 'lic' : youCash ? 'cash' : 'none');
el.innerHTML = text + why;
}
function buy(n){
const L = n.level + 1;
if (L > PLAN.maxL) return;
clearHot();
n.level = L; refreshNode(n);
const steps = [], flashes = [[n, 'flash-src']];
purchase(n, L, steps, flashes);
for (const [m, c] of flashes) { const g = document.getElementById('n' + m.id); if (g) g.classList.add(c); }
addLog(steps, `${name(n)} bought level ${L} for ${$$(price(L))}`);
renderLast(n, L, steps);
paintPays();
return steps;
}
function paintPays(){
for (const n of nodes) {
if (n === you) continue;
const g = document.getElementById('n' + n.id); if (!g) continue;
const r = n.level + 1 <= PLAN.maxL ? route(n, n.level + 1, n.level - 1, false, n) : { to: null };
g.classList.toggle('pays', r.to === you);
}
}
function wave(){
clearHot();
const before = you.level, cashBefore = totals.cash;
const steps = [];
quiet = true; tally = {};
const order = nodes.filter(n => n !== you).sort((a, b) => a.depth - b.depth || a.idx - b.idx);
let bought = 0, licYou = 0, skipYou = 0;
for (const n of order) {
if (!n.qual) continue;
const L = n.level + 1; if (L > PLAN.maxL) continue;
tallyOrigin = n.depth; const tf = tallyFor(n.depth); tf.n++; tf.L = L;
n.level = L; refreshNode(n); bought++;
const sub = [], fl = [];
const licBefore = totals.lic;
purchase(n, L, sub, fl);
if (totals.lic > licBefore) licYou++;
if (sub.some(s => /went on above you/.test(s.text))) skipYou++;
}
quiet = false;
renderWaveTable();
const after = you.level;
steps.push({ kind: 'lic', text: `${bought} positions each bought one level, shallow to deep.` });
steps.push({ kind: after > before ? 'lic' : 'cash', text: after > before
? `YOU went from level ${before} to ${after} without buying anything: ${licYou} licence${licYou === 1 ? '' : 's'} were funded from below.`
: (before >= DEPTHS
? `YOU stayed at level ${before}: your org is only ${DEPTHS} deep, and licence ${before + 1} is funded by a depth-${before} position buying level ${before + 1}. Nothing that deep exists here. Grow the org to keep climbing.`
: `YOU stayed at level ${before}: nothing at depth ${before} bought level ${before + 1} this wave.`) });
steps.push({ kind: 'cash', text: `Cash to you this wave: ${$$(totals.cash - cashBefore)}.` });
if (skipYou) steps.push({ kind: 'skip', text: `${skipYou} payment${skipYou === 1 ? '' : 's'} went on above you: levels that pay uplines higher than you. Not a miss.` });
addLog(steps, 'Wave');
renderLast(null, null, null, 'Wave: ' + steps.map(s => s.text).join(' '), after > before ? 'lic' : 'cash');
paintPays();
}
function renderWaveTable(){
const box = document.getElementById('waveBox'), tb = document.querySelector('#waveTable tbody');
if (!box || !tb || !tally) return;
tb.innerHTML = '';
for (const d of Object.keys(tally).map(Number).sort((a, b) => a - b)) {
const t = tally[d];
const toYou = [t.youLic ? '' + t.youLic + ' licence' + (t.youLic > 1 ? 's' : '') + '' : '', t.youCash ? '' + $$(t.youCash) + '' : ''].filter(Boolean).join(' + ') || '—';
const others = Object.keys(t.cash).concat(Object.keys(t.lic)).filter((v, i, a) => a.indexOf(v) === i).map(Number).sort((a, b) => a - b)
.map(k => 'depth ' + k + ': ' + (t.cash[k] ? $$(t.cash[k]) : '') + (t.lic[k] ? (t.cash[k] ? ' + ' : '') + t.lic[k] + ' licence' + (t.lic[k] > 1 ? 's' : '') : '')).join(' ') || '—';
const tr = document.createElement('tr');
const above = [t.above ? $$(t.above) : '', t.licUp ? $$(t.licUp) + ' (your licence cost, climbing)' : ''].filter(Boolean).join(' + ') || '—';
tr.innerHTML = '
' + d + '
' + t.L + ' (' + $$(price(t.L)) + ')
' + t.n + '
' + toYou + '
' + others + '
' + above + '
';
tb.appendChild(tr);
}
box.hidden = false;
}
function addLog(steps, head){
const ol = document.getElementById('logList');
if (ol.firstChild && /Nothing yet|Reset\./.test(ol.firstChild.textContent)) ol.innerHTML = '';
const h = document.createElement('li'); h.className = 'head'; h.textContent = head; ol.prepend(h);
let after = h;
for (const s of steps) {
const li = document.createElement('li');
const chip = document.createElement('span'); chip.className = 'chip ' + s.kind; chip.textContent = s.kind === 'lic' ? 'licence' : s.kind === 'skip' ? 'skip' : 'cash';
li.appendChild(chip); li.appendChild(document.createTextNode(' ' + s.text));
after.after(li); after = li;
}
while (ol.children.length > 60) ol.removeChild(ol.lastChild);
}
function clearLog(msg){ document.getElementById('logList').innerHTML = `
${msg}
`; }
const lastShown = {};
function setV(id, text){
const el = document.getElementById(id); if (!el) return;
if (lastShown[id] !== undefined && lastShown[id] !== text) { el.classList.remove('bump'); void el.offsetWidth; el.classList.add('bump'); }
lastShown[id] = text; el.textContent = text;
}
function renderTotals(){
if (!you) return;
setV('mLevel', you.level);
const sl = document.getElementById('youLevel'); if (sl && Number(sl.value) !== you.level && you.level <= Number(sl.max)) { sl.value = you.level; document.getElementById('youLevelVal').textContent = you.level; }
const mr = document.getElementById('mReach'); if (mr) mr.textContent = you.level;
setV('mEntry', $$(totals.entry));
const en = document.getElementById('mEntryN'); if (en) en.textContent = you.children.length + ' directs × ' + $$(net(1)) + ' (the ' + $$(price(1)) + ' entry after the fee), no qualification';
setV('mCash', $$(totals.cash));
const cn = document.getElementById('mCashN'); if (cn) cn.textContent = totals.cashN + ' payment' + (totals.cashN === 1 ? '' : 's');
setV('mLic', totals.lic);
const lv = document.getElementById('mLicV'); if (lv) lv.textContent = 'worth ' + $$(totals.licV) + ', paid by your org';
setV('mTotal', $$(totals.entry + totals.cash + totals.licV));
setV('mFuel', $$(AUD.treasury)); const fs = document.getElementById('mFuelS'); if (fs) fs.textContent = PLAN.fee ? Math.round(PLAN.fee * 100) + '% of every level bought so far (plus ' + $$(AUD.entryFee) + ' on the entries paid when this org joined)' : 'no fee: the whole price climbs';
const ol = document.getElementById('youLedger');
if (ol) {
ol.innerHTML = '';
if (!youLedger.length) ol.innerHTML = '
Nothing yet.
';
for (const e of youLedger.slice(0, 10)) {
const li = document.createElement('li');
li.innerHTML = '' + (e.kind === 'lic' ? 'licence' : 'cash') + ' ' + (e.kind === 'lic' ? 'your licence ' + e.L + ', funded by ' : 'level-' + e.L + ' money from ') + name(e.from) + '' + (e.kind === 'lic' ? 'licence ' + e.L + ' (' + $$(e.amt) + ')' : '+' + $$(e.amt)) + '';
ol.appendChild(li);
}
}
const ln = document.getElementById('ledgerNote'); if (ln) ln.textContent = 'Each position pays you at most once per level, so the count is what matters: at level L there are ' + PLAN.w + '^' + '(L−1) positions at the depth that pays you, and each pays the level-L price.';
ladder();
}
document.getElementById('youLevel').addEventListener('input', e => {
document.getElementById('youLevelVal').textContent = e.target.value;
you.level = Number(e.target.value); refreshNode(you); paintReach(); paintPays(); renderTotals();
});
document.getElementById('wave').addEventListener('click', wave);
function at(path){ let n = you; for (const ch of path) n = n.children[ch === 'L' ? 0 : 1]; return n; }
const TOUR = [
{ step: 'Step 1 of 7 · your licence', say: 'You hold level 1, like every new member, and so do your directs (labelled). Their entries already paid you. Your FIRST direct buys level 2. Level-2 money pays the first upline, which is you, and you hold exactly one level below, so this payment IS your licence 2: you are upgraded in the same transaction and the money continues above you. No cash yet.',
act: () => buy(at('L')) },
{ step: 'Step 2 of 7 · cash', say: 'Your SECOND direct buys level 2. You now hold level 2, so this one is cash: the level-2 price to you. Watch the bar.',
act: () => buy(at('R')) },
{ step: 'Step 3 of 7 · not yours', say: 'A grandchild, under your first direct, buys level 2. Level-2 money stops at the FIRST upline, its own parent, who already holds level 2, so it is that parent\'s cash and never reaches you. That is why the rings matter: a position pays you only at the one level that maps to its depth.',
act: () => buy(at('LL')) },
{ step: 'Step 4 of 7 · your licence 3', say: 'The same grandchild buys level 3. Level-3 money skips one upline and lands on you; you hold level 2, exactly one below, so this is your licence 3. The amber band steps down a depth and the rings move.',
act: () => buy(at('LL')) },
{ step: 'Step 5 of 7 · cascade', say: 'One purchase can pay two people. A depth-3 position under your second direct buys level 2. It pays its parent (depth 2), who holds exactly level 1: that parent gets licence 2, and the parent\'s own level-2 cost climbs to your second direct as cash. One buyer, one licence issued, one cash payment.',
act: () => buy(at('RLL')) },
{ step: 'Step 6 of 7 · skipped', say: 'Your first direct now has NO directs of its own (dashed). Its other child buys level 2. That money would pay the parent, but the parent has recruited nobody, so cash passes over it and keeps climbing: you catch it. Qualification decides who is skipped; it never holds money back. (With one direct, the half step would let the parent keep this one.)',
act: () => { const l = at('L'); l.directs = 0; refreshNode(l); paintPays(); buy(at('LR')); } },
{ step: 'Step 7 of 7 · the wave', say: 'Everything at once: every position buys one level, shallow to deep. Read the table that appears under the strip: one row per depth, where that depth\'s money went. Then press Run a wave again and watch your level climb without buying anything.',
act: () => wave() },
{ step: 'Done', say: 'That is the whole plan. Now switch the gap rule and the width at the top and run waves again: the tree resets, and the difference shows up in the money bar.', act: null }
];
let ti = -1;
function tourShow(){
const t = TOUR[ti]; document.getElementById('tourStep').textContent = t.step; document.getElementById('tourSay').textContent = t.say;
const nx = document.getElementById('tourNext'); nx.hidden = false; nx.textContent = t.act ? 'Show me' : 'Finish';
document.getElementById('tourStart').textContent = 'Restart tour';
}
document.getElementById('tourStart').addEventListener('click', () => { document.getElementById('youLevel').value = 1; document.getElementById('youLevelVal').textContent = '1'; build(); ti = 0; tourShow(); });
document.getElementById('tourNext').addEventListener('click', () => {
const t = TOUR[ti];
if (t.act) { t.act(); }
if (ti < TOUR.length - 1) { ti++; tourShow(); }
else { document.getElementById('tourNext').hidden = true; }
});
document.getElementById('reset').addEventListener('click', () => { document.getElementById('youLevel').value = 1; document.getElementById('youLevelVal').textContent = '1'; build(); });
document.getElementById('grow').addEventListener('click', grow);
function depthLabel(){ const el = document.getElementById('depthLabel'); if (el) el.textContent = DEPTHS + ' deep, ' + cnt(nodes.length - 1) + ' positions'; const g = document.getElementById('grow'); if (g) g.disabled = DEPTHS >= MAXD[PLAN.w]; }
function grow(){
if (DEPTHS >= MAXD[PLAN.w]) return;
const leaves = nodes.filter(n => n.depth === DEPTHS);
DEPTHS++;
let entries = 0;
for (const n of leaves) for (let k = 0; k < PLAN.w; k++) { mkNode(n.depth + 1, n, n.idx * PLAN.w + k); entries += net(1); }
geometry(); layout(); draw(); paintPays(); renderTotals(); depthLabel();
addLog([{ kind: 'lic', text: `The org is now ${DEPTHS} deep: ${leaves.length * PLAN.w} new level-1 positions joined at the bottom and paid ${$$(entries)} of entries to the row above. Everything else is as it was.` }], 'Grew one depth');
}
// ── ladder table ──
const priceEl = document.getElementById('price');
function ladder(){
document.getElementById('priceVal').textContent = '$' + priceEl.value;
const tb = document.querySelector('#ladder tbody'); if (!tb) return; tb.innerHTML = '';
const rows = [1]; const top = Math.min(12, PLAN.maxL);
for (let L = 2; L <= top; L++) rows.push(L);
if (PLAN.maxL > 12) rows.push('gap', 20, 'gap', PLAN.maxL);
let climb = 0; for (let L = 1; L <= PLAN.maxL; L++) climb += price(L);
for (const L of rows) {
const tr = document.createElement('tr');
if (L === 'gap') { tr.className = 'gap'; tr.innerHTML = '
⋮
'; tb.appendChild(tr); continue; }
const d = L === 1 ? 1 : L - 1, pos = Math.pow(PLAN.w, d);
if (you && L === you.level + 1) tr.className = 'you';
tr.innerHTML = `
${L}${you && L === you.level + 1 ? ' your next' : ''}
${product(L)} placeholder
${L === 1 ? 'entries from depth 1, no qualification' : 'depth ' + d + ', buying level ' + L}
${cnt(pos)}
${$$(pos * net(L))}
${$$(price(L))}${L === 1 ? ' everyone pays this to enter' : ' or the first payment from depth ' + d + ''}
`;
tb.appendChild(tr);
}
const ln = document.getElementById('ladderNote'); if (ln) ln.textContent = PLAN.w + '-wide, ' + PLAN.maxL + ' levels. Potential is one payment from every position at the depth that pays that level, so it grows ' + PLAN.w + '× per level on its own; the price ladder only sets the dollars, and ' + Math.round(PLAN.fee * 100) + '% of every price goes to the admin wallet before anything climbs. Climbing all ' + PLAN.maxL + ' levels yourself would cost ' + $$(climb) + ', which nobody has to do: the first payment at each depth is the licence.';
}
priceEl.addEventListener('input', () => { renderTotals(); for (const n of nodes) refreshNode(n); if (dp) deepRender(); });
// ── deep simulator: the same rules on counts per depth ──
let dp = null;
function deepReset(){
const fill = Number(document.getElementById('deepFill').value) / 100, D = PLAN.maxL;
dp = { D, levels: [], counts: [], lic: [], licV: [], cash: [], entry: [], pays: [], waves: 0, above: 0, spent: 0, treasury: 0, entryFee: 0 }; // treasury = fee on purchases made in waves; entryFee = fee on the entries paid when the org was built
window.deepAudit = () => { const rec = dp.cash.reduce((a, b) => a + b, 0) + dp.entry.reduce((a, b) => a + b, 0) + dp.above + dp.treasury + dp.entryFee; return { spent: Math.round(dp.spent), received: Math.round(rec), ok: Math.abs(dp.spent - rec) <= 1 + 1e-9 * dp.spent }; };
for (let d = 0; d <= D; d++) {
dp.levels[d] = 1;
dp.counts[d] = d === 0 ? 1 : Math.max(1, Math.round(Math.pow(PLAN.w * fill, d)));
dp.lic[d] = 0; dp.licV[d] = 0; dp.cash[d] = 0; dp.entry[d] = 0; dp.pays[d] = 0;
}
for (let d = 1; d <= D; d++) { dp.entry[d - 1] += dp.counts[d] * net(1); dp.spent += dp.counts[d] * price(1); dp.entryFee += dp.counts[d] * price(1) * PLAN.fee; } // Rule 0, once, when the org is built
deepRender('Reset. Everyone at level 1, you included; entries paid.');
const ol = document.getElementById('deepStory'); if (ol) ol.innerHTML = '
Start. You and everyone under you hold level 1. Your ' + cnt(dp.counts[1]) + ' directs paid their entries: ' + $$(dp.entry[0]) + ' to you after the fee. (All ' + cnt(dp.counts.reduce((a, b) => a + b, 0) - 1) + ' positions paid an entry when they joined; the admin wallet took ' + $$(dp.entryFee) + ' on those, which is not in the running total below.) Press Run one wave.
';
}
// n payments of `amt` for level L from depth `from`: skip `skipN`, then the gap rule on aggregate levels
function deepPay(n, from, L, amt, skipN, rem){
if (n <= 0) return;
let r = from - 1 - skipN;
while (r >= 0 && !(rem ? dp.levels[r] >= L : (PLAN.gap === 'fund' || dp.levels[r] >= L - 1))) r--;
if (r < 0) { dp.above += n * amt; return; }
if (dp.levels[r] >= L) { dp.cash[r] += n * amt; dp.pays[r] += n; return; }
const nl = dp.levels[r] + 1, cost = net(nl);
const granted = Math.min(dp.counts[r], n);
dp.lic[r] += granted; dp.licV[r] += granted * cost; dp.levels[r] = nl;
deepPay(granted, r, nl, cost, nl - 2); // the licence costs climb from depth r
if (amt - cost > 0.005) deepPay(granted, r, L, amt - cost, 0, true); // the remainder climbs on, cash only
deepPay(n - granted, from, L, amt, skipN); // the rest of this depth's payments, now that r has moved up
}
function deepWave(){
if (!dp) deepReset();
for (let d = 1; d <= dp.D; d++) { const L = dp.levels[d] + 1; if (L > PLAN.maxL) continue; dp.spent += dp.counts[d] * price(L); dp.treasury += dp.counts[d] * price(L) * PLAN.fee; deepPay(dp.counts[d], d, L, net(L), L - 2); dp.levels[d] = Math.max(dp.levels[d], L); }
dp.waves++;
}
function deepRender(note){
document.getElementById('dLevel').textContent = dp.levels[0];
document.getElementById('dReach').textContent = Math.min(dp.levels[0], dp.D);
document.getElementById('dEntry').textContent = $$(dp.entry[0]);
document.getElementById('dCash').textContent = $$(dp.cash[0]);
document.getElementById('dCashN').textContent = cnt(dp.pays[0]) + ' payment' + (dp.pays[0] === 1 ? '' : 's');
document.getElementById('dLic').textContent = dp.lic[0];
document.getElementById('dLicV').textContent = 'worth ' + $$(dp.licV[0]);
document.getElementById('dWaves').textContent = dp.waves;
document.getElementById('dAbove').textContent = $$(dp.above);
document.getElementById('dFuel').textContent = $$(dp.treasury);
const tb = document.querySelector('#deepTable tbody'); tb.innerHTML = '';
const maxCash = Math.max(1, ...dp.cash.map((c, d) => d === 0 ? 0 : c));
for (let d = 0; d <= dp.D; d++) {
const tr = document.createElement('tr');
tr.className = d === 0 ? 'you' : d <= dp.levels[0] ? 'reach' : d === dp.levels[0] + 1 ? 'next' : '';
const per = dp.counts[d] ? (dp.cash[d] + dp.entry[d]) / dp.counts[d] : 0;
tr.innerHTML = '
' + (d === 0 ? 'YOU' : d) + '
' + cnt(dp.counts[d]) + '
' + dp.levels[d] + '
' + cnt(dp.lic[d]) + '
' + $$(dp.entry[d]) + '
' + $$(dp.cash[d]) + '
'
+ '
' + (d === 0 ? '' : '') + '
' + $$(per) + '
';
tb.appendChild(tr);
}
if (note) document.getElementById('deepNote').textContent = note;
}
function storyLine(html, cls){ const ol = document.getElementById('deepStory'); if (!ol) return; if (ol.firstChild && /Nothing has happened/.test(ol.firstChild.textContent)) ol.innerHTML = ''; const li = document.createElement('li'); if (cls) li.className = cls; li.innerHTML = html; ol.prepend(li); while (ol.children.length > 40) ol.removeChild(ol.lastChild); }
function deepRun(k){
if (!dp) deepReset();
let ran = 0;
for (let i = 0; i < k; i++) {
if (dp.levels[0] >= PLAN.maxL) break;
const lv0 = dp.levels[0], cash0 = dp.cash[0], lic0 = dp.lic[0], adm0 = dp.treasury, above0 = dp.above;
deepWave(); ran++;
const lv1 = dp.levels[0], dCash = dp.cash[0] - cash0, dLic = dp.lic[0] - lic0;
const d = lv0; // the depth whose purchase this wave could reach you
const L = d + 1;
let html = 'Wave ' + dp.waves + '. Every position bought its next level. ';
if (d <= dp.D) html += 'The ' + cnt(dp.counts[d]) + ' positions at depth ' + d + ' bought level ' + L + ' (' + product(L) + ', ' + $$(price(L)) + ' each, ' + $$(net(L)) + ' after the fee), and level-' + L + ' money is yours. ';
if (dLic) html += 'The first one was your licence ' + lv1 + ': you were upgraded in the same block and its cost climbed on above you. ';
if (dCash > 0) html += 'The rest reached you as cash: ' + $$(dCash) + '. ';
if (!dLic && dCash <= 0) html += 'Nothing reached you: that depth is empty at this fill, or you are at the top. ';
html += 'Everything the other depths bought paid the people between you and them, or climbed past you. Admin wallet took ' + $$(dp.treasury - adm0) + ' this wave. ';
html += 'You: level ' + lv1 + ', reach depth 1 to ' + Math.min(lv1, dp.D) + '';
storyLine(html, dLic ? 'lic' : 'cash');
}
const after = dp.levels[0];
if (after >= PLAN.maxL) storyLine('Top of the ladder. You hold level ' + PLAN.maxL + '. From here every payment from every depth is cash, and there is no level 9: positions ' + PLAN.maxL + ' deep pay the people between you and them.', 'head');
deepRender(ran + ' wave' + (ran === 1 ? '' : 's') + ' run.');
}
document.getElementById('deepWave').addEventListener('click', () => deepRun(1));
document.getElementById('deepWave5').addEventListener('click', () => deepRun(5));
document.getElementById('deepToTop').addEventListener('click', () => deepRun(80));
document.getElementById('deepReset').addEventListener('click', deepReset);
document.getElementById('deepFill').addEventListener('input', e => { document.getElementById('deepFillVal').textContent = e.target.value + '%'; deepReset(); });
// ── grow a real org, month by month (the realistic run) ──
const SEED = {"apex":1,"m":[[1,0,6,18],[2,1,4,32],[3,1,4,17],[4,21,2,2],[5,3,1,2],[6,2,2,2],[7,2,2,5],[8,1,2,3],[9,2,1,2],[10,2,3,95],[11,3,2,2],[12,1,2,2],[13,8,2,2],[14,8,2,4],[15,14,2,4],[16,1,1,2],[17,1,2,2],[18,2,1,2],[19,3,1,2],[20,2,2,2],[21,2,3,9],[22,2,1,2],[23,3,1,2],[24,2,1,2],[25,11,1,2],[26,8,1,2],[27,12,1,2],[28,4,1,2],[29,4,1,2],[30,5,1,2],[31,5,1,2],[32,3,1,2],[33,7,2,2],[34,7,2,2],[35,9,1,2],[36,9,1,2],[37,15,1,2],[38,10,2,7],[39,10,2,3],[40,16,1,2],[41,10,2,2],[42,10,1,2],[43,16,1,2],[44,17,1,2],[45,17,1,2],[46,18,1,2],[47,18,1,2],[48,19,1,2],[49,19,1,2],[50,20,1,2],[51,10,1,2],[52,20,1,2],[53,10,1,2],[54,1,2,13],[55,22,1,2],[56,22,1,2],[57,23,1,2],[58,10,1,2],[59,10,1,2],[60,23,1,2],[61,10,1,2],[62,24,1,2],[63,10,2,2],[64,10,1,2],[65,54,1,2],[66,10,1,2],[67,24,1,2],[68,63,1,2],[69,25,1,2],[70,3,1,2],[71,54,1,2],[72,15,1,2],[73,54,1,2],[74,10,1,2],[75,25,1,2],[76,26,1,2],[77,26,1,2],[78,2,3,5],[79,27,1,2],[80,27,3,2],[81,10,1,2],[82,10,1,2],[83,28,1,2],[84,10,1,2],[85,10,1,2],[86,28,1,2],[87,29,1,2],[88,29,1,2],[89,30,1,2],[90,12,1,2],[91,54,2,0],[92,30,1,0],[93,2,3,3],[94,31,1,0],[95,54,1,0],[96,10,1,0],[97,54,1,3],[98,31,1,0],[99,32,1,0],[100,10,1,0],[101,1,2,12],[102,101,2,39],[103,32,1,0],[104,101,1,0],[105,101,2,39],[106,10,1,0],[107,101,1,0],[108,10,1,0],[109,3,1,0],[110,33,1,0],[111,33,1,0],[112,34,1,0],[113,2,2,7],[114,34,1,0],[115,102,1,0],[116,113,2,2],[117,101,2,19],[118,101,2,0],[119,11,1,0],[120,101,3,2],[121,101,1,0],[122,120,2,0],[123,116,1,0],[124,113,2,5],[125,113,3,2],[126,113,2,0],[127,124,2,0],[128,125,2,0],[129,120,1,0],[130,125,2,2],[131,130,1,0],[132,124,3,6],[133,3,1,0],[134,113,2,2],[135,10,1,0],[136,134,1,0],[137,134,2,0],[138,10,1,11],[139,132,2,5],[140,105,1,0],[141,102,1,0],[142,124,2,0],[143,102,1,0],[144,116,2,2],[145,144,1,0],[146,132,2,0],[147,139,3,10],[148,54,1,0],[149,147,2,2],[150,149,1,0],[151,139,2,0],[152,124,2,0],[153,124,2,0],[154,102,1,0],[155,35,1,0],[156,113,2,0],[157,147,2,0],[158,2,1,0],[159,10,1,0],[160,102,1,0],[161,2,2,2],[162,101,2,0],[163,101,2,2],[164,144,2,0],[165,10,1,2],[166,102,1,0],[167,35,1,0],[168,101,2,0],[169,10,2,5],[170,102,1,0],[171,10,1,0],[172,165,1,0],[173,97,1,0],[174,10,2,0],[175,10,2,5],[176,117,1,0],[177,10,1,0],[178,117,1,0],[179,102,1,0],[180,2,1,0],[181,113,2,0],[182,117,1,0],[183,10,1,0],[184,117,2,17],[185,102,1,0],[186,10,1,0],[187,117,1,0],[188,36,1,0],[189,36,1,0],[190,40,1,0],[191,117,1,0],[192,139,2,0],[193,117,1,3],[194,102,1,0],[195,139,2,3],[196,130,2,0],[197,102,1,0],[198,10,2,2],[199,101,1,10],[200,10,1,0],[201,10,1,0],[202,37,1,0],[203,117,1,0],[204,117,1,0],[205,40,1,0],[206,117,1,0],[207,41,1,0],[208,117,1,0],[209,41,1,0],[210,3,1,0],[211,117,1,0],[212,10,1,0],[213,238,1,0],[214,42,1,0],[215,54,1,0],[216,117,1,0],[217,10,2,2],[218,62,1,0],[219,62,1,0],[220,117,1,0],[221,169,1,0],[222,1,1,0],[223,199,2,7],[224,97,1,0],[225,42,1,0],[226,184,1,0],[227,217,2,2],[228,102,1,0],[229,184,1,0],[230,195,1,0],[231,227,1,0],[232,43,1,0],[233,43,1,0],[234,102,1,0],[235,1,2,4],[236,102,1,0],[237,169,1,0],[238,184,2,7],[239,169,1,0],[240,78,1,0],[241,184,1,2],[242,21,1,0],[243,132,2,0],[244,139,2,0],[245,223,1,0],[246,132,2,0],[247,184,1,0],[248,2,1,0],[249,241,1,2],[250,10,1,0],[251,10,1,0],[252,117,2,0],[253,102,1,0],[254,117,2,0],[255,227,1,0],[256,199,1,0],[257,10,1,0],[258,241,1,0],[259,184,1,0],[260,10,1,0],[261,249,1,0],[262,44,1,0],[263,138,1,0],[264,44,1,0],[265,10,1,0],[266,45,1,0],[267,10,1,0],[268,184,1,0],[269,45,1,0],[270,184,1,0],[271,138,1,0],[272,10,1,0],[273,46,1,0],[274,21,1,0],[275,46,1,0],[276,223,1,0],[277,10,1,0],[278,47,1,0],[279,1,1,0],[280,10,3,0],[281,184,1,0],[282,184,3,0],[283,47,1,0],[284,48,1,0],[285,102,1,0],[286,48,1,0],[287,49,1,0],[288,49,1,0],[289,50,1,0],[290,199,2,6],[291,184,1,0],[292,10,1,0],[293,199,1,2],[294,138,1,0],[295,10,1,0],[296,10,1,0],[297,199,1,0],[298,21,1,0],[299,223,1,0],[300,184,1,0],[301,10,1,0],[302,50,1,0],[303,293,1,0],[304,102,1,0],[305,51,1,0],[306,138,1,0],[307,51,1,0],[308,52,1,0],[309,52,1,0],[310,223,3,2],[311,310,1,0],[312,10,2,3],[313,53,1,0],[314,21,1,0],[315,249,1,0],[316,184,1,0],[317,184,1,0],[318,21,1,0],[319,184,1,0],[320,53,1,0],[321,195,1,0],[322,55,1,0],[323,55,1,0],[324,238,1,0],[325,199,1,0],[326,117,1,0],[327,56,1,0],[328,10,3,3],[329,238,1,0],[330,138,1,0],[331,199,3,2],[332,3,1,0],[333,10,1,0],[334,10,1,0],[335,199,1,0],[336,93,1,0],[337,293,1,0],[338,138,1,0],[339,217,2,0],[340,56,1,0],[341,238,1,0],[342,2,1,0],[343,138,1,0],[344,10,1,0],[345,37,1,0],[346,138,1,0],[347,10,2,2],[348,57,1,0],[349,184,1,0],[350,10,1,0],[351,57,1,0],[352,58,1,0],[353,138,1,0],[354,328,2,0],[355,328,2,2],[356,147,1,2],[357,102,1,0],[358,102,1,0],[359,132,1,0],[360,238,1,0],[361,58,1,0],[362,102,1,0],[363,312,2,2],[364,105,1,0],[365,105,1,0],[366,105,1,0],[367,102,1,0],[368,39,1,0],[369,105,1,0],[370,355,1,0],[371,10,1,0],[372,312,4,2],[373,238,1,0],[374,356,1,0],[375,105,1,0],[376,10,1,0],[377,223,1,0],[378,102,1,0],[379,310,1,0],[380,105,1,0],[381,2,1,0],[382,105,1,0],[383,102,1,0],[384,10,3,3],[385,290,1,0],[386,105,1,0],[387,105,1,0],[388,372,1,0],[389,331,1,0],[390,59,1,0],[391,21,1,0],[392,21,1,0],[393,102,1,0],[394,102,2,0],[395,328,2,0],[396,117,1,0],[397,21,1,0],[398,290,1,2],[399,105,1,0],[400,59,1,0],[401,102,1,0],[402,105,1,0],[403,199,1,0],[404,105,1,0],[405,102,1,0],[406,138,1,0],[407,54,1,0],[408,2,1,0],[409,105,1,0],[410,97,1,0],[411,105,1,0],[412,14,1,0],[413,355,1,0],[414,10,1,0],[415,398,1,2],[416,356,1,0],[417,105,1,0],[418,14,1,0],[419,105,1,0],[420,10,1,0],[421,415,2,2],[422,2,1,0],[423,54,1,0],[424,2,2,2],[425,424,1,0],[426,102,1,0],[427,105,1,0],[428,10,1,0],[429,105,1,0],[430,421,1,0],[431,93,1,0],[432,149,1,0],[433,147,1,0],[434,105,1,0],[435,2,1,0],[436,10,1,2],[437,93,1,0],[438,2,1,0],[439,398,1,0],[440,102,1,0],[441,38,1,0],[442,235,1,0],[443,331,1,0],[444,421,1,0],[445,102,1,0],[446,105,2,7],[447,312,1,0],[448,10,1,0],[449,235,1,0],[450,415,1,0],[451,2,1,0],[452,105,1,0],[453,105,1,0],[454,2,2,0],[455,10,1,0],[456,105,1,0],[457,424,1,0],[458,138,1,0],[459,10,1,0],[460,10,1,0],[461,10,1,0],[462,436,1,0],[463,10,3,2],[464,102,1,0],[465,10,1,0],[466,223,1,0],[467,10,1,0],[468,363,2,0],[469,363,2,0],[470,105,1,0],[471,10,1,0],[472,347,1,0],[473,132,1,0],[474,105,1,0],[475,446,1,0],[476,38,1,0],[477,463,1,0],[478,60,1,0],[479,2,1,0],[480,147,1,0],[481,199,1,0],[482,54,1,0],[483,105,1,0],[484,238,1,0],[485,105,1,0],[486,10,1,0],[487,60,1,0],[488,290,1,0],[489,10,1,0],[490,38,1,0],[491,10,1,0],[492,290,1,0],[493,61,1,0],[494,10,1,0],[495,10,1,0],[496,10,2,0],[497,2,1,0],[498,10,1,0],[499,61,1,0],[500,64,1,0],[501,10,1,0],[502,1,1,0],[503,10,1,0],[504,147,1,0],[505,10,1,0],[506,147,1,0],[507,64,1,0],[508,10,1,0],[509,195,1,0],[510,102,1,0],[511,193,1,0],[512,446,1,0],[513,65,1,0],[514,65,1,0],[515,102,1,0],[516,105,1,0],[517,66,1,0],[518,66,1,0],[519,175,1,0],[520,10,1,0],[521,67,1,0],[522,67,1,0],[523,68,1,0],[524,2,1,0],[525,3,1,0],[526,3,1,0],[527,1,1,0],[528,223,1,0],[529,78,1,0],[530,68,1,0],[531,69,1,0],[532,69,1,0],[533,105,1,0],[534,102,1,0],[535,70,1,0],[536,70,1,0],[537,10,1,0],[538,290,1,0],[539,38,1,0],[540,10,1,0],[541,71,1,0],[542,10,1,0],[543,71,1,0],[544,72,1,0],[545,63,1,0],[546,105,1,0],[547,10,2,0],[548,290,1,0],[549,235,1,0],[550,72,1,0],[551,384,1,0],[552,436,1,0],[553,105,1,0],[554,384,1,0],[555,73,1,0],[556,73,1,0],[557,161,1,0],[558,161,1,0],[559,446,1,0],[560,6,1,0],[561,74,1,0],[562,105,1,0],[563,384,1,0],[564,74,1,0],[565,2,1,0],[566,7,1,0],[567,1,1,0],[568,7,1,0],[569,105,1,0],[570,75,1,0],[571,446,1,0],[572,105,1,0],[573,3,1,0],[574,75,1,0],[575,13,1,0],[576,175,1,0],[577,13,1,0],[578,163,1,0],[579,102,1,0],[580,117,1,0],[581,347,1,0],[582,10,1,0],[583,76,1,0],[584,105,1,0],[585,76,1,0],[586,169,1,0],[587,6,1,0],[588,463,1,0],[589,446,1,0],[590,77,1,0],[591,193,1,0],[592,10,1,0],[593,15,1,0],[594,77,1,0],[595,10,1,0],[596,175,1,0],[597,38,1,0],[598,175,1,0],[599,446,1,0],[600,79,1,0],[601,79,1,0],[602,3,1,0],[603,81,1,0],[604,198,1,0],[605,2,1,0],[606,81,1,0],[607,102,1,0],[608,2,1,0],[609,163,1,0],[610,198,1,0],[611,165,1,0],[612,102,1,0],[613,3,1,0],[614,82,1,0],[615,3,1,0],[616,38,2,0],[617,39,1,0],[618,14,1,0],[619,147,1,0],[620,15,1,0],[621,193,1,0],[622,175,1,0],[623,105,1,0],[624,82,1,0],[625,83,1,0],[626,83,1,0],[627,84,1,0],[628,102,1,0],[629,84,1,0],[630,85,1,0],[631,10,2,0],[632,85,1,0],[633,86,1,0],[634,10,1,0],[635,105,1,0],[636,54,1,0],[637,102,1,0],[638,86,1,0],[639,87,1,0],[640,105,1,0],[641,3,1,0],[642,372,1,0],[643,184,1,0],[644,87,1,0],[645,78,1,0],[646,2,1,0],[647,78,1,0],[648,54,1,0],[649,38,1,0],[650,7,1,0],[651,1,2,15],[652,1,1,0],[653,88,1,0],[654,39,1,0],[655,10,1,0],[656,1,1,0],[657,147,1,0],[658,78,1,0],[659,88,1,0],[660,89,1,0],[661,651,1,0],[662,651,2,3],[663,651,1,0],[664,651,1,0],[665,651,1,0],[666,651,1,0],[667,651,1,0],[668,651,2,0],[669,662,1,0],[670,89,1,0],[671,90,1,0],[672,80,1,0],[673,662,1,0],[674,90,1,0],[675,662,1,0],[676,169,1,0],[677,446,1,0],[678,651,1,0],[679,651,1,0],[680,80,1,0],[681,651,1,0],[682,1,3,1],[683,651,1,0],[684,651,1,0],[685,147,1,0],[686,651,1,0],[687,682,2,0],[688,235,1,0],[689,651,1,0]]}; // the InstantAdPay org: [id, recruiterId, levelBought, recruits], no names
let G = null; window.reachG = () => ({ G, PLAN }); // read-only handle for the check scripts
let rnd = mulberry32(7);
function mulberry32(a){ return function(){ a |= 0; a = a + 0x6D2B79F5 | 0; let t = Math.imul(a ^ a >>> 15, 1 | a); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; }
function gAdd(parent, recruiter, month){
const m = { id: G.nextId++, parent, kids: [], depth: parent ? parent.depth + 1 : 0, level: 1, directs: 0, recruiter, cash: 0, entry: 0, lic: 0, licV: 0, cashFrom: 0, cashFromHalf: 0, month };
G.members.push(m); if (parent) parent.kids.push(m);
return m;
}
function gSlot(root){
if (PLAN.place !== 'spread') { const q = [root]; while (q.length) { const n = q.shift(); if (n.kids.length < PLAN.w) return n; for (const k of n.kids) q.push(k); } return root; }
// spread: still fill the shallowest depth first, but within that depth give the position with the fewest
// children the next one, so each gets one before any gets two (plain breadth-first fills one position to
// four before the next gets any). A pure "fewest children anywhere" rule turns the org into a chain.
let level = [root];
while (level.length) {
const open = level.filter(n => n.kids.length < PLAN.w);
if (open.length) { let best = open[0]; for (const n of open) if (n.kids.length < best.kids.length) best = n; return best; }
const next = []; for (const n of level) for (const k of n.kids) next.push(k); level = next;
}
return root;
}
// an entry: placed breadth-first under the recruiter (or assigned to a member short of a direct), paid, split
function gJoin(recruiter, month, unsponsored){
if (!recruiter) { // assigned: earliest member with one recruit, else earliest with none (never you)
recruiter = G.members.find(x => x !== G.you && x.directs === 1) || G.members.find(x => x !== G.you && x.directs === 0) || G.you;
}
const parent = gSlot(recruiter);
const m = gAdd(parent, recruiter, month); m.unsponsored = !!unsponsored;
recruiter.directs++; if (recruiter.directs === 1) recruiter.cashFromHalf = m.id; if (recruiter.directs === 2) recruiter.cashFrom = m.id;
const gross = price(1), fee = gross * PLAN.fee, n = gross - fee;
G.aud.spent += gross; G.aud.entryFee += fee; G.aud.received += fee;
const toRec = n * (1 - PLAN.split), toUp = n * PLAN.split;
recruiter.entry += toRec; parent.entry += toUp; G.aud.received += n;
G.mon.in += gross; G.mon.admin += fee; G.mon.joins++; if (unsponsored) G.mon.assigned++;
return m;
}
function gCashOk(u, origin, L){
const own = !PLAN.wm || origin.recruiter === u;
if (u.directs >= 2) return own || origin.id >= u.cashFrom;
if (PLAN.half && u.directs === 1 && L <= 3) return own || origin.id >= u.cashFromHalf;
if (PLAN.zero && L <= 2) return true; // level-2 money from your own depth 1 needs no recruits
return false;
}
function gRoute(from, L, skipN, rem, origin){
let cur = from.parent; for (let i = 0; i < skipN && cur; i++) cur = cur.parent;
while (cur) {
if (cur.level >= L) { if (gCashOk(cur, origin, L)) return { to: cur, kind: 'cash' }; }
else if (!rem) { const behindOk = PLAN.gap === 'fund' || cur.level >= L - 1, licOk = cur.directs >= 2 || PLAN.unq === 'licence'; if (behindOk && licOk) return { to: cur, kind: 'lic' }; }
cur = cur.parent;
}
return { to: null };
}
function gPay(from, L, amt, skipN, origin, rem){
const r = gRoute(from, L, skipN, rem, origin);
if (!r.to) { G.aud.above += amt; G.aud.received += amt; G.mon.above += amt; return; }
if (r.kind === 'cash') { r.to.cash += amt; G.aud.received += amt; return; }
const n = r.to.level + 1, cost = net(n);
r.to.level = n; r.to.lic++; r.to.licV += cost; G.mon.lics++;
gPay(r.to, n, cost, n - 2, origin, false);
if (amt - cost > 0.005) gPay(r.to, L, amt - cost, 0, origin, true);
}
function gBuy(m, L){ m.bought = (m.bought || 0) + 1; const gross = price(L), fee = gross * PLAN.fee; G.aud.spent += gross; G.aud.admin += fee; G.aud.received += fee; G.mon.in += gross; G.mon.admin += fee; G.mon.buys++; m.level = Math.max(m.level, L); gPay(m, L, gross - fee, L - 2, m, false); }
function gNewMonth(){ G.mon = { in: 0, admin: 0, joins: 0, assigned: 0, buys: 0, lics: 0, above: 0 }; }
function gReset(){
rnd = mulberry32(7);
G = { month: 0, members: [], nextId: 1, you: null, rows: [], aud: { spent: 0, received: 0, admin: 0, entryFee: 0, above: 0 } };
gNewMonth();
G.you = gAdd(null, null, 0);
const seed = document.getElementById('gSeed').value;
if (seed === 'iap') {
const map = new Map([[SEED.apex, G.you]]);
const rows = SEED.m.filter(r => r[0] !== SEED.apex).sort((a, b) => a[0] - b[0]);
for (const r of rows) { const rec = map.get(r[1]); const m = gJoin(rec && rec !== G.you ? rec : (r[1] === SEED.apex ? G.you : null), 0, !rec); map.set(r[0], m); m.seedLevel = r[2]; }
for (let L = 2; L <= PLAN.maxL; L++) for (const m of G.members.slice().sort((a, b) => a.depth - b.depth || a.id - b.id)) if (m !== G.you && (m.seedLevel || 1) >= L && m.level < L) gBuy(m, L);
}
// Start over means start over (Marty, 28 Sep): the replay above only builds the org. Its money is reported once,
// in the story head, then every counter goes back to zero and members keep only the levels they actually bought.
G.replay = { in: G.aud.spent, admin: G.aud.admin + G.aud.entryFee, you: G.you.cash + G.you.entry, youLevel: G.you.level };
for (const m of G.members) { m.cash = 0; m.entry = 0; m.lic = 0; m.licV = 0; m.level = m === G.you ? 1 : (m.seedLevel || 1); }
G.aud = { spent: 0, received: 0, admin: 0, entryFee: 0, above: 0 }; gNewMonth();
G.rows.push(gSnap(0));
const ol = document.getElementById('gStory'); if (ol) ol.innerHTML = '
' + (seed === 'iap' ? 'Start: the InstantAdPay org today, placed 4-wide under each member\u2019s real recruiter: ' + cnt(G.members.length - 1) + ' members, ' + cnt(G.members.filter(x => x.level >= 2).length) + ' of them past level 1 (the levels they actually bought), ' + cnt(G.members.filter(x => x.directs >= 2).length) + ' qualified. Every counter starts at zero and counts only what happens from month 1. For reference, had these rules been running all along, their past purchases would have moved ' + $$(G.replay.in) + ', put ' + $$(G.replay.admin) + ' in the admin wallet and ' + $$(G.replay.you) + ' to you, funding you to level ' + G.replay.youLevel + '.' : 'Start: just you at level 1, nobody under you yet. In month 1 you recruit two people yourself (everyone needs two to receive cash at full reach); after that the org grows the way InstantAdPay’s did.') + ' Now run a month.
';
gRender();
window.growAudit = () => ({ spent: Math.round(G.aud.spent), received: Math.round(G.aud.received), ok: Math.abs(G.aud.spent - G.aud.received) <= 0.01 + 1e-9 * G.aud.spent });
}
function gSnap(month){
const ms = G.members.filter(x => x !== G.you);
return { month, joins: G.mon.joins, assigned: G.mon.assigned, buys: G.mon.buys, lics: G.mon.lics, members: ms.length, in: G.mon.in, admin: G.mon.admin, above: G.mon.above,
toMembers: ms.reduce((a, x) => a + x.cash + x.entry, 0), toYou: G.you.cash + G.you.entry, youLevel: G.you.level, youLic: G.you.lic,
paid: ms.filter(x => x.cash + x.entry > 0).length, l2: ms.filter(x => x.level >= 2).length, qualified: ms.filter(x => x.directs >= 2).length };
}
function gMonth(){
G.month++; gNewMonth();
const J0 = Number(document.getElementById('gJoins').value), g = Number(document.getElementById('gGrowth').value) / 100, pBuy = Number(document.getElementById('gBuy').value) / 100;
const joins = Math.round(J0 * Math.pow(1 + g, G.month - 1));
const U = 0.24; // share who arrive with no recruiter (InstantAdPay: 164 of 688)
for (let i = 0; i < joins; i++) {
let rec = null, unsp = false;
if (G.you.directs < 2 && G.month === 1 && i < 2) rec = G.you; // starting from just you: you bring your first two yourself, like everyone must
else if (rnd() < U) { unsp = true; }
else {
const pool = G.members.filter(x => x !== G.you);
if (rnd() < 0.85 && pool.some(x => x.directs > 0)) { // an existing recruiter, weighted by how many they already brought (top tenth bring half)
const rs = pool.filter(x => x.directs > 0); let tot = 0; for (const x of rs) tot += x.directs + 1; let t = rnd() * tot;
for (const x of rs) { t -= x.directs + 1; if (t <= 0) { rec = x; break; } } if (!rec) rec = rs[rs.length - 1];
} else if (pool.length) { rec = pool[Math.floor(rnd() * pool.length)]; } // a first-time recruiter
else rec = G.you;
}
gJoin(rec, G.month, unsp);
}
// upgrades: each member at level L buys L+1 with probability pBuy, shallow to deep; licences from below make some of these free
for (const m of G.members.slice().sort((a, b) => a.depth - b.depth || a.id - b.id)) { if (m === G.you || m.level >= PLAN.maxL) continue; if (rnd() < pBuy) gBuy(m, m.level + 1); }
const s = gSnap(G.month); G.rows.push(s);
const prev = G.rows[G.rows.length - 2];
const dYou = s.toYou - prev.toYou;
storyG('Month ' + G.month + '. ' + cnt(s.joins) + ' joined' + (s.assigned ? ', ' + cnt(s.assigned) + ' of them with no recruiter, assigned to members short of a direct' : '') + '. ' + cnt(s.buys) + ' bought a level with their own money and ' + cnt(s.lics) + ' licences were funded by pass-ups. Money in ' + $$(s.in) + ': admin wallet ' + $$(s.admin) + ', the rest to members' + (s.above ? ' (' + $$(s.above) + ' climbed past you to the company)' : '') + '. You: ' + (dYou >= 0 ? '+' : '') + $$(dYou) + '' + (s.youLevel > prev.youLevel ? ', and your team moved you to level ' + s.youLevel + '' : '') + '. ' + cnt(s.paid) + ' of ' + cnt(s.members) + ' members have been paid something; ' + cnt(s.l2) + ' are past level 1.', s.youLevel > prev.youLevel ? 'lic' : 'cash');
gRender();
}
function storyG(html, cls){ const ol = document.getElementById('gStory'); if (!ol) return; const li = document.createElement('li'); if (cls) li.className = cls; li.innerHTML = html; ol.prepend(li); while (ol.children.length > 40) ol.removeChild(ol.lastChild); }
function gRender(){
const s = G.rows[G.rows.length - 1];
document.getElementById('gMonth').textContent = G.month; document.getElementById('gMembers').textContent = cnt(s.members) + ' members';
document.getElementById('gLevel').textContent = s.youLevel; document.getElementById('gLevelS').textContent = 'reach = depth 1 to ' + s.youLevel + ' \u00b7 ' + s.youLic + ' licence' + (s.youLic === 1 ? '' : 's') + ' funded for you';
document.getElementById('gYou').textContent = $$(s.toYou); document.getElementById('gYouS').textContent = 'cash ' + $$(G.you.cash) + ' + entries ' + $$(G.you.entry) + '; licences worth ' + $$(G.you.licV) + ' apart';
document.getElementById('gAdmin').textContent = $$(G.aud.admin + G.aud.entryFee);
document.getElementById('gPaid').textContent = cnt(s.paid); document.getElementById('gPaidS').textContent = 'of ' + cnt(s.members) + ' (' + (s.members ? Math.round(100 * s.paid / s.members) : 0) + '%) \u00b7 ' + cnt(s.qualified) + ' qualified';
document.getElementById('gIn').textContent = $$(G.aud.spent);
const tb = document.querySelector('#gTable tbody'); if (tb) { tb.innerHTML = ''; for (const r of G.rows) { const tr = document.createElement('tr'); tr.innerHTML = '
' + r.month + '
' + cnt(r.joins) + '
' + cnt(r.members) + '
' + cnt(r.buys) + '
' + $$(r.in) + '
' + $$(r.admin) + '
' + $$(r.in - r.admin - r.above) + '
' + $$(r.toYou) + '
' + r.youLevel + '
' + cnt(r.paid) + '
' + cnt(r.l2) + '
'; tb.appendChild(tr); } }
const sp = document.getElementById('gSpread');
if (sp) {
const ms = G.members.filter(x => x !== G.you).map(x => x.cash + x.entry).sort((a, b) => b - a);
const pct = (p) => ms.length ? ms[Math.min(ms.length - 1, Math.floor(ms.length * p))] : 0;
const top10 = ms.slice(0, Math.max(1, Math.round(ms.length * 0.1))).reduce((a, b) => a + b, 0), all = ms.reduce((a, b) => a + b, 0);
const lv = {}; for (const x of G.members) if (x !== G.you) lv[x.level] = (lv[x.level] || 0) + 1;
const older = G.members.filter(x => x !== G.you && G.month - x.month >= 6), olderPaid = older.filter(x => x.cash + x.entry > 0);
const olderVals = older.map(x => x.cash + x.entry).sort((a, b) => b - a), olderMed = olderVals.length ? olderVals[Math.floor(olderVals.length / 2)] : 0;
const carried = G.members.filter(x => x !== G.you && x.level >= 2 && !(x.bought || 0)).length, ownBuy = G.members.filter(x => x !== G.you && (x.bought || 0) > 0).length;
sp.innerHTML = 'Top earner in the org: ' + $$(ms[0] || 0) + '. Top 10% of members took ' + (all ? Math.round(100 * top10 / all) : 0) + '% of what members received. Median member: ' + $$(pct(0.5)) + '. Members with nothing: ' + cnt(ms.filter(v => v <= 0).length) + ' (most of them joined recently).'
+ ' Members in for 6 months or more: ' + cnt(older.length) + ', of whom ' + cnt(olderPaid.length) + ' (' + (older.length ? Math.round(100 * olderPaid.length / older.length) : 0) + '%) have been paid; their median is ' + $$(olderMed) + '.'
+ ' Product without paying: ' + cnt(carried) + ' members hold level 2 or higher purely from licences their team funded; ' + cnt(ownBuy) + ' bought at least one level themselves.'
+ ' Levels held: ' + Object.keys(lv).map(Number).sort((a, b) => a - b).map(L => 'L' + L + ' ' + cnt(lv[L])).join(' \u00b7 ') + '.';
}
}
function gRun(k){ for (let i = 0; i < k; i++) gMonth(); }
document.getElementById('gRun1').addEventListener('click', () => gRun(1));
document.getElementById('gRun6').addEventListener('click', () => gRun(6));
document.getElementById('gRun12').addEventListener('click', () => gRun(12));
document.getElementById('gReset').addEventListener('click', gReset);
document.getElementById('gSeed').addEventListener('change', gReset);
document.getElementById('gJoins').addEventListener('input', e => { document.getElementById('gJoinsVal').textContent = e.target.value; });
document.getElementById('gGrowth').addEventListener('input', e => { document.getElementById('gGrowthVal').textContent = (Number(e.target.value) >= 0 ? '+' : '') + e.target.value + '%'; });
document.getElementById('gBuy').addEventListener('input', e => { document.getElementById('gBuyVal').textContent = e.target.value + '%'; });
// ── plan controls ──
function applyPlan(){
PLAN.w = Number(document.getElementById('width').value);
PLAN.maxL = Number(document.getElementById('levels').value);
PLAN.shape = document.getElementById('shape').value;
PLAN.steps = document.getElementById('steps').value.split(',').map(s => Number(s.trim())).filter(n => n > 0);
PLAN.products = document.getElementById('products').value.split(',').map(s => s.trim()).filter(Boolean);
PLAN.fee = Number(document.getElementById('fee').value) || 0;
if (!PLAN.steps.length) PLAN.steps = [10];
document.getElementById('stepsCtl').hidden = PLAN.shape !== 'stepped';
document.getElementById('priceCtl').hidden = PLAN.shape === 'stepped';
document.getElementById('gapSkip').setAttribute('aria-pressed', String(PLAN.gap === 'skip'));
document.getElementById('gapFund').setAttribute('aria-pressed', String(PLAN.gap === 'fund'));
document.getElementById('unqSkip').setAttribute('aria-pressed', String(PLAN.unq === 'skip'));
document.getElementById('unqLic').setAttribute('aria-pressed', String(PLAN.unq === 'licence'));
document.getElementById('plShallow').setAttribute('aria-pressed', String(PLAN.place === 'shallow'));
document.getElementById('plSpread').setAttribute('aria-pressed', String(PLAN.place === 'spread'));
document.getElementById('zeroOff').setAttribute('aria-pressed', String(!PLAN.zero));
document.getElementById('zeroOn').setAttribute('aria-pressed', String(PLAN.zero));
document.getElementById('halfOff').setAttribute('aria-pressed', String(!PLAN.half));
document.getElementById('halfOn').setAttribute('aria-pressed', String(PLAN.half));
document.getElementById('wmOff').setAttribute('aria-pressed', String(!PLAN.wm));
document.getElementById('wmOn').setAttribute('aria-pressed', String(PLAN.wm));
const yl = document.getElementById('youLevel'); yl.max = PLAN.maxL; yl.value = 1; // a new plan starts you at level 1, like everyone
document.getElementById('youLevelVal').textContent = yl.value;
{ const dh = document.getElementById('deepHead'); if (dh) dh.textContent = 'Watch one position climb all ' + PLAN.maxL + ' levels'; }
svg.setAttribute('aria-label', PLAN.w + '-wide matrix under you');
build(); deepReset(); gReset();
}
for (const id of ['width', 'levels', 'shape']) document.getElementById(id).addEventListener('change', applyPlan);
document.getElementById('steps').addEventListener('change', applyPlan);
document.getElementById('products').addEventListener('change', applyPlan);
document.getElementById('fee').addEventListener('change', applyPlan);
document.getElementById('gapSkip').addEventListener('click', () => { PLAN.gap = 'skip'; applyPlan(); });
document.getElementById('gapFund').addEventListener('click', () => { PLAN.gap = 'fund'; applyPlan(); });
document.getElementById('unqSkip').addEventListener('click', () => { PLAN.unq = 'skip'; applyPlan(); });
document.getElementById('unqLic').addEventListener('click', () => { PLAN.unq = 'licence'; applyPlan(); });
document.getElementById('plShallow').addEventListener('click', () => { PLAN.place = 'shallow'; applyPlan(); });
document.getElementById('plSpread').addEventListener('click', () => { PLAN.place = 'spread'; applyPlan(); });
document.getElementById('zeroOff').addEventListener('click', () => { PLAN.zero = false; applyPlan(); });
document.getElementById('zeroOn').addEventListener('click', () => { PLAN.zero = true; applyPlan(); });
document.getElementById('halfOff').addEventListener('click', () => { PLAN.half = false; applyPlan(); });
document.getElementById('halfOn').addEventListener('click', () => { PLAN.half = true; applyPlan(); });
document.getElementById('wmOff').addEventListener('click', () => { PLAN.wm = false; applyPlan(); });
document.getElementById('wmOn').addEventListener('click', () => { PLAN.wm = true; applyPlan(); });
applyPlan();
})();