f26ac15712
$10 one-time DraftPosition purchases (sponsor 25%, L2-5 5%, 10% Best Coach pot, 45% company) with keeper-signed hub-line vouchers; pure draft engine with a mocked clock; no contact data anywhere; Telegram to an outbox unless TG_LIVE=1. Unit 60/60, smoke 156/156 (Playwright 1280/390). Not deployed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
4.9 KiB
JavaScript
77 lines
4.9 KiB
JavaScript
// WILD HEADS: the CryptoTeamBuild downline builder programs (outside programs we promote but do not run). Pure functions, no I/O,
|
|
// so the rules are unit-tested on their own (qa/unit.test.cjs).
|
|
//
|
|
// A member saves their referral ID for a program. Whoever looks at a program sees ONE link: their sponsor's.
|
|
// chain = the people whose link should come first, nearest first: [{ id: hub id, username, depth }]
|
|
// a member page /<u> or a ?ref= visitor: [u (depth 0), u's sponsor (1), ...]; a signed-in member: [sponsor (1), ...]
|
|
// walk = the first person in the chain who saved an ID for that program
|
|
// then = the site owner's saved ID (Marty, martbost), then the program's default ID (admin), then the program's plain home page
|
|
// "[N] in the builder have joined" counts real saved IDs only (never the CTB builder's own counts, never the owner's default).
|
|
'use strict';
|
|
|
|
const PH = /\{affil[a-z]*id\}/gi; // the builder's {affiliateid} (one entry has a misspelling; both are filled)
|
|
const ID_PATH = /^[A-Za-z0-9_.@+-]{1,64}$/; // an ID that goes into a path or query
|
|
const ID_SUB = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/; // an ID that becomes a subdomain (Tangled: https://{affiliateid}.tangled.com)
|
|
|
|
const PH1 = /\{affil[a-z]*id\}/i;
|
|
const kindOf = url => { const m = /^https?:\/\/([^/?#]+)/i.exec(String(url || '')); return m && PH1.test(m[1]) ? 'subdomain' : 'path'; };
|
|
// -> the cleaned ID, '' for "remove my ID", or null when it cannot be an ID for this program
|
|
function cleanId(program, raw) {
|
|
let s = String(raw == null ? '' : raw).trim();
|
|
if (!s) return '';
|
|
// people paste their whole referral link: take the part where the builder puts the ID
|
|
const m = /^https?:\/\//i.test(s) || /^(www\.)?[a-z0-9-]+(\.[a-z0-9-]+)+[/?]/i.test(s) ? idFromLink(program, s) : null; if (m) s = m;
|
|
if ((program.idKind || kindOf(program.url)) === 'subdomain') { s = s.toLowerCase(); return ID_SUB.test(s) ? s : null; }
|
|
return ID_PATH.test(s) ? s : null;
|
|
}
|
|
// a pasted link that matches the program's template -> the ID in it
|
|
function idFromLink(program, link) {
|
|
const parts = String(program.url || '').split(PH1); if (parts.length !== 2) return null;
|
|
const bare = x => String(x).trim().replace(/^https?:\/\//i, '').replace(/^www\./i, '');
|
|
const pre = bare(parts[0]).toLowerCase(), l = bare(link);
|
|
if (!l.toLowerCase().startsWith(pre)) return null;
|
|
const stop = '/?#&' + (parts[1] ? parts[1][0] : '');
|
|
let id = ''; for (const ch of l.slice(pre.length)) { if (stop.includes(ch)) break; id += ch; }
|
|
if (!id) return null;
|
|
try { return decodeURIComponent(id); } catch (e) { return id; }
|
|
}
|
|
function fillUrl(program, id) {
|
|
const v = (program.idKind || kindOf(program.url)) === 'subdomain' ? String(id).toLowerCase() : encodeURIComponent(String(id));
|
|
return String(program.url).replace(PH, v);
|
|
}
|
|
// the program without anyone's ID: its plain home page (no broken {affiliateid} link is ever shown)
|
|
function homeUrl(program) {
|
|
const u = String(program.url || '');
|
|
try { const x = new URL(u.replace(/\{affil[a-z]*id\}\./gi, '').replace(PH, 'x')); return x.origin + '/'; } catch (e) { return ''; }
|
|
}
|
|
|
|
// idOf(person, programId) -> saved ID or ''. owner = { id?, username }. -> { url, by, depth, fallback }
|
|
// fallback: null (someone in the chain), 'owner', 'default', 'none' (the plain home page, nobody's ID)
|
|
function sponsorLink(program, chain, idOf, owner) {
|
|
for (const p of chain || []) {
|
|
const id = idOf(p, program.id);
|
|
if (id) return { url: fillUrl(program, id), by: p.username || null, depth: p.depth, fallback: null };
|
|
}
|
|
const oid = owner ? idOf(owner, program.id) : '';
|
|
if (oid) return { url: fillUrl(program, oid), by: owner.username, depth: null, fallback: 'owner' };
|
|
if (program.defaultId) return { url: fillUrl(program, program.defaultId), by: owner ? owner.username : null, depth: null, fallback: 'default' };
|
|
return { url: homeUrl(program), by: null, depth: null, fallback: 'none' };
|
|
}
|
|
|
|
// the visible wild heads, in builder order: never hidden ones, never network sites
|
|
const visible = programs => programs.filter(p => !p.hidden && !p.network).sort((a, b) => (a.order || 0) - (b.order || 0) || String(a.id).localeCompare(String(b.id)));
|
|
|
|
// "My team joined": members whose sponsor line (stored when they last signed in) includes me, who saved an ID for the program.
|
|
// lines: [{ key, line: [{ id, username }] , ids: Set(programId) }]
|
|
function inLine(line, me) { return (line || []).findIndex(x => (me.id && x.id === me.id) || (me.username && x.username && x.username.toLowerCase() === me.username.toLowerCase())); }
|
|
function teamCounts(me, members, programIds) {
|
|
const out = Object.fromEntries(programIds.map(id => [id, 0]));
|
|
for (const m of members) {
|
|
if (m.key === me.key || inLine(m.line, me) < 0) continue;
|
|
for (const pid of programIds) if (m.ids && m.ids.has(pid)) out[pid]++;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
module.exports = { cleanId, idFromLink, fillUrl, homeUrl, sponsorLink, visible, teamCounts, inLine, kindOf, ID_PATH, ID_SUB };
|