| 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 (t1178: 15 + 10t blocks/s)', () => |
| 32 | { |
| 33 | it('block i leaves when 15t + 10t²/2 = i', () => |
| 34 | { |
| 35 | assert.equal(Reward.RATE0, 15); |
| 36 | assert.equal(Reward.ACCEL, 10); |
| 37 | assert.equal(Reward.spawnTime(0), 0); |
| 38 | for (const i of [1, 5, 10, 50, 100, 252, 1000]) |
| 39 | { |
| 40 | const t = Reward.spawnTime(i); |
| 41 | assert.ok(Math.abs(15 * t + 10 * t * t / 2 - i) < 1e-9, 'i=' + i + ' t=' + t); |
| 42 | } |
| 43 | // spawn times strictly increase, gaps shrink (accelerating) |
| 44 | let prevGap = Infinity; |
| 45 | for (let i = 1; i < 300; i++) |
| 46 | { |
| 47 | const gap = Reward.spawnTime(i) - Reward.spawnTime(i - 1); |
| 48 | assert.ok(gap > 0); |
| 49 | assert.ok(gap <= prevGap + 1e-12, 'gap grew at ' + i); |
| 50 | prevGap = gap; |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | it('starts at 15/s; 253 ≈ 5.8 s, 800 ≈ 11.2 s, 2000 ≈ 18.6 s (Tim\'s examples)', () => |
| 55 | { |
| 56 | // 1/5 s between the first two blocks. |
| 57 | assert.ok(Math.abs(Reward.spawnTime(1) - (-15 + Math.sqrt(245)) / 10) < 1e-12); |
| 58 | assert.ok(Reward.spawnTime(1) > 0.06 && Reward.spawnTime(1) < 0.067); |
| 59 | const last = (-15 + Math.sqrt(225 + 20 * 252)) / 10; |
| 60 | assert.ok(Math.abs(Reward.spawnTime(252) - last) < 1e-9); |
| 61 | assert.ok(last > 5.7 && last < 5.85, 'last=' + last); |
| 62 | assert.ok(Math.abs(Reward.spawnTime(799) - 11.2) < 0.05, '800: ' + Reward.spawnTime(799)); |
| 63 | assert.ok(Math.abs(Reward.spawnTime(1999) - 18.6) < 0.1, '2000: ' + Reward.spawnTime(1999)); |
| 64 | assert.equal(Reward.spawnedBy(last + 1e-6, 253), 253); |
| 65 | assert.equal(Reward.spawnedBy(last - 0.01, 253), 252); |
| 66 | assert.ok(Math.abs(Reward.countDuration(253) - (last + Reward.FLIGHT)) < 1e-9); |
| 67 | }); |
| 68 | |
| 69 | it('spawnedBy matches floor(15t + 10t²/2) + 1, capped by n', () => |
| 70 | { |
| 71 | for (let t = 0; t <= 20; t += 0.37) |
| 72 | { |
| 73 | const expect = Math.min(253, Math.floor(15 * t + 10 * t * t / 2 + 1e-9) + 1); |
| 74 | assert.equal(Reward.spawnedBy(t, 253), expect, 't=' + t); |
| 75 | } |
| 76 | assert.equal(Reward.spawnedBy(-1, 10), 0); |
| 77 | assert.equal(Reward.spawnedBy(5, 0), 0); |
| 78 | assert.equal(Reward.spawnedBy(100, 7), 7); |
| 79 | assert.equal(Reward.arrivedBy(Reward.FLIGHT - 0.01, 10), 0); |
| 80 | assert.equal(Reward.arrivedBy(Reward.FLIGHT, 10), 1); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | describe('Reward sequence (t1173)', () => |
| 85 | { |
| 86 | it('500 + 253 → counts every point from 500 up to 753 in ~6.5 s of counting', () => |
| 87 | { |
| 88 | const seq = Reward.createSequence({ won: true }); |
| 89 | seq.setResult({ before: 500, after: 753 }); |
| 90 | const r = runToEnd(seq, 1 / 60, 60); |
| 91 | assert.equal(r.last.phase, 'done'); |
| 92 | assert.equal(r.spawned, 253); |
| 93 | assert.equal(r.arrived, 253); |
| 94 | assert.ok(r.monotonic, 'counter never goes down'); |
| 95 | assert.equal(seq.counter, 753); |
| 96 | const counting = r.events.filter((e) => e.phase === 'count'); |
| 97 | assert.ok(counting.length > 0); |
| 98 | assert.ok(counting[0].counter >= 500 && counting[0].counter < 510); |
| 99 | // intro + rise + count + hold |
| 100 | const expected = Reward.INTRO + Reward.RISE + Reward.countDuration(253) + Reward.HOLD; |
| 101 | assert.ok(Math.abs(r.t - expected) < 0.1, 't=' + r.t + ' expected ' + expected); |
| 102 | }); |
| 103 | |
| 104 | it('phases run intro → rise → wait → count → hold → done', () => |
| 105 | { |
| 106 | const seq = Reward.createSequence({}); |
| 107 | const phases = []; |
| 108 | const push = (p) => { if (phases[phases.length - 1] !== p) { phases.push(p); } }; |
| 109 | push(seq.phase); |
| 110 | for (let i = 0; i < 60; i++) { push(seq.tick(0.05).phase); } // 3 s, no result yet |
| 111 | assert.deepEqual(phases, ['intro', 'rise', 'wait']); |
| 112 | assert.equal(seq.counter, 0); |
| 113 | seq.setResult({ before: 10, after: 13 }); |
| 114 | for (let i = 0; i < 200; i++) { push(seq.tick(0.05).phase); } |
| 115 | assert.deepEqual(phases, ['intro', 'rise', 'wait', 'count', 'hold', 'done']); |
| 116 | assert.equal(seq.counter, 13); |
| 117 | }); |
| 118 | |
| 119 | it('title stays centred for one second before rising', () => |
| 120 | { |
| 121 | const seq = Reward.createSequence({}); |
| 122 | assert.equal(seq.tick(0.99).phase, 'intro'); |
| 123 | assert.equal(seq.tick(0.02).phase, 'rise'); |
| 124 | }); |
| 125 | |
| 126 | it('nothing is counted before the server result arrives', () => |
| 127 | { |
| 128 | const seq = Reward.createSequence({ waitMax: 100 }); |
| 129 | let spawned = 0; |
| 130 | for (let i = 0; i < 100; i++) { spawned += seq.tick(0.05).spawn.length; } |
| 131 | assert.equal(spawned, 0); |
| 132 | assert.equal(seq.phase, 'wait'); |
| 133 | }); |
| 134 | |
| 135 | it('earned 0 shows the number briefly, no blocks', () => |
| 136 | { |
| 137 | const seq = Reward.createSequence({}); |
| 138 | seq.setResult({ before: 42, after: 42 }); |
| 139 | const r = runToEnd(seq, 0.05, 30); |
| 140 | assert.equal(r.spawned, 0); |
| 141 | assert.equal(seq.counter, 42); |
| 142 | assert.ok(r.events.some((e) => e.phase === 'hold' && e.counter === 42)); |
| 143 | assert.equal(r.last.phase, 'done'); |
| 144 | const expected = Reward.INTRO + Reward.RISE + Reward.HOLD_ZERO; |
| 145 | assert.ok(Math.abs(r.t - expected) < 0.11); |
| 146 | }); |
| 147 | |
| 148 | it('server failure: no count, still continues', () => |
| 149 | { |
| 150 | const seq = Reward.createSequence({}); |
| 151 | seq.setResult(null); |
| 152 | const r = runToEnd(seq, 0.05, 30); |
| 153 | assert.equal(r.spawned, 0); |
| 154 | assert.equal(seq.failed, true); |
| 155 | assert.equal(r.last.phase, 'done'); |
| 156 | }); |
| 157 | |
| 158 | it('no answer at all times out and continues', () => |
| 159 | { |
| 160 | const seq = Reward.createSequence({ waitMax: 2 }); |
| 161 | const r = runToEnd(seq, 0.05, 30); |
| 162 | assert.equal(r.last.phase, 'done'); |
| 163 | assert.equal(seq.failed, true); |
| 164 | }); |
| 165 | |
| 166 | it('before > after or a missing before is clamped (never counts down)', () => |
| 167 | { |
| 168 | const a = Reward.createSequence({}); |
| 169 | a.setResult({ before: 900, after: 800 }); |
| 170 | assert.equal(a.earned, 0); |
| 171 | assert.equal(a.counter, 800); |
| 172 | const b = Reward.createSequence({}); |
| 173 | b.setResult({ after: 77 }); |
| 174 | assert.equal(b.earned, 0); |
| 175 | assert.equal(b.before, 77); |
| 176 | }); |
| 177 | |
| 178 | it('a huge frame (tab in background) still lands exactly on the total', () => |
| 179 | { |
| 180 | const seq = Reward.createSequence({}); |
| 181 | seq.setResult({ before: 0, after: 40 }); |
| 182 | let spawned = 0; |
| 183 | let out; |
| 184 | for (let i = 0; i < 10; i++) |
| 185 | { |
| 186 | out = seq.tick(5); |
| 187 | spawned += out.spawn.length; |
| 188 | } |
| 189 | assert.equal(spawned, 40); |
| 190 | assert.equal(seq.counter, 40); |
| 191 | assert.equal(out.done, true); |
| 192 | }); |
| 193 | |
| 194 | it('only the hold phase can be finished early (tap to continue), not the count', () => |
| 195 | { |
| 196 | const seq = Reward.createSequence({}); |
| 197 | seq.setResult({ before: 0, after: 100 }); |
| 198 | seq.tick(2); |
| 199 | assert.equal(seq.phase, 'count'); |
| 200 | assert.equal(seq.finish(), false); |
| 201 | assert.equal(seq.phase, 'count'); |
| 202 | while (seq.phase === 'count') { seq.tick(0.1); } |
| 203 | assert.equal(seq.phase, 'hold'); |
| 204 | assert.equal(seq.counter, 100); |
| 205 | assert.equal(seq.finish(), true); |
| 206 | assert.equal(seq.tick(0.01).done, true); |
| 207 | }); |
| 208 | }); |
projects-timfalken-com meteors