| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const Reward = require('../src/reward.js'); |
| 6 | |
| 7 | function runToEnd (seq, dt, maxT) |
| 8 | { |
| 9 | const events = []; |
| 10 | let t = 0; |
| 11 | let spawned = 0; |
| 12 | let arrived = 0; |
| 13 | let lastCounter = null; |
| 14 | let monotonic = true; |
| 15 | while (t < maxT) |
| 16 | { |
| 17 | const out = seq.tick(dt); |
| 18 | t += dt; |
| 19 | spawned += out.spawn.length; |
| 20 | arrived += out.arrived; |
| 21 | if (lastCounter != null && out.counter < lastCounter) |
| 22 | { monotonic = false; } |
| 23 | lastCounter = out.counter; |
| 24 | events.push({ t, phase: out.phase, counter: out.counter }); |
| 25 | if (out.done) |
| 26 | { break; } |
| 27 | } |
| 28 | return { events, t, spawned, arrived, monotonic, last: events[events.length - 1] }; |
| 29 | } |
| 30 | |
| 31 | describe('Reward spawn schedule (t1194: 3 blocks/s, last block at 4 s)', () => |
| 32 | { |
| 33 | it('constants and per-reward acceleration a = 2((n−1) − 3·4) / 4²', () => |
| 34 | { |
| 35 | assert.equal(Reward.RATE0, 3); |
| 36 | assert.equal(Reward.SPAWN_END, 4); |
| 37 | assert.deepEqual(Reward.spawnParams(253), { r0: 3, a: 2 * (252 - 12) / 16 }); |
| 38 | assert.deepEqual(Reward.spawnParams(13), { r0: 3, a: 0 }); |
| 39 | assert.deepEqual(Reward.spawnParams(5), { r0: 1, a: 0 }, 'n ≤ 13: constant (n−1)/4'); |
| 40 | assert.deepEqual(Reward.spawnParams(1), { r0: 0, a: 0 }); |
| 41 | }); |
| 42 | |
| 43 | it('the last block always leaves at 4 s, whatever the point count', () => |
| 44 | { |
| 45 | for (const n of [2, 5, 12, 13, 14, 50, 253, 800, 2000, 10000]) |
| 46 | { |
| 47 | const last = Reward.spawnTime(n - 1, n); |
| 48 | assert.ok(Math.abs(last - 4) < 1e-9, 'n=' + n + ' last=' + last); |
| 49 | assert.ok(Reward.spawnedBy(4 - 1e-3, n) < n, 'n=' + n + ' not all out just before 4 s'); |
| 50 | assert.equal(Reward.spawnedBy(4 + 1e-6, n), n, 'n=' + n + ' at 4 s'); |
| 51 | assert.ok(Math.abs(Reward.countDuration(n) - (4 + Reward.FLIGHT)) < 1e-9); |
| 52 | } |
| 53 | assert.equal(Reward.spawnTime(0, 1), 0); |
| 54 | assert.equal(Reward.countDuration(1), Reward.FLIGHT); |
| 55 | }); |
| 56 | |
| 57 | it('block i leaves when 3t + a·t²/2 = i; starts at 3/s and accelerates', () => |
| 58 | { |
| 59 | const n = 253; |
| 60 | const { r0, a } = Reward.spawnParams(n); |
| 61 | for (const i of [1, 5, 10, 50, 100, 252]) |
| 62 | { |
| 63 | const t = Reward.spawnTime(i, n); |
| 64 | assert.ok(Math.abs(r0 * t + a * t * t / 2 - i) < 1e-9, 'i=' + i + ' t=' + t); |
| 65 | } |
| 66 | // First gap ≈ 1/3 s at the start rate (slightly less: it accelerates). |
| 67 | const g1 = Reward.spawnTime(1, n); |
| 68 | assert.ok(g1 < 1 / 3 && g1 > 0.1, 'first gap ' + g1); |
| 69 | let prevGap = Infinity; |
| 70 | for (let i = 1; i < n; i++) |
| 71 | { |
| 72 | const gap = Reward.spawnTime(i, n) - Reward.spawnTime(i - 1, n); |
| 73 | assert.ok(gap > 0); |
| 74 | assert.ok(gap <= prevGap + 1e-12, 'gap grew at ' + i); |
| 75 | prevGap = gap; |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | it('spawnedBy matches floor(r0·t + a·t²/2) + 1, capped by n', () => |
| 80 | { |
| 81 | const { r0, a } = Reward.spawnParams(253); |
| 82 | for (let t = 0; t <= 6; t += 0.137) |
| 83 | { |
| 84 | const expect = Math.min(253, Math.floor(r0 * t + a * t * t / 2 + 1e-9) + 1); |
| 85 | assert.equal(Reward.spawnedBy(t, 253), expect, 't=' + t); |
| 86 | } |
| 87 | assert.equal(Reward.spawnedBy(-1, 10), 0); |
| 88 | assert.equal(Reward.spawnedBy(5, 0), 0); |
| 89 | assert.equal(Reward.spawnedBy(100, 7), 7); |
| 90 | assert.equal(Reward.arrivedBy(Reward.FLIGHT - 0.01, 10), 0); |
| 91 | assert.equal(Reward.arrivedBy(Reward.FLIGHT, 10), 1); |
| 92 | }); |
| 93 | |
| 94 | it('measured end times through the real sequence (60 fps): ~4 s for 5…2000 points', () => |
| 95 | { |
| 96 | for (const n of [5, 50, 253, 800, 2000]) |
| 97 | { |
| 98 | const seq = Reward.createSequence({ won: true }); |
| 99 | seq.setResult({ before: 100, after: 100 + n }); |
| 100 | let t = 0, firstSpawn = null, lastSpawn = null, lastArrive = null, prev = 0; |
| 101 | for (let k = 0; k < 60 * 30; k++) |
| 102 | { |
| 103 | const out = seq.tick(1 / 60); |
| 104 | t += 1 / 60; |
| 105 | if (out.spawn.length) |
| 106 | { |
| 107 | if (firstSpawn == null) firstSpawn = t; |
| 108 | lastSpawn = t; |
| 109 | } |
| 110 | if (seq.arrived === n && lastArrive == null && prev < n) lastArrive = t; |
| 111 | prev = seq.arrived; |
| 112 | if (out.done) break; |
| 113 | } |
| 114 | const spawnSpan = lastSpawn - firstSpawn; |
| 115 | assert.ok(Math.abs(spawnSpan - 4) < 0.05, 'n=' + n + ' last spawn after ' + spawnSpan.toFixed(3) + ' s'); |
| 116 | assert.equal(seq.counter, 100 + n); |
| 117 | } |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | describe('Reward sequence (t1173)', () => |
| 122 | { |
| 123 | it('500 + 253 → counts every point from 500 up to 753 (last block at 4 s)', () => |
| 124 | { |
| 125 | const seq = Reward.createSequence({ won: true }); |
| 126 | seq.setResult({ before: 500, after: 753 }); |
| 127 | const r = runToEnd(seq, 1 / 60, 60); |
| 128 | assert.equal(r.last.phase, 'done'); |
| 129 | assert.equal(r.spawned, 253); |
| 130 | assert.equal(r.arrived, 253); |
| 131 | assert.ok(r.monotonic, 'counter never goes down'); |
| 132 | assert.equal(seq.counter, 753); |
| 133 | const counting = r.events.filter((e) => e.phase === 'count'); |
| 134 | assert.ok(counting.length > 0); |
| 135 | assert.ok(counting[0].counter >= 500 && counting[0].counter < 510); |
| 136 | // intro + rise + count + hold |
| 137 | const expected = Reward.INTRO + Reward.RISE + Reward.countDuration(253) + Reward.HOLD; |
| 138 | assert.ok(Math.abs(r.t - expected) < 0.1, 't=' + r.t + ' expected ' + expected); |
| 139 | }); |
| 140 | |
| 141 | it('phases run intro → rise → wait → count → hold → done', () => |
| 142 | { |
| 143 | const seq = Reward.createSequence({}); |
| 144 | const phases = []; |
| 145 | const push = (p) => { if (phases[phases.length - 1] !== p) { phases.push(p); } }; |
| 146 | push(seq.phase); |
| 147 | for (let i = 0; i < 60; i++) { push(seq.tick(0.05).phase); } // 3 s, no result yet |
| 148 | assert.deepEqual(phases, ['intro', 'rise', 'wait']); |
| 149 | assert.equal(seq.counter, 0); |
| 150 | seq.setResult({ before: 10, after: 13 }); |
| 151 | for (let i = 0; i < 200; i++) { push(seq.tick(0.05).phase); } |
| 152 | assert.deepEqual(phases, ['intro', 'rise', 'wait', 'count', 'hold', 'done']); |
| 153 | assert.equal(seq.counter, 13); |
| 154 | }); |
| 155 | |
| 156 | it('title stays centred for one second before rising', () => |
| 157 | { |
| 158 | const seq = Reward.createSequence({}); |
| 159 | assert.equal(seq.tick(0.99).phase, 'intro'); |
| 160 | assert.equal(seq.tick(0.02).phase, 'rise'); |
| 161 | }); |
| 162 | |
| 163 | it('nothing is counted before the server result arrives', () => |
| 164 | { |
| 165 | const seq = Reward.createSequence({ waitMax: 100 }); |
| 166 | let spawned = 0; |
| 167 | for (let i = 0; i < 100; i++) { spawned += seq.tick(0.05).spawn.length; } |
| 168 | assert.equal(spawned, 0); |
| 169 | assert.equal(seq.phase, 'wait'); |
| 170 | }); |
| 171 | |
| 172 | it('earned 0 shows the number briefly, no blocks', () => |
| 173 | { |
| 174 | const seq = Reward.createSequence({}); |
| 175 | seq.setResult({ before: 42, after: 42 }); |
| 176 | const r = runToEnd(seq, 0.05, 30); |
| 177 | assert.equal(r.spawned, 0); |
| 178 | assert.equal(seq.counter, 42); |
| 179 | assert.ok(r.events.some((e) => e.phase === 'hold' && e.counter === 42)); |
| 180 | assert.equal(r.last.phase, 'done'); |
| 181 | const expected = Reward.INTRO + Reward.RISE + Reward.HOLD_ZERO; |
| 182 | assert.ok(Math.abs(r.t - expected) < 0.11); |
| 183 | }); |
| 184 | |
| 185 | it('server failure: no count, still continues', () => |
| 186 | { |
| 187 | const seq = Reward.createSequence({}); |
| 188 | seq.setResult(null); |
| 189 | const r = runToEnd(seq, 0.05, 30); |
| 190 | assert.equal(r.spawned, 0); |
| 191 | assert.equal(seq.failed, true); |
| 192 | assert.equal(r.last.phase, 'done'); |
| 193 | }); |
| 194 | |
| 195 | it('no answer at all times out and continues', () => |
| 196 | { |
| 197 | const seq = Reward.createSequence({ waitMax: 2 }); |
| 198 | const r = runToEnd(seq, 0.05, 30); |
| 199 | assert.equal(r.last.phase, 'done'); |
| 200 | assert.equal(seq.failed, true); |
| 201 | }); |
| 202 | |
| 203 | it('before > after or a missing before is clamped (never counts down)', () => |
| 204 | { |
| 205 | const a = Reward.createSequence({}); |
| 206 | a.setResult({ before: 900, after: 800 }); |
| 207 | assert.equal(a.earned, 0); |
| 208 | assert.equal(a.counter, 800); |
| 209 | const b = Reward.createSequence({}); |
| 210 | b.setResult({ after: 77 }); |
| 211 | assert.equal(b.earned, 0); |
| 212 | assert.equal(b.before, 77); |
| 213 | }); |
| 214 | |
| 215 | it('numeric strings are accepted (t1278)', () => |
| 216 | { |
| 217 | const seq = Reward.createSequence({ intro: 0, rise: 0, flight: 0, hold: 0 }); |
| 218 | seq.setResult({ before: '100', after: '109' }); |
| 219 | assert.equal(seq.failed, false); |
| 220 | assert.equal(seq.earned, 9); |
| 221 | assert.equal(seq.before, 100); |
| 222 | assert.equal(seq.after, 109); |
| 223 | const out = seq.tick(0); |
| 224 | assert.equal(out.phase, 'count'); |
| 225 | }); |
| 226 | |
| 227 | it('a huge frame (tab in background) still lands exactly on the total', () => |
| 228 | { |
| 229 | const seq = Reward.createSequence({}); |
| 230 | seq.setResult({ before: 0, after: 40 }); |
| 231 | let spawned = 0; |
| 232 | let out; |
| 233 | for (let i = 0; i < 10; i++) |
| 234 | { |
| 235 | out = seq.tick(5); |
| 236 | spawned += out.spawn.length; |
| 237 | } |
| 238 | assert.equal(spawned, 40); |
| 239 | assert.equal(seq.counter, 40); |
| 240 | assert.equal(out.done, true); |
| 241 | }); |
| 242 | |
| 243 | it('only the hold phase can be finished early (tap to continue), not the count', () => |
| 244 | { |
| 245 | const seq = Reward.createSequence({}); |
| 246 | seq.setResult({ before: 0, after: 100 }); |
| 247 | seq.tick(2); |
| 248 | assert.equal(seq.phase, 'count'); |
| 249 | assert.equal(seq.finish(), false); |
| 250 | assert.equal(seq.phase, 'count'); |
| 251 | while (seq.phase === 'count') { seq.tick(0.1); } |
| 252 | assert.equal(seq.phase, 'hold'); |
| 253 | assert.equal(seq.counter, 100); |
| 254 | assert.equal(seq.finish(), true); |
| 255 | assert.equal(seq.tick(0.01).done, true); |
| 256 | }); |
| 257 | }); |
projects-timfalken-com meteors