// Admin: status + the cycle reward ledger, read with the ADMIN_TOKEN (kept in this tab only). (function () { VBB.renderNav(''); const $ = VBB.$, esc = VBB.esc; let tok = ''; try { tok = sessionStorage.getItem('vbb.admin') || ''; } catch (e) {} const get = u => VBB.getJSON(u, { headers: { 'x-admin-token': tok } }); async function open() { const s = await get('/api/admin/status'); if (s.error) { VBB.status('That token was not accepted.', 'err'); return; } try { sessionStorage.setItem('vbb.admin', tok); } catch (e) {} $('tokCard').hidden = true; $('adm').hidden = false; VBB.status(''); $('aStats').innerHTML = [['Loop filled', s.loop.filled + ' / ' + s.loop.size], ['Waiting', s.queue.length], ['Next shift', s.progress + ' / ' + s.config.perShift + (s.held ? ' (held)' : '')], ['Shifts today', s.today.shifts], ['Cycles today', s.today.cycles], ['Visitors today', s.today.visits], ['Cycles total', s.totals.cycles], ['Views recorded', VBB.n(s.ledger.views)], ['Views credited', VBB.n(s.ledger.credited)], ['Members', s.members]] .map(x => '
' + esc(x[1]) + '' + esc(x[0]) + '
').join(''); $('aRaw').textContent = JSON.stringify(s, null, 2); prizes(); setInterval(prizes, 30000); actions(); const l = await get('/api/admin/ledger?n=300'); const who = r => r.username ? '@' + r.username : VBB.short(r.address || r.owner); $('aMembers').innerHTML = l.members && l.members.length ? '' + l.members.map(m => '').join('') + '
MemberCyclesViews
' + esc(who(m)) + '' + m.cycles + '' + VBB.n(m.views) + '
' : '

No cycles yet.

'; $('aLedger').innerHTML = l.entries && l.entries.length ? '' + l.entries.map(e => '').join('') + '
When (CT)EntryBannerMemberViewsStatus
' + esc(new Date(e.at).toLocaleString('en-US', { timeZone: 'America/Chicago' })) + '' + esc(e.id) + '#' + e.bannerId + '' + esc(who(e)) + '' + VBB.n(e.views) + '' + esc(e.status) + '
' : '

The ledger is empty.

'; } // ---- weekly prize payouts: review, then "Approve and pay" (two clicks), once per week ---- const pst = { prepared: 'Waiting for approval', nothing: 'No winners', paying: 'Paying', sent: 'Sent, confirming', paid: 'Paid', partial: 'Partly paid: check below', 'claimed-elsewhere': 'Claimed elsewhere: check the wallet' }; async function prizes() { const z = await get('/api/admin/prizes'); if (z.error) return; const cur = z.current || {}; $('pzNow').innerHTML = '

This week (' + esc(z.week) + '): sales $' + VBB.n(cur.salesUsd) + ' in ' + VBB.n(cur.purchases) + ' purchases, 10% = $' + VBB.n(cur.shareUsd) + ', pot $' + VBB.n(cur.potUsd) + (cur.topUpUsd > 0 ? ' (company top-up $' + VBB.n(cur.topUpUsd) + ')' : '') + (cur.atCap ? ' (at the cap)' : '') + '. Prize key: ' + (z.prize.keyOn ? esc(VBB.short(z.prize.keyAddress)) : 'not set') + '.

'; const tx = h => z.explorer ? '' + esc(h.slice(0, 10)) + '...' : esc(h); $('pzWeeks').innerHTML = (z.weeks || []).map(w => { const q = w.quote || {}; const lines = w.transfers.map((t, i) => '' + esc(t.board) + '' + t.place + '' + esc(t.name) + '
' + esc(t.address) + '' + VBB.n(t.count) + '$' + VBB.n(t.usd) + '' + esc(t.pol || (q.lines && q.lines[i] ? q.lines[i].pol : '')) + '' + esc(t.status || '') + (t.tx ? ' ' + tx(t.tx) : '') + (t.error ? '
' + esc(t.error) + '' : '') + '').join(''); let foot = ''; if (w.status === 'prepared') { foot = q.error ? '

' + esc(q.error) + '

' : '

POL at $' + Number(q.priceUsd).toFixed(4) + '. Needs ' + esc(q.totalPol) + ' POL + about ' + esc(q.gasPol) + ' POL gas. Prize wallet ' + esc(VBB.short(q.contractPrizeWallet || '')) + ' holds ' + esc(q.balancePol) + ' POL.' + (BigInt(q.shortWei || 0) > 0n ? ' Short by ' + esc(q.shortPol) + ' POL.' : '') + (q.keyMatches ? '' : ' PRIZE_KEY is missing or is not the prize wallet.') + '

' + ''; } return '

Week of ' + esc(w.week) + ': ' + esc(pst[w.status] || w.status) + '

Sales $' + VBB.n(w.salesUsd) + ', pot $' + VBB.n(w.potUsd) + ', paying $' + VBB.n(w.payUsd) + (w.unfilledUsd > 0 ? ', $' + VBB.n(w.unfilledUsd) + ' unfilled (stays in the prize wallet)' : '') + (w.payment && w.payment.priceUsd ? ', paid at $' + Number(w.payment.priceUsd).toFixed(4) + '/POL' : '') + '

' + (w.transfers.length ? '
' + lines + '
Board#WinnerCountUSDPOLStatus
' : '

Nobody on the leaderboards that week.

') + foot + '
'; }).join('') || '

No weeks prepared yet.

