PolyWarriors mints: every $5 of packages bought while a mint waits earns one mint pass (bigger packages earn more, kept on the account); one-tap 'use a pass'; members keep the ad credits; existing credits never pay for mints (replaces the 100-credit checkout)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -478,7 +478,8 @@ async function boot() {
|
||||
emailOnEvent(ev).catch(() => {});
|
||||
telegramOnEvent(ev).catch(() => {});
|
||||
friday.onEvent(ev).catch(e => console.error('friday bonus', e.message));
|
||||
starter.onEvent(ev).catch(e => console.error('starter bonus', e.message));
|
||||
starter.onEvent(ev).catch(e => console.error('starter bonus', e.message));
|
||||
pwMintOnEvent(ev).catch(e => console.error('polywarriors mint', e.message));
|
||||
wednesday.onEvent(ev).catch(e => console.error('wednesday bonus', e.message));
|
||||
sponsorSyncOnEvent(ev).catch(e => console.error('sponsor sync', e.message));
|
||||
}).catch(e => console.error('marks claim', e.message));
|
||||
@@ -1349,12 +1350,37 @@ function pwOpen(token) {
|
||||
if (!c.exp || Date.now() > c.exp) return { error: 'That mint order has expired. Tap Mint in PolyWarriors again.' };
|
||||
return { ok: true, claims: c };
|
||||
}
|
||||
const pwOrders = persistedMap('pworders'); // order -> { status: 'paying'|'paid', email, campaignId, ts }
|
||||
const pwPasses = persistedMap('pwpasses'); // email -> { n, earned, used, log: [{ at, d, why }] }
|
||||
const pwPassN = e => ((pwPasses.get(e) || {}).n || 0);
|
||||
function pwPassAdd(e, d, why) { const c = pwPasses.get(e) || { n: 0, earned: 0, used: 0, log: [] }; const v = { n: Math.max(0, c.n + d), earned: c.earned + Math.max(0, d), used: c.used + Math.max(0, -d), log: [{ at: Date.now(), d, why }].concat(c.log || []).slice(0, 50) }; pwPasses.set(e, v); return v.n; }
|
||||
const pwOrders = persistedMap('pworders'); // order -> { status: 'waiting'|'paid', email, serial, ret, cb, ts, tx, cents, delivered }
|
||||
const pwIsGameUrl = x => [PW_URL()].concat(PW_ALT()).some(o => String(x || '').startsWith(o + '/'));
|
||||
async function pwCallback(cbUrl, receipt) {
|
||||
if (!pwIsGameUrl(cbUrl)) return false;
|
||||
try { const r = await fetch(cbUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ t: receipt }), signal: AbortSignal.timeout(8000) }); return r.ok; } catch (e) { console.warn('polywarriors callback', e.message); return false; }
|
||||
}
|
||||
const pwReceipt = (order, o) => pwMint({ kind: 'pw-mint-receipt', order, email: o.email, serial: o.serial, paid: 5, usd: (o.cents || 500) / 100, ref: o.tx || null }, 3 * 86400000);
|
||||
async function pwDeliver(order) { // the game mints on the receipt; retried each minute until the game says ok (up to a day)
|
||||
const o = pwOrders.get(order); if (!o || o.status !== 'paid' || o.delivered) return;
|
||||
const ok = await pwCallback(o.cb, pwReceipt(order, o));
|
||||
const cur = pwOrders.get(order); if (!cur) return;
|
||||
pwOrders.set(order, Object.assign({}, cur, ok ? { delivered: Date.now() } : { tries: (cur.tries || 0) + 1 }));
|
||||
}
|
||||
setInterval(() => { for (const [k, o] of pwOrders) { if (o.status === 'paid' && !o.delivered && (o.tries || 0) < 1440) pwDeliver(k).catch(() => {}); if (o.status === 'waiting' && Date.now() - o.ts > 7 * 86400000) pwOrders.delete(k); } }, 60000).unref();
|
||||
async function pwMintOnEvent(ev) { // a package bought while a mint waits earns one mint pass per $5; waiting mints use them first
|
||||
if (!ev || ev.type !== 'Purchase' || (ev.priceCents || 0) < 500) return null;
|
||||
const a = (!IS_PROD && ev.devEmail) ? { email: ev.devEmail } : await accounts.byMemberId(ev.buyerId); if (!a || !a.email) return null; // devEmail: local tests only
|
||||
const e = String(a.email).toLowerCase(), when = ev.ts || Date.now(), seen = pwPasses.get(e) || {};
|
||||
if ((seen.log || []).some(x => x.why === 'buy:' + ev.tx)) return null; // each purchase counts once
|
||||
const waiting = [...pwOrders.entries()].filter(([k, o]) => o.status === 'waiting' && o.email === e && o.ts <= when + 60000).sort((x, y) => x[1].ts - y[1].ts);
|
||||
if (!waiting.length) return null; // passes come from packages bought for minting
|
||||
const earned = Math.floor(ev.priceCents / 500); pwPassAdd(e, earned, 'buy:' + ev.tx);
|
||||
const used = [];
|
||||
for (const [k, o] of waiting) { if (pwPassN(e) < 1) break; pwPassAdd(e, -1, 'mint:' + k); pwOrders.set(k, Object.assign({}, o, { status: 'paid', tx: ev.tx, cents: ev.priceCents, paidAt: Date.now() })); used.push(k); }
|
||||
console.log('polywarriors mint passes', e, '+' + earned, 'from tx', String(ev.tx).slice(0, 12), 'used', used.length, 'left', pwPassN(e));
|
||||
for (const k of used) await pwDeliver(k);
|
||||
return { earned, used, left: pwPassN(e) };
|
||||
}
|
||||
async function gkCallback(cbUrl, receipt) {
|
||||
if (!String(cbUrl || '').startsWith(GK_URL() + '/')) return false;
|
||||
try { const r = await fetch(cbUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ t: receipt }), signal: AbortSignal.timeout(8000) }); return r.ok; } catch (e) { console.warn('gemkingdom callback', e.message); return false; }
|
||||
@@ -2926,53 +2952,44 @@ const server = http.createServer(async (req, res) => {
|
||||
+ '<a class="btn" href="/gk/shop/topup?t=' + encodeURIComponent(t) + '">Buy a credit package</a><a class="btn blue" href="' + GK_URL() + '/play?open=shop">Pick a smaller pack</a>'
|
||||
+ '<p class="fine">A $5 package comes with 500 credits. After you buy, a banner on your dashboard brings you straight back to finish this order.</p>');
|
||||
}
|
||||
// ---- PolyWarriors paid mint checkout
|
||||
if (p === '/pw/mint' && req.method === 'GET') {
|
||||
const t = u.searchParams.get('t') || ''; const r = pwOpen(t);
|
||||
if (r.error) return gkPage(res, 400, 'Mint problem', '<h1>That mint did not work</h1><p class="sub">' + gkEsc(r.error) + '</p><a class="btn blue" href="' + PW_URL() + '/play">Back to PolyWarriors</a>');
|
||||
const c = r.claims; if (c.kind !== 'pw-mint' || !c.order || !(c.credits > 0) || !(c.serial > 0)) return gkPage(res, 400, 'Mint problem', '<h1>That is not a mint order</h1>');
|
||||
const done = pwOrders.get(c.order);
|
||||
if (done && done.status === 'paid') { const rc = pwMint({ kind: 'pw-mint-receipt', order: c.order, email: c.email, serial: c.serial, credits: c.credits, paid: c.credits, ref: done.campaignId }, 24 * 3600000); res.writeHead(302, { Location: (pwIsGameUrl(c.ret) ? c.ret : PW_URL() + '/api/iap/mint-receipt') + '?t=' + rc, 'Cache-Control': 'no-store' }); return res.end(); }
|
||||
const s = await auth.fromRequest(req);
|
||||
if (!s || !s.email) { res.writeHead(302, { Location: '/my', 'Cache-Control': 'no-store', 'Set-Cookie': [gkCookie('iap.pwmint', t, 7200), gkCookie('iap.return', 'pwmint', 1800)] }); return res.end(); }
|
||||
if (String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Different account', '<h1>Different account</h1><p class="sub">This mint was started by the PolyWarriors player ' + gkEsc(c.email) + ', but you are signed in to InstantAdPay as ' + gkEsc(s.email) + '. Sign in with the same account and tap Mint again.</p>');
|
||||
const { ids } = await myMemberIds(s); const bal = await ads.balances(ids, s.email);
|
||||
const best = (bal.per || []).slice().sort((a, b) => b.avail - a.avail)[0] || { avail: 0 };
|
||||
const head = '<p class="sub">PolyWarriors · Mint to your wallet</p><h1>' + gkEsc(c.label || ('Warrior #' + c.serial)) + '</h1><div class="big">' + Number(c.credits) + ' credits</div>'
|
||||
+ '<div class="row"><span>Paid from</span><span>credits from your packages</span></div>'
|
||||
+ '<div class="row"><span>Your package credits</span><span>' + Number(best.avail || 0).toLocaleString('en-US') + '</span></div>';
|
||||
if (best.avail >= c.credits) {
|
||||
const csrf = pwSign('csrf:' + c.order + ':' + String(s.email).toLowerCase());
|
||||
return gkPage(res, 200, 'Confirm your mint', head + '<form method="post" action="/pw/mint/confirm"><input type="hidden" name="t" value="' + gkEsc(t) + '"><input type="hidden" name="csrf" value="' + csrf + '"><button class="btn" type="submit">Spend ' + Number(c.credits) + ' credits and mint</button></form>'
|
||||
+ '<a class="alt" href="' + PW_URL() + '/play">Cancel and go back</a><p class="fine">Minting puts this warrior on Polygon in the wallet on your InstantAdPay account. Earned credits cannot be used for mints; credits from your packages can. The game pays the network fee.</p>');
|
||||
}
|
||||
return gkPage(res, 200, 'Package credits needed', head + '<p class="warn">Mints after your first free one are paid with credits from your packages, and you need ' + Math.max(0, c.credits - best.avail).toLocaleString('en-US') + ' more. Earned credits do not count for mints.</p>'
|
||||
+ '<a class="btn" href="/pw/mint/topup?t=' + encodeURIComponent(t) + '">Buy a credit package</a><a class="btn blue" href="' + PW_URL() + '/play">Back to PolyWarriors</a>'
|
||||
+ '<p class="fine">A $5 package comes with 500 credits. After you buy, a banner on your dashboard brings you back to finish this mint.</p>');
|
||||
}
|
||||
if (p === '/pw/mint/topup' && req.method === 'GET') { const t = u.searchParams.get('t') || ''; if (pwOpen(t).error) { res.writeHead(302, { Location: '/my#buy' }); return res.end(); } res.writeHead(302, { Location: '/my#buy', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.pwmint', t, 7200) }); return res.end(); }
|
||||
if (p === '/pw/mint/resume' && req.method === 'GET') { const t = parseCookies(req)['iap.pwmint']; res.writeHead(302, { Location: t ? '/pw/mint?t=' + encodeURIComponent(t) : PW_URL() + '/play', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.return', '', 0) }); return res.end(); }
|
||||
if (p === '/pw/mint/confirm' && req.method === 'POST') {
|
||||
const f = await gkFormBody(req); const t = f.get('t') || ''; const r = pwOpen(t);
|
||||
if (r.error) return gkPage(res, 400, 'Mint problem', '<h1>That mint did not work</h1><p class="sub">' + gkEsc(r.error) + '</p>');
|
||||
const c = r.claims; const s = await auth.fromRequest(req);
|
||||
if (!s || !s.email || String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Sign in', '<h1>Please sign in again</h1><a class="btn blue" href="/pw/mint?t=' + encodeURIComponent(t) + '">Continue</a>');
|
||||
const want = pwSign('csrf:' + c.order + ':' + String(s.email).toLowerCase()); const got = String(f.get('csrf') || '');
|
||||
if (want.length !== got.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(got))) return gkPage(res, 403, 'Mint problem', '<h1>That form expired</h1><a class="btn blue" href="/pw/mint?t=' + encodeURIComponent(t) + '">Try again</a>');
|
||||
const prev = pwOrders.get(c.order);
|
||||
if (prev && (prev.status === 'paid' || (prev.status === 'paying' && Date.now() - prev.ts < 60000))) { res.writeHead(302, { Location: '/pw/mint?t=' + encodeURIComponent(t) }); return res.end(); }
|
||||
pwOrders.set(c.order, { status: 'paying', email: s.email, ts: Date.now() });
|
||||
const { ids } = await myMemberIds(s);
|
||||
const out = await ads.gamePurchase(s.email, ids, Number(c.credits), 'PolyWarriors: mint warrior #' + c.serial, PW_URL(), { purchasedOnly: true });
|
||||
if (out.error) { pwOrders.delete(c.order); if (out.error === 'short') { res.writeHead(302, { Location: '/pw/mint?t=' + encodeURIComponent(t) }); return res.end(); } return gkPage(res, 500, 'Mint problem', '<h1>Something went wrong</h1><p class="sub">' + gkEsc(out.error) + '</p>'); }
|
||||
pwOrders.set(c.order, { status: 'paid', email: s.email, campaignId: out.campaignId, ts: Date.now() });
|
||||
console.log('polywarriors mint fee', s.email, 'serial', c.serial, c.credits, 'purchased credits, campaign', out.campaignId);
|
||||
const rc = pwMint({ kind: 'pw-mint-receipt', order: c.order, email: String(c.email).toLowerCase(), serial: Number(c.serial), credits: Number(c.credits), paid: Number(out.paid), ref: out.campaignId }, 24 * 3600000);
|
||||
pwCallback(c.cb, rc);
|
||||
const back = gkEsc((pwIsGameUrl(c.ret) ? c.ret : PW_URL() + '/api/iap/mint-receipt') + '?t=' + rc);
|
||||
res.setHeader('Set-Cookie', gkCookie('iap.pwmint', '', 0));
|
||||
return gkPage(res, 200, 'Paid', '<meta http-equiv="refresh" content="1;url=' + back + '"><p class="sub">PolyWarriors · Mint</p><h1>Paid!</h1><div class="big">Minting #' + Number(c.serial) + '</div><p class="sub">Taking you back to PolyWarriors...</p><a class="btn" href="' + back + '">Back to PolyWarriors</a>');
|
||||
}
|
||||
// ---- PolyWarriors paid mints: every mint after the free first one comes with a new $5+ package purchase
|
||||
if (p === '/pw/mint' && req.method === 'GET') {
|
||||
const t = u.searchParams.get('t') || ''; const r = pwOpen(t);
|
||||
if (r.error) return gkPage(res, 400, 'Mint problem', '<h1>That mint did not work</h1><p class="sub">' + gkEsc(r.error) + '</p><a class="btn blue" href="' + PW_URL() + '/play">Back to PolyWarriors</a>');
|
||||
const c = r.claims; if (c.kind !== 'pw-mint' || !c.order || !(c.serial > 0)) return gkPage(res, 400, 'Mint problem', '<h1>That is not a mint order</h1>');
|
||||
const done = pwOrders.get(c.order);
|
||||
if (done && done.status === 'paid') { const rc = pwReceipt(c.order, done); res.writeHead(302, { Location: (pwIsGameUrl(done.ret) ? done.ret : PW_URL() + '/api/iap/mint-receipt') + '?t=' + rc, 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.pwmint', '', 0) }); return res.end(); }
|
||||
const s = await auth.fromRequest(req);
|
||||
if (!s || !s.email) { res.writeHead(302, { Location: '/my', 'Cache-Control': 'no-store', 'Set-Cookie': [gkCookie('iap.pwmint', t, 7 * 86400), gkCookie('iap.return', 'pwmint', 1800)] }); return res.end(); }
|
||||
if (String(s.email).toLowerCase() !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Different account', '<h1>Different account</h1><p class="sub">This mint was started by the PolyWarriors player ' + gkEsc(c.email) + ', but you are signed in to InstantAdPay as ' + gkEsc(s.email) + '. Sign in with the same account and tap Mint again.</p>');
|
||||
// the order waits here until a new package purchase from this account lands on chain (oldest waiting order first)
|
||||
if (!done) pwOrders.set(c.order, { status: 'waiting', email: String(s.email).toLowerCase(), serial: Number(c.serial), label: String(c.label || '').slice(0, 60), ret: c.ret, cb: c.cb, ts: Date.now() });
|
||||
res.setHeader('Set-Cookie', gkCookie('iap.pwmint', t, 7 * 86400));
|
||||
const have = pwPassN(String(s.email).toLowerCase());
|
||||
if (have > 0) {
|
||||
const csrf = pwSign('pass:' + c.order + ':' + String(s.email).toLowerCase());
|
||||
return gkPage(res, 200, 'Mint your warrior', '<p class="sub">PolyWarriors · Mint to your wallet</p><h1>' + gkEsc(c.label || ('Warrior #' + c.serial)) + '</h1><div class="big">' + have + ' mint pass' + (have === 1 ? '' : 'es') + '</div>'
|
||||
+ '<form method="post" action="/pw/mint/pass"><input type="hidden" name="t" value="' + gkEsc(t) + '"><input type="hidden" name="csrf" value="' + csrf + '"><button class="btn" type="submit">Use 1 mint pass and mint</button></form>'
|
||||
+ '<a class="alt" href="' + PW_URL() + '/play">Cancel and go back</a><p class="fine">You earned these passes with your package purchases: one mint for every $5. The game pays the network fee.</p>');
|
||||
}
|
||||
return gkPage(res, 200, 'Mint your warrior', '<p class="sub">PolyWarriors · Mint to your wallet</p><h1>' + gkEsc(c.label || ('Warrior #' + c.serial)) + '</h1><div class="big">Comes with a $5 package</div>'
|
||||
+ '<p class="sub">Your first mint was free. After that, every $5 of new ad packages earns one mint: $5 mints this warrior, $10 earns 2 mints, $20 earns 4, and any extra mints stay on your account for next time. You keep every package\'s ad credits.</p>'
|
||||
+ '<a class="btn" href="/my?from=polywarriors#buy">Buy a $5 package</a><a class="btn blue" href="' + PW_URL() + '/play">Back to PolyWarriors</a>'
|
||||
+ '<p class="fine">Buy while this mint is waiting and it mints to your wallet automatically. Credits you already have cannot be used for mints. The game pays the network fee.</p>');
|
||||
}
|
||||
if (!IS_PROD && p === '/api/dev/pw-purchase' && req.method === 'POST') { const b = await readBody(req); return json(res, 200, { order: await pwMintOnEvent({ type: 'Purchase', priceCents: Number(b.cents) || 500, buyerId: 0, tx: '0x' + crypto.randomBytes(32).toString('hex'), ts: Date.now(), devEmail: b.email }) }); } // local tests only
|
||||
if (p === '/pw/mint/pass' && req.method === 'POST') {
|
||||
const f = await gkFormBody(req); const t = f.get('t') || ''; const r = pwOpen(t);
|
||||
if (r.error) return gkPage(res, 400, 'Mint problem', '<h1>That mint did not work</h1><p class="sub">' + gkEsc(r.error) + '</p>');
|
||||
const c = r.claims; const s = await auth.fromRequest(req); const e = s && s.email ? String(s.email).toLowerCase() : '';
|
||||
if (!e || e !== String(c.email).toLowerCase()) return gkPage(res, 403, 'Sign in', '<h1>Please sign in again</h1><a class="btn blue" href="/pw/mint?t=' + encodeURIComponent(t) + '">Continue</a>');
|
||||
const want = pwSign('pass:' + c.order + ':' + e), got = String(f.get('csrf') || '');
|
||||
if (want.length !== got.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(got))) return gkPage(res, 403, 'Mint problem', '<h1>That form expired</h1><a class="btn blue" href="/pw/mint?t=' + encodeURIComponent(t) + '">Try again</a>');
|
||||
const o = pwOrders.get(c.order);
|
||||
if (o && o.status === 'waiting' && pwPassN(e) > 0) { pwPassAdd(e, -1, 'mint:' + c.order); pwOrders.set(c.order, Object.assign({}, o, { status: 'paid', tx: 'pass', cents: 500, paidAt: Date.now() })); await pwDeliver(c.order); }
|
||||
res.writeHead(303, { Location: '/pw/mint?t=' + encodeURIComponent(t), 'Cache-Control': 'no-store' }); return res.end();
|
||||
}
|
||||
if (p === '/pw/mint/resume' && req.method === 'GET') { const t = parseCookies(req)['iap.pwmint']; res.writeHead(302, { Location: t ? '/pw/mint?t=' + encodeURIComponent(t) : PW_URL() + '/play', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.return', '', 0) }); return res.end(); }
|
||||
if (p === '/gk/shop/topup' && req.method === 'GET') { // the detour to Buy packages, remembered for the way back
|
||||
const t = u.searchParams.get('t') || ''; if (gkOpen(t).error) { res.writeHead(302, { Location: '/my#buy' }); return res.end(); }
|
||||
res.writeHead(302, { Location: '/my#buy', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.gkshop', t, 7200) }); return res.end();
|
||||
|
||||
Reference in New Issue
Block a user