diff --git a/gamestats.js b/gamestats.js index 3ed5653..7e27379 100644 --- a/gamestats.js +++ b/gamestats.js @@ -10,7 +10,7 @@ const fs = require('fs'); const path = require('path'); let R = null; -const NAMES = { spookydoors: 'Spooky Doors', gemkingdom: 'Gem Kingdom', clutterhunt: 'ClutterHunt', driprunner: 'Drip Runner', polywarriors: 'PolyWarriors', spellwheel: 'SpellWheel', roadagent: 'Road Agent', polhunter: 'PolHunter', passivedrip: 'PassiveDrip' }; +const NAMES = { spookydoors: 'Spooky Doors', gemkingdom: 'Gem Kingdom', clutterhunt: 'ClutterHunt', driprunner: 'Drip Runner', polywarriors: 'PolyWarriors', spellwheel: 'SpellWheel', roadagent: 'Road Agent', polhunter: 'PolHunter', passivedrip: 'PassiveDrip', polybreak: 'PolyBreak' }; const wallets = () => Object.fromEntries(String(process.env.GAME_WALLETS || '').split(',').map(x => x.trim()).filter(Boolean).map(x => { const [k, a] = x.split(':'); return [String(k).toLowerCase(), String(a || '').toLowerCase()]; }).filter(([k, a]) => k && /^0x[0-9a-f]{40}$/.test(a))); const KEY = () => String(process.env.ETHERSCAN_KEY || '').trim(); // money never flows to these from a game wallet as a payout: Marty's fee wallets, the other game wallets @@ -55,4 +55,47 @@ async function gather(since) { const totals = games.reduce((t, g) => ({ sentIn: t.sentIn + g.sentIn, payouts: t.payouts + g.payouts, wei: t.wei + g.wei }), { sentIn: 0, payouts: 0, wei: 0n }); return { games, totals, chainChecked: !!KEY() && Object.keys(W).length > 0 }; } -module.exports = { init, handoff, gather, NAMES }; +// ---- all-time running total of game payouts, for the admin P&L (Marty 2026-10-18) ---- +// Every outgoing POL send from each game wallet to a player, from the wallet's first transaction. +// Kept in DATA_DIR/game-payouts-alltime.json per wallet { block, count, wei, players[] } and topped up +// incrementally (only blocks after the last one seen), so after the first scan a refresh is one call per game. +const AT_FILE = () => path.join(R.dataDir, 'game-payouts-alltime.json'); +let atState = null, atAt = 0, atBusy = null; +const sleep = ms => new Promise(r => setTimeout(r, ms)); +function atLoad() { if (!atState) { try { atState = JSON.parse(fs.readFileSync(AT_FILE(), 'utf8')); } catch (e) { atState = {}; } } return atState; } +function atSave() { try { fs.writeFileSync(AT_FILE() + '.tmp', JSON.stringify(atState)); fs.renameSync(AT_FILE() + '.tmp', AT_FILE()); } catch (e) {} } +async function atScan(slug, addr) { + const st = atLoad(); let w = st[slug]; + if (!w || w.addr !== addr) w = st[slug] = { addr, block: 0, count: 0, wei: '0', players: [] }; + const skip = SKIP(), players = new Set(w.players); let wei = BigInt(w.wei), count = w.count, maxBlock = w.block; // committed only when the scan finishes, so a failed page never double-counts + for (let page = 1; page <= 10; page++) { // Etherscan caps page*offset at 10,000; the next refresh carries on from the last block + const url = 'https://api.etherscan.io/v2/api?chainid=137&module=account&action=txlist&address=' + addr + '&startblock=' + (w.block + 1) + '&endblock=99999999&sort=asc&page=' + page + '&offset=1000&apikey=' + KEY(); + const r = await fetch(url, { signal: AbortSignal.timeout(20000) }); const j = await r.json(); + if (!Array.isArray(j.result)) { if (/no transactions/i.test(String(j.message || ''))) break; throw new Error(String(j.result || j.message || 'etherscan')); } + for (const x of j.result) { + const b = Number(x.blockNumber); if (b > maxBlock) maxBlock = b; + if (x.isError !== '0' || String(x.from).toLowerCase() !== addr || !x.value || x.value === '0') continue; + const to = String(x.to).toLowerCase(); if (skip.has(to)) continue; + count++; wei += BigInt(x.value); players.add(to); + } + if (j.result.length < 1000) break; + await sleep(400); + } + w.block = maxBlock; w.count = count; w.wei = wei.toString(); w.players = [...players]; delete w.error; +} +async function atRefresh() { + const W = wallets(); + for (const [slug, addr] of Object.entries(W)) { try { await atScan(slug, addr); } catch (e) { (atLoad()[slug] || {}).error = e.message; } await sleep(400); } + atSave(); atAt = Date.now(); +} +// { games: [{ slug, name, count, wei, players }], totals, at }; refreshed at most every 10 minutes, never blocks the page past 25 s +async function allTime() { + if (!KEY()) return { error: 'ETHERSCAN_KEY not set' }; + if (Date.now() - atAt > 600000 && !atBusy) atBusy = atRefresh().finally(() => { atBusy = null; }); + if (atBusy && !atAt) await Promise.race([atBusy, sleep(25000)]); + const st = atLoad(), W = wallets(), games = []; + for (const slug of Object.keys(W)) { const w = st[slug]; if (!w) continue; games.push({ slug, name: NAMES[slug] || slug, count: w.count, wei: w.wei, players: w.players.length, error: w.error || null }); } + games.sort((a, b) => (BigInt(b.wei) > BigInt(a.wei) ? 1 : -1)); + return { games, at: atAt || null, scanning: !!atBusy }; +} +module.exports = { init, handoff, gather, allTime, NAMES }; diff --git a/public/admin.html b/public/admin.html index 9d51782..7af8735 100644 --- a/public/admin.html +++ b/public/admin.html @@ -462,9 +462,11 @@
Marketing spend, paid in POL from the PolHunter wallet. None of it passes through the ad contract, so it is not in the lines above.
+Marketing spend: every POL send from each game's own payout wallet to a player, read from Polygon since the wallet's first transaction. Top-ups, moves between our wallets and returns to the fee wallets are left out. None of it passes through the ad contract, so it is not in the lines to the left.
+