// The holding tank behind The Draft. ONE interface, two implementations: // // candidates(now) -> [{ id, username, joinedAt, lastSeen, door, wallet, locked, sponsored, spam, throwaway, adoptions, // games: { plays, title, earnedWei } | null, surfWei | null }] people waiting for a sponsor // status(ids, now) -> { id: { lastSeen, username, wallet, payouts, purchase, games, surfWei, adoptions } | null } // milestones are timestamps (0 = not yet); null = the network cannot say right now // adopt({ coach, person, kind, note }) -> { ok, ref } | { error } the pick becomes this coach's adoption // release({ coach, person, why }) -> { ok } | { error } back to the tank (return window ended, or released) // // Contact data never crosses this interface: no emails, no phone numbers. Coaches get the RIGHT TO SPONSOR AND HELP; the only // mail a pick gets is the tank's normal "X is now your sponsor" adoption email, which the tank (InstantAdPay / LinkSpin) sends. // // 'stub' (phase 1, TANK_MODE unset): a local fixture (STUB_POOL_FILE, else qa/fixtures/pool.json) with relative times, adoption // state in the document stub-tank.json. It adopts nobody for real and sends no mail. Admin test routes can move a person's // milestones (POST /api/admin/stub/status) so the coaching tools, cards and boards can be exercised. // 'hub' (phase 2): NOT WIRED. Every call answers { error } / [] so nothing is drafted. What it needs (open for Marty + the hub): // - a pool read: the network tank's waiting list (InstantAdPay tank.waiting() is the source today, the hub has no tank route) // with rule 3 applied (hub /v1/locked detail), username, door (hub joinedVia), last seen, wallet linked // - scouting stats where they exist: game plays + POL earned (game wallet watchers), surf earnings (IAP ads.earnedTotals) // - adopt: the tank's own adopt (so the 2-adoption count, the hold, the adoption email and the Telegram line stay in one place), // e.g. IAP POST /api/admin/tank/adopt with the coach as adopter, then hub /v1/sponsor-change { by: 'adopt' } // - status: payouts on / first purchase per person (IAP memberId + chain Purchase), hub /v1/activity/person for last seen 'use strict'; const fs = require('fs'); const path = require('path'); const H = 3600000, D = 86400000; let C = null, mode = 'stub', fixture = null, nowFn = () => Date.now(); const FILE = () => path.join(C.dataDir, 'stub-tank.json'); function st() { let s = {}; try { s = JSON.parse(fs.readFileSync(FILE(), 'utf8')); } catch (e) {} s.adopted = s.adopted || {}; s.over = s.over || {}; s.log = s.log || []; s.mails = s.mails || 0; return s; } function put(s) { s.log = s.log.slice(-300); fs.writeFileSync(FILE() + '.tmp', JSON.stringify(s)); fs.renameSync(FILE() + '.tmp', FILE()); } function init(cfg, opts) { C = cfg; mode = cfg.tankMode; if (opts && opts.now) nowFn = opts.now; if (mode === 'stub') { const f = cfg.stubPool || path.join(__dirname, 'qa', 'fixtures', 'pool.json'); try { fixture = JSON.parse(fs.readFileSync(f, 'utf8')); } catch (e) { fixture = { people: [] }; console.error('tankpool: no stub pool at', f); } fixture.base = nowFn(); // relative times are measured from boot console.log('tankpool: STUB mode (' + fixture.people.length + ' fixture people, nobody is adopted for real, no mail)'); } else console.log('tankpool: hub mode is not wired yet (phase 2): the pool is empty and no pick can be made'); } const isStub = () => mode === 'stub'; const ago = (h, base) => (h == null ? 0 : base - h * H); // a fixture person at time `now` (relative hours), with any admin override applied function person(p, s, now) { const o = s.over[p.id] || {}; const base = fixture.base; const ts = k => (o[k] !== undefined ? o[k] : p[k + 'HoursAgo'] != null ? ago(p[k + 'HoursAgo'], base) : 0); return { id: p.id, username: p.username || null, door: p.door || null, joinedAt: ago(p.joinedHoursAgo, base), lastSeen: ts('seen'), wallet: ts('wallet'), payouts: ts('payouts'), purchase: ts('purchase'), locked: !!p.locked, sponsored: !!p.sponsored || !!(s.adopted[p.id] && s.adopted[p.id].sticky), spam: !!p.spam, throwaway: !!p.throwaway, adoptions: Number(p.adoptionsElsewhere) || 0, games: p.games ? { plays: Number(p.games.plays) || 0, title: p.games.title || null, earnedWei: p.games.earnedWei || null } : null, surfWei: p.surfWei || null }; } async function candidates(now) { if (!isStub()) return []; const s = st(); now = now || nowFn(); return fixture.people.filter(p => !s.adopted[p.id]).map(p => { const x = person(p, s, now); return Object.assign(x, { wallet: !!x.wallet }); }) .filter(x => !x.payouts && !x.purchase); // payouts on = out of the tank (the IAP rule) } async function status(ids, now) { const out = {}; if (!isStub()) { for (const id of ids) out[id] = null; return out; } const s = st(); now = now || nowFn(); for (const id of ids) { const p = fixture.people.find(x => x.id === id); if (!p) { out[id] = null; continue; } const x = person(p, s, now); out[id] = { lastSeen: x.lastSeen, username: x.username, wallet: x.wallet, payouts: x.payouts, purchase: x.purchase, games: x.games, surfWei: x.surfWei, adoptions: x.adoptions, door: x.door }; } return out; } async function adopt(o) { if (!isStub()) return { error: 'The holding tank is not connected yet.' }; const s = st(); const p = fixture.people.find(x => x.id === o.person); if (!p) return { error: 'Not in the holding tank.' }; if (p.locked) return { error: 'Locked on a network contract.' }; if (p.sponsored) return { error: 'Already has a sponsor.' }; if (s.adopted[p.id]) return { error: 'Already adopted.' }; s.adopted[p.id] = { coach: o.coach, at: nowFn(), kind: o.kind || 'draft' }; s.mails++; // phase 2: the tank sends the normal adoption email here s.log.push({ at: nowFn(), adopt: p.id, coach: o.coach, kind: o.kind }); put(s); return { ok: true, ref: 'stub:' + p.id }; } async function release(o) { if (!isStub()) return { error: 'The holding tank is not connected yet.' }; const s = st(); if (!s.adopted[o.person]) return { ok: true }; delete s.adopted[o.person]; s.log.push({ at: nowFn(), release: o.person, why: o.why || '' }); put(s); return { ok: true }; } // admin test hook (stub only): move a person's milestones, e.g. { id, wallet: true, payouts: true, seen: true } function setStatus(id, patch) { if (!isStub()) return { error: 'stub only' }; const s = st(); const o = s.over[id] || (s.over[id] = {}); const t = nowFn(); for (const k of ['wallet', 'payouts', 'purchase', 'seen']) if (patch[k] !== undefined) o[k] = patch[k] ? (typeof patch[k] === 'number' ? patch[k] : t) : 0; put(s); return { ok: true, over: o }; } function view() { if (!C) return {}; const s = isStub() ? st() : null; return { mode, fixture: fixture ? fixture.people.length : 0, adopted: s ? Object.keys(s.adopted).length : 0, adoptionMailsThatWouldSend: s ? s.mails : 0, log: s ? s.log.slice(-20) : [] }; } module.exports = { init, candidates, status, adopt, release, setStatus, view, isStub, H, D };