BlueGit

projects-timfalken-com meteors

Files

meteors / test / offline.test.js

offline.test.js

offline.test.js 6.9 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, Planet, Rng, GameCore } = require('./helpers.js');
6const Offline = require('../src/offline.js');
7
8describe('Offline Strijd progression (t1119)', () =>
9{
10 it('one opponent for one round, then N opponents for N-1 rounds', () =>
11 {
12 assert.equal(Offline.roundsAtOpponentCount(1), 1);
13 assert.equal(Offline.roundsAtOpponentCount(2), 1);
14 assert.equal(Offline.roundsAtOpponentCount(3), 2);
15 assert.equal(Offline.roundsAtOpponentCount(6), 5);
16
17 const expect = {
18 1: { opponents: 1 },
19 2: { opponents: 2 },
20 3: { opponents: 3 },
21 4: { opponents: 3 },
22 5: { opponents: 4 },
23 6: { opponents: 4 },
24 7: { opponents: 4 },
25 8: { opponents: 5 }
26 };
27 for (const [round, exp] of Object.entries(expect))
28 {
29 const s = Offline.scheduleForRound(Number(round));
30 assert.equal(s.opponents, exp.opponents, 'round ' + round + ' opponents=' + s.opponents);
31 assert.equal(s.round, Number(round));
32 }
33 });
34
35 it('skill starts at 5% and rises 1% per round, capped at 100%', () =>
36 {
37 assert.equal(Offline.skillForRound(1), 0.05);
38 assert.equal(Offline.skillForRound(2), 0.06);
39 assert.equal(Offline.skillForRound(96), 1);
40 assert.equal(Offline.skillForRound(97), 1);
41 assert.equal(Offline.skillForRound(200), 1);
42 assert.equal(Offline.scheduleForRound(1).skill, 0.05);
43 assert.equal(Offline.scheduleForRound(10).skill, 0.14);
44 });
45});
46
47describe('Bot move finder and roll (t1119)', () =>
48{
49 it('finds the shortest vertical move that creates a match', () =>
50 {
51 const g = makeGame({ noSpawn: true });
52 // Column 0: [0, 0, 1, 0] — moving the 1 up (or the top 0 down) makes [0,0,0,1].
53 g.place(0, 0, 0);
54 g.place(0, 1, 0);
55 g.place(0, 2, 1);
56 g.place(0, 3, 0);
57 const move = Offline.findBotMove(g);
58 assert.ok(move, 'expected a move');
59 assert.equal(move.col, 0);
60 assert.equal(move.dist, 1);
61 assert.ok(
62 (move.fromY === 2 && move.toY === 3) || (move.fromY === 3 && move.toY === 2),
63 'from=' + move.fromY + ' to=' + move.toY
64 );
65 assert.ok(Offline.executeBotMove(g, move));
66 const launched = g.resolveMatches();
67 assert.ok(launched.length >= 1, 'the move must create a match');
68 });
69
70 it('returns null when no vertical move creates a match', () =>
71 {
72 const g = makeGame({ noSpawn: true });
73 g.place(0, 0, 0);
74 g.place(0, 1, 1);
75 g.place(0, 2, 2);
76 g.place(1, 0, 3);
77 g.place(1, 1, 4);
78 assert.equal(Offline.findBotMove(g), null);
79 });
80
81 it('a roll at skill 0 never moves, skill 1 always tries', () =>
82 {
83 const setup = () =>
84 {
85 const g = makeGame({ noSpawn: true });
86 g.place(0, 0, 0);
87 g.place(0, 1, 0);
88 g.place(0, 2, 1);
89 g.place(0, 3, 0);
90 return g;
91 };
92 const g0 = setup();
93 const r0 = Offline.tryBotRoll(g0, 0, () => 0);
94 assert.equal(r0.success, false);
95 assert.equal(r0.moved, false);
96 assert.equal(g0.blocks[2].y, 2, 'nothing moved at skill 0 even with roll 0');
97
98 const g1 = setup();
99 const r1 = Offline.tryBotRoll(g1, 1, () => 0.999);
100 assert.equal(r1.success, true);
101 assert.equal(r1.moved, true);
102 assert.ok(r1.move);
103
104 // roll == skill is a failure (success only when roll < skill)
105 const g2 = setup();
106 const r2 = Offline.tryBotRoll(g2, 0.05, () => 0.05);
107 assert.equal(r2.success, false);
108 assert.equal(r2.moved, false);
109 });
110});
111
112describe('Offline battle flow (t1119)', () =>
113{
114 it('starts a round with the scheduled number of bots, each a random planet', () =>
115 {
116 const rng = Rng.create(42);
117 const battle = Offline.createBattle({
118 playerPlanet: Planet.defaultPlanet(),
119 rng: rng,
120 round: 5
121 });
122 assert.equal(battle.schedule.opponents, 4);
123 assert.equal(battle.bots.length, 4);
124 assert.equal(battle.schedule.skill, Offline.skillForRound(5));
125 for (const bot of battle.bots)
126 {
127 assert.ok(bot.planet && bot.planet.id);
128 assert.ok(bot.core instanceof GameCore);
129 assert.equal(bot.eliminated, false);
130 }
131 const ids = new Set(battle.bots.map((b) => b.planet.id));
132 // With 12 planets and 4 bots, seed 42 is very likely to vary; just
133 // assert they are real catalogue planets.
134 const catalogue = new Set(Planet.allPlanets().map((p) => p.id));
135 for (const id of ids)
136 { assert.ok(catalogue.has(id), 'unknown planet ' + id); }
137 });
138
139 it('a forced miss never delivers garbage; a forced hit does', () =>
140 {
141 const player = makeGame({ noSpawn: true });
142 // Forced miss: rng always returns 0 for pick? miss if rng() < missChance.
143 // Use a planet with 60% miss (power 0) and rng that always returns 0
144 // → always miss? 0 < 0.60 is true, so miss. Wait, I used rng() < missChance
145 // in battle... _dispatchShots: miss = targets.length===0 || this.rng() < missChance
146 // Yes 0 < 0.60 → miss.
147 const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3, id: 'easy' });
148 let n = 0;
149 const missRng = () => { n++; return 0; }; // always miss (0 < 0.60)
150 const battle = Offline.createBattle({ playerPlanet: easy, rng: missRng, round: 1 });
151 // Give the player an outgoing shot without actually flying.
152 player.outgoingShots.push({ id: 99, color: 1, col: 0, y: 13 });
153 battle.tick(0.01, player);
154 assert.ok(battle.flightResults.length >= 1);
155 assert.equal(battle.flightResults[0].miss, true);
156 assert.equal(battle.pending.length, 0);
157
158 // Forced hit: rng always 0.99, missChance 0.60, 0.99 < 0.60 is false.
159 const hitRng = () => 0.99;
160 const battle2 = Offline.createBattle({ playerPlanet: easy, rng: hitRng, round: 1 });
161 const player2 = makeGame({ noSpawn: true });
162 player2.outgoingShots.push({ id: 7, color: 2, col: 1, y: 13 });
163 const botBlocksBefore = battle2.bots[0].core.blocks.length;
164 battle2.tick(0.01, player2);
165 assert.equal(battle2.flightResults[0].miss, false);
166 assert.equal(battle2.pending.length, 1);
167 // Deliver after the flight duration.
168 battle2.tick(Offline.FLIGHT_DURATION + 0.05, player2);
169 assert.equal(battle2.pending.length, 0);
170 assert.ok(battle2.bots[0].core.blocks.length > botBlocksBefore,
171 'garbage was ingested by the bot');
172 });
173
174 it('winning a round means every bot is defeated, then advanceRound grows the field', () =>
175 {
176 const rng = Rng.create(7);
177 const battle = Offline.createBattle({
178 playerPlanet: Planet.defaultPlanet(),
179 rng,
180 round: 1
181 });
182 assert.equal(battle.bots.length, 1);
183 const player = makeGame({ noSpawn: true });
184 battle.bots[0].core.state = 'gameover';
185 battle.tick(0.05, player);
186 assert.equal(battle.state, 'round_won');
187 assert.equal(battle.aliveBots().length, 0);
188 battle.advanceRound();
189 assert.equal(battle.round, 2);
190 assert.equal(battle.state, 'playing');
191 assert.equal(battle.bots.length, 2);
192 assert.equal(battle.schedule.skill, 0.06);
193 });
194
195 it('the player losing sets state to lost at the current round', () =>
196 {
197 const battle = Offline.createBattle({
198 playerPlanet: Planet.defaultPlanet(),
199 rng: Rng.create(1),
200 round: 3
201 });
202 const player = makeGame({ noSpawn: true });
203 player.state = 'gameover';
204 battle.tick(0.05, player);
205 assert.equal(battle.state, 'lost');
206 assert.equal(battle.round, 3);
207 });
208});