Game Pass: no pass is ever bought with credits again (Marty, 2026-10-22). Buy only opens a waiting order and sends the member to buy a package of at least the tier price; the order is fulfilled only by a fresh on-chain package Purchase by one of the member's own positions (main or linked), mined after the order was opened (block time, 30 s clock grace), each purchase tx used once across every account. The member keeps the package credits for ads; sponsors and team are paid by the package as usual. Existing passes untouched. Card and confirm copy say so; my.js tag bumped. qa/gamepass.mjs updated for the new flow (older-than-order, too small, another member, replayed tx): ALL PASS.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+56
-30
@@ -4,7 +4,7 @@
|
||||
//
|
||||
// What lives here:
|
||||
// - the pass store email -> { until, bought: [{ ts, via, tx | credits, usd }], lastCapHit } (durable persisted map 'gamepass')
|
||||
// - waiting orders order -> { status, email, ts, tx } (a $5+ package bought while an order waits fulfils it: the pwpasses pattern)
|
||||
// - pass orders order -> { status, email, ts, tx } (every pass is paid by a FRESH package purchase: see buy() / onEvent())
|
||||
// - the cap ledger per member per Central day: used daily/total, deferred queue per game, every answered (game, ref) kept 90 days
|
||||
// - the weekly Pass Tournament: ranks pass holders by grants reported through polgrant, records POL prizes for the games to pay
|
||||
// - admin stats
|
||||
@@ -20,6 +20,11 @@
|
||||
// `until` as the weekly tier). flag().tier = the highest active tier. Explorer/Champion sales sit behind the site switch
|
||||
// `sovTiers` (default off; only ADMIN_EMAIL can see and buy them while it is off). No proration, no auto-renew: buying again
|
||||
// extends that tier from max(now, until).
|
||||
// 2026-10-22 (Marty): every pass rides the package rail so sponsors get paid. Buying a pass never spends a credit balance any more
|
||||
// (today's buyer paid from an old balance, so no new money came in and nobody upline was paid). A pass click opens an order; the
|
||||
// order is fulfilled ONLY by an on-chain package Purchase of at least the tier price, mined after the order was opened, bought by
|
||||
// one of that member's own positions (main or linked), each purchase tx used once. The member keeps that package's credits for ads.
|
||||
// Bought rows: via 'package' (with tx and cents). The two rows from before this change are via 'credits' and 'package' and stay.
|
||||
// 2026-10-22 (Dallas's structure, Marty approved): Explorer $19.99 and Champion $39.99 (30 days), plus Sovereign $99.99 (30 days)
|
||||
// and Legend $249.99 (365 days, the yearly pass), all in TIER_DEFS below and all behind the same sovTiers switch. The weekly
|
||||
// pass is unchanged ($5 / 7 days / the same caps). Rank weekly < explorer < champion < sovereign < legend.
|
||||
@@ -35,7 +40,8 @@ const KEEP_DAYS = 90; // answered refs and day rows are kept this long
|
||||
// THE pass table (2026-10-22, Dallas's structure, Marty approved): lowest to highest, a higher tier includes everything below.
|
||||
// Rename a tier here (name) and nowhere else: the member card, the admin pane, the charge label and the chatbot read it from here.
|
||||
// key = the wire value the games read (flag().tier / tiers); set = the Admin > Settings stem (gamePass<set>Enabled /
|
||||
// PriceCredits / Days; weekly keeps its original gamePassPriceCredits / gamePassDays); price = purchased-grade credits (100 = $1);
|
||||
// PriceCredits / Days; weekly keeps its original gamePassPriceCredits / gamePassDays); price = the pass price in cents (100 = $1;
|
||||
// the setting keeps its old 'PriceCredits' name): the package that pays for a pass must cost at least this much;
|
||||
// perks = what the member card lists. Every tier is a Game Pass holder everywhere with the SAME caps (no extra POL).
|
||||
const TIER_DEFS = [
|
||||
{ key: 'weekly', name: 'Game Pass', set: '', price: 500, days: 7,
|
||||
@@ -193,46 +199,66 @@ function view(email, now, opts) {
|
||||
show: c.enabled || tiered, passes: passRows, tierPreview: tiered && admin && !c.tierSales };
|
||||
}
|
||||
|
||||
// POST /api/my/gamepass/buy { tier }. `charge(price, label)` runs ads.gamePurchase(purchasedOnly) and resolves { ok } | { error: 'short', ... } | { error }.
|
||||
// tier missing = weekly (exactly the pass as it was before tiers). opts.admin = the ADMIN_EMAIL account.
|
||||
async function buy(email, charge, now, tier, opts) {
|
||||
// POST /api/my/gamepass/buy { tier }. tier missing = weekly (exactly the pass as it was before tiers). opts.admin = the ADMIN_EMAIL account.
|
||||
// A pass is ALWAYS paid by a fresh package purchase (2026-10-22, Marty: the pass rides the package rail so sponsors get paid). This never
|
||||
// spends credits: it opens (or reuses) a waiting order for that tier and sends the member to buy a package of at least the tier price.
|
||||
// The order is fulfilled by onEvent() below; the member keeps every credit of that package for ads.
|
||||
function buy(email, now, tier, opts) {
|
||||
const e = String(email || '').toLowerCase(); const c = cfg(); const t = now || Date.now();
|
||||
tier = (tier === undefined || tier === null || tier === '') ? 'weekly' : String(tier).toLowerCase();
|
||||
if (!tierOk(tier)) return { error: 'There is no such pass.' };
|
||||
const tc = c.tiers[tier]; const name = TIER_NAMES[tier];
|
||||
if (tier === 'weekly') { if (!c.enabled) return { error: 'The Game Pass is not open yet.' }; }
|
||||
else if (!tierOpen(c, tier, !!(opts && opts.admin))) return { error: 'The ' + name + ' pass is not open yet.' };
|
||||
const r = await charge(tc.price, (tier === 'weekly' ? 'Game Pass' : name + ' pass') + ' (' + tc.days + ' days)');
|
||||
if (r && r.ok) {
|
||||
const x = extend(e, 'credits', { credits: tc.price, campaignId: r.campaignId || null }, t, tier);
|
||||
console.log('gamepass bought with credits', tier, e, tc.price, 'until', new Date(x.until).toISOString());
|
||||
return { ok: true, active: true, tier, until: x.until, days: tc.days, paid: 'credits' };
|
||||
}
|
||||
if (r && r.error === 'short') {
|
||||
// no purchased-grade credits on a position: the pass waits for a package of at least its price (bought while the order waits, it fulfils it)
|
||||
let w = waitingOrder(e, tier);
|
||||
if (!w) { const k = 'gp-' + t.toString(36) + '-' + Math.random().toString(36).slice(2, 8); orders.set(k, { status: 'waiting', email: e, ts: t, tier }); w = Object.assign({ order: k }, orders.get(k)); }
|
||||
const pkg = packageFor(tc.price / 100);
|
||||
return { ok: false, needPay: true, tier, payUrl: tier === 'weekly' ? '/my?from=gamepass#buy' : '/my?from=gamepass&tier=' + tier + '#buy', order: w.order, price: tc.price, usd: tc.price / 100, package: pkg, available: r.available || 0, need: r.need || tc.price,
|
||||
message: (tier === 'weekly' ? 'The pass' : 'The ' + name + ' pass') + ' is paid with purchased credits. Buy a $' + pkg + ' package and the pass starts the moment it settles; you keep every credit of the package for your ads.' };
|
||||
}
|
||||
return { error: (r && r.error) || 'The purchase did not go through. Nothing was taken.' };
|
||||
let w = waitingOrder(e, tier); const reused = !!w;
|
||||
if (!w) { const k = 'gp-' + t.toString(36) + '-' + Math.random().toString(36).slice(2, 8); orders.set(k, { status: 'waiting', email: e, ts: t, tier }); w = Object.assign({ order: k }, orders.get(k)); }
|
||||
const pkg = packageFor(tc.price / 100);
|
||||
if (!reused) console.log('gamepass order opened', tier, e, w.order, 'needs a package of', tc.price, 'cents');
|
||||
return { ok: false, needPay: true, tier, payUrl: tier === 'weekly' ? '/my?from=gamepass#buy' : '/my?from=gamepass&tier=' + tier + '#buy', order: w.order, orderTs: w.ts, reused,
|
||||
price: tc.price, usd: tc.price / 100, package: pkg,
|
||||
message: (tier === 'weekly' ? 'Your Game Pass' : 'Your ' + name + ' pass') + ' starts the moment your next package of $' + pkg + ' or more settles on chain. Your sponsor and team are paid from it as usual, and you keep every credit of the package for your ads.' };
|
||||
}
|
||||
|
||||
// a package purchase landing on chain fulfils one of this account's waiting orders: the highest-priced one the package covers
|
||||
// (one pass per purchase of $5 or more, each tx counted once)
|
||||
// A package purchase landing on chain fulfils one of this account's waiting orders: the highest-priced one the package covers.
|
||||
// Rules (2026-10-22): the buyer must be one of this account's own positions (main member id or a linked position); the purchase must
|
||||
// be mined AFTER the order was opened (block time, ORDER_SKEW_MS for clock drift); one pass per purchase of $5 or more; each tx used
|
||||
// once across every account and order. Credit balances never fulfil an order.
|
||||
const ORDER_SKEW_MS = 30000;
|
||||
function txUsed(tx) {
|
||||
if (!tx) return false;
|
||||
for (const [, o] of orders) if (o && o.tx === tx) return true;
|
||||
for (const [, p] of passes) if (p && (p.bought || []).some(b => b.tx && b.tx === tx)) return true;
|
||||
return false;
|
||||
}
|
||||
async function ownerOf(ev) { // the account behind the buying position
|
||||
if (ev.devEmail && !R.isProd) return String(ev.devEmail).toLowerCase();
|
||||
const id = Number(ev.buyerId) || 0; if (!id) return null;
|
||||
let a = await R.accounts.byMemberId(id);
|
||||
if (!a && R.accounts.positionByMember) { const pos = await R.accounts.positionByMember(id); if (pos && pos.email) a = { email: pos.email }; }
|
||||
return a && a.email ? String(a.email).toLowerCase() : null;
|
||||
}
|
||||
async function minedAt(ev) { // block time in ms; the indexed-at time only when the block cannot be read (it is never earlier than the block)
|
||||
if (ev.devMinedAt && !R.isProd) return Number(ev.devMinedAt);
|
||||
if (ev.block && R.blockTime) {
|
||||
for (let i = 0; i < 3; i++) { try { const s = await R.blockTime(ev.block); if (s > 0) return s * 1000; } catch (e) {} await new Promise(r => setTimeout(r, 1500)); }
|
||||
console.warn('gamepass: block time unavailable for', ev.block, '- using the indexed-at time');
|
||||
}
|
||||
return Number(ev.ts) || Date.now();
|
||||
}
|
||||
async function onEvent(ev) {
|
||||
if (!ev || ev.type !== 'Purchase' || (ev.priceCents || 0) < 500) return null;
|
||||
const c = cfg(); const cents = ev.priceCents || 0;
|
||||
if (!TIERS.some(k => cents >= c.tiers[k].price)) return null;
|
||||
const a = (ev.devEmail && !R.isProd) ? { email: ev.devEmail } : await R.accounts.byMemberId(ev.buyerId); if (!a || !a.email) return null;
|
||||
const e = String(a.email).toLowerCase();
|
||||
const p = passes.get(e); if (p && (p.bought || []).some(b => b.tx && b.tx === ev.tx)) return null; // each purchase counts once
|
||||
const w = waitingOrders(e).filter(o => cents >= c.tiers[o.tier].price).sort((x, y) => c.tiers[y.tier].price - c.tiers[x.tier].price || x.ts - y.ts)[0];
|
||||
if (!w) return null;
|
||||
orders.set(w.order, Object.assign({}, orders.get(w.order), { status: 'paid', tx: ev.tx, cents, paidAt: Date.now() }));
|
||||
const x = extend(e, 'package', { tx: ev.tx, cents, order: w.order }, ev.ts || Date.now(), w.tier);
|
||||
console.log('gamepass fulfilled by package', w.tier, e, 'tx', String(ev.tx).slice(0, 12), 'until', new Date(x.until).toISOString());
|
||||
const e = await ownerOf(ev); if (!e) return null;
|
||||
if (!waitingOrders(e).length) return null;
|
||||
if (txUsed(ev.tx)) return null; // each purchase counts once, everywhere
|
||||
const at = await minedAt(ev);
|
||||
const w = waitingOrders(e).filter(o => cents >= c.tiers[o.tier].price && at + ORDER_SKEW_MS >= o.ts)
|
||||
.sort((x, y) => c.tiers[y.tier].price - c.tiers[x.tier].price || x.ts - y.ts)[0];
|
||||
if (!w) { console.log('gamepass: package', String(ev.tx).slice(0, 12), 'by', e, 'fulfils no order (bought before the order or too small)'); return null; }
|
||||
orders.set(w.order, Object.assign({}, orders.get(w.order), { status: 'paid', tx: ev.tx, cents, paidAt: Date.now(), minedAt: at, buyerId: Number(ev.buyerId) || 0 }));
|
||||
const x = extend(e, 'package', { tx: ev.tx, cents, order: w.order, buyerId: Number(ev.buyerId) || 0, usd: c.tiers[w.tier].price / 100, packageUsd: cents / 100 }, (ev.devNow && !R.isProd) ? Number(ev.devNow) : Date.now(), w.tier);
|
||||
console.log('gamepass fulfilled by package', w.tier, e, 'tx', String(ev.tx).slice(0, 12), cents, 'cents, until', new Date(x.until).toISOString());
|
||||
return { order: w.order, until: x.until, tier: w.tier };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user