Files
instantadpay/tools/patch_door_repeat.py
T

80 lines
6.2 KiB
Python

# 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 });"),
])