BlueGit

projects-timfalken-com meteors

Files

meteors / test / multiplayer-core.test.js

multiplayer-core.test.js

multiplayer-core.test.js 3.6 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js');
6
7describe('Outgoing shots and incoming garbage', () =>
8{
9 it('blocks that leave the top are recorded as outgoing shots and can be consumed', () =>
10 {
11 const g = makeGame({
12 noSpawn: true,
13 planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 }
14 });
15 placeRow(g, 0, [0, 1, 2], 0);
16 g.place(1, 1, 4);
17 g.resolveMatches();
18 assert.equal(g.consumeOutgoingShots().length, 0);
19 g.step(2, 1 / 120);
20 const shots = g.consumeOutgoingShots();
21 assert.equal(shots.length, 4, '3 rockets + 1 cargo, got ' + shots.length);
22 assert.ok(shots.every((s) => 'id' in s && 'color' in s && 'col' in s));
23 assert.equal(g.consumeOutgoingShots().length, 0, 'consume clears the list');
24 assert.equal(g.score, 4);
25 });
26
27 it('ingestGarbage drops a falling grey that settles 3s then recolors', () =>
28 {
29 const g = makeGame({ noSpawn: true, seed: 4 });
30 const block = g.ingestGarbage(3);
31 assert.ok(block);
32 assert.equal(block.col, 3);
33 assert.equal(block.color, GameCore.COLOR_GRAY);
34 assert.equal(block.state, 'falling');
35 assert.ok(almost(block.y, g.height, 0.05));
36 let landed = false;
37 for (let i = 0; i < 3000; i++)
38 {
39 g.tick(1 / 120);
40 if (block.state === 'settling')
41 {
42 landed = true;
43 break;
44 }
45 }
46 assert.ok(landed, 'should settle after landing, state=' + block.state + ' y=' + block.y + ' color=' + block.color);
47 assert.ok(almost(block.y, 0, 0.08), 'landed on the floor, y=' + block.y);
48 assert.equal(block.color, GameCore.COLOR_GRAY);
49 assert.ok(block.settleTime < 0.05, 'timer starts at landing');
50 g.step(2.9, 1 / 120);
51 assert.equal(block.state, 'settling', 'still settling at 2.9s, t=' + block.settleTime);
52 g.step(0.2, 1 / 120);
53 assert.equal(block.state, 'still');
54 assert.ok(block.color >= 0 && block.color < 5, 'recolored, color=' + block.color);
55 });
56
57 it('ingestGarbage skips locked and full columns, and returns null when none are open', () =>
58 {
59 const g = makeGame({ noSpawn: true });
60 for (let c = 0; c < 10; c++)
61 {
62 for (let r = 0; r < 13; r++)
63 { g.place(c, r, r % 5); }
64 }
65 g.checkLocks();
66 assert.equal(g.lockedColumnCount(), 10);
67 assert.equal(g.ingestGarbage(), null);
68 });
69
70 it('missChance defaults to the planet Power formula and can still be overridden', () =>
71 {
72 const g = makeGame({ noSpawn: true });
73 const terraMiss = Planet.missChance(Planet.defaultPlanet());
74 assert.ok(Math.abs(g.missChance - terraMiss) < 1e-12);
75 assert.ok(Math.abs(terraMiss - 0.30) > 0.01, 'Terra Power miss is not the old flat 30%');
76 assert.equal(GameCore.DEFAULT_MISS_CHANCE, 0.30);
77 const custom = new GameCore(Planet.defaultPlanet(), Math.random, 0.05);
78 assert.equal(custom.missChance, 0.05);
79 const clamped = new GameCore(Planet.defaultPlanet(), Math.random, 2);
80 assert.equal(clamped.missChance, 1);
81 const cinder = new GameCore(Planet.planetById('cinder'), Math.random);
82 assert.ok(Math.abs(cinder.missChance - 0.05) < 1e-12);
83 const aurel = new GameCore(Planet.planetById('aurel'), Math.random);
84 assert.ok(Math.abs(aurel.missChance - 0.60) < 1e-12);
85 });
86
87 it('fieldSnapshot lists every block for opponent miniatures', () =>
88 {
89 const g = makeGame({ noSpawn: true });
90 g.place(0, 0, 1);
91 g.place(1, 2, 3);
92 const snap = g.fieldSnapshot();
93 assert.equal(snap.score, 0);
94 assert.equal(snap.state, 'playing');
95 assert.equal(snap.blocks.length, 2);
96 assert.equal(snap.blocks[0].col, 0);
97 assert.equal(snap.blocks[1].y, 2);
98 assert.equal(snap.locked, 0);
99 });
100});