// Boost finishing through Network Ad Space (the Downline Banners dlbnas.js pattern, Marty's Boost rule 2026-11-14: "if a Boost // can't be delivered on-site within 30 days, finish it through the network"). NAS is Marty's own EvolutionScript site; its MySQL // is the API (InstantAdPay nas.js: INSERT payments, then sponsorads approved=1, pid 1 = TEXT ad, read `remaining` back). // Why NAS and not LinkSpin zones: NAS serves text ads natively (Subject + Body + link), LinkSpin zones are banner placements. // - A Boost past its deliverDays with views still owed gets ONE NAS text ad for exactly the views still owed (headline = Subject, // the two lines = Body, link = viraladblitz.com/go/?p=nas so the clicks count here too). What NAS serves counts toward // the Boost's guarantee (served = assigned - remaining, clamped; the InstantAdPay counter rules). // - On-site views keep counting meanwhile; a Boost is done when on-site + NAS >= its guaranteed views. Over-delivery is fine. // State in vab-nas.json (a MySQL document through docstore). Inert unless NAS_DB_* are set (tests inject a stand-in query). 'use strict'; const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); const cfgMod = require('./config'); const CATID_CRYPTO = 5; let pool = null, S = null, R = null, q = null; const on = () => !!(q || (process.env.NAS_DB_HOST && process.env.NAS_DB_USER && process.env.NAS_DB_PASSWORD && process.env.NAS_DB_NAME)); const FILE = () => path.join(cfgMod.get().dataDir, 'vab-nas.json'); function load() { if (S) return S; try { S = JSON.parse(fs.readFileSync(FILE(), 'utf8')); } catch (e) { S = {}; } S.boosts = S.boosts || {}; return S; } function save() { try { fs.writeFileSync(FILE(), JSON.stringify(load())); } catch (e) { console.error('vabnas save', e.message); } } function dbq(sql, args) { if (q) return q(sql, args); if (!pool) pool = require('mysql2/promise').createPool({ host: process.env.NAS_DB_HOST, user: process.env.NAS_DB_USER, password: process.env.NAS_DB_PASSWORD, database: process.env.NAS_DB_NAME, port: Number(process.env.NAS_DB_PORT || 3306), connectionLimit: 2, connectTimeout: 10000 }); return pool.query(sql, args).then(r => r[0]); } async function push({ subject, body, link, assigned, ref }) { const token = 'vab_' + crypto.randomBytes(8).toString('hex'); const now = new Date(); const edate = new Date(now.getTime() + 60 * 86400000); const pay = await dbq('INSERT INTO payments (Username, Amount, Currency_code, status, Date, pay_address) VALUES (?,?,?,1,?,?)', [token, 0, 'VAB', now, ref]); try { const ad = await dbq(`INSERT INTO sponsorads (Username, Subject, Body, WebsiteURL, assigned, remaining, hits, approved, Date, adtype, Name, Email, PaymentDetails, EDate, sp, width, height, BannerURL, pid, ref_by, catid) VALUES (?,?,?,?,?,?,0,1,?,1,?,?,?,?,'','','',NULL,1,0,?)`, [token, subject, body, link, assigned, assigned, now, 'ViralAdBlitz', 'ads@viraladblitz.com', ref, edate, CATID_CRYPTO]); return { id: ad.insertId, assigned }; } catch (e) { try { await dbq('DELETE FROM payments WHERE ID=?', [pay.insertId]); } catch (x) {} throw e; } } async function served(id) { const r = await dbq('SELECT assigned, remaining FROM sponsorads WHERE ID=?', [Number(id)]); if (!r.length) return null; return Math.max(0, Math.min(Number(r[0].assigned), Number(r[0].assigned) - Number(r[0].remaining))); } async function tick() { if (!on() || !R) return { off: true }; const c = cfgMod.get(); const s = load(); const site = 'https://' + c.site; let placed = 0; try { for (const b of R.boosts()) { const rec = s.boosts[b.key]; if (rec) { const sv = await served(rec.id); if (sv != null) rec.served = sv; continue; } if (!b.adId || !b.dueAt || Date.now() < b.dueAt) continue; const owed = R.remaining(b); if (owed <= 0) continue; const a = R.ad(b.adId); if (!a || a.status !== 'live') continue; const ad = await push({ subject: String(a.headline).slice(0, 60), body: [a.line1, a.line2].filter(Boolean).join(' ').slice(0, 200), link: site + '/go/' + a.id + '?p=nas', assigned: owed, ref: 'vab_boost:' + b.key }); s.boosts[b.key] = Object.assign(ad, { served: 0, at: Date.now(), adId: a.id }); placed++; console.log('vabnas: Boost', b.key.slice(0, 14), 'finishing', owed, 'views in NAS, ad', ad.id); } s.lastOk = Date.now(); delete s.lastErr; } catch (e) { s.lastErr = String(e.message).slice(0, 200); console.error('vabnas', e.message); } save(); return { placed }; } const boostServed = key => { const r = load().boosts[String(key)]; return r ? (r.served || 0) : 0; }; // deps: { boosts: () => [boost], remaining: b => n, ad: id => ad }; opts.query = a stand-in for tests function init(deps, opts) { R = deps; if (opts && opts.query) q = opts.query; if (!on()) { console.log('vabnas: off (no NAS_DB_*)'); return; } load(); setTimeout(() => tick().catch(() => {}), 30000).unref(); setInterval(() => tick().catch(() => {}), 6 * 3600000).unref(); } function view() { const s = load(); return { on: on(), boosts: s.boosts, lastOk: s.lastOk || null, lastErr: s.lastErr || null }; } module.exports = { init, tick, boostServed, view, on, _reset: () => { S = null; q = null; } };