Files
instantadpay/wednesday.js
T

161 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Wired Wednesday (Marty, 2026-09-29). Every Wednesday, Central time, any ad package of $10 or more
// earns +25% bonus credits that are WIRED straight into a verified-visits campaign pointed at the
// buyer's invite link: the bonus is running before they close the tab, and the network's daily
// earners get fresh visit inventory (the one format that keeps selling out). Credited the moment
// the purchase indexes, labelled in the credit log, campaign named after the promo.
//
// Same bones as Five Dollar Friday (friday.js): Central-day window, once per tx, the day's wave on
// the bridge page and the Overview card, Telegram at every ten packs, a wrap-up after midnight.
// The Tuesday notice + email and the Wednesday kickoff post are OFF until Marty approves the copy
// (wednesdayNotice / wednesdayKick settings), because both are outward-facing.
//
// Settings (siteConfig): wednesdayPromo '1'|'0', wednesdayBonusPct (25), wednesdayMinCents (1000),
// wednesdayStart 'YYYY-MM-DD' (first Wednesday, 2026-09-30), wednesdayNotice '1'|'0', wednesdayKick '1'|'0'.
'use strict';
const fs = require('fs');
const path = require('path');
let X = {}, FILE = null, S = null;
const TZ = 'America/Chicago';
const SITE = 'https://instantadpay.com';
const dayOf = ts => new Date(ts).toLocaleDateString('en-CA', { timeZone: TZ });
const weekdayOf = ts => new Date(ts).toLocaleDateString('en-US', { timeZone: TZ, weekday: 'short' });
const hourOf = ts => Number(new Date(ts).toLocaleString('en-US', { timeZone: TZ, hour: 'numeric', hour12: false }));
const isWednesday = ts => weekdayOf(ts) === 'Wed';
function init(opts) { X = opts; FILE = path.join(opts.dataDir, 'wednesday.json'); S = load(); }
function load() { try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); } catch (e) { return { granted: {}, posts: {}, sent: {} }; } }
function save() { try { fs.writeFileSync(FILE, JSON.stringify(S)); } catch (e) {} }
// shared across the two app copies that overlap during a deploy (marks.js); true when nobody else has it
async function claimOnce(key) { try { return X.marks ? await X.marks.claim(key) : true; } catch (e) { return true; } }
function cfg() {
const c = (X.siteConfig && X.siteConfig()) || {};
return {
on: String(c.wednesdayPromo == null ? '1' : c.wednesdayPromo) === '1',
pct: Math.max(0, Number(c.wednesdayBonusPct) || 25),
minCents: Math.max(0, Number(c.wednesdayMinCents) || 1000),
start: String(c.wednesdayStart || '2026-09-30'),
notice: String(c.wednesdayNotice || '0') === '1',
kick: String(c.wednesdayKick || '0') === '1',
};
}
function nextWednesday(now) {
const t0 = now || Date.now();
for (let d = 0; d < 8; d++) { const ts = t0 + d * 86400000; if (isWednesday(ts) && dayOf(ts) >= cfg().start) return dayOf(ts); }
return null;
}
function labelOf(dayKey) { const [y, m, d] = dayKey.split('-').map(Number); return new Date(Date.UTC(y, m - 1, d, 12)).toLocaleDateString('en-US', { month: 'long', day: 'numeric', timeZone: 'UTC' }); }
function liveNow(now) { const c = cfg(); const t = now || Date.now(); return c.on && isWednesday(t) && dayOf(t) >= c.start; }
// every qualifying purchase on a Central day, from the chain index (never a marketing counter)
function purchasesOn(dayKey) {
const min = cfg().minCents, out = [];
try { for (const ev of X.chain.recentEvents(1e9)) if (ev.type === 'Purchase' && ev.priceCents >= min && dayOf(ev.ts) === dayKey) out.push(ev); } catch (e) {}
return out;
}
function paidOn(dayKey) {
let wei = 0n, n = 0;
try { for (const ev of X.chain.recentEvents(1e9)) if ((ev.type === 'TierPaid' || ev.type === 'AwardPaid') && dayOf(ev.ts) === dayKey) { wei += BigInt(ev.amountWei); n++; } } catch (e) {}
return { pol: Number(wei / 10n ** 14n) / 10000, n };
}
function stats(dayKey) {
const day = dayKey || dayOf(Date.now());
const p = purchasesOn(day); const paid = paidOn(day);
const usd = p.reduce((s, e) => s + e.priceCents, 0) / 100;
const buyers = new Set(p.map(e => e.buyerId)).size;
const g = Object.values(S.granted || {}).filter(x => x.day === day);
return { day, packs: p.length, usd, buyers, polPaid: paid.pol, payouts: paid.n,
bonusCredits: g.reduce((s, x) => s + x.bonus, 0), visits: g.reduce((s, x) => s + (x.visits || 0), 0), campaigns: g.filter(x => x.campaignId).length };
}
function view() {
const c = cfg(); const now = Date.now(); const live = liveNow(now); const next = nextWednesday(now);
return { on: c.on, pct: c.pct, minUsd: c.minCents / 100, live, today: live ? stats(dayOf(now)) : null, nextWednesday: next, nextLabel: next ? labelOf(next) : null, start: c.start };
}
// the bonus: once per purchase tx, on a Wednesday on or after the start, packs of $10 and up.
// Credits are added, then the same amount is spent on a verified-visits pack aimed at the buyer's
// invite link, so the member's own balance is untouched and the bonus is live immediately. If the
// pack cannot be created (below the visit minimum, or a validation refusal) the credits stay on
// the balance and the notice says so; nothing is ever lost.
async function onEvent(ev) {
if (!ev || ev.type !== 'Purchase' || !liveNow(ev.ts) || ev.priceCents < cfg().minCents) return null;
if (S.granted[ev.tx]) return null;
const c = cfg(); const bonus = Math.round((Number(ev.creditAmount) || 0) * c.pct / 100);
if (bonus <= 0) return null;
const a = await X.accounts.byMemberId(ev.buyerId); if (!a || !a.email) return null;
const usd = ('$' + (ev.priceCents / 100).toFixed(2)).replace(/\.00$/, '');
const ref = 'ww:' + String(ev.tx).slice(0, 30);
await X.ads.addEarned(a.email, bonus, { log: { kind: 'bonus', note: 'Wired Wednesday: +' + c.pct + '% on your ' + usd + ' package, wired into a verified-visits campaign', ref } });
S.granted[ev.tx] = { day: dayOf(ev.ts), memberId: ev.buyerId, bonus, at: Date.now() }; save();
// wire it: a visits pack for as many verified visits as the bonus buys
const perVisit = Number((X.rates && X.rates().visitCostPerVisit) || 3);
const count = Math.floor(bonus / perVisit);
const link = SITE + '/join/' + (a.username || a.code || ev.buyerId);
let made = null, why = '';
try {
const r = await X.ads.createCampaign(a.email, ev.buyerId, { type: 'visits', name: 'Wired Wednesday: verified visits to my invite', targetUrl: link, count, title: 'Wired Wednesday bonus' }, [ev.buyerId]);
if (r && !r.error) { made = r.campaign || r; S.granted[ev.tx].campaignId = made.id; S.granted[ev.tx].visits = count; save(); }
else why = (r && r.error) || 'could not create the campaign';
} catch (e) { why = e.message; }
const text = made
? 'Your ' + usd + ' package landed on a Wired Wednesday, so ' + bonus + ' bonus credits (+' + c.pct + '%) were wired straight into a verified-visits campaign: ' + count + ' real visits to your invite link, already running. Watch it under Campaigns: ' + SITE + '/my#campaigns'
: 'Your ' + usd + ' package landed on a Wired Wednesday, so ' + bonus + ' bonus credits (+' + c.pct + '%) were added to your balance. We could not start the visits campaign for you automatically (' + why + '), so the credits are yours to spend on any format: ' + SITE + '/my#campaigns';
try { await X.notify(ev.buyerId, 'Wired Wednesday: +' + bonus + ' bonus credits' + (made ? ', already running' : ''), text, 'notice'); } catch (e) {}
if (!made) console.warn('wednesday: campaign not created for member', ev.buyerId, why);
// the wave, every ten packs
const st = stats(dayOf(ev.ts));
if (st.packs > 0 && st.packs % 10 === 0 && !S.posts[st.day + ':' + st.packs]) {
S.posts[st.day + ':' + st.packs] = Date.now(); save();
X.telegram('\u{26A1} <b>Wired Wednesday</b> · <b>' + st.packs + ' packs</b> so far today from ' + st.buyers + ' buyers, <b>' + st.visits.toLocaleString() + ' verified visits</b> wired to their invite pages, <b>' + st.polPaid.toFixed(2) + ' POL</b> paid to members in the same transactions.\nEvery $' + (c.minCents / 100) + '+ pack today gets +' + c.pct + '% wired in: ' + SITE + '/my#buy').catch(() => {});
}
return bonus;
}
// Tuesday 18:00 CT notice + email (when approved), Wednesday 09:00 CT kickoff (when approved),
// Thursday 00:xx CT wrap-up of the Wednesday that just ended
async function tick() {
const c = cfg(); if (!c.on) return;
const now = Date.now(); const day = dayOf(now); const wd = weekdayOf(now); const h = hourOf(now);
const next = nextWednesday(now);
if (c.notice && wd === 'Tue' && h >= 18 && next && !S.sent['tue:' + next] && await claimOnce('wed:tue:' + next)) {
S.sent['tue:' + next] = now; save();
await broadcast('Tomorrow is Wired Wednesday', noticeText(c));
}
if (c.kick && wd === 'Wed' && h >= 9 && day >= c.start && !S.sent['kick:' + day] && await claimOnce('wed:kick:' + day)) {
S.sent['kick:' + day] = now; save();
X.telegram(kickoffText(c)).catch(() => {});
}
if (wd === 'Thu' && h < 3) {
const wed = dayOf(now - 86400000);
if (isWednesday(now - 86400000) && wed >= c.start && !S.sent['wrap:' + wed] && await claimOnce('wed:wrap:' + wed)) {
S.sent['wrap:' + wed] = now; save();
const st = stats(wed);
X.telegram('\u{1F3C1} <b>Wired Wednesday wrap-up</b> (' + labelOf(wed) + ')\n<b>' + st.packs + ' packs</b> from ' + st.buyers + ' buyers · $' + st.usd.toFixed(0) + ' in ad packages · <b>' + st.polPaid.toFixed(2) + ' POL</b> paid to members · ' + st.bonusCredits.toLocaleString() + ' bonus credits wired into ' + st.visits.toLocaleString() + ' verified visits'
+ '\nNext one is a week away: ' + SITE + '/my#buy').catch(() => {});
}
}
}
function noticeText(c) {
return 'Tomorrow is Wired Wednesday on InstantAdPay.\n\nAny ad package of $' + (c.minCents / 100) + ' or more bought on Wednesday (Central time, midnight to midnight) earns +' + c.pct + '% bonus credits, and here is the twist: the bonus is wired straight into a verified-visits campaign pointed at your invite link. Real people, on your page, before you close the tab. Nothing to set up, nothing to claim.\n\nYour pack still pays your sponsor line the second it clears, and your line’s packs pay you.\n\nLine up your pack: ' + SITE + '/my#buy\nHow it works: ' + SITE + '/wednesday';
}
function kickoffText(c) {
return '\u{26A1} <b>It’s Wired Wednesday.</b> Any ad package from $' + (c.minCents / 100) + ' up earns <b>+' + c.pct + '% credits</b> today, wired straight into a verified-visits campaign for your invite link, running before you close the tab. Every pack pays its sponsor line in the same transaction. Buy yours: ' + SITE + '/my#buy';
}
// on-site notice + email to every active member (quiet 45+ days skipped, drip unsubscribes honoured)
async function broadcast(subject, text) {
let all = []; try { all = await X.accounts.listAll(5000); } catch (e) { return; }
const now = Date.now(); let n = 0;
for (const a of all) {
if (!a.email) continue;
if (now - (a.lastSeen || a.created || 0) > 45 * 86400000) continue;
try { await X.notify(a.memberId, subject, text, 'notice'); } catch (e) {}
try { if (X.mailer && X.mailer.hasKey() && !(X.drip && await X.drip.isUnsubscribed(a.email))) { await X.mailer.send(a.email, subject, text + '\n\nInstantAdPay'); n++; } } catch (e) {}
}
return n;
}
// what Marty reviews before switching the notice and kickoff on
function previews() { const c = cfg(); return { notice: { subject: 'Tomorrow is Wired Wednesday', text: noticeText(c) }, kickoff: kickoffText(c).replace(/<[^>]+>/g, '') }; }
module.exports = { init, cfg, view, stats, onEvent, tick, liveNow, nextWednesday, isWednesday, labelOf, previews };