/* * End-of-round reward screen (t1173 / t1174). * * Logged-in only. Sequence: a centred "You won!" / "You lost..." for * INTRO seconds, then it tweens to the top (RISE seconds). Once the * server has confirmed the new profile total, blocks of the player's * planet arc from the top of the screen into a counter at the bottom * centre, one block per newly earned point. Each arrival adds 1. * * Spawn rate (t1194): starts at RATE0 = 3 blocks/s and accelerates * linearly, rate(t) = RATE0 + a·t, with `a` chosen per reward so the last * block always leaves at SPAWN_END = 4 s, whatever the point count. * Blocks spawned after t seconds: N(t) = RATE0·t + a·t²/2; block i * (0-based) leaves at T(i) = the t where N(t) = i. The last block is * i = n − 1, so N(4) = n − 1 → a = 2·((n − 1) − RATE0·T) / T². * If n − 1 ≤ RATE0·T (n ≤ 13) a would be ≤ 0: then a constant rate * (n − 1) / T is used (a = 0), so it still ends at 4 s (1 point: one * block at t = 0). * * The pure timing / sequencing part (Reward.spawnTime, Reward.createSequence) * has no DOM and is unit-tested. Reward.play wires it to an overlay. */ (function (root) { 'use strict'; /** Blocks per second at t = 0 (t1177: 10, t1178: 15, t1194: 3). */ var RATE0 = 3; /** Seconds after the first block when the last block leaves (t1194). */ var SPAWN_END = 4; var INTRO = 1.0; // centred title before it rises var RISE = 0.6; // title tween to the top var FLIGHT = 0.7; // one block's arc duration var HOLD = 1.5; // final total shown before continuing var HOLD_ZERO = 1.5; // earned 0: show the number briefly var WAIT_MAX = 8; // give up waiting for the server after this /** * Spawn parameters for a reward of n blocks (t1194): { r0, a } with * rate(t) = r0 + a·t and the last block (i = n − 1) leaving at SPAWN_END. */ function spawnParams (n) { var T = SPAWN_END; var last = Math.max(0, (n | 0) - 1); if (last <= RATE0 * T) { return { r0: last / T, a: 0 }; } return { r0: RATE0, a: 2 * (last - RATE0 * T) / (T * T) }; } /** Seconds after the start of counting when block i (0-based) of n leaves. */ function spawnTime (i, n) { if (!(i > 0)) { return 0; } var p = spawnParams(n == null ? i + 1 : n); if (p.a === 0) { return p.r0 > 0 ? i / p.r0 : 0; } return (-p.r0 + Math.sqrt(p.r0 * p.r0 + 2 * p.a * i)) / p.a; } /** Blocks spawned (left the top) by time t, capped at n. */ function spawnedBy (t, n) { if (!(t >= 0) || !(n > 0)) { return 0; } var p = spawnParams(n); // Block i has left when T(i) <= t, i.e. i <= N(t). var k = Math.floor(p.r0 * t + p.a * t * t / 2 + 1e-9) + 1; return Math.min(n, Math.max(0, k)); } /** Blocks that have arrived in the counter by time t. */ function arrivedBy (t, n, flight) { flight = flight == null ? FLIGHT : flight; return spawnedBy(t - flight, n); } /** Total seconds from the first spawn until the last block arrives. */ function countDuration (n, flight) { flight = flight == null ? FLIGHT : flight; if (!(n > 0)) { return 0; } return spawnTime(n - 1, n) + flight; } /** * A pure, dt-driven sequence. Phases: intro → rise → wait → count → * hold → done. `setResult({ before, after })` supplies the confirmed * totals (earned = after − before, never negative); `setResult(null)` * means the server did not confirm, so nothing is counted and the * title is held briefly. tick(dt) returns * { phase, counter, spawn: [indices], arrived, done }. */ function createSequence (opts) { opts = opts || {}; var s = { phase: 'intro', t: 0, // time inside the current phase countT: 0, won: !!opts.won, flight: opts.flight == null ? FLIGHT : opts.flight, intro: opts.intro == null ? INTRO : opts.intro, rise: opts.rise == null ? RISE : opts.rise, hold: opts.hold == null ? HOLD : opts.hold, holdZero: opts.holdZero == null ? HOLD_ZERO : opts.holdZero, waitMax: opts.waitMax == null ? WAIT_MAX : opts.waitMax, hasResult: false, failed: false, before: 0, after: 0, earned: 0, spawned: 0, arrived: 0, counter: 0 }; function toCount (v) { var n = Number(v); if (!isFinite(n)) { return null; } return Math.max(0, Math.floor(n)); } s.setResult = function (res) { if (s.hasResult) { return; } s.hasResult = true; if (!res) { s.failed = true; return; } var after = toCount(res.after); if (after == null) { s.failed = true; return; } var before = (res.before != null && res.before !== '') ? toCount(res.before) : after; if (before == null) { before = after; } if (before > after) { before = after; } s.before = before; s.after = after; s.earned = after - before; s.counter = before; }; s.finish = function () { if (s.phase !== 'hold') { return false; } s.phase = 'done'; return true; }; function enter (phase) { s.phase = phase; s.t = 0; } s.tick = function (dt) { var out = { phase: s.phase, counter: s.counter, spawn: [], arrived: 0, done: false }; if (s.phase === 'done') { out.done = true; return out; } dt = Math.max(0, Number(dt) || 0); var left = dt; // Allow several phase changes inside one large dt. for (var guard = 0; guard < 8 && left >= 0; guard++) { if (s.phase === 'intro') { if (s.t + left < s.intro) { s.t += left; left = -1; break; } left -= (s.intro - s.t); enter('rise'); } else if (s.phase === 'rise') { if (s.t + left < s.rise) { s.t += left; left = -1; break; } left -= (s.rise - s.t); enter('wait'); } else if (s.phase === 'wait') { if (!s.hasResult) { s.t += left; if (s.t >= s.waitMax) { s.hasResult = true; s.failed = true; enter('hold'); continue; } left = -1; break; } if (s.failed || s.earned <= 0) { enter('hold'); continue; } enter('count'); s.countT = 0; } else if (s.phase === 'count') { s.countT += left; left = -1; var sp = spawnedBy(s.countT, s.earned); for (var i = s.spawned; i < sp; i++) { out.spawn.push(i); } s.spawned = sp; var ar = arrivedBy(s.countT, s.earned, s.flight); out.arrived = ar - s.arrived; s.arrived = ar; s.counter = s.before + ar; if (ar >= s.earned) { s.counter = s.after; enter('hold'); } break; } else if (s.phase === 'hold') { var need = (s.failed || s.earned <= 0) ? s.holdZero : s.hold; if (s.t + left < need) { s.t += left; left = -1; break; } enter('done'); break; } else { break; } } out.phase = s.phase; out.counter = s.counter; out.done = s.phase === 'done'; return out; }; return s; } /* ---------------- DOM / canvas player (browser only) ---------------- */ var active = null; function reducedMotion () { try { return !!(root.matchMedia && root.matchMedia('(prefers-reduced-motion: reduce)').matches); } catch (e) { return false; } } function tr (key, fallback) { if (typeof I18n !== 'undefined' && I18n.t) { var v = I18n.t(key); if (v && v !== key) { return v; } } return fallback; } function planetImages (planet) { var list = []; var colors = (planet && planet.colors) || []; for (var i = 0; i < colors.length; i++) { if (!colors[i] || !colors[i].image) { continue; } var img = new Image(); img.decoding = 'async'; img.src = colors[i].image; list.push({ img: img, hex: colors[i].hex || '#ffffff' }); } if (!list.length) { list.push({ img: null, hex: '#ffffff' }); } return list; } /** * Show the reward overlay. opts: { won, planet, result: Promise<{before, * after}|null>, onDone }. Returns a handle with .active and .cancel(). */ function play (opts) { opts = opts || {}; if (active) { active.cancel(); } var doc = root.document; var overlay = doc.getElementById('reward'); var title = doc.getElementById('reward-title'); var counter = doc.getElementById('reward-total'); var label = doc.getElementById('reward-label'); var canvas = doc.getElementById('reward-canvas'); var hint = doc.getElementById('reward-hint'); if (!overlay || !title || !counter || !canvas) { if (opts.onDone) { opts.onDone(); } return null; } var still = reducedMotion(); var seq = createSequence({ won: opts.won, rise: still ? 0 : RISE }); var imgs = planetImages(opts.planet); var flying = []; var last = 0; var raf = 0; var pop = 0; var ctx = canvas.getContext('2d'); var dpr = Math.min(2, root.devicePixelRatio || 1); var W = 0; var H = 0; // t1193: quitting via the in-game menu shows a neutral title. title.textContent = opts.title ? String(opts.title) : (opts.won ? tr('reward.won', 'You won!') : tr('reward.lost', 'You lost...')); title.className = 'reward-title' + (opts.title ? ' ended' : (opts.won ? ' won' : ' lost')); if (label) { label.textContent = tr('reward.total', 'Total points'); } counter.textContent = ''; counter.parentNode.hidden = true; if (hint) { hint.hidden = true; hint.textContent = tr('reward.continue', 'Tap to continue'); } overlay.classList.toggle('reduced', still); overlay.hidden = false; function resize () { W = root.innerWidth || 360; H = root.innerHeight || 640; canvas.width = Math.round(W * dpr); canvas.height = Math.round(H * dpr); canvas.style.width = W + 'px'; canvas.style.height = H + 'px'; if (ctx) { ctx.setTransform(dpr, 0, 0, dpr, 0, 0); } } resize(); root.addEventListener('resize', resize); function target () { var r = counter.getBoundingClientRect(); if (r.width > 0) { return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; } return { x: W / 2, y: H - 80 }; } function spawn () { var size = Math.max(28, Math.min(56, Math.round(Math.min(W, H) * 0.075))); var sx = size + Math.random() * Math.max(1, W - 2 * size); var sy = -size; var tg = target(); // Random arc: control point pushed sideways and up. var mx = (sx + tg.x) / 2 + (Math.random() - 0.5) * W * 0.9; var my = Math.min(sy, tg.y) + (Math.random() * 0.5) * H; flying.push({ t: 0, sx: sx, sy: sy, cx: mx, cy: my, pick: imgs[Math.floor(Math.random() * imgs.length)], size: size, rot: (Math.random() - 0.5) * 6 }); } function draw (dt) { if (!ctx) { return; } ctx.clearRect(0, 0, W, H); var tg = target(); for (var i = flying.length - 1; i >= 0; i--) { var f = flying[i]; f.t += dt / FLIGHT; if (f.t >= 1) { flying.splice(i, 1); continue; } var u = f.t; // Ease in a little so blocks accelerate into the counter. var e = u * u * (3 - 2 * u) * 0.35 + u * 0.65; var a = 1 - e; var x = a * a * f.sx + 2 * a * e * f.cx + e * e * tg.x; var y = a * a * f.sy + 2 * a * e * f.cy + e * e * tg.y; var s = f.size * (1 - 0.45 * e); ctx.save(); ctx.translate(x, y); ctx.rotate(f.rot * e); if (f.pick.img && f.pick.img.complete && f.pick.img.naturalWidth) { ctx.drawImage(f.pick.img, -s / 2, -s / 2, s, s); } else { ctx.fillStyle = f.pick.hex; ctx.fillRect(-s / 2, -s / 2, s, s); } ctx.restore(); } } function setCounter (v) { counter.textContent = String(v); } var handle = { active: true, seq: seq, flyingCount: function () { return flying.length; }, cancel: function () { cleanup(false); } }; function onTap (e) { if (seq.phase !== 'hold') { return; } if (e && e.type === 'keydown' && e.key !== ' ' && e.key !== 'Enter' && e.key !== 'Spacebar') { return; } if (e && e.preventDefault) { e.preventDefault(); } seq.finish(); } overlay.addEventListener('pointerup', onTap); doc.addEventListener('keydown', onTap); function cleanup (callDone) { if (!handle.active) { return; } handle.active = false; if (raf && root.cancelAnimationFrame) { root.cancelAnimationFrame(raf); } root.removeEventListener('resize', resize); overlay.removeEventListener('pointerup', onTap); doc.removeEventListener('keydown', onTap); overlay.hidden = true; overlay.classList.remove('risen'); if (ctx) { ctx.clearRect(0, 0, W, H); } if (active === handle) { active = null; } if (callDone && opts.onDone) { opts.onDone(); } } Promise.resolve(opts.result).then(function (res) { seq.setResult(res); }, function () { seq.setResult(null); }); function frame (now) { if (!handle.active) { return; } var dt = last ? Math.min(0.1, (now - last) / 1000) : 0; last = now; var prev = seq.phase; var out = seq.tick(dt); if (prev === 'intro' && out.phase !== 'intro') { overlay.classList.add('risen'); } if ((out.phase === 'count' || out.phase === 'hold' || out.phase === 'done') && counter.parentNode.hidden && seq.hasResult && !seq.failed) { counter.parentNode.hidden = false; setCounter(seq.before); } if (!still) { for (var i = 0; i < out.spawn.length; i++) { spawn(); } } if (out.arrived > 0) { pop = 1; setCounter(out.counter); } if (out.phase === 'hold' && !seq.failed) { setCounter(seq.after); } if (out.phase === 'hold' && hint && hint.hidden) { hint.hidden = false; } if (pop > 0) { pop = Math.max(0, pop - dt * 6); counter.style.transform = 'scale(' + (1 + 0.18 * pop).toFixed(3) + ')'; } draw(dt); if (out.done) { cleanup(true); return; } raf = root.requestAnimationFrame(frame); } raf = root.requestAnimationFrame(frame); active = handle; return handle; } var Reward = { RATE0: RATE0, SPAWN_END: SPAWN_END, spawnParams: spawnParams, INTRO: INTRO, RISE: RISE, FLIGHT: FLIGHT, HOLD: HOLD, HOLD_ZERO: HOLD_ZERO, WAIT_MAX: WAIT_MAX, spawnTime: spawnTime, spawnedBy: spawnedBy, arrivedBy: arrivedBy, countDuration: countDuration, createSequence: createSequence, play: play, isActive: function () { return !!(active && active.active); }, /** Read-only snapshot for tests and debugging. */ debugState: function () { if (!active) { return null; } var q = active.seq; return { phase: q.phase, counter: q.counter, before: q.before, after: q.after, earned: q.earned, spawned: q.spawned, arrived: q.arrived, failed: q.failed, won: q.won, flying: active.flyingCount() }; } }; if (typeof module !== 'undefined' && module.exports) { module.exports = Reward; } root.Reward = Reward; })(typeof globalThis !== 'undefined' ? globalThis : this);