9809953ae0
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>
75 lines
6.8 KiB
JavaScript
75 lines
6.8 KiB
JavaScript
/* Spooky Doors door: one verified visit (a member's page or ad), then back to the street with a signed receipt. */
|
||
(function () {
|
||
'use strict';
|
||
var $ = function (id) { return document.getElementById(id); };
|
||
var q = new URLSearchParams(location.search); var tok = q.get('t') || '';
|
||
var nonce = '', tokRet = ''; try { var claims = JSON.parse(atob(tok.split('.')[0].replace(/-/g, '+').replace(/_/g, '/'))); nonce = claims.nonce || ''; tokRet = claims.ret || ''; } catch (e) {}
|
||
// every dead end on this page keeps a way back to the street
|
||
var gameHome = tokRet ? tokRet.split('?')[0] : 'https://spookydoors.com/play';
|
||
var dwell = 8, left = 8, timer = null;
|
||
function esc(s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]; }); }
|
||
function msg(title, text, backUrl, auto) {
|
||
$('sdFrame').innerHTML = '<div class="dr-msg"><div class="in"><h1>' + esc(title) + '</h1><p>' + esc(text) + '</p></div></div>';
|
||
$('sdDone').hidden = true; $('sdCheck').hidden = true; $('sdRow').hidden = true;
|
||
var b = $('sdBack'); b.hidden = false; var to = backUrl || gameHome; b.textContent = 'Back to the street'; b.onclick = function () { location.href = to; };
|
||
if (auto && backUrl) setTimeout(function () { location.href = backUrl; }, 2500);
|
||
}
|
||
function api(p, body) { return fetch(p, body ? { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) } : {}).then(function (r) { return r.json(); }); }
|
||
function ring() { $('sdN').textContent = left; $('sdRing').style.setProperty('--p', ((dwell - left) / dwell * 100) + '%'); }
|
||
function tick() {
|
||
if (document.hidden) return; // the timer only runs while the page is in front
|
||
var vid = document.getElementById('sdVideo'); if (vid && (vid.paused || vid.ended && left > 1)) return; // a video must actually be playing
|
||
left--; if (left > 0) return ring();
|
||
clearInterval(timer); ring(); $('sdHint').textContent = 'One quick check to count the visit:';
|
||
challenge();
|
||
}
|
||
function challenge() {
|
||
api('/api/door/challenge?n=' + encodeURIComponent(nonce)).then(function (c) {
|
||
if (c.early) { setTimeout(challenge, (c.wait || 1) * 1000 + 300); return; }
|
||
if (c.error) { $('sdHint').textContent = c.error; return; }
|
||
$('sdPrompt').textContent = 'Tap the ' + c.prompt + ':'; var w = $('sdOpts'); w.innerHTML = '';
|
||
c.options.forEach(function (em, i) { var b = document.createElement('button'); b.type = 'button'; b.textContent = em; b.setAttribute('aria-label', 'Option ' + (i + 1)); b.addEventListener('click', function () { answer(i); }); w.appendChild(b); });
|
||
$('sdCheck').hidden = false;
|
||
});
|
||
}
|
||
function answer(i) {
|
||
api('/api/door/done', { n: nonce, answer: i }).then(function (r) {
|
||
if (r.error) {
|
||
if (r.retry) return challenge();
|
||
if (r.cap && r.ret) return msg('That is tonight’s doors', 'You have knocked on every door for today. Your candy is safe, and the doors open again at midnight Central.', r.ret, true);
|
||
$('sdHint').textContent = r.error; $('sdCheck').hidden = true; var bb = $('sdBack'); bb.hidden = false; bb.onclick = function () { location.href = gameHome; }; return;
|
||
}
|
||
$('sdCheck').hidden = true; $('sdHint').textContent = r.house ? 'Visit counted. A smaller treat from ' + (r.program || 'the house') + ' is on its way.' : r.host ? 'Visit counted. @' + r.host + ' has a treat for you.' : 'Visit counted. Someone has a treat for you.';
|
||
var b = $('sdDone'); b.disabled = false; b.textContent = 'Back to the street'; b.onclick = function () { location.href = r.ret; };
|
||
setTimeout(function () { location.href = r.ret; }, 1200);
|
||
});
|
||
}
|
||
if (!tok) return msg('No door here', 'Open this page by knocking on a door in Spooky Doors.');
|
||
api('/api/door/open?t=' + encodeURIComponent(tok)).then(function (r) {
|
||
if (r.error) return msg('Door link problem', r.error);
|
||
if (r.done) { location.href = r.ret; return; }
|
||
if (r.none) return r.why === 'cap'
|
||
? msg('That is tonight’s doors', 'You have knocked on every door for today. Your candy is safe, and the doors open again at midnight Central. Your regular InstantAdPay visits are not affected.', r.ret, true)
|
||
: msg('Nobody’s home', 'The lights are out at this house: there are no member pages or videos left for you right now. No candy here, and this door does not count. Head back and try the next house.', r.ret, true);
|
||
dwell = r.dwell || 8; left = Math.max(1, dwell - (r.elapsed || 0));
|
||
if (r.doors && r.doors.cap) { var ch = $('sdDoors'); ch.textContent = 'Door ' + Math.min(r.doors.cap, (r.doors.used || 0) + 1) + ' of ' + r.doors.cap; ch.hidden = false; }
|
||
var who = r.house ? 'House ad' : r.host ? '@' + r.host + '’s door' : 'A member’s door';
|
||
if (r.mode === 'video') { // no member pages left: a member video ad instead
|
||
$('sdTitle').textContent = who + ': ' + (r.title || 'member video'); $('sdUrl').textContent = 'video ad';
|
||
$('sdHint').textContent = 'You have visited every member page running right now, so this door is a short member video. Keep it playing.';
|
||
var v = document.createElement('video'); v.id = 'sdVideo'; v.src = r.video.url; v.autoplay = true; v.muted = true; v.playsInline = true; v.setAttribute('playsinline', ''); v.loop = true; v.controls = false;
|
||
v.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;object-fit:contain;background:#000';
|
||
$('sdFrame').innerHTML = ''; $('sdFrame').appendChild(v);
|
||
var un = document.createElement('button'); un.type = 'button'; un.textContent = 'Tap for sound'; un.style.cssText = 'position:absolute;right:10px;bottom:10px;z-index:2;padding:8px 12px;border-radius:8px;border:0;background:rgba(0,0,0,.6);color:#fff;font:600 12px system-ui';
|
||
un.onclick = function () { v.muted = false; v.play().catch(function () {}); un.remove(); }; $('sdFrame').appendChild(un);
|
||
v.play().catch(function () { $('sdHint').textContent = 'Tap the video to start it. The timer runs while it plays.'; v.onclick = function () { v.play(); }; });
|
||
ring(); timer = setInterval(tick, 1000); return;
|
||
}
|
||
$('sdTitle').textContent = who + ': ' + (r.title || 'member site'); try { $('sdUrl').textContent = new URL(r.target).hostname; } catch (e) {}
|
||
if (r.house) $('sdHint').textContent = 'A house ad: a smaller treat while the street’s ads are resting. Stay on this page.';
|
||
var f = document.createElement('iframe'); f.src = r.target; f.setAttribute('referrerpolicy', 'no-referrer'); f.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-forms allow-popups'); $('sdFrame').innerHTML = ''; $('sdFrame').appendChild(f);
|
||
var o = $('sdOpen'); o.href = r.target; o.hidden = false;
|
||
ring(); timer = setInterval(tick, 1000);
|
||
}).catch(function () { msg('Could not open the door', 'Check your connection and go back to the street.'); });
|
||
})();
|