Files
vbb/lsspins.js
2026-11-14 10:07:41 -06:00

75 lines
5.3 KiB
JavaScript

// LinkSpin Banner Spins in Viral Banner Blitz (Marty 2026-11-14: "make sure Viral Banner Blitz incorporates banner spins that could
// be pulled in just like downline banners"). The Downline Banners pattern:
// 1. My Banners: "Use my LinkSpin Banner Spin" lists the member's 125x125 (or 250x250) Banner Spins and posts one as a banner (one
// credit, like any post). The banner's image is the spin's rotating image on LinkSpin and its link is the spin's click link,
// so clicks count in LinkSpin Reports under the member's "Viral Banner Blitz" source.
// 2. Fill: the splash shows members' Banner Spins in the loop's open places, and in the waiting line while nobody waits. Fill tiles
// are never loop banners: they are not in the loop list, never shift it, never cycle and never earn. One spin per member, no
// member who already has a banner on the page, no image twice (the board dedupe from Downline Banners).
// LinkSpin's partner endpoint /api/partner/bl/spins, called with { site: 'vbb' }, signed with NETWORK_SSO_SECRET:
// X-Net-Ts + X-Net-Sig = hex HMAC-SHA256('net.v1.' + ts + newline + raw body). LinkSpin answers for the account holding the wallet
// (else the account with the member's network username) only.
'use strict';
const crypto = require('crypto');
let base = () => 'https://linkspin.app';
let members = () => [];
const HOST_RE = /^[a-z0-9.-]+\.[a-z]{2,}$/i;
async function call(body) {
const sec = String(process.env.NETWORK_SSO_SECRET || '').trim(); if (sec.length < 32) return { error: 'The LinkSpin link is not set up yet.' };
const raw = JSON.stringify(Object.assign({ site: 'vbb' }, body)); const ts = String(Date.now());
const sig = crypto.createHmac('sha256', sec).update('net.v1.' + ts + '\n' + raw).digest('hex');
const ac = new AbortController(); const t = setTimeout(() => ac.abort(), 8000);
try {
const r = await fetch(base() + '/api/partner/bl/spins', { method: 'POST', body: raw, signal: ac.signal, headers: { 'Content-Type': 'application/json', 'X-Net-Ts': ts, 'X-Net-Sig': sig } });
const j = await r.json().catch(() => ({})); return r.ok ? j : { error: j.error || 'LinkSpin did not answer. Try again in a minute.' };
} catch (e) { return { error: 'LinkSpin did not answer. Try again in a minute.' }; } finally { clearTimeout(t); }
}
// a member's spins: { account, username, host, spins: [{ id, name, code, size, paused, banners }] } or { error }
function list(addr, username) { return call({ wallet: String(addr).toLowerCase(), username: username || undefined }); }
// connect one spin: { use: { spinId, name, size, img, link, otherSource } } or { error }
function use(addr, username, spinId) { return call({ wallet: String(addr).toLowerCase(), username: username || undefined, spinId: Number(spinId) || 0 }); }
const showable = sp => !sp.paused && sp.banners > 0;
const imgUrl = (host, code) => 'https://' + host + '/i/' + code + '.gif';
// ---- fill: one showable spin per member, refreshed every 10 minutes ----
let SPINS = []; let lastRefresh = null;
async function refresh() {
const out = [];
for (const m of members()) {
const r = await list(m.address, m.username).catch(() => null);
if (!r || !r.account || !HOST_RE.test(String(r.host || ''))) continue;
const sp = (r.spins || []).find(showable); if (!sp || !/^[a-z0-9]+$/i.test(sp.code)) continue;
out.push({ owner: m.address, code: sp.code, img: imgUrl(r.host, sp.code), link: 'https://' + r.host + '/c/' + sp.code + '?placement=fill&s1=fill',
owner_name: r.username || m.username || null, title: sp.name });
}
SPINS = out; lastRefresh = { at: Date.now(), spins: out.length };
return out.length;
}
function set(list) { SPINS = Array.isArray(list) ? list : []; }
const codeOf = u => (String(u || '').match(/\/[ic]\/([a-z0-9]+)/i) || [])[1] || String(u || '');
function shuffled() { const a = SPINS.slice(); for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [a[i], a[j]] = [a[j], a[i]]; } return a; }
// fill tiles for one splash view. pageOwners: addresses with a banner on the page (loop or waiting line); pageImgs: their images.
// -> { loop: tiles for the open loop places, queue: tiles for the empty waiting line }
function fill(o) {
const owners = new Set(o.pageOwners || []); const used = new Set((o.pageImgs || []).map(codeOf));
const pick = []; const want = Math.max(0, o.openPlaces || 0) + Math.max(0, o.queueSlots || 0);
for (const sp of shuffled()) {
if (pick.length >= want) break;
if ((sp.owner && owners.has(sp.owner)) || used.has(codeOf(sp.img))) continue;
used.add(codeOf(sp.img)); if (sp.owner) owners.add(sp.owner);
pick.push({ t: 'spin', img: sp.img, href: sp.link, owner: sp.owner_name ? '@' + sp.owner_name : null, title: sp.title || 'LinkSpin Banner Spin' });
}
const nLoop = Math.min(pick.length, Math.max(0, o.openPlaces || 0));
return { loop: pick.slice(0, nLoop), queue: pick.slice(nLoop) };
}
function init(o) {
base = o.base || base; members = o.members || members;
const first = Number(process.env.LS_SPINS_FIRST_MS || 20000), every = Number(process.env.LS_SPINS_EVERY_MS || 10 * 60000);
setTimeout(() => refresh().catch(() => {}), first).unref(); setInterval(() => refresh().catch(() => {}), every).unref();
}
function view() { return { fill: SPINS.length, at: lastRefresh ? lastRefresh.at : 0, lastRefresh }; }
module.exports = { init, list, use, refresh, set, fill, codeOf, showable, view };