Spooky Doors: doors may repeat a visit campaign after doorRepeatDays (7) via visit_repeat; Earn and refuels stay once-ever
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -217,6 +217,17 @@ async function bootstrap() {
|
|||||||
ts BIGINT NOT NULL,
|
ts BIGINT NOT NULL,
|
||||||
INDEX (campaign_id, ts)
|
INDEX (campaign_id, ts)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
|
||||||
|
// repeat door visits after the cooldown (Spooky Doors, 2026-10-02); visit_seen keeps its one-row-per-campaign rule
|
||||||
|
await q(`CREATE TABLE IF NOT EXISTS visit_repeat (
|
||||||
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
campaign_id INT NOT NULL,
|
||||||
|
email VARCHAR(190) NOT NULL,
|
||||||
|
day CHAR(10) NOT NULL,
|
||||||
|
ts BIGINT NOT NULL,
|
||||||
|
kind VARCHAR(8) NOT NULL DEFAULT 'door',
|
||||||
|
INDEX (email, day, kind),
|
||||||
|
INDEX (email, campaign_id)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
|
||||||
await q(`CREATE TABLE IF NOT EXISTS visit_seen (
|
await q(`CREATE TABLE IF NOT EXISTS visit_seen (
|
||||||
campaign_id INT NOT NULL,
|
campaign_id INT NOT NULL,
|
||||||
email VARCHAR(190) NOT NULL,
|
email VARCHAR(190) NOT NULL,
|
||||||
|
|||||||
@@ -105,6 +105,13 @@ try {
|
|||||||
const o2 = (await api('/api/door/open?t=' + encodeURIComponent(mint({ kind: 'door', memberId: 77, email: player, nonce: n2, ret: SD + '/play?door=' + n2 })))).j;
|
const o2 = (await api('/api/door/open?t=' + encodeURIComponent(mint({ kind: 'door', memberId: 77, email: player, nonce: n2, ret: SD + '/play?door=' + n2 })))).j;
|
||||||
ok(o2 && o2.none && o2.why === 'empty' && o2.ret === SD + '/play?door=' + n2 + '&none=1', 'nothing to serve -> ret&none=1', JSON.stringify(o2));
|
ok(o2 && o2.none && o2.why === 'empty' && o2.ret === SD + '/play?door=' + n2 + '&none=1', 'nothing to serve -> ret&none=1', JSON.stringify(o2));
|
||||||
|
|
||||||
|
// 2b. doors may repeat a campaign after doorRepeatDays (0 here = no cooldown): the same pack is served again
|
||||||
|
await api('/api/admin/rates', { method: 'PATCH', data: { doorRepeatDays: 0 } }, adminH);
|
||||||
|
const n2b = crypto.randomBytes(12).toString('hex');
|
||||||
|
const o2b = (await api('/api/door/open?t=' + encodeURIComponent(mint({ kind: 'door', memberId: 77, email: player, nonce: n2b, ret: SD + '/play?door=' + n2b })))).j;
|
||||||
|
ok(o2b && o2b.mode === 'visit', 'after the cooldown a door repeats an already-visited pack', JSON.stringify(o2b));
|
||||||
|
await api('/api/admin/rates', { method: 'PATCH', data: { doorRepeatDays: 7 } }, adminH);
|
||||||
|
|
||||||
// 3. the kinds stay separate: one door used, refuel cap 1 still open, Earn visits untouched
|
// 3. the kinds stay separate: one door used, refuel cap 1 still open, Earn visits untouched
|
||||||
const nr = crypto.randomBytes(12).toString('hex');
|
const nr = crypto.randomBytes(12).toString('hex');
|
||||||
const rf = (await api('/api/refuel/open?t=' + encodeURIComponent(mint({ kind: 'refuel', memberId: 77, email: player, nonce: nr, ret: SD + '/play?refuel=' + nr }, DRSECRET)))).j;
|
const rf = (await api('/api/refuel/open?t=' + encodeURIComponent(mint({ kind: 'refuel', memberId: 77, email: player, nonce: nr, ret: SD + '/play?refuel=' + nr }, DRSECRET)))).j;
|
||||||
|
|||||||
@@ -2802,7 +2802,7 @@ const server = http.createServer(async (req, res) => {
|
|||||||
const status = await ads.visitStatus(email, 'door'); // doors have their own daily allowance
|
const status = await ads.visitStatus(email, 'door'); // doors have their own daily allowance
|
||||||
if (!t) {
|
if (!t) {
|
||||||
if (status.count >= status.cap) return json(res, 200, { none: true, why: 'cap', ret: sdJoin(c.ret, 'cap=1') });
|
if (status.count >= status.cap) return json(res, 200, { none: true, why: 'cap', ret: sdJoin(c.ret, 'cap=1') });
|
||||||
const ad = await ads.serveVisit(email, viewerGeo(req));
|
const ad = await ads.serveVisit(email, viewerGeo(req), { kind: 'door' }); // doors may repeat a campaign after doorRepeatDays
|
||||||
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 };
|
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
|
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)));
|
const v = await ads.serveVideo(Object.assign({ excludeEmail: email, refuel: true }, viewerGeo(req)));
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
# Spooky Doors doors may repeat a visit campaign after doorRepeatDays (default 7) (Marty, 2026-10-02: doors kept
|
||||||
|
# falling back to videos because the once-ever rule had used up every campaign for active members).
|
||||||
|
# Earn and refuel keep the once-ever rule exactly. Repeats are logged in visit_repeat (visit_seen's unique key untouched).
|
||||||
|
import re
|
||||||
|
ROOT = 'D:/Projects/HighRisk/onchain-ad-membership/site/'
|
||||||
|
def edit(path, pairs):
|
||||||
|
b = open(ROOT + path, 'rb').read(); crlf = b.count(b'\r\n') > b.count(b'\n') // 2
|
||||||
|
s = b.decode('utf-8').replace('\r\n', '\n')
|
||||||
|
for a, r in pairs:
|
||||||
|
if s.count(a) != 1: raise SystemExit('%s anchor %d: %s' % (path, s.count(a), a[:90]))
|
||||||
|
s = s.replace(a, r)
|
||||||
|
if crlf: s = s.replace('\n', '\r\n')
|
||||||
|
open(ROOT + path, 'wb').write(s.encode('utf-8')); print('patched', path, 'crlf' if crlf else 'lf')
|
||||||
|
|
||||||
|
edit('db.js', [
|
||||||
|
(" await q(`CREATE TABLE IF NOT EXISTS visit_seen (",
|
||||||
|
" // repeat door visits after the cooldown (Spooky Doors, 2026-10-02); visit_seen keeps its one-row-per-campaign rule\n"
|
||||||
|
" await q(`CREATE TABLE IF NOT EXISTS visit_repeat (\n"
|
||||||
|
" id BIGINT AUTO_INCREMENT PRIMARY KEY,\n"
|
||||||
|
" campaign_id INT NOT NULL,\n"
|
||||||
|
" email VARCHAR(190) NOT NULL,\n"
|
||||||
|
" day CHAR(10) NOT NULL,\n"
|
||||||
|
" ts BIGINT NOT NULL,\n"
|
||||||
|
" kind VARCHAR(8) NOT NULL DEFAULT 'door',\n"
|
||||||
|
" INDEX (email, day, kind),\n"
|
||||||
|
" INDEX (email, campaign_id)\n"
|
||||||
|
" ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);\n"
|
||||||
|
" await q(`CREATE TABLE IF NOT EXISTS visit_seen ("),
|
||||||
|
])
|
||||||
|
|
||||||
|
edit('ads.js', [
|
||||||
|
# daily count: door repeats count toward the door cap
|
||||||
|
(" if (db.enabled()) { const r = await db.q('SELECT COUNT(*) n FROM visit_seen WHERE email=? AND day=? AND kind=?', [e, today(), kind]); return r[0].n; }\n if (!SVJ.db) SVJ.load();\n return SVJ.db.items.filter(i => i.email === e && i.day === today() && (i.kind || 'earn') === kind).length;",
|
||||||
|
" if (db.enabled()) { const r = await db.q('SELECT COUNT(*) n FROM visit_seen WHERE email=? AND day=? AND kind=?', [e, today(), kind]);\n"
|
||||||
|
" if (kind !== 'door') return r[0].n;\n"
|
||||||
|
" const r2 = await db.q('SELECT COUNT(*) n FROM visit_repeat WHERE email=? AND day=? AND kind=?', [e, today(), kind]); return Number(r[0].n) + Number(r2[0].n); }\n"
|
||||||
|
" if (!SVJ.db) SVJ.load();\n return SVJ.db.items.filter(i => i.email === e && i.day === today() && (i.kind || 'earn') === kind).length;"),
|
||||||
|
# serveVisit: doors fall back to campaigns last visited more than the cooldown ago
|
||||||
|
("async function serveVisit(viewerEmail, viewer) {\n const e = String(viewerEmail || '').toLowerCase();",
|
||||||
|
"// when did this viewer last visit each campaign (first visit or a door repeat)\n"
|
||||||
|
"async function lastVisits(e) {\n"
|
||||||
|
" const last = new Map();\n"
|
||||||
|
" if (db.enabled()) {\n"
|
||||||
|
" (await db.q('SELECT campaign_id, ts FROM visit_seen WHERE email=?', [e])).forEach(r => last.set(r.campaign_id, Number(r.ts)));\n"
|
||||||
|
" (await db.q('SELECT campaign_id, MAX(ts) ts FROM visit_repeat WHERE email=? GROUP BY campaign_id', [e])).forEach(r => last.set(r.campaign_id, Math.max(last.get(r.campaign_id) || 0, Number(r.ts))));\n"
|
||||||
|
" } else { if (!SVJ.db) SVJ.load(); SVJ.db.items.filter(i => i.email === e).forEach(i => last.set(i.cid, Math.max(last.get(i.cid) || 0, Number(i.ts) || 0))); }\n"
|
||||||
|
" return last;\n"
|
||||||
|
"}\n"
|
||||||
|
"const repeatMs = () => Math.max(1, Number(rates().doorRepeatDays) || 7) * 86400000;\n"
|
||||||
|
"async function serveVisit(viewerEmail, viewer, opts) {\n"
|
||||||
|
" const first = await serveVisitFirst(viewerEmail, viewer);\n"
|
||||||
|
" if (first || !(opts && opts.kind === 'door')) return first;\n"
|
||||||
|
" // doors only: every campaign already seen, so offer one last visited longer ago than the cooldown (the oldest first)\n"
|
||||||
|
" const e = String(viewerEmail || '').toLowerCase(), tier = viewer && viewer.tier, vcc = viewer && viewer.cc, cut = Date.now() - repeatMs();\n"
|
||||||
|
" const last = await lastVisits(e);\n"
|
||||||
|
" let pool;\n"
|
||||||
|
" if (db.enabled()) pool = (await db.q(\"SELECT * FROM campaigns WHERE type='visits' AND status='active' AND owner_email<>?\" + SCHED_SQL + GEO_SQL, [e, Date.now(), Date.now(), String(tier || '')])).map(rowC);\n"
|
||||||
|
" else pool = J.db.campaigns.filter(c => c.type === 'visits' && c.status === 'active' && c.owner !== e && inWindow(c) && geoOk(c, tier));\n"
|
||||||
|
" const ok = pool.filter(x => (x.imps || 0) < Number(x.body || 0) && last.has(x.id) && last.get(x.id) <= cut).sort((a, b) => last.get(a.id) - last.get(b.id));\n"
|
||||||
|
" const c = ok[0]; if (c) bumpGeo(c.id, vcc);\n"
|
||||||
|
" return c ? { id: c.id, title: c.title, url: '/api/ads/click/' + c.id, target: c.targetUrl, repeat: true } : null;\n"
|
||||||
|
"}\n"
|
||||||
|
"async function serveVisitFirst(viewerEmail, viewer) {\n const e = String(viewerEmail || '').toLowerCase();"),
|
||||||
|
# completeVisit (MySQL): a door may log a repeat when the first-visit row already exists and the cooldown has passed
|
||||||
|
(" try { await db.q('INSERT INTO visit_seen (campaign_id,email,day,ts,kind) VALUES (?,?,?,?,?)', [c.id, e, today(), Date.now(), kind]); }\n catch (er) { return { error: 'You already visited this one.' }; } // unique key",
|
||||||
|
" try { await db.q('INSERT INTO visit_seen (campaign_id,email,day,ts,kind) VALUES (?,?,?,?,?)', [c.id, e, today(), Date.now(), kind]); }\n"
|
||||||
|
" catch (er) { // unique key: already visited once. Doors may repeat after the cooldown; everything else stays once-ever\n"
|
||||||
|
" if (kind !== 'door') return { error: 'You already visited this one.' };\n"
|
||||||
|
" const lv = (await lastVisits(e)).get(c.id) || 0; if (Date.now() - lv < repeatMs()) return { error: 'You already visited this one.' };\n"
|
||||||
|
" await db.q('INSERT INTO visit_repeat (campaign_id,email,day,ts,kind) VALUES (?,?,?,?,?)', [c.id, e, today(), Date.now(), kind]);\n"
|
||||||
|
" }"),
|
||||||
|
# completeVisit (JSON store): same rule
|
||||||
|
(" if (SVJ.db.items.some(i => i.cid === c.id && i.email === e)) return { error: 'You already visited this one.' };\n SVJ.db.items.push({ cid: c.id, email: e, day: today(), ts: Date.now(), kind });",
|
||||||
|
" if (SVJ.db.items.some(i => i.cid === c.id && i.email === e)) {\n"
|
||||||
|
" if (kind !== 'door') return { error: 'You already visited this one.' };\n"
|
||||||
|
" const lv = (await lastVisits(e)).get(c.id) || 0; if (Date.now() - lv < repeatMs()) return { error: 'You already visited this one.' };\n"
|
||||||
|
" }\n"
|
||||||
|
" SVJ.db.items.push({ cid: c.id, email: e, day: today(), ts: Date.now(), kind });"),
|
||||||
|
])
|
||||||
Reference in New Issue
Block a user