From e6b5a9fb226bb472b849fdb5b849c8b9fef18359 Mon Sep 17 00:00:00 2001 From: martbost Date: Mon, 28 Sep 2026 10:01:47 -0500 Subject: [PATCH] plan-lab: long runs paint every month with progress and can be interrupted; O(recruiters) placement and picks Co-Authored-By: Claude Fable 5.1 --- public/assets/plan-lab.js | 50 +++++++++++++++++++++++++++++---------- public/plan-lab.html | 2 +- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/public/assets/plan-lab.js b/public/assets/plan-lab.js index 94c3c3b..b4fc5e8 100644 --- a/public/assets/plan-lab.js +++ b/public/assets/plan-lab.js @@ -578,6 +578,7 @@ 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, m0: 0, mDirects: 0, sp: 0, pool: 0, spent: 0, dc: 0 }; G.members.push(m); if (parent) parent.kids.push(m); + for (let a = parent; a; a = a.parent) { const c = a.slotCache; if (c && c.depth === m.depth - a.depth) c.nodes.push(m); } // keep every ancestor's frontier complete return m; } function gSlot(root){ @@ -585,13 +586,16 @@ // 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; + // Each recruiter keeps the list of positions at their current frontier depth (full ones included, skipped at + // pick time). Walking the whole organisation on every join was O(members) per join and froze phones on long runs. + let c = root.slotCache; if (!c) c = root.slotCache = { depth: 0, nodes: [root] }; + for (;;) { + let best = null; for (const n of c.nodes) if (n.kids.length < PLAN.w && (!best || n.kids.length < best.kids.length)) best = n; + if (best) return best; + const next = []; for (const n of c.nodes) for (const k of n.kids) next.push(k); + if (!next.length) return root; + c.depth++; c.nodes = 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){ @@ -600,7 +604,9 @@ } const parent = gSlot(recruiter); const m = gAdd(parent, recruiter, month); m.unsponsored = !!unsponsored; - recruiter.directs++; recruiter.mDirects++; if (recruiter.directs === 1) recruiter.cashFromHalf = m.id; if (recruiter.directs === 2) recruiter.cashFrom = m.id; + recruiter.directs++; recruiter.mDirects++; + if (recruiter !== G.you) { if (recruiter.directs === 1) { let lo = 0, hi = G.rs.length; while (lo < hi) { const mid = (lo + hi) >> 1; if (G.rs[mid].id < recruiter.id) lo = mid + 1; else hi = mid; } G.rs.splice(lo, 0, recruiter); G.rsW += 2; } else G.rsW += 1; } + 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, poolAmt = gross * PLAN.pool, company = fee - poolAmt; m.spent += gross; G.aud.spent += gross; G.aud.entryFee += company; G.aud.pool += poolAmt; G.aud.received += fee; G.mon.pool += poolAmt; const toRec = n * (1 - PLAN.split), toUp = n * PLAN.split; @@ -682,8 +688,9 @@ G.mon.poolN = sh.size; G.mon.poolShares = total; G.mon.poolNew = newN; G.mon.poolNewAvg = newN ? (amt / total) * (newSh / newN) : 0; } function gReset(){ + gRunStop(); rnd = mulberry32(7); - G = { month: 0, members: [], nextId: 1, you: null, rows: [], poolCarry: 0, aud: { spent: 0, received: 0, admin: 0, entryFee: 0, above: 0, pool: 0 } }; + G = { month: 0, members: [], nextId: 1, you: null, rows: [], poolCarry: 0, rs: [], rsW: 0, aud: { spent: 0, received: 0, admin: 0, entryFee: 0, above: 0, pool: 0 } }; gNewMonth(); G.you = gAdd(null, null, 0); const seed = document.getElementById('gSeed').value; @@ -725,11 +732,13 @@ 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; + // G.rs is the list of members who have recruited (id order) and G.rsW their total weight, kept up to date in + // gJoin, so a pick is O(recruiters) instead of filtering every member twice per join (what froze phones). + const poolLen = G.members.length - 1; // everyone but you; you are members[0] + if (rnd() < 0.85 && G.rs.length) { // an existing recruiter, weighted by how many they already brought (top tenth bring half) + const rs = G.rs; let t = rnd() * G.rsW; 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 if (poolLen) { rec = G.members[1 + Math.floor(rnd() * poolLen)]; } // a first-time recruiter else rec = G.you; } gJoin(rec, G.month, unsp); @@ -789,7 +798,22 @@ + '
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(); } + // Long runs used to block the page for the whole loop (10 s+ on a phone, which reads as frozen). Now one month + // per tick: the story and tiles update as it goes, the buttons show progress, and Start over interrupts. + let gTimer = null; const RUN_LABELS = {}; + function gRunButtons(){ return ['gRun1', 'gRun6', 'gRun12'].map(id => document.getElementById(id)).filter(Boolean); } + function gRunStop(){ if (gTimer) { clearTimeout(gTimer); gTimer = null; } for (const b of gRunButtons()) { b.disabled = false; if (RUN_LABELS[b.id]) b.textContent = RUN_LABELS[b.id]; } } + function gRun(k){ + if (gTimer) return; + const btns = gRunButtons(); for (const b of btns) RUN_LABELS[b.id] = RUN_LABELS[b.id] || b.textContent; + let i = 0; + const step = () => { + gMonth(); i++; + if (i < k) { for (const b of btns) b.disabled = true; const b12 = document.getElementById('gRun12'); if (b12) b12.textContent = 'Running month ' + (i + 1) + ' of ' + k + '\u2026'; gTimer = setTimeout(step, 16); } + else gRunStop(); + }; + step(); + } document.getElementById('gRun1').addEventListener('click', () => gRun(1)); document.getElementById('gRun6').addEventListener('click', () => gRun(6)); document.getElementById('gRun12').addEventListener('click', () => gRun(12)); diff --git a/public/plan-lab.html b/public/plan-lab.html index 77494ae..164d938 100644 --- a/public/plan-lab.html +++ b/public/plan-lab.html @@ -366,6 +366,6 @@ details.adv.big summary{font-size:18px}

Unlisted working page for planning. Not a member page, not an offer. Every number here is a mechanic, not a prediction.

- +