Game Pass bridge (GAMEPASS=on): chapter first clears via InstantAdPay polgrant (wallet-less members by email), shield at run start perk, lobby line + clear note, tests
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
+53
-2
@@ -105,6 +105,57 @@ function spend(me, item, pol) {
|
||||
return { ok: true, pol };
|
||||
}
|
||||
|
||||
// ---- Game Pass (GAMEPASS=on, docs/GAME-PASS.md 3a): InstantAdPay's account-wide caps decide what a member is paid; the
|
||||
// local cap and pool above stay as a backstop and are never raised. The only POL Drip Runner pays is a chapter's first clear,
|
||||
// once per member, so every reward here is 'progress': what today's cap does not allow is deferred on IAP and folded into a
|
||||
// later answer. A member with no wallet has a negative local id (sso.emailId): IAP gets memberId 0 and the email instead.
|
||||
const gp = require('./polgrant');
|
||||
const kindOf = key => 'progress'; // refs: chapter:<n>
|
||||
const iapId = me => Number(me.memberId) > 0 ? Number(me.memberId) : 0;
|
||||
const passActive = me => !!(gp.on() && me && !me.guest && gp.passOf(iapId(me), me.email).active);
|
||||
// the same checks as chapterDrip() up to (not including) the ledger write -> { pol, key, chapter } or { pol: 0, reason }
|
||||
function quote(me, chapter, eligible) {
|
||||
const s = settings(); const pol = r4(Number(s.dripPol));
|
||||
if (!eligible) return { pol: 0, reason: 'replay' };
|
||||
if (!(pol > 0)) return { pol: 0, reason: 'none' };
|
||||
const p = pool(); if (p.capLeft < pol) return { pol: 0, reason: 'cap' }; if (p.available < pol) return { pol: 0, reason: 'pool' };
|
||||
return { pol, key: 'chapter:' + chapter, chapter };
|
||||
}
|
||||
// IAP answered: the ledger gets exactly what was allowed (0 included, so the chapter counts as handled); `carried` (inside
|
||||
// allowed) is what IAP paid from an earlier deferral; the account tracks what IAP still owes.
|
||||
function settleGrant(me, q, r, gkind) {
|
||||
const allowed = r4(r.allowed), extra = r.carried > 0 ? r4(r.carried) : r4(Math.max(0, allowed - q.pol));
|
||||
const row = { id: id(), memberId: Number(me.memberId), username: me.username || null, wallet: me.wallet || null, pol: allowed, chapter: q.chapter, day: ctDay(), ts: Date.now(), status: 'granted', gp: { kind: gkind, ref: q.key, nominal: q.pol, deferred: r4(r.deferred || 0), pass: !!r.pass } };
|
||||
store.update('drips', [], all => { if (!all.some(d => d.memberId === row.memberId && d.gp && d.gp.ref === q.key)) all.push(row); return all; });
|
||||
if (allowed > 0) poolAdd('out', allowed);
|
||||
updAccount(me.memberId, a => { a.unpaid += allowed; a.earnedTotal = r4((a.earnedTotal || 0) + allowed); a.wallet = me.wallet || a.wallet || null; a.username = me.username || a.username || null; a.email = me.email || a.email || null; a.gpOwed = r4(Math.max(0, (a.gpOwed || 0) + (Number(r.deferred) || 0) - extra)); });
|
||||
return { pol: allowed, reason: allowed > 0 ? 'paid' : (gkind === 'daily' ? 'capday' : 'deferred'), nominal: q.pol, deferred: r4(r.deferred || 0), pass: !!r.pass, note: gp.note(r, gkind, q.pol), id: row.id };
|
||||
}
|
||||
// chapterDrip() with IAP in front of it. Switch off, or a guest: the plain chapterDrip, unchanged. Fail closed: no answer from
|
||||
// IAP = nothing paid now, the ref queued and retried (polgrant.js), the player told it is recorded.
|
||||
async function chapterDripGated(me, chapter, eligible) {
|
||||
if (!gp.on() || me.guest || !gp.whoOf(iapId(me), me.email)) return chapterDrip(me, chapter, eligible);
|
||||
const q = quote(me, chapter, eligible); if (q.reason) return q;
|
||||
if (gp.queued(iapId(me), me.email, q.key)) return { pol: 0, reason: 'iap', nominal: q.pol };
|
||||
const gkind = kindOf(q.key), r = await gp.grant({ memberId: iapId(me), email: me.email, kind: gkind, pol: q.pol, ref: q.key });
|
||||
if (r.retry) { gp.enqueue({ memberId: iapId(me), email: me.email, ref: q.key, kind: gkind, pol: q.pol, me: { memberId: Number(me.memberId), username: me.username || null, wallet: me.wallet || null, email: me.email || null }, q }); return { pol: 0, reason: 'iap', nominal: q.pol, note: gp.note(r, gkind, q.pol) }; }
|
||||
return settleGrant(me, q, r, gkind);
|
||||
}
|
||||
const payQueued = (item, r) => settleGrant(item.me, item.q, r, item.kind); // a queued ref finally answered
|
||||
// once a Central day per account IAP still owes: a zero-POL progress call collects the carry-forward (IAP folds it into `allowed`)
|
||||
async function carrySweep() {
|
||||
if (!gp.on()) return 0; const today = ctDay(), accts = store.read('accounts', {}); let got = 0;
|
||||
for (const k of Object.keys(accts)) {
|
||||
const a = accts[k]; if (!(a.gpOwed > 0) || a.gpCarryDay === today) continue;
|
||||
const me = { memberId: a.memberId, email: a.email || null, username: a.username, wallet: a.wallet };
|
||||
if (!gp.whoOf(iapId(me), me.email)) continue;
|
||||
const r = await gp.grant({ memberId: iapId(me), email: me.email, kind: 'progress', pol: 0, ref: 'carry:' + today }); if (r.retry) continue;
|
||||
updAccount(a.memberId, x => { x.gpCarryDay = today; });
|
||||
if (r.allowed > 0) { settleGrant(me, { pol: 0, key: 'carry:' + today, chapter: 0 }, r, 'progress'); got += r.allowed; }
|
||||
}
|
||||
return got;
|
||||
}
|
||||
|
||||
// ---- payouts ----------------------------------------------------------------------------------
|
||||
// a member whose unpaid balance reached the threshold gets one payout row for the whole amount;
|
||||
// the balance moves to the row so drips earned meanwhile start a fresh count
|
||||
@@ -136,7 +187,7 @@ function mark(pid, patch) {
|
||||
function balance(memberId) {
|
||||
const a = account(memberId); const s = settings();
|
||||
const hist = []
|
||||
.concat(store.read('drips', []).filter(d => d.memberId === Number(memberId)).map(d => ({ t: d.ts, kind: 'drip', label: 'Chapter ' + d.chapter + ' · first clear', pol: d.pol })))
|
||||
.concat(store.read('drips', []).filter(d => d.memberId === Number(memberId)).map(d => ({ t: d.ts, kind: 'drip', label: !d.chapter ? 'Carried-over reward paid' : 'Chapter ' + d.chapter + ' · first clear' + (d.gp && d.gp.deferred > 0 ? ' (' + d.gp.deferred + ' POL paying out over the next days)' : ''), pol: d.pol })))
|
||||
.concat(store.read('spends', []).filter(d => d.memberId === Number(memberId)).map(d => ({ t: d.ts, kind: 'spend', label: 'Hangar · ' + d.item, pol: -d.pol })))
|
||||
.concat(store.read('refslices', []).filter(d => d.memberId === Number(memberId)).map(d => ({ t: d.ts, kind: 'ref', label: 'Referral · ' + (d.fromUsername ? '@' + d.fromUsername : 'a player') + ' refuelled', pol: d.pol })))
|
||||
.concat(store.read('payouts', []).filter(d => d.memberId === Number(memberId)).map(d => ({ t: d.ts, kind: 'payout', label: d.status === 'paid' ? 'Paid to your wallet' : d.status === 'failed' ? 'Payout failed: ' + (d.error || '') : 'Payout on its way', pol: d.pol, status: d.status, tx: d.tx || null })))
|
||||
@@ -144,4 +195,4 @@ function balance(memberId) {
|
||||
return { unpaid: r4(a.unpaid), paidTotal: r4(a.paidTotal || 0), spentTotal: r4(a.spentTotal || 0), earnedTotal: r4(a.earnedTotal || 0), refTotal: r4(a.refTotal || 0), refSharePct: Number(s.refSharePct) || 0, threshold: Number(s.thresholdPol), history: hist, wallet: a.wallet || null };
|
||||
}
|
||||
|
||||
module.exports = { DEFAULTS, settings, setSettings, ctDay, pool, refuel, refuelsToday, account, chapterDrip, spend, sweep, payable, mark, balance, usdPerPol, r4 };
|
||||
module.exports = { DEFAULTS, settings, setSettings, ctDay, pool, refuel, refuelsToday, account, chapterDrip, chapterDripGated, quote, settleGrant, kindOf, passActive, iapId, payQueued, carrySweep, gp, spend, sweep, payable, mark, balance, usdPerPol, r4 };
|
||||
|
||||
Reference in New Issue
Block a user