Spooky Doors house ads: an empty street serves one of Marty's programs instead of "Nobody's home"
When a door finds no visit pack and no video, /api/door/open serves a house ad from rates.doorHouseAds (RM Circle, Drip Runner, Gem Kingdom, PolHunter by default; ref templates carry the player's IAP username where the program takes a share link). Rotation: never the same program twice in a row, else the one least recently shown to that member today. Same dwell + Halloween icon check. No advertiser charge and no campaign imps, but it counts toward doorCapPerDay (visit_repeat row with campaign 0). Callback and receipt add house: true and program, with credits 0, host null, campaignId 0. doorHouseAds = [] restores none=1. Admin rates table keeps text columns as text so the list survives a save. qa/door.mjs covers it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1311,7 +1311,7 @@ const doorTokens = persistedMap('door'); // nonce -> { kind, email, memberId, id
|
||||
const sdOrders = persistedMap('sdorders'); // order -> { status: 'paying'|'paid', email, campaignId, ts }
|
||||
const sdIsGameUrl = x => String(x || '').startsWith(SD_URL() + '/');
|
||||
const sdJoin = (ret, q) => ret + (String(ret).includes('?') ? '&' : '?') + q; // ret is SD_URL/play?door=<nonce>, so this appends &...
|
||||
async function sdCallback(body) { // door done: POST {nonce, memberId, campaignId, credits, host}, X-SD-Sig = hex HMAC(secret, ts + "\n" + rawBody)
|
||||
async function sdCallback(body) { // door done: POST {nonce, memberId, campaignId, credits, host} (+ house, program on a house ad), X-SD-Sig = hex HMAC(secret, ts + "\n" + rawBody)
|
||||
const raw = JSON.stringify(body); const ts = String(Date.now());
|
||||
const sig = crypto.createHmac('sha256', SD_SECRET()).update(ts + '\n' + raw).digest('hex');
|
||||
try { const r = await fetch(SD_URL() + '/api/door/callback', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-SD-Ts': ts, 'X-SD-Sig': sig }, body: raw, signal: AbortSignal.timeout(8000) }); return r.ok; }
|
||||
@@ -1325,6 +1325,25 @@ async function sdShopCallback(cbUrl, receipt) {
|
||||
async function sdHost(campaignId) {
|
||||
try { const c = await ads.campaignById(campaignId); if (!c || !c.owner) return null; const a = await accounts.byEmail(String(c.owner).toLowerCase()); return (a && a.username) || null; } catch (e) { return null; }
|
||||
}
|
||||
// house ads (2026-10-02): when a door finds no visit pack and no video, one of Marty's own programs (rates.doorHouseAds)
|
||||
// instead of "Nobody's home". Never the same one twice in a row for a member; otherwise the one least recently shown to
|
||||
// them today. The player's own IAP username rides along where the program takes a share link (ref with {u}).
|
||||
const doorHouse = persistedMap('doorhouse'); // email -> { ts, day, last, shown: { program: ts } }
|
||||
function doorHouseList() {
|
||||
const l = ads.rates().doorHouseAds;
|
||||
return (Array.isArray(l) ? l : []).filter(x => x && String(x.name || '').trim() && /^https:\/\/[^\s"'<>]+$/i.test(String(x.url || ''))).slice(0, 20);
|
||||
}
|
||||
async function doorHousePick(email, claimedUsername) {
|
||||
const list = doorHouseList(); if (!list.length) return null;
|
||||
const day = todayCT(); const prev = doorHouse.get(email) || {}; const shown = prev.day === day ? Object.assign({}, prev.shown) : {};
|
||||
const pool = list.length > 1 ? list.filter(x => x.name !== prev.last) : list;
|
||||
const h = pool.slice().sort((a, b) => (shown[a.name] || 0) - (shown[b.name] || 0))[0]; // never shown today sorts first, list order breaks ties
|
||||
shown[h.name] = Date.now(); doorHouse.set(email, { ts: Date.now(), day, last: h.name, shown });
|
||||
let username = null; try { const a = await accounts.byEmail(email); username = (a && a.username) || null; } catch (e) {}
|
||||
if (!username && claimedUsername) username = String(claimedUsername);
|
||||
const withRef = username && /^[A-Za-z0-9_.-]{1,40}$/.test(username) && /^https:\/\/[^\s"'<>]*\{u\}/i.test(String(h.ref || ''));
|
||||
return { name: String(h.name).trim().slice(0, 60), title: String(h.title || h.name).trim().slice(0, 120), target: withRef ? String(h.ref).replace('{u}', encodeURIComponent(username)) : String(h.url) };
|
||||
}
|
||||
function sdPage(res, code, title, inner) {
|
||||
res.writeHead(code, baseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' }));
|
||||
res.end('<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex,nofollow"><title>' + gkEsc(title) + ' | InstantAdPay</title>'
|
||||
@@ -2806,14 +2825,19 @@ const server = http.createServer(async (req, res) => {
|
||||
if (ad) t = { kind: 'visit', email, memberId: Number(c.memberId) || 0, id: ad.id, title: ad.title, target: ad.target, ts: Date.now(), ret: c.ret, challenge: null, done: false };
|
||||
else { // every visit pack already visited: a member video ad instead, one door per video per person
|
||||
const v = await ads.serveVideo(Object.assign({ excludeEmail: email, refuel: true }, viewerGeo(req)));
|
||||
if (!v) return json(res, 200, { none: true, why: 'empty', ret: sdJoin(c.ret, 'none=1') });
|
||||
t = { kind: 'video', email, memberId: Number(c.memberId) || 0, id: v.id, title: v.title, videoUrl: v.videoUrl, secs: v.watchSecs, width: v.width, height: v.height, ts: Date.now(), ret: c.ret, challenge: null, done: false };
|
||||
if (v) t = { kind: 'video', email, memberId: Number(c.memberId) || 0, id: v.id, title: v.title, videoUrl: v.videoUrl, secs: v.watchSecs, width: v.width, height: v.height, ts: Date.now(), ret: c.ret, challenge: null, done: false };
|
||||
else { // nothing left on the street: a house ad (one of Marty's programs, a smaller treat); none=1 only when the list is empty
|
||||
const h = await doorHousePick(email, c.username);
|
||||
if (!h) return json(res, 200, { none: true, why: 'empty', ret: sdJoin(c.ret, 'none=1') });
|
||||
t = { kind: 'house', email, memberId: Number(c.memberId) || 0, id: 0, program: h.name, title: h.title, target: h.target, ts: Date.now(), ret: c.ret, challenge: null, done: false };
|
||||
}
|
||||
}
|
||||
t.host = await sdHost(t.id);
|
||||
t.host = t.kind === 'house' ? null : await sdHost(t.id);
|
||||
doorTokens.set(c.nonce, t);
|
||||
}
|
||||
const doors = { used: status.count, cap: status.cap };
|
||||
if (t.kind === 'video') return json(res, 200, { mode: 'video', title: t.title, host: t.host || null, doors, video: { url: t.videoUrl, width: t.width, height: t.height }, dwell: t.secs, ret: t.ret, elapsed: Math.floor((Date.now() - t.ts) / 1000) });
|
||||
if (t.kind === 'house') return json(res, 200, { mode: 'visit', house: true, program: t.program, title: t.title, host: null, doors, target: t.target, dwell: ads.rates().visitDwellSeconds || 8, ret: t.ret, elapsed: Math.floor((Date.now() - t.ts) / 1000) });
|
||||
return json(res, 200, { mode: 'visit', title: t.title, host: t.host || null, doors, target: t.target, dwell: ads.rates().visitDwellSeconds || 8, ret: t.ret, elapsed: Math.floor((Date.now() - t.ts) / 1000) });
|
||||
}
|
||||
if (p === '/api/door/challenge' && req.method === 'GET') {
|
||||
@@ -2838,6 +2862,10 @@ const server = http.createServer(async (req, res) => {
|
||||
const tier = await ads.chargeVideoView(t.id); // the advertiser is charged one view, as on the Watch tab
|
||||
if (!tier) return json(res, 400, { error: 'That video just ran out of budget. Go back and try another door.' });
|
||||
await ads.markVideoSeen(t.email, t.id); credits = tier.cost;
|
||||
} else if (t.kind === 'house') { // no advertiser and no campaign imps, but it uses one of today's doors
|
||||
const r = await ads.markHouseDoor(t.email);
|
||||
if (r.error) return json(res, 400, { error: r.error, cap: true, ret: sdJoin(t.ret, 'cap=1') });
|
||||
credits = 0;
|
||||
} else {
|
||||
const r = await ads.completeVisit(t.email, t.id, { reward: false, kind: 'door', note: 'Spooky Doors door' });
|
||||
if (r.error) return json(res, 400, r);
|
||||
@@ -2845,11 +2873,12 @@ const server = http.createServer(async (req, res) => {
|
||||
}
|
||||
t.done = true;
|
||||
const host = t.host || null;
|
||||
const posted = await sdCallback({ nonce, memberId: t.memberId, campaignId: t.id, credits, host });
|
||||
const receipt = sdMint({ kind: 'door-receipt', nonce, memberId: t.memberId, campaignId: t.id, credits, host }, 24 * 3600000);
|
||||
const house = t.kind === 'house' ? { house: true, program: t.program } : {}; // a house ad: credits 0, host null, campaignId 0
|
||||
const posted = await sdCallback(Object.assign({ nonce, memberId: t.memberId, campaignId: t.id, credits, host }, house));
|
||||
const receipt = sdMint(Object.assign({ kind: 'door-receipt', nonce, memberId: t.memberId, campaignId: t.id, credits, host }, house), 24 * 3600000);
|
||||
t.doneRet = sdJoin(t.ret, 'ok=1&r=' + encodeURIComponent(receipt));
|
||||
doorTokens.set(nonce, t);
|
||||
return json(res, 200, { ok: true, posted, host, ret: t.doneRet });
|
||||
return json(res, 200, Object.assign({ ok: true, posted, host, ret: t.doneRet }, house));
|
||||
}
|
||||
// -- Games hub (2026-10-01): the registry is here, the live list is site config (gamesLive)
|
||||
if (p === '/api/my/games' && req.method === 'GET') {
|
||||
|
||||
Reference in New Issue
Block a user