/* ===================================================================== FREE THROW FRENZY — Sport on Earth Games arcade Single-file canvas game in the Family Arcade house style. EMBED (inline):
Or serve the matching free-throw-frenzy.html wrapper in an iframe. CONTROLS: tap the court, click, or press Space to release each shot. Pure timing: stop the needle inside the green for a make — dead center for a perfect swish. The needle speeds up and the green shrinks every shot. 10 shots total. SCORING: make = 1 pt • perfect swish = 2 pts. Max score 20. ===================================================================== */ (function () { "use strict"; /* ================= LEADERBOARD BACKEND — UPGRADE HOOK ================= Default: LOCAL board (localStorage top-10) so the game runs standalone. To switch to the shared family KV leaderboard later, set KV_ENABLED to true. The site must then serve the paper's functions/api/arcade.js with "free-throw-frenzy" added to its GAMES list and ARCADE_KV bound — the fetch calls below already mirror that API exactly: GET /api/arcade/leaderboard?game= -> { game, scores:[{manager,score,at}] } POST /api/arcade/score {game, manager, score} -> { ok, rank } No other code changes are needed. */ var KV_ENABLED = false; var GAME_ID = "free-throw-frenzy"; var LS_KEY = "fbiArcade." + GAME_ID; var LS_NAME = "fbiArcade.name"; /* Team Account identity (Batch 3.1, Task I, Oct 22, 2026): when a Team Account session is live, ACCT = { username, club } (from the accounts function's GET /api/accounts/me) and score saves go under that identity automatically — the visitor never re-types who they are. The board itself stays exactly what it has always been: this device's localStorage top-10 (KV_ENABLED above remains off — there is no /api/arcade backend on this site, and none is invented here). Guests keep the free-text name flow unchanged. */ var ACCT = null; function escapeHtml(s) { return String(s).replace(/[&<>"']/g, function (c) { return { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]; }); } function localScores() { try { return JSON.parse(localStorage.getItem(LS_KEY)) || []; } catch (e) { return []; } } function renderBoard(el, scores) { if (!scores || !scores.length) { el.innerHTML = "
  • No scores yet — be the first!
  • "; return; } el.innerHTML = scores.slice(0, 10).map(function (s, i) { return "
  • #" + (i + 1) + " " + escapeHtml(s.manager) + (s.club ? " · " + escapeHtml(s.club) : "") + " — " + s.score + "
  • "; }).join(""); } function loadBoard(el) { if (!KV_ENABLED) { renderBoard(el, localScores()); return; } fetch("/api/arcade/leaderboard?game=" + encodeURIComponent(GAME_ID)) .then(function (r) { return r.json(); }) .then(function (d) { renderBoard(el, d.scores || []); }) .catch(function () { el.innerHTML = "
  • Could not load scores.
  • "; }); } function saveScore(name, score, msgEl, boardEl) { if (ACCT) name = ACCT.username; name = (name || "").trim(); if (!name) { msgEl.textContent = "Enter your name first!"; return; } try { localStorage.setItem(LS_NAME, name); } catch (e) {} if (!KV_ENABLED) { var scores = localScores(); var entry = { manager: name.slice(0, 40), score: Math.floor(score), at: new Date().toISOString() }; if (ACCT) { entry.club = ACCT.club || ""; entry.account = true; } scores.push(entry); scores.sort(function (a, b) { return b.score - a.score; }); scores = scores.slice(0, 10); try { localStorage.setItem(LS_KEY, JSON.stringify(scores)); } catch (e) {} var rank = scores.indexOf(entry) + 1; msgEl.textContent = rank ? ("Saved! You rank #" + rank + " on this device's board.") : "Score saved!"; renderBoard(boardEl, scores); return; } fetch("/api/arcade/score", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ game: GAME_ID, manager: name, club: ACCT ? (ACCT.club || "") : "", score: Math.floor(score) }) }).then(function (r) { return r.json(); }) .then(function (d) { msgEl.textContent = d.rank ? ("Saved! You rank #" + d.rank + " on the family board.") : "Score saved!"; loadBoard(boardEl); }) .catch(function () { msgEl.textContent = "Could not save score."; }); } /* ================= SHARED ARCADE CSS (injected once per page) ================= */ if (!document.getElementById("hoopsArcadeCss")) { var st = document.createElement("style"); st.id = "hoopsArcadeCss"; st.textContent = ".hoops-arcade{max-width:440px;margin:0 auto;font-family:inherit;-webkit-user-select:none;user-select:none;-webkit-touch-callout:none}" + ".hoops-arcade *{-webkit-user-select:none;user-select:none;-webkit-touch-callout:none}" + ".hoops-arcade input,.hoops-arcade select{-webkit-user-select:text;user-select:text;-webkit-touch-callout:default}" + ".hoops-arcade .arcade-canvas{display:block;width:100%;max-width:400px;height:auto;margin:0 auto;background:#8a5a2b;border:3px solid var(--line,#d8cfc0);border-radius:8px;touch-action:none;-webkit-user-select:none;user-select:none;-webkit-touch-callout:none}" + ".hoops-arcade .arcade-ui{text-align:center;margin:12px 0}" + ".hoops-arcade .arcade-score{font-size:.85rem;margin-bottom:8px}" + ".hoops-arcade .arcade-score b{color:var(--red,#c8102e)}" + ".hoops-arcade .arcade-btn{background:var(--red,#c8102e);color:#fff;border:none;border-radius:6px;padding:10px 24px;font-size:1rem;font-weight:700;cursor:pointer}" + ".hoops-arcade .arcade-btn.small{padding:6px 12px;font-size:.8rem}" + ".hoops-arcade .arcade-btn:disabled{opacity:.5;cursor:default}" + ".hoops-arcade .arcade-msg{min-height:2.4em;font-size:.8rem;color:var(--muted,#666);margin:8px 0}" + ".hoops-arcade .arcade-name{display:flex;gap:8px;justify-content:center;margin:8px 0}" + ".hoops-arcade .arcade-name input{padding:8px;border:1px solid var(--line,#d8cfc0);border-radius:6px;font-size:.9rem;max-width:220px}" + ".hoops-arcade .arcade-board{margin-top:16px}" + ".hoops-arcade .arcade-board h3{font-size:1rem;margin-bottom:8px}" + ".hoops-arcade .arcade-board ol{list-style-position:inside;padding:0}" + ".hoops-arcade .arcade-board li{padding:4px 0;border-bottom:1px solid var(--line,#d8cfc0);font-size:.85rem}"; document.head.appendChild(st); } /* Long-press fix (iOS copy callout on held controls): kill the context menu anywhere inside an arcade mount. Installed once per page. */ if (!window.__hoopsNoCallout) { window.__hoopsNoCallout = true; document.addEventListener("contextmenu", function (e) { var t = e.target; if (t && t.closest && t.closest(".hoops-arcade")) e.preventDefault(); }); } /* ================= MOUNT ================= */ var mount = document.querySelector('[data-hoops-game="' + GAME_ID + '"]'); if (!mount) { mount = document.createElement("div"); mount.setAttribute("data-hoops-game", GAME_ID); var cs = document.currentScript; if (cs && cs.parentNode) cs.parentNode.insertBefore(mount, cs); else document.body.appendChild(mount); } mount.classList.add("hoops-arcade"); mount.innerHTML = '' + '
    ' + '
    Score: 0 • Shot: –/10 • Makes: 0
    ' + '
    ' + '
    The needle sweeps the gauge — tap or press Space to release in the green. Dead center = perfect swish, 2 points!
    ' + '
    ' + '
    ' + "
    " + '

    Leaderboard — Free Throw Frenzy

    1. Loading…
    '; var cv = mount.querySelector("canvas"), ctx = cv.getContext("2d"); var el = { score: mount.querySelector(".sc"), shot: mount.querySelector(".shot"), makes: mount.querySelector(".makes"), msg: mount.querySelector(".arcade-msg"), start: mount.querySelector(".start"), name: mount.querySelector(".arcade-name input"), save: mount.querySelector(".save"), board: mount.querySelector(".arcade-board ol") }; try { el.name.value = localStorage.getItem(LS_NAME) || ""; } catch (e) {} /* Team Account session check (Task I): non-blocking; any failure simply leaves the guest flow exactly as it was. */ try { fetch("/api/accounts/me", { credentials: "same-origin" }) .then(function (r) { if (!r.ok) throw 0; return r.json(); }) .then(function (d) { if (!d || !d.ok || !d.username) return; ACCT = { username: d.username, club: d.club || "" }; el.name.value = d.username; el.name.readOnly = true; el.name.title = "Signed in — your scores save under your team account"; var hint = document.createElement("div"); hint.className = "arcade-msg acct-hint"; hint.textContent = "🏀 Signed in as " + d.username + (d.club ? " · " + d.club : "") + " — your high scores save under your account, no typing needed."; el.name.parentNode.parentNode.insertBefore(hint, el.name.parentNode); }) .catch(function () {}); } catch (e) {} /* ================= GAME ================= */ var W = 400, H = 480; var HOOP = { x: 200, y: 128 }; var GAUGE = { x: 200, y: 392, r: 118 }; // semicircle gauge around the shooter var state = "idle"; // idle | playing | done var shotNum = 0, score = 0, makes = 0, perfects = 0; var needle = -80, needleDir = 1; // degrees, -80..80, 0 = top center var ball = null, floaters = [], netJiggle = 0, pauseT = 0; function greenHalf() { return Math.max(5, 13 - (shotNum - 1) * 0.8); } function sweepSpeed() { return 118 * Math.pow(1.09, shotNum - 1); } // deg per second function reset() { state = "idle"; shotNum = 0; score = 0; makes = 0; perfects = 0; needle = -80; needleDir = 1; ball = null; floaters = []; pauseT = 0; updateUI(); } function updateUI() { el.score.textContent = score; el.shot.textContent = (shotNum === 0 ? "–" : Math.min(10, shotNum)) + "/10"; el.makes.textContent = makes; } function startGame() { reset(); state = "playing"; shotNum = 1; el.start.textContent = "Restart"; el.msg.textContent = "Shot 1 of 10 — release in the green!"; updateUI(); } function release() { if (state !== "playing" || ball || pauseT > 0) return; var a = needle, gh = greenHalf(); var kind = Math.abs(a) <= 2.5 ? "perfect" : Math.abs(a) <= gh ? "make" : Math.abs(a) <= gh + 6 ? "rimout" : "miss"; var tx = HOOP.x, ty = HOOP.y + 6; if (kind === "rimout") tx += a < 0 ? -13 : 13; if (kind === "miss") { tx += a < 0 ? -52 : 52; ty += 18; } ball = { x0: GAUGE.x, y0: GAUGE.y - 40, x1: tx, y1: ty, cx: (GAUGE.x + tx) / 2, cy: Math.min(GAUGE.y - 40, ty) - 120, t: 0, kind: kind, bouncing: false, bx: 0, by: 0, bvx: 0, bvy: 0, bt: 0 }; } function resolveBall() { var kind = ball.kind; if (kind === "perfect" || kind === "make") { var pts = kind === "perfect" ? 2 : 1; score += pts; makes++; if (kind === "perfect") perfects++; netJiggle = 14; floaters.push({ x: HOOP.x, y: HOOP.y + 38, text: kind === "perfect" ? "+2 PERFECT SWISH!" : "+1 Good!", life: 1 }); el.msg.textContent = kind === "perfect" ? "🎯 Dead center — perfect swish!" : "Count it!"; } else { floaters.push({ x: HOOP.x, y: HOOP.y + 38, text: kind === "rimout" ? "Off the rim!" : "Airball…", life: 1 }); el.msg.textContent = kind === "rimout" ? "Rattled out — a hair off." : "Way off. Find the rhythm!"; } updateUI(); } function nextShot() { if (shotNum >= 10) { state = "done"; el.msg.textContent = "That's 10! Final score " + score + " of 20 — save it to the board!"; return; } shotNum++; needle = -80; needleDir = 1; pauseT = 0.55; el.msg.textContent = "Shot " + shotNum + " of 10 — the needle's quicker, the green is tighter…"; updateUI(); } function drawCourt() { ctx.fillStyle = "#5b3a17"; ctx.fillRect(0, 0, W, 66); ctx.fillStyle = "rgba(255,255,255,.13)"; for (var i = 0; i < 30; i++) ctx.fillRect((i * 41) % W, 10 + (i * 11) % 42, 5, 5); ctx.fillStyle = "#b57e3e"; ctx.fillRect(0, 66, W, H - 66); ctx.fillStyle = "rgba(0,0,0,.05)"; for (var y = 96; y < H; y += 44) ctx.fillRect(0, y, W, 2); // Paint + free-throw line ctx.fillStyle = "rgba(200,16,46,.20)"; ctx.fillRect(HOOP.x - 54, 66, 108, 218); ctx.strokeStyle = "rgba(255,255,255,.75)"; ctx.lineWidth = 2; ctx.strokeRect(HOOP.x - 54, 66, 108, 218); ctx.beginPath(); ctx.moveTo(0, 356); ctx.lineTo(W, 356); ctx.stroke(); ctx.fillStyle = "rgba(255,255,255,.65)"; ctx.font = "11px sans-serif"; ctx.textAlign = "center"; ctx.fillText("FREE-THROW LINE", 200, 348); // Backboard + rim + net ctx.fillStyle = "#f4f4f4"; ctx.fillRect(HOOP.x - 36, HOOP.y - 34, 72, 28); ctx.strokeStyle = "#c8102e"; ctx.lineWidth = 2; ctx.strokeRect(HOOP.x - 36, HOOP.y - 34, 72, 28); ctx.strokeRect(HOOP.x - 11, HOOP.y - 23, 22, 15); ctx.strokeStyle = "#e25822"; ctx.lineWidth = 4; ctx.beginPath(); ctx.moveTo(HOOP.x - 16, HOOP.y); ctx.lineTo(HOOP.x + 16, HOOP.y); ctx.stroke(); ctx.strokeStyle = "rgba(255,255,255,.9)"; ctx.lineWidth = 1.5; var jig = netJiggle > 0 ? Math.sin(netJiggle * 1.4) * 3 : 0; for (var i = -1; i <= 1; i++) { ctx.beginPath(); ctx.moveTo(HOOP.x + i * 14, HOOP.y + 2); ctx.lineTo(HOOP.x + i * 7 + jig, HOOP.y + 26); ctx.stroke(); } } function drawGaugeAndShooter() { var gh = greenHalf(); // Gauge arc background ctx.lineWidth = 15; ctx.strokeStyle = "rgba(0,0,0,.4)"; ctx.beginPath(); ctx.arc(GAUGE.x, GAUGE.y, GAUGE.r, Math.PI, 2 * Math.PI); ctx.stroke(); // Green zone (center = -90 deg in canvas terms) var c = -Math.PI / 2, halfRad = gh * Math.PI / 180; ctx.strokeStyle = "#2ecc40"; ctx.beginPath(); ctx.arc(GAUGE.x, GAUGE.y, GAUGE.r, c - halfRad, c + halfRad); ctx.stroke(); // Perfect sliver ctx.strokeStyle = "#ffd700"; var pRad = 2.5 * Math.PI / 180; ctx.beginPath(); ctx.arc(GAUGE.x, GAUGE.y, GAUGE.r, c - pRad, c + pRad); ctx.stroke(); // Needle var na = (-90 + needle) * Math.PI / 180; var inGreen = Math.abs(needle) <= gh; ctx.strokeStyle = state === "playing" && !ball ? (inGreen ? "#2ecc40" : "#ffd700") : "#fff"; ctx.lineWidth = 4; ctx.beginPath(); ctx.moveTo(GAUGE.x, GAUGE.y); ctx.lineTo(GAUGE.x + Math.cos(na) * (GAUGE.r + 12), GAUGE.y + Math.sin(na) * (GAUGE.r + 12)); ctx.stroke(); ctx.fillStyle = "#fff"; ctx.beginPath(); ctx.arc(GAUGE.x, GAUGE.y, 6, 0, 7); ctx.fill(); // Shooter ctx.fillStyle = "rgba(0,0,0,.2)"; ctx.beginPath(); ctx.ellipse(GAUGE.x, GAUGE.y + 30, 17, 5, 0, 0, 7); ctx.fill(); var Fig = window.HoopsFig || null; if (Fig) { var apF = Fig.appearance('ftf-shooter'); Fig.draw(ctx, { x: GAUGE.x, y: GAUGE.y + 30, s: 0.72, pose: 'shoot', t: Date.now() / 1000, ball: !ball, jersey: '#14418f', num: '7', skin: apF.skin, hair: apF.hair, hairColor: apF.hairColor }); } else { ctx.fillStyle = "#14418f"; ctx.beginPath(); ctx.arc(GAUGE.x, GAUGE.y + 12, 13, 0, 7); ctx.fill(); ctx.fillStyle = "#f2c79b"; ctx.beginPath(); ctx.arc(GAUGE.x, GAUGE.y - 9, 7, 0, 7); ctx.fill(); if (!ball) { ctx.fillStyle = "#e25822"; ctx.beginPath(); ctx.arc(GAUGE.x, GAUGE.y - 40, 8, 0, 7); ctx.fill(); ctx.strokeStyle = "rgba(0,0,0,.35)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(GAUGE.x, GAUGE.y - 40, 8, 0, 7); ctx.stroke(); } } // Shot pips for (var i = 0; i < 10; i++) { ctx.fillStyle = i < shotNum - (state === "playing" ? 1 : 0) || state === "done" ? "#2ecc40" : i === shotNum - 1 && state === "playing" ? "#ffd700" : "rgba(255,255,255,.35)"; ctx.beginPath(); ctx.arc(128 + i * 16, 462, 5, 0, 7); ctx.fill(); } } function draw() { ctx.clearRect(0, 0, W, H); drawCourt(); drawGaugeAndShooter(); if (ball) { var bx, by; if (!ball.bouncing) { var t = ball.t; bx = (1 - t) * (1 - t) * ball.x0 + 2 * (1 - t) * t * ball.cx + t * t * ball.x1; by = (1 - t) * (1 - t) * ball.y0 + 2 * (1 - t) * t * ball.cy + t * t * ball.y1; } else { bx = ball.bx; by = ball.by; } ctx.fillStyle = "#e25822"; ctx.beginPath(); ctx.arc(bx, by, 9, 0, 7); ctx.fill(); ctx.strokeStyle = "rgba(0,0,0,.4)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(bx, by, 9, 0, 7); ctx.stroke(); } floaters.forEach(function (f) { ctx.globalAlpha = Math.max(0, f.life); ctx.fillStyle = "#ffd700"; ctx.font = "bold 17px sans-serif"; ctx.textAlign = "center"; ctx.fillText(f.text, f.x, f.y); ctx.globalAlpha = 1; }); if (state === "idle") { ctx.fillStyle = "rgba(0,0,0,.55)"; ctx.fillRect(0, 196, W, 92); ctx.fillStyle = "#fff"; ctx.font = "bold 23px sans-serif"; ctx.textAlign = "center"; ctx.fillText("🎯 FREE THROW FRENZY", W / 2, 232); ctx.font = "14px sans-serif"; ctx.fillText("10 shots. Pure timing. How pure are you?", W / 2, 256); } if (state === "done") { ctx.fillStyle = "rgba(0,0,0,.72)"; ctx.fillRect(0, 178, W, 128); ctx.fillStyle = "#fff"; ctx.font = "bold 24px sans-serif"; ctx.textAlign = "center"; ctx.fillText("🏁 That's 10!", W / 2, 214); ctx.font = "16px sans-serif"; ctx.fillText("Final score: " + score + " / 20", W / 2, 240); ctx.fillText(makes + "/10 makes • " + perfects + " perfect swishes", W / 2, 264); ctx.font = "13px sans-serif"; ctx.fillText("Enter your name below and Save Score!", W / 2, 288); } } var lastFrame = Date.now(); function loop() { var now = Date.now(); var dt = Math.min(0.05, (now - lastFrame) / 1000); lastFrame = now; if (state === "playing") { if (pauseT > 0) pauseT -= dt; if (!ball && pauseT <= 0) { needle += needleDir * sweepSpeed() * dt; if (needle >= 80) { needle = 80; needleDir = -1; } if (needle <= -80) { needle = -80; needleDir = 1; } } if (ball) { if (!ball.bouncing) { ball.t += dt / 0.55; if (ball.t >= 1) { resolveBall(); if (ball.kind === "perfect" || ball.kind === "make") { ball = null; nextShot(); } else { ball.bouncing = true; ball.bx = ball.x1; ball.by = ball.y1; ball.bvx = (ball.x1 < HOOP.x ? -1 : 1) * (40 + Math.random() * 50); ball.bvy = -110; ball.bt = 0; } } } else { ball.bvy += 700 * dt; ball.bx += ball.bvx * dt; ball.by += ball.bvy * dt; ball.bt += dt; if (ball.bt > 0.45 || ball.by > H - 8) { ball = null; nextShot(); } } } if (netJiggle > 0) netJiggle--; floaters.forEach(function (f) { f.life -= dt * 0.9; f.y -= 26 * dt; }); floaters = floaters.filter(function (f) { return f.life > 0; }); } draw(); requestAnimationFrame(loop); } /* ================= INPUT ================= */ /* Touch overhaul: flick/swipe on the court to shoot — a press that travels 26px+ releases the shot mid-gesture, the way a finger actually shoots. A plain tap still works: it releases on lift. */ var flickStart = null, flickFired = false; cv.addEventListener("pointerdown", function (e) { e.preventDefault(); if (state !== "playing") return; flickStart = { x: e.clientX, y: e.clientY }; flickFired = false; }); cv.addEventListener("pointermove", function (e) { if (!flickStart || flickFired || state !== "playing") return; var dx = e.clientX - flickStart.x, dy = e.clientY - flickStart.y; if (dx * dx + dy * dy >= 26 * 26) { flickFired = true; release(); } }); function flickEnd() { if (flickStart && !flickFired && state === "playing") release(); flickStart = null; } cv.addEventListener("pointerup", flickEnd); cv.addEventListener("pointercancel", function () { flickStart = null; }); window.addEventListener("keydown", function (e) { if (e.code === "Space") { if (document.activeElement && /INPUT|BUTTON/.test(document.activeElement.tagName)) return; e.preventDefault(); if (e.repeat) return; if (state !== "playing") { startGame(); return; } release(); } }); el.start.addEventListener("click", startGame); el.save.addEventListener("click", function () { saveScore(el.name.value, score, el.msg, el.board); }); loadBoard(el.board); reset(); draw(); loop(); })();