|
| 1 | +/* |
| 2 | + * Hot right now (Back Room page, home page Back Room and phone page): a row near the top of the page with the games the |
| 3 | + * most people are playing at this moment, busiest first, and a small flame badge ("🔥 5 playing") on those games' cards. |
| 4 | + * |
| 5 | + * Data: the Online list's own nodes, rooms/ol-lobby/players/<uid> (social.js writes them and already listens to them, so |
| 6 | + * this listener downloads nothing extra). A player counts for the game in `g` (the game they have open, one of |
| 7 | + * social.js's GAMES keys; single-player games aren't in that list, so they never show here) while their node has a |
| 8 | + * heartbeat (`ts`) in the last 75 s, social.js's own limit. People with Hide me on have no node and aren't counted. |
| 9 | + * "Surging" (▲) marks a game with at least 3 more players than 10 minutes ago, from counts this page has kept since |
| 10 | + * it opened. The row is hidden while nobody is in a game. Writes nothing; signs in only through party.js (Party.boot). |
| 11 | + * Clicking a game opens it the way its card does (window.BackRoom.openGame on the home page and the phone page). |
| 12 | + */ |
| 13 | +(function () { |
| 14 | + 'use strict'; |
| 15 | + if (window.BackRoomHot) return; |
| 16 | + var PATH = 'rooms/ol-lobby/players', FRESH = 75000, SHOW = 6, BASE = '/e63334287a3deaa3/'; |
| 17 | + // social.js's game keys → folder and name (only games that live in the Back Room folder). |
| 18 | + var GAMES = { |
| 19 | + sc: ['shell-crackers', 'Shell Crackers'], rk: ['rumble-karts', 'Rumble Karts'], ss: ['slipstream', 'Slipstream'], |
| 20 | + bb: ['boostball', 'Boostball'], cl: ['coil', 'Coil'], sd: ['showdown', 'Showdown'], br: ['backrooms-1v1', 'Backrooms 1v1–4v4'], |
| 21 | + cf: ['chromaflage', 'Chromaflage'], tw: ['spire', 'Spire'], sr: ['turbo-trail', 'Turbo Trail'], er: ['escapade', 'Escapade'], |
| 22 | + up: ['updraft', 'Updraft'], ph: ['odd-one-out', 'Odd One Out'], ko: ['tumble-trials', 'Tumble Trials'], dw: ['deepword', 'Deepword'], |
| 23 | + pf: ['puffer-party', 'Puffer Party'], bs: ['blockstorm', 'Blockstorm'], lh: ['lanternhold', 'Lanternhold'], |
| 24 | + hp: ['hush-pools', 'Blackwade'], vq: ['sky-duel', 'Sky Duel'], qg: ['putterby', 'Putterby'], tv: ['trivium', 'Trivium'], |
| 25 | + dz: ['drop-zone', 'Driftfall'], gd: ['lunge-line', 'Lokop'] |
| 26 | + }; |
| 27 | + var ES = { 'Hot right now': 'Lo más jugado ahora', 'The games people are in right now.': 'Los juegos en los que hay gente ahora mismo.', 'playing': 'jugando', 'surging': 'subiendo' }; |
| 28 | + function es() { try { return !!(window.BackRoomLang && BackRoomLang.get && BackRoomLang.get() === 'es'); } catch (e) { return false; } } |
| 29 | + function T(s) { return es() && ES[s] ? ES[s] : s; } |
| 30 | + |
| 31 | + var css = document.createElement('style'); |
| 32 | + css.textContent = |
| 33 | + '#hotnow{padding-top:14px}' + |
| 34 | + '.hot-row{list-style:none;margin:0;padding:0 0 2px;display:flex;gap:8px;overflow-x:auto;scrollbar-width:thin}' + |
| 35 | + '.hot-it{flex:0 0 auto}' + |
| 36 | + '.hot-it a{display:flex;align-items:center;gap:10px;padding:6px 12px 6px 6px;border-radius:10px;background:var(--panel-2);border:1px solid var(--line);color:var(--ink);text-decoration:none;min-width:0}' + |
| 37 | + '.hot-it a:hover{border-color:var(--accent)}' + |
| 38 | + '.hot-it img{width:64px;height:40px;border-radius:6px;object-fit:cover;flex:none;background:var(--line)}' + |
| 39 | + '.hot-it span{display:grid;gap:2px;line-height:1.2}' + |
| 40 | + '.hot-it b{font-weight:600;white-space:nowrap}' + |
| 41 | + '.hot-it small{font-family:var(--mono);font-size:11px;font-weight:600;letter-spacing:.04em;color:#e8590c;white-space:nowrap}' + |
| 42 | + '.hot-it small i{font-style:normal;color:var(--up,#1fbf62);margin-left:6px}' + |
| 43 | + '.hot-badge{position:absolute;right:8px;top:8px;z-index:3;padding:3px 8px;border-radius:999px;background:rgba(20,10,4,.82);color:#ffb37a;' + |
| 44 | + 'font:700 12px/1.2 system-ui,-apple-system,"Segoe UI",sans-serif;pointer-events:none;box-shadow:0 2px 8px rgba(0,0,0,.3);white-space:nowrap}' + |
| 45 | + '.pz .hot-badge,.grid.pz .hot-badge{top:4px;right:4px;font-size:10px;padding:2px 6px}' + |
| 46 | + '.gc .shot .hot-badge{top:auto;right:auto;left:8px;bottom:8px}' + /* the card's genre badge and i button are on top */ |
| 47 | + '@media (prefers-reduced-motion:no-preference){.hot-badge{animation:hotin .4s ease-out}}' + |
| 48 | + '@keyframes hotin{from{opacity:0;transform:translateY(-4px)}to{opacity:1;transform:none}}'; |
| 49 | + document.head.appendChild(css); |
| 50 | + |
| 51 | + var sec = document.createElement('section'); |
| 52 | + sec.id = 'hotnow'; sec.setAttribute('aria-label', 'Hot right now'); sec.hidden = true; |
| 53 | + sec.innerHTML = '<div class="pty hot"><div class="pty-top"><h2></h2><p class="pty-sub"></p></div><ol class="hot-row"></ol></div>'; |
| 54 | + var $h = sec.querySelector('h2'), $sub = sec.querySelector('.pty-sub'), $row = sec.querySelector('.hot-row'); |
| 55 | + var placed = false; |
| 56 | + function place() { |
| 57 | + if (placed) return; |
| 58 | + var at = document.getElementById('climbing') || document.getElementById('playingnow') || document.getElementById('party'), head = document.querySelector('header'); |
| 59 | + if (!at && !head) return; placed = true; |
| 60 | + if (at && at.parentNode) at.parentNode.insertBefore(sec, at); else head.parentNode.insertBefore(sec, head.nextSibling); |
| 61 | + var party = document.getElementById('party'); |
| 62 | + if (party) { var mb = getComputedStyle(party).marginBottom; if (parseFloat(mb) > 0) { sec.style.paddingTop = '0'; sec.style.marginBottom = mb; } } |
| 63 | + } |
| 64 | + |
| 65 | + var raw = {}, hist = []; // hist: [{ t, c: { folder: count } }] once a minute, for "surging" |
| 66 | + function now() { try { return window.Party && Party.now ? Party.now() : Date.now(); } catch (e) { return Date.now(); } } |
| 67 | + function counts() { |
| 68 | + var t = now(), c = {}; |
| 69 | + Object.keys(raw || {}).forEach(function (u) { |
| 70 | + var v = raw[u]; if (!v || typeof v.g !== 'string' || !GAMES[v.g]) return; |
| 71 | + var ts = +v.ts; if (!(ts > t - FRESH)) return; |
| 72 | + var d = GAMES[v.g][0]; c[d] = (c[d] || 0) + 1; |
| 73 | + }); |
| 74 | + return c; |
| 75 | + } |
| 76 | + function before(d) { |
| 77 | + var t = Date.now() - 10 * 60000, old = null; |
| 78 | + for (var i = 0; i < hist.length; i++) if (hist[i].t <= t) old = hist[i]; |
| 79 | + return old ? (old.c[d] || 0) : null; |
| 80 | + } |
| 81 | + function nameOf(d) { for (var k in GAMES) if (GAMES[k][0] === d) return GAMES[k][1]; return d; } |
| 82 | + function openIt(e, d) { |
| 83 | + var B = window.BackRoom; if (!B || !B.openGame || e.button || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return; |
| 84 | + e.preventDefault(); e.stopPropagation(); B.openGame(BASE + d + '/', nameOf(d)); |
| 85 | + } |
| 86 | + function badges(c) { |
| 87 | + document.querySelectorAll('.hot-badge').forEach(function (b) { b.remove(); }); |
| 88 | + Object.keys(c).forEach(function (d) { |
| 89 | + var p = BASE + d + '/', label = '🔥 ' + c[d] + ' ' + T('playing'); |
| 90 | + document.querySelectorAll('.gc a.shot[href="' + p + '"], .pz a[href="' + p + '"], button.tile[data-path="' + p + '"]').forEach(function (host) { |
| 91 | + if (getComputedStyle(host).position === 'static') host.style.position = 'relative'; |
| 92 | + var b = document.createElement('span'); b.className = 'hot-badge'; b.textContent = label; host.appendChild(b); |
| 93 | + }); |
| 94 | + }); |
| 95 | + } |
| 96 | + var sigLast = ''; |
| 97 | + function draw() { |
| 98 | + var c = counts(), list = Object.keys(c).sort(function (a, b) { return c[b] - c[a] || (a < b ? -1 : 1); }); |
| 99 | + var sig = JSON.stringify(c) + (es() ? 'es' : 'en'); |
| 100 | + if (sig === sigLast) return; sigLast = sig; |
| 101 | + place(); |
| 102 | + $h.textContent = '🔥 ' + T('Hot right now'); $sub.textContent = T('The games people are in right now.'); |
| 103 | + sec.hidden = !list.length; |
| 104 | + $row.textContent = ''; |
| 105 | + list.slice(0, SHOW).forEach(function (d) { |
| 106 | + var li = document.createElement('li'); li.className = 'hot-it'; |
| 107 | + var a = document.createElement('a'); a.href = BASE + d + '/'; |
| 108 | + a.addEventListener('click', function (e) { openIt(e, d); }); |
| 109 | + var im = document.createElement('img'); im.src = BASE + 'm/thumbs/' + d + '.jpg'; im.alt = ''; im.loading = 'lazy'; |
| 110 | + var sp = document.createElement('span'), b = document.createElement('b'), sm = document.createElement('small'); |
| 111 | + b.textContent = nameOf(d); sm.textContent = '🔥 ' + c[d] + ' ' + T('playing'); |
| 112 | + var was = before(d); if (was !== null && c[d] - was >= 3) { var i = document.createElement('i'); i.textContent = '▲ ' + T('surging'); sm.appendChild(i); } |
| 113 | + sp.append(b, sm); a.append(im, sp); li.appendChild(a); $row.appendChild(li); |
| 114 | + }); |
| 115 | + badges(c); |
| 116 | + } |
| 117 | + function sample() { hist.push({ t: Date.now(), c: counts() }); while (hist.length > 30) hist.shift(); } |
| 118 | + |
| 119 | + function start() { |
| 120 | + var n = 0; |
| 121 | + (function wait() { |
| 122 | + if (window.Party && Party.boot) { |
| 123 | + Party.boot().then(function (b) { |
| 124 | + b.db.ref(PATH).on('value', function (s) { raw = s.val() || {}; draw(); }); |
| 125 | + }).catch(function () {}); |
| 126 | + setInterval(function () { sigLast = ''; draw(); }, 15000); // heartbeats go stale even when nothing changes |
| 127 | + setInterval(sample, 60000); setTimeout(sample, 3000); |
| 128 | + document.addEventListener('backroom:lang', function () { sigLast = ''; draw(); }); |
| 129 | + return; |
| 130 | + } |
| 131 | + if (++n < 300) setTimeout(wait, 200); |
| 132 | + })(); |
| 133 | + } |
| 134 | + window.BackRoomHot = { counts: counts, draw: draw }; |
| 135 | + if (navigator.webdriver) window.__hot = { set: function (r) { raw = r || {}; sigLast = ''; draw(); }, hist: hist }; // tests only |
| 136 | + start(); |
| 137 | +})(); |
0 commit comments