'; document.querySelectorAll('[data-pay]').forEach(b => { b.onclick = async () => { const wk = b.dataset.pay; if (!b.dataset.armed) { b.dataset.armed = '1'; b.textContent = 'Click again to send the POL for ' + wk; setTimeout(() => { if (b.isConnected) { delete b.dataset.armed; b.textContent = 'Approve and pay'; } }, 8000); return; } b.disabled = true; b.textContent = 'Sending...'; const r = await VBB.getJSON('/api/admin/prizes/pay', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-admin-token': tok }, body: JSON.stringify({ week: wk, confirm: wk }) }); VBB.status(r.error || 'Payout sent for the week of ' + wk + '. Receipts confirm below.', r.error ? 'err' : 'ok'); prizes(); }; }); } $('pzPrep').onclick = async () => { const r = await VBB.getJSON('/api/admin/prizes/prepare', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-admin-token': tok }, body: '{}' }); VBB.status(r.error || 'Prepared the week of ' + r.week + '.', r.error ? 'err' : 'ok'); prizes(); }; // ---- banner takedown / restore (Marty 2026-11-15): reason required, credit back or none; restore takes a given credit back ---- const send = (u, body) => VBB.getJSON(u, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-admin-token': tok }, body: JSON.stringify(body) }); const ct = t => t ? new Date(t).toLocaleString('en-US', { timeZone: 'America/Chicago' }) + ' CT' : ''; async function actions() { const a = await get('/api/admin/actions?n=200'); if (a.error) return; $('aActions').innerHTML = a.actions.length ? '' + a.actions.map(e => '').join('') + '
When (CT)ActionBannerOwnerByReasonCredit
' + esc(ct(e.at)) + '' + esc(e.action) + (e.cycleRecorded ? ' (cycle recorded)' : '') + '#' + e.bannerId + '' + esc(e.username ? '@' + e.username : VBB.short(e.owner)) + '' + esc(e.by) + '' + esc(e.reason || '') + '' + esc(e.credit) + '
' : '

No takedowns yet.

'; } async function findBanners() { const q = $('bnQ').value.trim(); if (!q) return; const d = await get('/api/admin/banners?q=' + encodeURIComponent(q)); if (d.error) { $('bnList').innerHTML = '

' + esc(d.error) + '

'; return; } $('bnList').innerHTML = ''; for (const b of d.banners) { const el = document.createElement('div'); el.className = 'adm-b'; const st = b.down ? 'TAKEN DOWN ' + ct(b.down.at) + ' by ' + b.down.by + ': ' + b.down.reason + (b.down.creditBack ? ' (credit given back)' : ' (no credit)') + '. Slot: ' + b.state : b.state === 'queued' ? 'Waiting #' + b.position + ' of ' + b.of : b.state === 'loop' ? 'In the loop, place ' + b.place + ' of ' + b.of : b.state; el.innerHTML = 'Banner #' + b.id + '
#' + b.id + ' ' + esc(b.username ? '@' + b.username : '') + ' ' + esc(b.owner) + '
' + esc(st) + '
Link: ' + esc(b.link) + '
' + VBB.n(b.clicks) + ' clicks. Posted ' + esc(ct(b.createdAt)) + '.' + (b.swaps.length ? ' Swapped ' + esc(ct(b.swaps[0].at)) + ' from ' + esc(b.swaps[0].old.link) + '.' : '') + (b.downs.length ? ' Restored before: ' + b.downs.length + 'x.' : '') + '
'; el.querySelector('img').src = b.img; const act = el.querySelector('.act'); if (b.down) { act.innerHTML = '
'; act.querySelector('button').onclick = async () => { const r = await send('/api/admin/banners/' + b.id + '/restore', { reason: act.querySelector('.rs').value }); VBB.status(r.error || ('Banner #' + b.id + ' restored.' + (r.creditReversed ? ' The credit was taken back.' : r.creditAlreadyUsed ? ' The owner already used the credit, so it was not taken back.' : '') + (r.cycle ? ' Its place had ridden out, so its cycle is recorded now.' : '')), r.error ? 'err' : 'ok'); findBanners(); actions(); }; } else if (b.state === 'queued' || b.state === 'loop') { act.innerHTML = '
' + '
' + '
'; const bt = act.querySelector('button'); bt.onclick = async () => { const reason = act.querySelector('.why').value.trim(); const c = act.querySelector('input[type=radio]:checked'); if (reason.length < 3) return VBB.status('Write the reason first.', 'err'); if (!c) return VBB.status('Choose credit back or no credit.', 'err'); if (!bt.dataset.armed) { bt.dataset.armed = '1'; bt.textContent = 'Click again to take down #' + b.id; setTimeout(() => { if (bt.isConnected) { delete bt.dataset.armed; bt.textContent = 'Take down'; } }, 8000); return; } const r = await send('/api/admin/banners/' + b.id + '/takedown', { reason, credit: c.value }); VBB.status(r.error || ('Banner #' + b.id + ' taken down' + (c.value === 'back' ? ', credit given back.' : ', no credit.')), r.error ? 'err' : 'ok'); findBanners(); actions(); }; } $('bnList').appendChild(el); } } $('bnFind').onclick = findBanners; $('bnQ').addEventListener('keydown', e => { if (e.key === 'Enter') findBanners(); }); $('tokBtn').onclick = () => { tok = $('tok').value.trim(); open(); }; $('tok').addEventListener('keydown', e => { if (e.key === 'Enter') $('tokBtn').click(); }); if (tok) open(); })();