diff --git a/lib/tournament.js b/lib/tournament.js index 2466c90..fc84d06 100644 --- a/lib/tournament.js +++ b/lib/tournament.js @@ -21,7 +21,7 @@ const ctDay = () => new Date().toLocaleDateString('en-CA', { timeZone: 'America/ const r4 = n => Math.round(Number(n) * 10000) / 10000; const keyOf = row => row.id != null ? 'id:' + row.id : 'wk:' + row.week + ':' + row.place; const claims = () => store.read('tourney-claims', {}); -function claim(k, row, now) { let got = false; store.update('tourney-claims', {}, all => { const c = all[k]; if (!c || (!c.tx && !c.dry && now - (c.claimedAt || 0) >= CLAIM_TTL)) { all[k] = { claimedAt: now, week: row.week, place: row.place, memberId: row.memberId || 0, email: row.email || null, wallet: row.wallet, pol: r4(row.pol), tries: ((c && c.tries) || 0) + 1 }; got = true; } return all; }); return got; } +function claim(k, row, now) { let got = false; store.update('tourney-claims', {}, all => { const c = all[k]; if (!c || (c.dry && !c.tx && !deps.dry()) || (!c.tx && !c.dry && now - (c.claimedAt || 0) >= CLAIM_TTL)) { all[k] = { claimedAt: now, week: row.week, place: row.place, memberId: row.memberId || 0, email: row.email || null, wallet: row.wallet, pol: r4(row.pol), tries: ((c && c.tries) || 0) + 1 }; got = true; } return all; }); return got; } const mark = (k, patch) => store.update('tourney-claims', {}, all => { if (all[k]) Object.assign(all[k], patch); return all; }); const release = (k, patch) => store.update('tourney-claims', {}, all => { if (all[k] && !all[k].tx) { Object.assign(all[k], patch || {}); all[k].claimedAt = 0; } return all; }); // free for the next run let running = false; @@ -41,7 +41,7 @@ async function run(now) { if (c && c.tx) { // paid before (this copy or the other one, or before a restart): IAP still lists it, so report the hash again const ack = await gp.tournamentPaid(row, c.tx); if (ack.ok) { mark(k, { acked: now }); st.acked++; } else st.failures.push({ k, error: 'paid report failed: ' + (ack.error || ack.status) }); continue; } - if (c && c.dry) { st.skipped++; st.wouldPay = r4(st.wouldPay + pol); continue; } // recorded in a dry run already + if (c && c.dry && deps.dry()) { st.skipped++; st.wouldPay = r4(st.wouldPay + pol); continue; } // recorded in a dry run already (and still dry); once live, the row is paid if (c && !c.tx && now - (c.claimedAt || 0) < CLAIM_TTL) { st.skipped++; continue; } // the other app copy is on it right now if (!claim(k, row, now)) { st.skipped++; continue; } // claimed BEFORE anything is sent if (deps.dry()) { mark(k, { dry: true, wouldPay: pol, at: now }); st.wouldPay = r4(st.wouldPay + pol); continue; }