Referral layer: guest play from share links (/r/<username>), guest clears paid on sign-in, sharer's link via their IAP join page, 10% referrer slice of each referral refuel's pool income (IAP sponsor), share buttons + stats in the lobby and on chapter clears, wallet-less progress follows the member once a wallet is linked; 11 new checks
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+32
-2
@@ -94,8 +94,9 @@ function events(me, runId, t, list, 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);
|
||||
const grant = me.guest ? { pol: 0, reason: 'guest' } : ledger.chapterDrip(me, ch, first && !out.flagged);
|
||||
if (first && out.flagged) grant.reason = 'flagged';
|
||||
if (me.guest && first && !out.flagged) updPlayer(me.memberId, x => { x.pending = Array.from(new Set([...(x.pending || []), ch])); }); // paid when they sign in
|
||||
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) };
|
||||
@@ -129,6 +130,35 @@ function buy(me, item) {
|
||||
}
|
||||
return { error: 'No such item.' };
|
||||
}
|
||||
// Move everything one id owns onto another (a guest who signs in, or a wallet-less member who links a wallet).
|
||||
// Returns the chapters the old id cleared as a guest that the new id has never cleared: the caller pays those.
|
||||
function adopt(fromId, toId) {
|
||||
fromId = Number(fromId); toId = Number(toId); if (!fromId || fromId === toId) return { moved: false, pending: [] };
|
||||
const all = store.read('players', {}); const a = all[String(fromId)]; if (!a) return { moved: false, pending: [] };
|
||||
const b = all[String(toId)]; const pending = (a.pending || []).filter(ch => !(b && b.cleared && b.cleared[ch]));
|
||||
store.update('players', {}, ps => {
|
||||
const src = ps[String(fromId)]; const dst = ps[String(toId)];
|
||||
if (!dst) { ps[String(toId)] = Object.assign({}, src, { memberId: toId, pending: [] }); }
|
||||
else { dst.bank += src.bank; dst.runs += src.runs; dst.best = Math.max(dst.best, src.best); dst.chapter = Math.max(dst.chapter, src.chapter); dst.cleared = Object.assign({}, src.cleared, dst.cleared); dst.items = Object.assign({}, src.items, dst.items); }
|
||||
delete ps[String(fromId)]; return ps;
|
||||
});
|
||||
for (const name of ['drips', 'spends', 'refuels', 'payouts']) store.update(name, [], rows => { for (const r of rows) if (r.memberId === fromId) r.memberId = toId; return rows; });
|
||||
store.update('accounts', {}, acc => { const x = acc[String(fromId)]; if (x) { const y = acc[String(toId)]; if (y) { y.unpaid = Math.round((y.unpaid + x.unpaid) * 10000) / 10000; y.earnedTotal = Math.round(((y.earnedTotal || 0) + (x.earnedTotal || 0)) * 10000) / 10000; y.spentTotal = Math.round(((y.spentTotal || 0) + (x.spentTotal || 0)) * 10000) / 10000; } else acc[String(toId)] = Object.assign({}, x, { memberId: toId }); delete acc[String(fromId)]; } return acc; });
|
||||
store.update('runs', {}, rs => { for (const k of Object.keys(rs)) if (rs[k].memberId === fromId) rs[k].memberId = toId; return rs; });
|
||||
store.update('posted', {}, ps => { if (ps[String(fromId)]) { ps[String(toId)] = Array.from(new Set([...(ps[String(toId)] || []), ...ps[String(fromId)]])); delete ps[String(fromId)]; } return ps; });
|
||||
return { moved: true, pending };
|
||||
}
|
||||
// the players whose InstantAdPay sponsor is this member (their referrals in the game)
|
||||
function referralsOf(me) {
|
||||
const keys = [String(me.memberId), String(me.username || '').toLowerCase()].filter(Boolean);
|
||||
return Object.values(store.read('players', {})).filter(p => p.memberId > 0 && p.sponsorRef && keys.includes(String(p.sponsorRef).toLowerCase()));
|
||||
}
|
||||
// the game player a sponsorRef points at (username, or on-chain member id)
|
||||
function byRef(ref) {
|
||||
const r = String(ref || '').trim().toLowerCase(); if (!r) return null;
|
||||
return Object.values(store.read('players', {})).find(p => p.memberId > 0 && ((p.username && p.username.toLowerCase() === r) || String(p.memberId) === r)) || null;
|
||||
}
|
||||
function byUsername(u) { const r = String(u || '').trim().toLowerCase().replace(/^@/, ''); return r ? Object.values(store.read('players', {})).find(p => p.username && p.username.toLowerCase() === r) || null : null; }
|
||||
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 };
|
||||
module.exports = { player, updPlayer, view, run, start, events, end, refuelled, buy, recent, prune, adopt, referralsOf, byRef, byUsername };
|
||||
|
||||
Reference in New Issue
Block a user