Drip Runner trial: IAP hand-off, seeded runs with server replay check, self-funding POL pool (refuel ad share + hangar spends), chapter drips, threshold payouts via faucet (dry run), tester allowlist + briefing page, admin page, e2e test

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-30 13:21:39 -05:00
commit 903b5065b9
19 changed files with 1601 additions and 0 deletions
+134
View File
@@ -0,0 +1,134 @@
// Runs, players and the server-side replay check.
//
// The client never says "pay me". It reports what happened (drip #i collected at run time t,
// chapter goal reached) and the server checks each claim against the seeded schedule it
// handed out at run start: a drip cannot be collected before it spawned, nor far from the
// moment it would have crossed the courier, nor twice. A run with too many refused claims is
// flagged and its chapter clears pay nothing. Bots, crashes and fuel are the client's business:
// a modified client can only avoid dying, it cannot invent a drip.
'use strict';
const crypto = require('crypto');
const store = require('./store');
const spawn = require('./spawn');
const ledger = require('./ledger');
const EARLY = 1.2, LATE = 0.9; // seconds around the computed arrival (magnet pulls ~150 px early)
const KEEP_RUNS_MS = 2 * 86400000;
const sched = new Map(); // runId -> { next, groups[] } regenerated lazily after a restart
function player(memberId) { return store.read('players', {})[String(memberId)] || null; }
function updPlayer(memberId, fn) {
return store.update('players', {}, all => {
const k = String(memberId);
const p = all[k] || (all[k] = { memberId: Number(memberId), bank: 0, chapter: 1, cleared: {}, items: {}, best: 0, runs: 0, since: Date.now() });
fn(p); return all;
})[String(memberId)];
}
function view(me) {
const p = player(me.memberId) || updPlayer(me.memberId, () => {});
const s = ledger.settings();
return { bank: p.bank, chapter: p.chapter, cleared: p.cleared, items: p.items, best: p.runs ? p.best : 0, runs: p.runs,
goal: spawn.goalFor(p.chapter), prices: { pol: s.itemPol, drips: s.itemDrips }, dripPol: Number(s.dripPol) };
}
function runs() { return store.read('runs', {}); }
function run(id) { return runs()[id] || null; }
function updRun(id, fn) { let out = null; store.update('runs', {}, all => { if (all[id]) { fn(all[id]); out = all[id]; } return all; }); return out; }
function prune() {
const cut = Date.now() - KEEP_RUNS_MS;
store.update('runs', {}, all => { for (const k of Object.keys(all)) if (all[k].status !== 'live' && all[k].startedAt < cut) { delete all[k]; sched.delete(k); } return all; });
}
function start(me, W, H) {
W = Math.max(300, Math.min(1200, Math.round(Number(W) || 390))); H = Math.max(500, Math.min(1400, Math.round(Number(H) || 760)));
const p = updPlayer(me.memberId, x => { x.runs++; });
// any live run this member left open is closed; a player has one run at a time
store.update('runs', {}, all => { for (const k of Object.keys(all)) if (all[k].memberId === Number(me.memberId) && all[k].status === 'live') all[k].status = 'abandoned'; return all; });
const armed = { magnet: !!p.items.magnet, shield: !!p.items.shield, slow: !!p.items.slow };
updPlayer(me.memberId, x => { delete x.items.magnet; delete x.items.shield; delete x.items.slow; }); // one-run items are consumed now
const r = { id: crypto.randomBytes(8).toString('hex'), memberId: Number(me.memberId), seed: crypto.randomInt(1, 2 ** 31), W, H,
speed: spawn.speedFor(p.chapter), chapter: p.chapter, goal: spawn.goalFor(p.chapter), startedAt: Date.now(), t: 0, drips: 0, chapDrips: 0,
collected: [], bad: 0, refuels: 0, status: 'live', armed, ship: p.items.ship ? 2 : 1, flagged: false, cleared: [] };
store.update('runs', {}, all => { all[r.id] = r; return all; });
if (Math.random() < 0.05) prune();
return r;
}
function groupsUpTo(r, t) {
let s = sched.get(r.id);
if (!s) { s = { next: spawn.drips(r.seed, r.W, r.H), groups: [] }; sched.set(r.id, s); }
while (!s.groups.length || s.groups[s.groups.length - 1].t <= t + 5) s.groups.push(s.next());
return s.groups;
}
function dripInfo(r, i, t) {
const groups = groupsUpTo(r, t);
for (const g of groups) for (const d of g.drips) if (d.i === i) return { s: g.t, x0: d.x0 };
return null;
}
// the client's batch of claims for a live run
function events(me, runId, t, list, wantGoal) {
const r = run(runId);
if (!r || r.memberId !== Number(me.memberId)) return { error: 'That run is not yours or no longer exists.' };
if (r.status !== 'live') return { error: 'That run is over.', over: true };
t = Math.max(0, Number(t) || 0);
const wall = (Date.now() - r.startedAt) / 1000 + 3; // run time cannot outrun the clock
const seen = new Set(r.collected); let ok = 0, bad = 0, badWhy = [];
for (const e of (Array.isArray(list) ? list : []).slice(0, 400)) {
if (!e || e.k !== 'd') continue;
const i = Number(e.i), et = Number(e.t);
if (!Number.isInteger(i) || i < 0 || !(et >= 0) || et > wall) { bad++; badWhy.push('shape'); continue; }
if (seen.has(i)) { bad++; badWhy.push('dup'); continue; }
const d = dripInfo(r, i, Math.max(et, t)); if (!d || d.s > et + 0.05) { bad++; badWhy.push('early'); continue; }
const arr = spawn.arrival(d.s, d.x0, r.W, r.speed);
if (et < arr - EARLY || et > arr + LATE) { bad++; badWhy.push('window'); continue; }
seen.add(i); ok++;
}
const out = updRun(runId, x => {
x.collected = [...seen]; x.drips += ok; x.chapDrips += ok; x.bad += bad; x.t = Math.max(x.t, t);
if (x.bad > 5 && x.bad > (x.drips + x.bad) * 0.1) x.flagged = true;
});
if (ok) updPlayer(me.memberId, p => { p.bank += ok; if (out.drips > p.best) p.best = out.drips; });
const res = { ok, refused: bad, drips: out.drips, chapDrips: out.chapDrips, goal: out.goal, flagged: out.flagged };
if (bad) res.why = badWhy.slice(0, 5);
if (wantGoal) {
if (out.chapDrips < out.goal) { res.cleared = null; res.short = out.goal - out.chapDrips; return res; }
const ch = out.chapter; const p = player(me.memberId);
const first = !(p.cleared || {})[ch];
const grant = ledger.chapterDrip(me, ch, first && !out.flagged);
if (first && out.flagged) grant.reason = 'flagged';
updPlayer(me.memberId, x => { x.cleared[ch] = true; if (x.chapter <= ch) x.chapter = ch + 1; });
updRun(runId, x => { x.cleared.push(ch); x.chapter = ch + 1; x.chapDrips = 0; x.goal = spawn.goalFor(ch + 1); });
res.cleared = { chapter: ch, first, pol: grant.pol, reason: grant.reason, next: ch + 1, nextGoal: spawn.goalFor(ch + 1), playTime: Math.round(out.t) };
}
return res;
}
function end(me, runId) {
const r = run(runId); if (!r || r.memberId !== Number(me.memberId)) return { error: 'Not your run.' };
updRun(runId, x => { if (x.status === 'live') x.status = 'ended'; x.endedAt = Date.now(); });
sched.delete(runId);
return { ok: true };
}
function refuelled(me, runId) { return updRun(runId, x => { x.refuels++; }); }
// hangar
function buy(me, item) {
const s = ledger.settings(); const p = player(me.memberId) || updPlayer(me.memberId, () => {});
if (s.itemDrips[item] != null) {
const cost = Number(s.itemDrips[item]);
if (item === 'ship' && p.items.ship) return { error: 'You already own the Courier Mk II.' };
if (p.items[item] && item !== 'ship') return { error: 'That one is already armed for your next run.' };
if (p.bank < cost) return { error: 'Not enough drips. You have ' + p.bank + ', it costs ' + cost + '.' };
updPlayer(me.memberId, x => { x.bank -= cost; x.items[item] = true; });
return { ok: true, paid: cost + ' drips' };
}
if (s.itemPol[item] != null) {
if (p.items[item]) return { error: 'That one is already armed for your next run.' };
const r = ledger.spend(me, item, Number(s.itemPol[item])); if (r.error) return r;
updPlayer(me.memberId, x => { x.items[item] = true; });
return { ok: true, paid: r.pol + ' POL' };
}
return { error: 'No such item.' };
}
function recent(n) { return Object.values(runs()).sort((a, b) => b.startedAt - a.startedAt).slice(0, n || 50).map(r => Object.assign({}, r, { collected: r.collected.length })); }
module.exports = { player, updPlayer, view, run, start, events, end, refuelled, buy, recent, prune };