Game Pass bridge (GAMEPASS=on): finds and Ad Detective cases via InstantAdPay polgrant (daily, fail closed), 5 missions + 2 cases perks, Pass Tournament prize payer from this faucet (claims before send, dry run, alerts), tests
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
+25
-9
@@ -83,7 +83,9 @@ function order(box, q) { return q.options.map((o, i) => ({ i, k: h(box.id + ':'
|
||||
|
||||
// ---- the box ----------------------------------------------------------------------------------
|
||||
const boxId = (memberId, day) => Number(memberId) + ':' + day;
|
||||
function getBox(memberId, day) { return store.read('boxes', {})[boxId(memberId, day || ctDay())] || null; }
|
||||
// the day's current box: the second one (the Game Pass perk, id '<member>:<day>:2') once it exists, else the first
|
||||
function getBox(memberId, day) { const all = store.read('boxes', {}), id = boxId(memberId, day || ctDay()); return all[id + ':2'] || all[id] || null; }
|
||||
const closed = b => !!b && (b.status === 'won' || b.status === 'missed');
|
||||
function putBox(b) { store.update('boxes', {}, all => { all[b.id] = b; return all; }); return b; }
|
||||
function patchBox(id, patch) { let out = null; store.update('boxes', {}, all => { if (all[id]) { Object.assign(all[id], patch); out = all[id]; } return all; }); return out; }
|
||||
|
||||
@@ -105,13 +107,16 @@ function view(me) {
|
||||
return out;
|
||||
}
|
||||
|
||||
function open(me) {
|
||||
// pass = the Game Pass perk (GAME-PASS.md 4): a second case a day once the first is closed; the first case stays the rule for everyone else
|
||||
function open(me, pass) {
|
||||
if (!earned(me)) return { error: 'Finish today’s Code Hunts first. The box unlocks when your last one is found.' };
|
||||
const day = ctDay(); const cur = getBox(me.memberId, day); if (cur) return { ok: true, box: cur };
|
||||
const day = ctDay(); const cur = getBox(me.memberId, day);
|
||||
const second = !!(pass && cur && closed(cur) && !/:2$/.test(cur.id));
|
||||
if (cur && !second) return { ok: true, box: cur };
|
||||
const c = pickCase(me.memberId, day); if (!c) return { error: 'No cases are open right now. Check back tomorrow.' };
|
||||
let made = null;
|
||||
// idempotent per member per day: a double tap or two tabs can never mint a second box
|
||||
store.update('boxes', {}, all => { const id = boxId(me.memberId, day); if (!all[id]) all[id] = { id, memberId: Number(me.memberId), day, missionId: c.missionId, qid: c.qid, pol: settings().pol, status: 'open', openedAt: Date.now() }; made = all[id]; return all; });
|
||||
store.update('boxes', {}, all => { const id = boxId(me.memberId, day) + (second ? ':2' : ''); if (!all[id]) all[id] = { id, memberId: Number(me.memberId), day, missionId: c.missionId, qid: c.qid, pol: settings().pol, status: 'open', openedAt: Date.now(), second: second || undefined }; made = all[id]; return all; });
|
||||
return { ok: true, box: made };
|
||||
}
|
||||
|
||||
@@ -134,7 +139,9 @@ function visited(rec) {
|
||||
store.update('boxes', {}, all => { const b = all[rec.boxId]; if (b && b.status === 'started' && b.token === rec.t) { b.status = 'visited'; b.visitedAt = Date.now(); } return all; });
|
||||
}
|
||||
|
||||
function answer(me, choice) {
|
||||
// gate (the Game Pass, GAME-PASS.md 3a): { ask(pol, ref) -> IAP's answer, wait(rowId, pol, ref) } set by server.js when GAMEPASS=on;
|
||||
// the case is scored first (the answer is final either way), then InstantAdPay decides what the account-wide cap allows
|
||||
async function answer(me, choice, gate) {
|
||||
const b = getBox(me.memberId, ctDay()); if (!b) return { error: 'No case open today.' };
|
||||
if (b.status === 'won' || b.status === 'missed') return { error: 'You already answered today’s case.' };
|
||||
if (b.status !== 'visited') return { error: 'Visit the site first. The question unlocks once your visit is recorded.' };
|
||||
@@ -147,17 +154,26 @@ function answer(me, choice) {
|
||||
if (!claimed) return { error: 'You already answered today’s case.' };
|
||||
const correct = ord[n] === 0; const at = Date.now();
|
||||
store.update('answers', [], all => { all.push({ memberId: Number(me.memberId), boxId: b.id, missionId: b.missionId, qid: q.id, correct, picked: q.options[ord[n]], at, ms: b.shownAt ? at - b.shownAt : null }); return all; });
|
||||
let g = null;
|
||||
let g = null, note = null;
|
||||
try {
|
||||
if (correct) {
|
||||
const m = missions.get(b.missionId) || { id: b.missionId, site: b.missionId };
|
||||
g = rewards.grantFixed(me, { missionId: 'detective:' + m.id, site: m.site, pol: b.pol, detective: { boxId: b.id, qid: q.id } });
|
||||
const t = { missionId: 'detective:' + m.id, site: m.site, pol: b.pol, detective: { boxId: b.id, qid: q.id } };
|
||||
if (gate) { // the Game Pass: a case is a repeatable ('daily') reward; what the account-wide cap does not allow is dropped, not deferred
|
||||
const ref = 'detective:' + b.id, r = await gate.ask(b.pol, ref);
|
||||
if (r.retry) { t.status = 'gp-wait'; t.gp = { ref, kind: 'daily', nominal: b.pol, pending: true }; t.nominal = b.pol; }
|
||||
else { t.pol = r.allowed; t.nominal = b.pol; t.gp = { ref, kind: 'daily', nominal: b.pol, pass: !!r.pass, capHit: r.capHit || null }; }
|
||||
note = r.note || null;
|
||||
g = rewards.grantFixed(me, t);
|
||||
if (r.retry && g && g.rec) gate.wait(g.rec.id, b.pol, ref);
|
||||
} else g = rewards.grantFixed(me, t);
|
||||
}
|
||||
} finally {
|
||||
// the answer is final either way; a grant that failed shows in the admin as a won box with no payout
|
||||
patchBox(b.id, { status: correct ? 'won' : 'missed', answeredAt: at, payoutId: g && g.rec ? g.rec.id : null });
|
||||
}
|
||||
return { ok: true, correct, pol: correct ? b.pol : 0, queued: !!(g && g.queued), rec: g && g.rec, answer: q.options[0] };
|
||||
const paid = correct && g && g.rec && g.rec.status !== 'gp-wait' ? g.rec.pol : 0; // a gp-wait row holds the nominal amount until IAP answers: nothing is paid yet
|
||||
return { ok: true, correct, pol: paid, nominal: b.pol, note, queued: !!(g && g.queued), waiting: !!(g && g.waiting), capped: !!(g && g.capped), rec: g && g.rec, answer: q.options[0] };
|
||||
}
|
||||
|
||||
// ---- stats for the admin -------------------------------------------------------------------------
|
||||
@@ -173,4 +189,4 @@ function stats() {
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { init, bank, saveQuestion, setActive, view, open, start, answer, visited, stats, pickCase, earned };
|
||||
module.exports = { init, bank, saveQuestion, setActive, view, open, start, answer, visited, stats, pickCase, earned, getBox };
|
||||
|
||||
Reference in New Issue
Block a user