BlueGit

projects-timfalken-com allianties

Files

allianties / server / tests / engine_test.php

engine_test.php

engine_test.php 60.9 KiB Download Raw
1<?php
2declare(strict_types=1);
3require_once dirname(__DIR__) . '/lib/Engine.php';
4
5$failed = 0;
6$passed = 0;
7
8function expect(bool $cond, string $msg): void
9{
10 global $failed, $passed;
11 if ($cond) {
12 $passed++;
13 echo " ok $msg\n";
14 } else {
15 $failed++;
16 echo " FAIL $msg\n";
17 }
18}
19
20function game(array $ids, array $rules = [], ?int $now = null): array
21{
22 $now = $now ?? 1_700_000_000; // 2023-11-14 22:13:20 UTC — a mid-evening
23 $g = Engine::newLobby('g1', 'ABC234', $ids[0], $ids[0], null, $rules, $now);
24 for ($i = 1; $i < count($ids); $i++) {
25 $g = Engine::join($g, $ids[$i], $ids[$i], null, $now);
26 }
27 $g = Engine::start($g, $ids[0], $now);
28 return $g;
29}
30
31echo "Engine: grid suggestion and placement\n";
32// max(6 + ceil(n/2), n + 1), cap 24. ceil rounds halves up.
33expect(Engine::suggestedGrid(1) === 7, '1 player → 7 (max(6+ceil(0.5), 2) = max(7, 2))');
34expect(Engine::suggestedGrid(2) === 7, '2 players → 7 (max(6+1, 3))');
35expect(Engine::suggestedGrid(3) === 8, '3 players → 8 (ceil(1.5)=2, max(8, 4))');
36expect(Engine::suggestedGrid(6) === 9, '6 players → 9 (max(6+3, 7))');
37expect(Engine::suggestedGrid(9) === 11, '9 players → 11 (ceil(4.5)=5, max(11, 10))');
38expect(Engine::suggestedGrid(10) === 11, '10 players → 11 (max(6+5, 11))');
39expect(Engine::suggestedGrid(14) === 15, '14 players → 15 (max(6+7, 15))');
40expect(Engine::suggestedGrid(16) === 17, '16 players → 17');
41expect(Engine::suggestedGrid(30) === 24, 'capped at MAX_GRID 24');
42expect(Engine::gridSize(2) === 7, 'gridSize aliases suggestedGrid');
43expect(Engine::clampGrid(3) === 6, 'clamp below min → 6');
44expect(Engine::clampGrid(100) === 24, 'clamp above max → 24');
45expect(Engine::clampGrid(12) === 12, 'clamp mid unchanged');
46expect(Engine::gridFits(6, 2), '6×6 fits 2');
47expect(Engine::gridFits(6, 16), '6×6 has enough tiles for 16');
48expect(!Engine::gridFits(5, 2), 'below MIN_GRID does not fit');
49expect(!Engine::gridFits(25, 2), 'above MAX_GRID does not fit');
50expect(Engine::maxInset(6) === 1, '6×6 max inset 1 (leaves 4×4, min arena 3)');
51expect(Engine::maxInset(7) === 2, '7×7 max inset 2 (leaves 3×3)');
52$pos = Engine::startingPositions(4, 6);
53expect(count($pos) === 4, '4 starting positions');
54$keys = array_map(fn ($p) => $p['x'] . ',' . $p['y'], $pos);
55expect(count(array_unique($keys)) === 4, 'starting positions are unique');
56foreach ($pos as $p) {
57 expect(Engine::inBounds(6, $p['x'], $p['y']), "start in bounds {$p['x']},{$p['y']}");
58}
59
60echo "Engine: Chebyshev range\n";
61expect(Engine::chebyshev(0, 0, 2, 2) === 2, 'diagonal 2 is range 2');
62expect(Engine::chebyshev(0, 0, 2, 0) === 2, 'orthogonal 2 is range 2');
63expect(Engine::chebyshev(5, 5, 5, 5) === 0, 'same tile is 0');
64
65echo "Engine: midnight Amsterdam\n";
66// 2026-11-13 23:00 CET = 2026-11-13 22:00 UTC = 1763071200? Let's compute.
67$tz = new DateTimeZone('Europe/Amsterdam');
68$dt = new DateTime('2026-11-13 23:30:00', $tz);
69$from = $dt->getTimestamp();
70$next = Engine::nextMidnight($from);
71$nx = (new DateTime('@' . $next))->setTimezone($tz);
72expect($nx->format('Y-m-d H:i:s') === '2026-11-14 00:00:00', 'next midnight after 23:30 is 00:00 next day: ' . $nx->format('Y-m-d H:i:s'));
73$dt2 = new DateTime('2026-11-14 00:00:00', $tz);
74$next2 = Engine::nextMidnight($dt2->getTimestamp());
75$nx2 = (new DateTime('@' . $next2))->setTimezone($tz);
76expect($nx2->format('Y-m-d H:i:s') === '2026-11-15 00:00:00', 'at exactly 00:00, next is tomorrow');
77// DST spring-forward 2026-03-29 in NL
78$dt3 = new DateTime('2026-03-28 23:00:00', $tz);
79$next3 = Engine::nextMidnight($dt3->getTimestamp());
80$nx3 = (new DateTime('@' . $next3))->setTimezone($tz);
81expect($nx3->format('Y-m-d H:i:s') === '2026-03-29 00:00:00', 'DST: next midnight still 00:00: ' . $nx3->format('Y-m-d H:i:s'));
82
83echo "Engine: lobby and start\n";
84$now = (new DateTime('2026-11-13 15:00:00', $tz))->getTimestamp();
85$g = Engine::newLobby('g', 'CODE12', 'a', 'Alice', null, [], $now);
86expect($g['status'] === 'lobby', 'new lobby');
87expect($g['rules']['jury'] === true, 'jury default ON');
88expect($g['rules']['shrinking_arena'] === false, 'shrink default OFF');
89expect($g['rules']['shrink_interval_days'] === 7, 'shrink interval default 7');
90expect($g['rules']['ceasefire'] === false, 'ceasefire default OFF');
91expect($g['rules']['ceasefire_ranges'] === [], 'ceasefire ranges empty by default');
92expect($g['rules']['vis_shots'] === 'public', 'vis_shots default public');
93expect($g['rules']['vis_gifts'] === 'public', 'vis_gifts default public');
94expect($g['rules']['vis_ap'] === 'public', 'vis_ap default public');
95expect($g['rules']['vis_range'] === 'public', 'vis_range default public');
96expect($g['rules']['vis_lives'] === 'public', 'vis_lives default public');
97expect($g['rules']['ap_loot'] === false, 'ap_loot default OFF');
98expect($g['rules']['ap_time'] === '00:00', 'ap_time default 00:00');
99expect($g['rules']['grid'] === 7, 'lobby suggested grid 7 for 1 player');
100expect($g['rules']['grid_auto'] === true, 'grid_auto default ON');
101expect($g['grid'] === 7, 'game.grid mirrors suggestion in lobby');
102expect($g['host_id'] === 'a', 'host is creator');
103try {
104 Engine::start($g, 'a', $now);
105 expect(false, 'start alone should throw');
106} catch (RuntimeException $e) {
107 expect($e->getMessage() === 'min_players', 'start alone: min_players');
108}
109$g = Engine::join($g, 'b', 'Bob', null, $now);
110expect(count($g['players']) === 2, '2 players');
111try {
112 Engine::start($g, 'b', $now);
113 expect(false, 'non-host start');
114} catch (RuntimeException $e) {
115 expect($e->getMessage() === 'not_host', 'non-host cannot start');
116}
117$g = Engine::start($g, 'a', $now);
118expect($g['status'] === 'running', 'started');
119expect($g['grid'] === 7, 'auto grid for 2 players is 7');
120expect($g['players'][0]['ap'] === 1, 'start with 1 AP');
121expect($g['players'][0]['lives'] === 3, 'start with 3 lives');
122expect($g['players'][0]['range'] === 2, 'start with range 2');
123expect($g['players'][0]['x'] !== $g['players'][1]['x'] || $g['players'][0]['y'] !== $g['players'][1]['y'], 'not overlapping');
124$nd = (new DateTime('@' . $g['next_distribution_at']))->setTimezone($tz);
125expect($nd->format('Y-m-d H:i:s') === '2026-11-14 00:00:00', 'next AP at next midnight: ' . $nd->format('Y-m-d H:i:s'));
126
127echo "Engine: move, blocked, AP\n";
128$a = $g['players'][0];
129$legal = Engine::legal($g, 'a');
130expect(count($legal['move']) >= 1, 'Alice has at least one move');
131$t = $legal['move'][0];
132$g2 = Engine::applyAction($g, 'a', ['type' => 'move', 'x' => $t['x'], 'y' => $t['y']], $now);
133expect($g2['players'][0]['x'] === $t['x'] && $g2['players'][0]['y'] === $t['y'], 'moved');
134expect($g2['players'][0]['ap'] === 0, 'spent 1 AP');
135expect(end($g2['events'])['type'] === 'move', 'move event');
136expect(end($g2['events'])['spend'] === true, 'move is a spend tick');
137try {
138 Engine::applyAction($g2, 'a', ['type' => 'move', 'x' => $t['x'] + 1, 'y' => $t['y']], $now);
139 expect(false, 'move without AP');
140} catch (RuntimeException $e) {
141 expect($e->getMessage() === 'no_ap' || $e->getMessage() === 'invalid_move', 'no AP: ' . $e->getMessage());
142}
143
144echo "Engine: shoot and death and win\n";
145// Place two players adjacent, Alice shoots Bob three times (give extra AP).
146$g = game(['a', 'b'], [], $now);
147$g['players'][0]['x'] = 2;
148$g['players'][0]['y'] = 2;
149$g['players'][0]['ap'] = 5;
150$g['players'][1]['x'] = 3;
151$g['players'][1]['y'] = 3;
152$g['players'][1]['ap'] = 0;
153$legal = Engine::legal($g, 'a');
154expect(in_array('b', $legal['shoot'], true), 'Bob is in range 2 (actually dist 1)');
155$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now);
156expect($g['players'][1]['lives'] === 2, 'Bob has 2 lives');
157$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now);
158$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now);
159expect($g['players'][1]['alive'] === false, 'Bob is dead');
160expect($g['status'] === 'finished', 'game finished');
161expect($g['winner_id'] === 'a', 'Alice wins');
162$types = array_column($g['events'], 'type');
163expect(in_array('death', $types, true), 'death event');
164expect(in_array('win', $types, true), 'win event');
165
166echo "Engine: give AP and upgrade\n";
167$g = game(['a', 'b'], [], $now);
168$g['players'][0]['x'] = 2;
169$g['players'][0]['y'] = 2;
170$g['players'][0]['ap'] = 4;
171$g['players'][1]['x'] = 3;
172$g['players'][1]['y'] = 2;
173$g['players'][1]['ap'] = 0;
174$g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $now);
175expect($g['players'][0]['ap'] === 2, 'Alice spent 2');
176expect($g['players'][1]['ap'] === 2, 'Bob received 2');
177try {
178 Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 5], $now);
179 expect(false, 'give more than you have');
180} catch (RuntimeException $e) {
181 expect($e->getMessage() === 'bad_give', 'bad_give');
182}
183$g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $now);
184expect($g['players'][0]['range'] === 3, 'range is now 3');
185expect($g['players'][0]['ap'] === 1, 'upgrade costs 1');
186
187echo "Engine: out of range and occupied\n";
188$g = game(['a', 'b'], [], $now);
189$g['players'][0]['x'] = 0;
190$g['players'][0]['y'] = 0;
191$g['players'][0]['ap'] = 2;
192$g['players'][1]['x'] = 5;
193$g['players'][1]['y'] = 5;
194$legal = Engine::legal($g, 'a');
195expect(!in_array('b', $legal['shoot'], true), 'Bob at (5,5) is out of range 2 from (0,0)');
196try {
197 Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now);
198 expect(false, 'shoot out of range');
199} catch (RuntimeException $e) {
200 expect($e->getMessage() === 'out_of_range', 'out_of_range');
201}
202// Occupied neighbour is not a legal move.
203$g['players'][1]['x'] = 1;
204$g['players'][1]['y'] = 0;
205$legal = Engine::legal($g, 'a');
206$occ = false;
207foreach ($legal['move'] as $m) {
208 if ($m['x'] === 1 && $m['y'] === 0) {
209 $occ = true;
210 }
211}
212expect($occ === false, 'cannot move onto Bob');
213
214echo "Engine: surrender\n";
215$g = game(['a', 'b'], [], $now);
216$g = Engine::applyAction($g, 'b', ['type' => 'surrender'], $now);
217expect($g['players'][1]['alive'] === false, 'Bob surrendered');
218expect($g['players'][1]['surrendered'] === true, 'flagged surrendered');
219expect($g['winner_id'] === 'a', 'Alice wins after surrender');
220
221echo "Engine: daily AP grant\n";
222$g = game(['a', 'b'], ['jury' => false], $now);
223$g['players'][0]['ap'] = 0;
224$g['players'][1]['ap'] = 0;
225$at = $g['next_distribution_at'];
226$g = Engine::applyDue($g, $at);
227expect($g['day'] === 1, 'day is 1');
228expect($g['players'][0]['ap'] === 1, 'Alice got 1 AP');
229expect($g['players'][1]['ap'] === 1, 'Bob got 1 AP');
230// Catch-up two more midnights
231$at2 = Engine::nextMidnight($at);
232$at3 = Engine::nextMidnight($at2);
233$g = Engine::applyDue($g, $at3);
234expect($g['day'] === 3, 'caught up to day 3, day=' . $g['day']);
235expect($g['players'][0]['ap'] === 3, '3 AP after 3 days (started 0): ' . $g['players'][0]['ap']);
236
237echo "Engine: jury\n";
238$g = game(['a', 'b', 'c', 'd'], ['jury' => true, 'shrinking_arena' => false], $now);
239// Kill c and d without ending? Need 4 players, kill only c (one dead voter).
240// Place and shoot c twice then once more... easier: set lives.
241$g['players'][2]['lives'] = 0;
242$g['players'][2]['alive'] = false;
243$g['players'][2]['ap'] = 0;
244$g['players'][3]['lives'] = 0;
245$g['players'][3]['alive'] = false;
246$g['players'][3]['ap'] = 0;
247expect($g['status'] === 'running', 'still running with 2 alive');
248// c votes for a, d votes for a
249$g = Engine::applyAction($g, 'c', ['type' => 'vote', 'target' => 'a'], $now);
250$g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'a'], $now);
251expect($g['votes']['c'] === 'a', 'c voted a');
252// Vote is changeable
253$g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'b'], $now);
254expect($g['votes']['d'] === 'b', 'd changed vote to b');
255// Living player cannot vote
256try {
257 Engine::applyAction($g, 'a', ['type' => 'vote', 'target' => 'b'], $now);
258 expect(false, 'living vote');
259} catch (RuntimeException $e) {
260 expect($e->getMessage() === 'cannot_vote', 'living cannot vote');
261}
262// Present hides votes from others
263$viewA = Engine::present($g, 'a');
264expect($viewA['legal']['my_vote'] === null, 'Alice (living) has no vote');
265$viewC = Engine::present($g, 'c');
266expect($viewC['legal']['my_vote'] === 'a', 'Charlie sees own vote');
267expect(!isset($viewA['votes']), 'votes not in present payload');
268// Distribution: 1 vote a, 1 vote b → tie, both get +1 bonus (+1 regular) = +2
269$g['players'][0]['ap'] = 0;
270$g['players'][1]['ap'] = 0;
271$g = Engine::applyDue($g, $g['next_distribution_at']);
272expect($g['players'][0]['ap'] === 2, 'Alice got 1+1 (tie): ' . $g['players'][0]['ap']);
273expect($g['players'][1]['ap'] === 2, 'Bob got 1+1 (tie): ' . $g['players'][1]['ap']);
274expect($g['votes'] === [], 'votes reset after distribution');
275// Unique winner
276$g['players'][2]['alive'] = false;
277$g = Engine::applyAction($g, 'c', ['type' => 'vote', 'target' => 'a'], $now + 1);
278$g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'a'], $now + 1);
279$g['players'][0]['ap'] = 0;
280$g['players'][1]['ap'] = 0;
281$g = Engine::applyDue($g, $g['next_distribution_at']);
282expect($g['players'][0]['ap'] === 2, 'Alice unique jury winner: 2');
283expect($g['players'][1]['ap'] === 1, 'Bob only regular 1: ' . $g['players'][1]['ap']);
284// Jury off: no vote
285$gOff = game(['a', 'b', 'c'], ['jury' => false], $now);
286$gOff['players'][2]['alive'] = false;
287$gOff['players'][2]['lives'] = 0;
288try {
289 Engine::applyAction($gOff, 'c', ['type' => 'vote', 'target' => 'a'], $now);
290 expect(false, 'vote with jury off');
291} catch (RuntimeException $e) {
292 expect($e->getMessage() === 'cannot_vote', 'jury off: cannot_vote');
293}
294
295echo "Engine: shrinking arena\n";
296$g = game(['a', 'b'], ['shrinking_arena' => true, 'shrink_interval_days' => 2, 'jury' => false, 'grid' => 6, 'grid_auto' => false], $now);
297expect($g['grid'] === 6, 'host-chosen 6×6');
298expect(Engine::daysUntilShrink($g) === 2, 'shrink in 2 days (interval 2, day 0): ' . (Engine::daysUntilShrink($g) ?? 'null'));
299// Put Alice on the outer ring (0,0), Bob in the centre (2,2)
300$g['players'][0]['x'] = 0;
301$g['players'][0]['y'] = 0;
302$g['players'][0]['lives'] = 3;
303$g['players'][0]['ap'] = 0;
304$g['players'][1]['x'] = 2;
305$g['players'][1]['y'] = 2;
306$g['players'][1]['ap'] = 0;
307// Day 1: no shrink
308$g = Engine::applyDue($g, $g['next_distribution_at']);
309expect($g['day'] === 1, 'day 1');
310expect($g['arena_inset'] === 0, 'no shrink on day 1');
311expect($g['players'][0]['x'] === 0 && $g['players'][0]['y'] === 0, 'Alice still on (0,0)');
312expect($g['players'][0]['lives'] === 3, 'Alice still 3 lives');
313expect(Engine::daysUntilShrink($g) === 1, 'shrink in 1 day');
314// Day 2: shrink
315$g = Engine::applyDue($g, $g['next_distribution_at']);
316expect($g['day'] === 2, 'day 2');
317expect($g['arena_inset'] === 1, 'inset is 1');
318expect($g['players'][0]['lives'] === 2, 'Alice lost 1 life: ' . $g['players'][0]['lives']);
319expect(Engine::isPassable(6, 1, $g['players'][0]['x'], $g['players'][0]['y']), 'Alice pushed inward to passable ' . $g['players'][0]['x'] . ',' . $g['players'][0]['y']);
320expect(!($g['players'][0]['x'] === 2 && $g['players'][0]['y'] === 2), 'not onto Bob');
321expect($g['players'][1]['lives'] === 3, 'Bob unhurt in centre');
322expect($g['players'][0]['ap'] === 2, 'Alice got day-1 and day-2 AP after surviving shrink: ' . $g['players'][0]['ap']);
323// Outer ring is impassable
324$legal = Engine::legal($g, $g['players'][0]['id'] === 'a' ? 'a' : 'a');
325foreach ($legal['move'] as $m) {
326 expect(Engine::isPassable(6, 1, $m['x'], $m['y']), "legal move {$m['x']},{$m['y']} is passable");
327}
328// Max one shrink on 6×6 (maxInset=1); day 4 would try again but stop
329$g = Engine::applyDue($g, $g['next_distribution_at']); // day 3
330$g = Engine::applyDue($g, $g['next_distribution_at']); // day 4
331expect($g['arena_inset'] === 1, 'does not shrink past max inset: ' . $g['arena_inset']);
332expect(Engine::daysUntilShrink($g) === null, 'no upcoming shrink at max');
333
334echo "Engine: shrink kills and crush and draw\n";
335$g = game(['a', 'b'], ['shrinking_arena' => true, 'shrink_interval_days' => 1, 'jury' => false, 'grid' => 6, 'grid_auto' => false], $now);
336$g['players'][0]['x'] = 0;
337$g['players'][0]['y'] = 0;
338$g['players'][0]['lives'] = 1;
339$g['players'][1]['x'] = 5;
340$g['players'][1]['y'] = 5;
341$g['players'][1]['lives'] = 1;
342$g = Engine::applyDue($g, $g['next_distribution_at']);
343expect($g['players'][0]['alive'] === false, 'Alice died on shrink (1 life)');
344expect($g['players'][1]['alive'] === false, 'Bob died on shrink (1 life)');
345expect($g['status'] === 'finished', 'finished');
346expect($g['draw'] === true, 'draw when both die in the same shrink');
347
348echo "Engine: notes and chat privacy\n";
349$g = game(['a', 'b'], [], $now);
350$g = Engine::setNote($g, 'a', 'b', 'looks dangerous');
351$viewA = Engine::present($g, 'a');
352$viewB = Engine::present($g, 'b');
353$noteAonB = null;
354$noteBonB = null;
355foreach ($viewA['players'] as $p) {
356 if ($p['id'] === 'b') {
357 $noteAonB = $p['note'];
358 }
359}
360foreach ($viewB['players'] as $p) {
361 if ($p['id'] === 'b') {
362 $noteBonB = $p['note'];
363 }
364}
365expect($noteAonB === 'looks dangerous', 'Alice sees her note on Bob');
366expect($noteBonB === '', 'Bob does not see Alice’s note');
367$g = Engine::addMessage($g, 'a', 'b', 'hello', $now);
368$viewA = Engine::present($g, 'a');
369$viewB = Engine::present($g, 'b');
370expect(count($viewA['messages']) === 1 && $viewA['messages'][0]['text'] === 'hello', 'Alice sees chat');
371expect(count($viewB['messages']) === 1, 'Bob sees chat');
372// A third player would not — add c in lobby... already started. Fine.
373
374echo "Engine: modular rule flags\n";
375$r = Engine::normalizeRules(['jury' => '0', 'shrinking_arena' => '1', 'shrink_interval_days' => 99, 'ceasefire' => '1']);
376expect($r['jury'] === false, 'jury "0" is false');
377expect($r['shrinking_arena'] === true, 'shrink "1" is true');
378expect($r['shrink_interval_days'] === 30, 'interval clamped to max 30: ' . $r['shrink_interval_days']);
379expect($r['ceasefire'] === true, 'ceasefire "1" is true');
380
381echo "Engine: cease-fire ranges and midnight wrap\n";
382$ranges = Engine::normalizeRanges([
383 ['start' => '22:00', 'end' => '06:00'],
384 ['start' => '12:00', 'end' => '13:30'],
385 ['start' => '25:00', 'end' => '01:00'], // invalid
386 ['start' => '08:00', 'end' => '08:00'], // zero-length dropped
387]);
388expect(count($ranges) === 2, '2 valid ranges: ' . count($ranges));
389expect($ranges[0]['start'] === '12:00', 'sorted, first is 12:00');
390expect($ranges[1]['start'] === '22:00', 'wrap range kept');
391
392$g = game(['a', 'b'], [
393 'jury' => false,
394 'ceasefire' => true,
395 'ceasefire_ranges' => [
396 ['start' => '22:00', 'end' => '06:00'],
397 ['start' => '12:00', 'end' => '13:00'],
398 ],
399], $now);
400// Place adjacent so shoot would otherwise be legal.
401$g['players'][0]['x'] = 2;
402$g['players'][0]['y'] = 2;
403$g['players'][0]['ap'] = 3;
404$g['players'][1]['x'] = 3;
405$g['players'][1]['y'] = 2;
406
407// 2026-11-13 23:30 Amsterdam — inside 22:00-06:00 wrap
408$inCf = (new DateTime('2026-11-13 23:30:00', $tz))->getTimestamp();
409$info = Engine::ceasefireInfo($g, $inCf);
410expect($info !== null && $info['active'] === true, '23:30 is cease-fire (wrap)');
411$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
412expect($end->format('Y-m-d H:i') === '2026-11-14 06:00', 'ends at 06:00 next day: ' . $end->format('Y-m-d H:i'));
413$legal = Engine::legal($g, 'a', $inCf);
414expect($legal['shoot'] === [], 'Attack disabled during cease-fire');
415expect($legal['ceasefire'] === true, 'legal.ceasefire true');
416expect(count($legal['move']) >= 1 || $legal['upgrade'] || true, 'non-shoot actions still considered');
417// Move is still allowed
418$legalMove = $legal['move'];
419expect(!empty($legalMove), 'can still move during cease-fire');
420try {
421 Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $inCf);
422 expect(false, 'shoot during cease-fire should throw');
423} catch (RuntimeException $e) {
424 expect($e->getMessage() === 'ceasefire', 'error code ceasefire: ' . $e->getMessage());
425}
426
427// 2026-11-14 06:00 exactly — end is exclusive, so not active
428$atEnd = (new DateTime('2026-11-14 06:00:00', $tz))->getTimestamp();
429$info = Engine::ceasefireInfo($g, $atEnd);
430expect($info['active'] === false, '06:00 exactly is not cease-fire (end exclusive)');
431$until = (new DateTime('@' . $info['until']))->setTimezone($tz);
432expect($until->format('H:i') === '12:00', 'next cease-fire at 12:00: ' . $until->format('H:i'));
433
434// 12:30 — second range
435$noon = (new DateTime('2026-11-14 12:30:00', $tz))->getTimestamp();
436$info = Engine::ceasefireInfo($g, $noon);
437expect($info['active'] === true, '12:30 is cease-fire');
438$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
439expect($end->format('H:i') === '13:00', 'ends 13:00: ' . $end->format('H:i'));
440
441// 15:00 — free, next is 22:00 same day
442$afternoon = (new DateTime('2026-11-14 15:00:00', $tz))->getTimestamp();
443$info = Engine::ceasefireInfo($g, $afternoon);
444expect($info['active'] === false, '15:00 is free');
445$until = (new DateTime('@' . $info['until']))->setTimezone($tz);
446expect($until->format('Y-m-d H:i') === '2026-11-14 22:00', 'until 22:00: ' . $until->format('Y-m-d H:i'));
447$legal = Engine::legal($g, 'a', $afternoon);
448expect(in_array('b', $legal['shoot'], true), 'can shoot when not in cease-fire');
449$g2 = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $afternoon);
450expect($g2['players'][1]['lives'] === 2, 'shot landed outside cease-fire');
451
452// Just before midnight wrap: 21:59 — until 22:00
453$before = (new DateTime('2026-11-14 21:59:00', $tz))->getTimestamp();
454$info = Engine::ceasefireInfo($g, $before);
455expect($info['active'] === false, '21:59 is free');
456expect($info['seconds'] === 60, '60 seconds until cease-fire: ' . $info['seconds']);
457
458// 05:59 next morning: 60 seconds left IN cease-fire
459$late = (new DateTime('2026-11-15 05:59:00', $tz))->getTimestamp();
460$info = Engine::ceasefireInfo($g, $late);
461expect($info['active'] === true, '05:59 still cease-fire');
462expect($info['seconds'] === 60, '60 seconds left in cease-fire: ' . $info['seconds']);
463
464echo "Engine: cease-fire DST spring-forward (2026-03-29 02:00→03:00)\n";
465// Range 01:00-04:00 on spring-forward day: 02:00 does not exist, period is 2 hours of real time.
466$gD = game(['a', 'b'], [
467 'ceasefire' => true,
468 'ceasefire_ranges' => [['start' => '01:00', 'end' => '04:00']],
469 'jury' => false,
470], $now);
471$start = (new DateTime('2026-03-29 01:00:00', $tz))->getTimestamp();
472$info = Engine::ceasefireInfo($gD, $start);
473expect($info['active'] === true, '01:00 CEST-transition day is cease-fire');
474$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
475expect($end->format('Y-m-d H:i') === '2026-03-29 04:00', 'ends 04:00: ' . $end->format('Y-m-d H:i'));
476// Real duration should be 2 hours (01:00 CET + skipped 02:00 hour → 04:00 CEST)
477// 01:00 on that day is still CET (UTC+1), 04:00 is CEST (UTC+2).
478$dur = $info['until'] - $start;
479expect($dur === 2 * 3600, 'spring-forward 01:00-04:00 is 2 real hours: ' . $dur);
480// 02:30 does not exist; 03:00 should still be inside
481$three = (new DateTime('2026-03-29 03:00:00', $tz))->getTimestamp();
482$info = Engine::ceasefireInfo($gD, $three);
483expect($info['active'] === true, '03:00 (after the jump) is still cease-fire');
484expect($info['seconds'] === 3600, '1 hour left at 03:00: ' . $info['seconds']);
485// Before: 00:30, until 01:00 = 30 min
486$half = (new DateTime('2026-03-29 00:30:00', $tz))->getTimestamp();
487$info = Engine::ceasefireInfo($gD, $half);
488expect($info['active'] === false, '00:30 is free');
489expect($info['seconds'] === 1800, '30 min until 01:00: ' . $info['seconds']);
490
491echo "Engine: cease-fire DST fall-back (2026-10-25 03:00→02:00)\n";
492// Range 01:00-03:30. The 02:00 hour happens twice. amsterdamWallclock adds minutes
493// from midnight, so 03:30 is unambiguous (after the fallback). Duration:
494// midnight CEST 00:00 = UTC 22:00 previous; +3.5h = 03:30 CET? Let's assert via DateTime.
495$gF = game(['a', 'b'], [
496 'ceasefire' => true,
497 'ceasefire_ranges' => [['start' => '01:00', 'end' => '03:30']],
498 'jury' => false,
499], $now);
500$fbStart = (new DateTime('2026-10-25 01:00:00', $tz))->getTimestamp();
501$info = Engine::ceasefireInfo($gF, $fbStart);
502expect($info['active'] === true, '01:00 fall-back day is cease-fire');
503$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
504expect($end->format('Y-m-d H:i') === '2026-10-25 03:30', 'ends 03:30: ' . $end->format('Y-m-d H:i'));
505$dur = $info['until'] - $fbStart;
506// 01:00 CEST (UTC 23:00) to 03:30 CET (UTC 02:30) = 3.5 hours (the 02:00 hour repeats).
507expect($dur === (int) (3.5 * 3600), 'fall-back 01:00-03:30 is 3.5 real hours: ' . $dur);
508// A moment during the repeated 02:xx should still be active.
509// 02:15 CEST (first) = 01:00 CEST + 75 min.
510$first215 = $fbStart + 75 * 60;
511$info = Engine::ceasefireInfo($gF, $first215);
512expect($info['active'] === true, 'first 02:15 (still DST) is cease-fire');
513// 02:15 CET (second) = 01:00 + 75 min + 60 min (the repeated hour)
514$second215 = $fbStart + (75 + 60) * 60;
515$info = Engine::ceasefireInfo($gF, $second215);
516expect($info['active'] === true, 'second 02:15 (CET) is still cease-fire');
517// After 03:30 CET: free
518$after = (new DateTime('2026-10-25 03:30:00', $tz))->getTimestamp();
519$info = Engine::ceasefireInfo($gF, $after);
520expect($info['active'] === false, '03:30 exactly is free (end exclusive)');
521
522echo "Engine: cease-fire off or empty ranges\n";
523$gN = game(['a', 'b'], ['ceasefire' => false, 'ceasefire_ranges' => [['start' => '00:00', 'end' => '23:59']]], $now);
524expect(Engine::ceasefireInfo($gN, $now) === null, 'rule off → no info (ranges ignored)');
525$gE = game(['a', 'b'], ['ceasefire' => true, 'ceasefire_ranges' => []], $now);
526expect(Engine::ceasefireInfo($gE, $now) === null, 'on but empty ranges → treated as no cease-fire');
527
528echo "Engine: overlapping ranges merge for countdown\n";
529$gM = game(['a', 'b'], [
530 'ceasefire' => true,
531 'ceasefire_ranges' => [
532 ['start' => '10:00', 'end' => '12:00'],
533 ['start' => '11:00', 'end' => '13:00'],
534 ],
535 'jury' => false,
536], $now);
537$t = (new DateTime('2026-11-14 10:30:00', $tz))->getTimestamp();
538$info = Engine::ceasefireInfo($gM, $t);
539$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
540expect($end->format('H:i') === '13:00', 'merged overlapping ends at 13:00: ' . $end->format('H:i'));
541
542
543echo "Engine: visibility defaults and invalid values fall back to public\n";
544$norm = Engine::normalizeRules([
545 'vis_shots' => 'nope',
546 'vis_gifts' => 123,
547 'vis_ap' => 'PUBLIC',
548 'vis_range' => '',
549 'vis_lives' => null,
550]);
551expect($norm['vis_shots'] === 'public', 'bad vis_shots → public');
552expect($norm['vis_gifts'] === 'public', 'bad vis_gifts → public');
553expect($norm['vis_ap'] === 'public', 'bad vis_ap → public');
554expect($norm['vis_range'] === 'public', 'bad vis_range → public');
555expect($norm['vis_lives'] === 'public', 'bad vis_lives → public');
556$ok = Engine::normalizeRules([
557 'vis_shots' => 'elim_known_target',
558 'vis_gifts' => 'anonymous',
559 'vis_ap' => 'within_range',
560 'vis_range' => 'known_in_range',
561 'vis_lives' => 'within_range',
562 'grid' => 14,
563 'grid_auto' => false,
564]);
565expect($ok['vis_shots'] === 'elim_known_target', 'accepts elim_known_target');
566expect($ok['vis_gifts'] === 'anonymous', 'accepts anonymous');
567expect($ok['vis_ap'] === 'within_range', 'accepts vis_ap within_range');
568expect($ok['vis_range'] === 'known_in_range', 'accepts known_in_range');
569expect($ok['vis_lives'] === 'within_range', 'accepts vis_lives within_range');
570expect($ok['grid'] === 14 && $ok['grid_auto'] === false, 'accepts manual grid 14');
571$ok2 = Engine::normalizeRules(['vis_ap' => 'hidden', 'vis_lives' => 'hidden']);
572expect($ok2['vis_ap'] === 'hidden' && $ok2['vis_lives'] === 'hidden', 'still accepts hidden');
573$badG = Engine::normalizeRules(['vis_ap' => 'in_range', 'vis_lives' => 'known_in_range']);
574expect($badG['vis_ap'] === 'public' && $badG['vis_lives'] === 'public', 'unknown vis_ap/lives values → public');
575
576/**
577 * Three-player game with A next to B (in starting range 2) and C far away.
578 * Extra AP so we can shoot/give/upgrade more than once.
579 *
580 * @return array{0:array,1:int}
581 */
582function visGame(array $vis, int $now): array
583{
584 $g = game(['a', 'b', 'c'], array_merge(['jury' => false], $vis), $now);
585 // Place: A at (2,2), B at (3,2) dist 1, C at (5,5) dist 3 from A.
586 foreach ($g['players'] as $i => $p) {
587 if ($p['id'] === 'a') {
588 $g['players'][$i]['x'] = 2;
589 $g['players'][$i]['y'] = 2;
590 $g['players'][$i]['ap'] = 5;
591 $g['players'][$i]['range'] = 2;
592 } elseif ($p['id'] === 'b') {
593 $g['players'][$i]['x'] = 3;
594 $g['players'][$i]['y'] = 2;
595 $g['players'][$i]['ap'] = 5;
596 $g['players'][$i]['range'] = 2;
597 } else {
598 $g['players'][$i]['x'] = 5;
599 $g['players'][$i]['y'] = 5;
600 $g['players'][$i]['ap'] = 5;
601 $g['players'][$i]['range'] = 2;
602 }
603 }
604 return [$g, $now];
605}
606
607function visEvents(array $view, string $type): array
608{
609 return array_values(array_filter($view['events'] ?? [], fn ($e) => ($e['type'] ?? '') === $type));
610}
611
612function visPlayer(array $view, string $id): ?array
613{
614 foreach ($view['players'] as $p) {
615 if ($p['id'] === $id) {
616 return $p;
617 }
618 }
619 return null;
620}
621
622echo "Engine: vis shots — public (everyone sees, including lives_left when lives public)\n";
623[$g, $t] = visGame(['vis_shots' => 'public'], $now);
624$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
625$va = Engine::present($g, 'a', $t);
626$vb = Engine::present($g, 'b', $t);
627$vc = Engine::present($g, 'c', $t);
628expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees public shot');
629expect(count(visEvents($vb, 'shoot')) === 1, 'target sees public shot');
630expect(count(visEvents($vc, 'shoot')) === 1, 'third party sees public shot');
631expect(($vc['events'] === visEvents($vc, 'shoot') ? true : isset(visEvents($vc, 'shoot')[0]['player'])) && visEvents($vc, 'shoot')[0]['player'] === 'a', 'third party sees shooter id');
632expect(visEvents($vc, 'shoot')[0]['lives_left'] === 2, 'public lives_left on shot');
633expect(visEvents($vc, 'shoot')[0]['damage'] === 1, 'damage is 1');
634expect(visPlayer($vc, 'b')['lives'] === 2, 'public lives in state');
635
636echo "Engine: vis shots — elim_public (non-elim hidden from third party, visible to target)\n";
637[$g, $t] = visGame(['vis_shots' => 'elim_public'], $now);
638$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
639$va = Engine::present($g, 'a', $t);
640$vb = Engine::present($g, 'b', $t);
641$vc = Engine::present($g, 'c', $t);
642expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees non-elim');
643expect(count(visEvents($vb, 'shoot')) === 1, 'target sees non-elim under elim_public');
644expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see non-elim');
645expect(count(visEvents($vc, 'death')) === 0, 'no death on non-elim');
646// Two more shots to eliminate (B started with 3, now 2).
647$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
648$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
649$vc = Engine::present($g, 'c', $t);
650$vb = Engine::present($g, 'b', $t);
651$shotsC = visEvents($vc, 'shoot');
652expect(count($shotsC) === 1, 'third party sees only the eliminating shot: ' . count($shotsC));
653expect(!empty($shotsC[0]['killed']), 'the visible shot is the kill');
654expect(count(visEvents($vc, 'death')) === 1, 'third party sees death');
655expect((visEvents($vc, 'death')[0]['by'] ?? null) === 'a', 'elim_public death includes killer');
656expect((visEvents($vb, 'death')[0]['by'] ?? null) === 'a', 'target sees killer');
657expect(visPlayer($vc, 'b')['alive'] === false, 'death/alive is always public');
658
659echo "Engine: vis shots — known_target (others never see the shot, death without by)\n";
660[$g, $t] = visGame(['vis_shots' => 'known_target'], $now);
661$g['players'][array_key_first(array_filter($g['players'], fn ($p) => $p['id'] === 'b'))]['lives'] = 1;
662// find b index properly
663foreach ($g['players'] as $i => $p) {
664 if ($p['id'] === 'b') {
665 $g['players'][$i]['lives'] = 1;
666 }
667}
668$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
669$va = Engine::present($g, 'a', $t);
670$vb = Engine::present($g, 'b', $t);
671$vc = Engine::present($g, 'c', $t);
672expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees known_target shot');
673expect(count(visEvents($vb, 'shoot')) === 1, 'target sees known_target shot');
674expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see known_target shot');
675$deaths = visEvents($vc, 'death');
676expect(count($deaths) === 1, 'third party still sees that B died');
677expect(!array_key_exists('by', $deaths[0]), 'killer stripped for third party under known_target');
678expect((visEvents($vb, 'death')[0]['by'] ?? null) === 'a', 'target still sees who shot them');
679
680echo "Engine: vis shots — elim_known_target (non-elim hidden even from target)\n";
681[$g, $t] = visGame(['vis_shots' => 'elim_known_target'], $now);
682$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
683$va = Engine::present($g, 'a', $t);
684$vb = Engine::present($g, 'b', $t);
685$vc = Engine::present($g, 'c', $t);
686expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees non-elim under elim_known_target');
687expect(count(visEvents($vb, 'shoot')) === 0, 'target does NOT see non-elim shot (must deduce)');
688expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see non-elim');
689expect(visPlayer($vb, 'b')['lives'] === 2, 'target still sees own lives drop');
690expect(visPlayer($vc, 'b')['lives'] === 2, 'lives still public in this game (default)');
691// Eliminate
692$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
693$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
694$vb = Engine::present($g, 'b', $t);
695$vc = Engine::present($g, 'c', $t);
696expect(count(visEvents($vb, 'shoot')) === 1, 'target sees only the eliminating shot');
697expect((visEvents($vb, 'shoot')[0]['player'] ?? null) === 'a', 'elim shot names the shooter for the target');
698expect(count(visEvents($vc, 'shoot')) === 0, 'third party never sees shots under elim_known_target');
699expect(!array_key_exists('by', visEvents($vc, 'death')[0]), 'third party death has no by');
700
701echo "Engine: vis shots + hidden lives — lives_left stripped except for the target\n";
702[$g, $t] = visGame(['vis_shots' => 'public', 'vis_lives' => 'hidden'], $now);
703$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
704$va = Engine::present($g, 'a', $t);
705$vb = Engine::present($g, 'b', $t);
706$vc = Engine::present($g, 'c', $t);
707expect(!array_key_exists('lives_left', visEvents($va, 'shoot')[0]), 'shooter does not get target lives_left');
708expect((visEvents($vb, 'shoot')[0]['lives_left'] ?? null) === 2, 'target sees own lives_left');
709expect(!array_key_exists('lives_left', visEvents($vc, 'shoot')[0]), 'third party does not get lives_left');
710expect(visEvents($va, 'shoot')[0]['damage'] === 1, 'shooter still sees damage 1');
711expect(visPlayer($va, 'a')['lives'] === 3, 'own lives always visible');
712expect(visPlayer($va, 'b')['lives'] === null, 'other lives hidden from shooter');
713expect(visPlayer($vc, 'b')['lives'] === null, 'other lives hidden from third party');
714expect(visPlayer($vb, 'b')['lives'] === 2, 'target sees own lives');
715expect(is_bool(visPlayer($vc, 'b')['alive']), 'alive flag still present when lives hidden');
716
717echo "Engine: vis gifts — public, known_recipient, anonymous\n";
718[$g, $t] = visGame(['vis_gifts' => 'public'], $now);
719$g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t);
720$vc = Engine::present($g, 'c', $t);
721$gives = visEvents($vc, 'give');
722expect(count($gives) === 1 && $gives[0]['player'] === 'a' && $gives[0]['amount'] === 2, 'public gift visible to third party with giver and amount');
723
724[$g, $t] = visGame(['vis_gifts' => 'known_recipient'], $now);
725$g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t);
726$va = Engine::present($g, 'a', $t);
727$vb = Engine::present($g, 'b', $t);
728$vc = Engine::present($g, 'c', $t);
729expect(count(visEvents($va, 'give')) === 1 && visEvents($va, 'give')[0]['player'] === 'a', 'giver sees own gift');
730expect(count(visEvents($vb, 'give')) === 1 && visEvents($vb, 'give')[0]['player'] === 'a', 'recipient sees giver');
731expect(count(visEvents($vc, 'give')) === 0, 'third party does not see known_recipient gift');
732
733[$g, $t] = visGame(['vis_gifts' => 'anonymous'], $now);
734$beforeB = Engine::player($g, 'b')['ap'];
735$g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t);
736$va = Engine::present($g, 'a', $t);
737$vb = Engine::present($g, 'b', $t);
738$vc = Engine::present($g, 'c', $t);
739expect(count(visEvents($va, 'give')) === 1 && visEvents($va, 'give')[0]['player'] === 'a', 'giver sees full anonymous gift in their own log');
740$rec = visEvents($vb, 'give');
741expect(count($rec) === 1, 'recipient sees an anonymous gift event');
742expect(!empty($rec[0]['anonymous']), 'event marked anonymous');
743expect(!array_key_exists('player', $rec[0]), 'giver id stripped for recipient');
744expect(($rec[0]['amount'] ?? 0) === 2, 'recipient sees the AP amount');
745expect(($rec[0]['target'] ?? '') === 'b', 'recipient is named as target');
746expect(count(visEvents($vc, 'give')) === 0, 'third party does not see anonymous gift');
747expect(visPlayer($vb, 'b')['ap'] === $beforeB + 2, 'recipient AP actually increased');
748// Stored (unfiltered) event still has the giver — present() must not mutate.
749$rawGive = null;
750foreach ($g['events'] as $e) {
751 if (($e['type'] ?? '') === 'give') {
752 $rawGive = $e;
753 }
754}
755expect($rawGive !== null && ($rawGive['player'] ?? null) === 'a', 'stored event still has the giver (present does not mutate)');
756expect(!array_key_exists('player', $rec[0]) || $rec[0]['player'] !== 'a' || true, 'sanity');
757expect(($rawGive['player'] ?? null) === 'a' && !array_key_exists('player', $rec[0]), 'clone is independent of stored event');
758
759echo "Engine: vis AP totals hidden\n";
760[$g, $t] = visGame(['vis_ap' => 'hidden'], $now);
761$va = Engine::present($g, 'a', $t);
762$vc = Engine::present($g, 'c', $t);
763expect(visPlayer($va, 'a')['ap'] === 5, 'own AP visible when hidden rule is on');
764expect(visPlayer($va, 'b')['ap'] === null, 'other AP hidden');
765expect(visPlayer($vc, 'a')['ap'] === null, 'third party does not see A’s AP');
766// Daily grants: only the viewer’s own grant is included.
767$midnight = Engine::nextMidnight($t);
768$g = Engine::applyDue($g, $midnight);
769$va = Engine::present($g, 'a', $midnight);
770$dailies = visEvents($va, 'daily');
771expect(count($dailies) === 1, 'daily event present');
772expect(array_keys($dailies[0]['grants']) === ['a'], 'hidden AP: only own grant in daily event: ' . json_encode($dailies[0]['grants']));
773expect(!isset($dailies[0]['jury']['counts']), 'jury vote counts stripped');
774
775echo "Engine: vis range — hidden and known_in_range\n";
776[$g, $t] = visGame(['vis_range' => 'hidden'], $now);
777$g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $t);
778$va = Engine::present($g, 'a', $t);
779$vc = Engine::present($g, 'c', $t);
780expect(visPlayer($va, 'a')['range'] === 3, 'own range after upgrade');
781expect(visPlayer($vc, 'a')['range'] === null, 'other range hidden');
782expect(count(visEvents($va, 'upgrade')) === 1, 'upgrader sees own upgrade');
783expect(count(visEvents($vc, 'upgrade')) === 0, 'third party does not see hidden upgrade');
784
785[$g, $t] = visGame(['vis_range' => 'known_in_range'], $now);
786$va = Engine::present($g, 'a', $t);
787$vb = Engine::present($g, 'b', $t);
788$vc = Engine::present($g, 'c', $t);
789// B is at dist 1 from A (range 2) so A is in B’s range and B is in A’s range.
790expect(visPlayer($vb, 'a')['range'] === 2, 'B is within A’s range, sees A’s range');
791expect(visPlayer($va, 'b')['range'] === 2, 'A is within B’s range, sees B’s range');
792expect(visPlayer($vc, 'a')['range'] === null, 'C is outside A’s range (dist 3 > 2)');
793expect(visPlayer($va, 'c')['range'] === null, 'A is outside C’s range (dist 3 > 2)');
794expect(visPlayer($vc, 'c')['range'] === 2, 'C always sees own range');
795// Upgrade A to range 3: C (dist 3) is now in A’s range.
796$g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $t);
797$vc = Engine::present($g, 'c', $t);
798$va = Engine::present($g, 'a', $t);
799expect(visPlayer($vc, 'a')['range'] === 3, 'after upgrade, C is within A’s range and sees it');
800expect(count(visEvents($vc, 'upgrade')) === 1, 'C now also sees the upgrade event');
801expect(visPlayer($va, 'c')['range'] === null, 'A is still outside C’s range 2');
802
803echo "Engine: movement is always public, even with everything hidden\n";
804[$g, $t] = visGame([
805 'vis_shots' => 'elim_known_target',
806 'vis_gifts' => 'anonymous',
807 'vis_ap' => 'hidden',
808 'vis_range' => 'hidden',
809 'vis_lives' => 'hidden',
810], $now);
811// A moves to (2,3) — adjacent empty.
812$g = Engine::applyAction($g, 'a', ['type' => 'move', 'x' => 2, 'y' => 3], $t);
813$vc = Engine::present($g, 'c', $t);
814$moves = visEvents($vc, 'move');
815expect(count($moves) === 1, 'move is public');
816expect($moves[0]['player'] === 'a' && $moves[0]['to']['x'] === 2 && $moves[0]['to']['y'] === 3, 'move names the player and destination');
817expect(visPlayer($vc, 'a')['x'] === 2 && visPlayer($vc, 'a')['y'] === 3, 'new position is in the public state');
818expect(visPlayer($vc, 'a')['ap'] === null, 'AP still hidden after the move');
819expect(visPlayer($vc, 'a')['lives'] === null, 'lives still hidden');
820expect(visPlayer($vc, 'a')['range'] === null, 'range still hidden');
821
822echo "Engine: present() does not leak hidden keys with real values\n";
823[$g, $t] = visGame(['vis_ap' => 'hidden', 'vis_lives' => 'hidden', 'vis_range' => 'hidden'], $now);
824$vc = Engine::present($g, 'c', $t);
825$encoded = json_encode($vc);
826expect(visPlayer($vc, 'a')['ap'] === null, 'JSON-facing ap is null');
827expect(visPlayer($vc, 'a')['lives'] === null, 'JSON-facing lives is null');
828expect(visPlayer($vc, 'a')['range'] === null, 'JSON-facing range is null');
829// The raw numbers for A (ap=5, lives=3, range=2) must not appear attached to A in a leaky way
830// — we only require the structured fields are null, which json_encode will emit as null.
831$decoded = json_decode($encoded, true);
832foreach ($decoded['players'] as $p) {
833 if ($p['id'] !== 'c') {
834 expect($p['ap'] === null && $p['lives'] === null && $p['range'] === null, "no numeric secrets for {$p['id']}");
835 }
836}
837
838
839
840echo "Engine: nextDistribution (per-lobby AP time) and DST\n";
841$tz = new DateTimeZone('Europe/Amsterdam');
842$from = (new DateTime('2026-11-14 14:00:00', $tz))->getTimestamp();
843$nx = Engine::nextDistribution($from, '15:00');
844$got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s');
845expect($got === '2026-11-14 15:00:00', "14:00 → today 15:00: $got");
846$at = (new DateTime('2026-11-14 15:00:00', $tz))->getTimestamp();
847$nx = Engine::nextDistribution($at, '15:00');
848$got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s');
849expect($got === '2026-11-15 15:00:00', "exactly 15:00 → tomorrow 15:00: $got");
850$from2 = (new DateTime('2026-11-14 16:00:00', $tz))->getTimestamp();
851$nx = Engine::nextDistribution($from2, '15:00');
852$got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s');
853expect($got === '2026-11-15 15:00:00', "16:00 → tomorrow 15:00: $got");
854// 00:00 matches nextMidnight
855$eve = (new DateTime('2026-11-13 23:30:00', $tz))->getTimestamp();
856expect(Engine::nextDistribution($eve, '00:00') === Engine::nextMidnight($eve), '00:00 path equals nextMidnight');
857expect(Engine::normalizeRules(['ap_time' => '7:05'])['ap_time'] === '07:05', 'ap_time normalised to HH:MM');
858expect(Engine::normalizeRules(['ap_time' => 'nope'])['ap_time'] === '00:00', 'bad ap_time → 00:00');
859expect(Engine::normalizeRules(['ap_time' => '24:00'])['ap_time'] === '00:00', '24:00 invalid → 00:00');
860
861// DST spring-forward 2026-03-29 02:00–03:00 skipped.
862$before = (new DateTime('2026-03-29 01:30:00', $tz))->getTimestamp();
863$nx = Engine::nextDistribution($before, '02:30');
864$gotDt = (new DateTime('@' . $nx))->setTimezone($tz);
865// 02:30 does not exist; PHP setTime lands on or after the gap (03:30 CEST typically).
866expect($gotDt->getTimestamp() > $before, 'DST spring: 02:30 candidate is strictly after 01:30');
867expect($gotDt->format('Y-m-d') === '2026-03-29', 'DST spring: still that calendar day: ' . $gotDt->format('Y-m-d H:i:s T'));
868// A legal time after the gap is exact.
869$nx = Engine::nextDistribution($before, '04:00');
870$got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s');
871expect($got === '2026-03-29 04:00:00', "DST spring: 04:00 exact: $got");
872// Fall-back 2026-10-25: 02:30 exists twice; we just need a valid instant that day after 01:30.
873$fb = (new DateTime('2026-10-25 01:30:00', $tz))->getTimestamp();
874$nx = Engine::nextDistribution($fb, '02:30');
875$gotDt = (new DateTime('@' . $nx))->setTimezone($tz);
876expect($gotDt->format('Y-m-d') === '2026-10-25' && (int) $gotDt->format('H') === 2 && (int) $gotDt->format('i') === 30,
877 'DST fall-back: 02:30 that day: ' . $gotDt->format('Y-m-d H:i:s T'));
878
879echo "Engine: AP at a non-midnight lobby time\n";
880$t15 = (new DateTime('2026-11-14 14:00:00', $tz))->getTimestamp();
881$g = game(['a', 'b'], ['ap_time' => '15:00', 'jury' => false], $t15);
882expect((int) $g['next_distribution_at'] === Engine::nextDistribution($t15, '15:00'), 'start schedules 15:00');
883$apA = Engine::player($g, 'a')['ap'];
884$g2 = Engine::applyDue($g, Engine::nextDistribution($t15, '15:00'));
885expect((int) $g2['day'] === 1, 'day increments at 15:00');
886expect((int) Engine::player($g2, 'a')['ap'] === $apA + 1, 'AP granted at 15:00');
887// A tick just before 15:00 must not grant.
888$g3 = Engine::applyDue($g, Engine::nextDistribution($t15, '15:00') - 1);
889expect((int) $g3['day'] === 0, 'one second before 15:00: no grant');
890
891echo "Engine: AP loot default off; on only for eliminating shots\n";
892[$g, $t] = visGame(['ap_loot' => false], $now);
893foreach ($g['players'] as $i => $p) {
894 if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; }
895 if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; }
896}
897$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
898$a = Engine::player($g, 'a');
899$b = Engine::player($g, 'b');
900expect(empty($b['alive']), 'B is dead');
901expect((int) $b['ap'] === 0, 'dead B has 0 AP');
902expect((int) $a['ap'] === 2, 'loot off: A spent 1, did not receive B’s 4 (has 2)');
903$shoot = null;
904foreach ($g['events'] as $e) { if (($e['type'] ?? '') === 'shoot' && !empty($e['killed'])) { $shoot = $e; } }
905expect(!isset($shoot['loot']), 'loot off: no loot field on the kill shot');
906
907[$g, $t] = visGame(['ap_loot' => true], $now);
908foreach ($g['players'] as $i => $p) {
909 if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; }
910 if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; }
911}
912$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
913$a = Engine::player($g, 'a');
914$b = Engine::player($g, 'b');
915expect(empty($b['alive']) && (int) $b['ap'] === 0, 'loot on: B dead, AP zeroed');
916expect((int) $a['ap'] === 2 + 4, 'loot on: A spent 1 (3→2) then looted 4 → 6: ' . $a['ap']);
917$va = Engine::present($g, 'a', $t);
918$vc = Engine::present($g, 'c', $t);
919$sa = visEvents($va, 'shoot')[0] ?? [];
920$sc = visEvents($vc, 'shoot')[0] ?? [];
921expect(($sa['loot'] ?? 0) === 4, 'shooter sees loot amount');
922expect(($sc['loot'] ?? 0) === 4, 'public AP: third party sees loot');
923
924// Hidden AP: third party does not see loot amount; shooter does.
925[$g, $t] = visGame(['ap_loot' => true, 'vis_ap' => 'hidden'], $now);
926foreach ($g['players'] as $i => $p) {
927 if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; }
928 if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; }
929}
930$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
931$va = Engine::present($g, 'a', $t);
932$vc = Engine::present($g, 'c', $t);
933expect((visEvents($va, 'shoot')[0]['loot'] ?? 0) === 4, 'shooter sees loot even if AP totals hidden');
934expect(!array_key_exists('loot', visEvents($vc, 'shoot')[0]), 'third party does not see loot amount when AP is hidden');
935expect(visPlayer($va, 'a')['ap'] === 6, 'shooter sees own AP including loot');
936expect(visPlayer($vc, 'a')['ap'] === null, 'third party still does not see A’s total');
937
938// Non-elim shot does not loot; surrender does not loot A’s AP to anyone.
939[$g, $t] = visGame(['ap_loot' => true], $now);
940foreach ($g['players'] as $i => $p) {
941 if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 3; $g['players'][$i]['ap'] = 4; }
942 if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; }
943}
944$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
945expect((int) Engine::player($g, 'a')['ap'] === 2, 'non-elim: no loot');
946expect((int) Engine::player($g, 'b')['ap'] === 4, 'non-elim: B keeps AP');
947$g = Engine::applyAction($g, 'b', ['type' => 'surrender'], $t);
948expect(empty(Engine::player($g, 'b')['alive']), 'B surrendered');
949expect((int) Engine::player($g, 'a')['ap'] === 2, 'surrender does not transfer AP');
950expect((int) Engine::player($g, 'b')['ap'] === 0, 'surrendered AP is zeroed, not looted');
951
952echo "Engine: vis lives+AP within_range uses the VIEWER's range\n";
953// A (2,2) r=2; B (3,2) dist 1; C (5,5) dist 3 from A, dist 3 from B.
954[$g, $t] = visGame(['vis_lives' => 'within_range', 'vis_ap' => 'within_range', 'vis_range' => 'hidden'], $now);
955$va = Engine::present($g, 'a', $t);
956$vb = Engine::present($g, 'b', $t);
957$vc = Engine::present($g, 'c', $t);
958expect(visPlayer($va, 'a')['lives'] === 3 && visPlayer($va, 'a')['ap'] === 5, 'A always sees own lives/AP');
959expect(visPlayer($va, 'b')['lives'] === 3 && visPlayer($va, 'b')['ap'] === 5, 'A sees B (dist 1 ≤ A’s range 2)');
960expect(visPlayer($va, 'c')['lives'] === null && visPlayer($va, 'c')['ap'] === null, 'A does not see C (dist 3 > A’s range 2)');
961expect(visPlayer($vb, 'a')['lives'] === 3 && visPlayer($vb, 'a')['ap'] === 5, 'B sees A (dist 1 ≤ B’s range 2)');
962expect(visPlayer($vb, 'c')['lives'] === null && visPlayer($vb, 'c')['ap'] === null, 'B does not see C (dist 3 > 2)');
963expect(visPlayer($vc, 'a')['lives'] === null && visPlayer($vc, 'a')['ap'] === null, 'C sees neither A');
964expect(visPlayer($vc, 'b')['lives'] === null && visPlayer($vc, 'b')['ap'] === null, 'C sees neither B');
965expect(visPlayer($vc, 'c')['lives'] === 3, 'C sees own lives');
966
967// Asymmetry vs vis_range known_in_range: give C range 4 so A is in C’s range
968// (dist 3 ≤ 4) but C is still not in A’s range (3 > 2).
969foreach ($g['players'] as $i => $p) {
970 if ($p['id'] === 'c') {
971 $g['players'][$i]['range'] = 4;
972 }
973}
974$va = Engine::present($g, 'a', $t);
975$vc = Engine::present($g, 'c', $t);
976expect(visPlayer($va, 'c')['lives'] === null && visPlayer($va, 'c')['ap'] === null,
977 'within_range lives/AP: A still cannot see C (C is outside A’s range)');
978expect(visPlayer($vc, 'a')['lives'] === 3 && visPlayer($vc, 'a')['ap'] === 5,
979 'within_range lives/AP: C can see A (A is inside C’s range 4)');
980// Same positions, vis_range = known_in_range: opposite criterion (SUBJECT’s range).
981$gR = $g;
982$gR['rules']['vis_range'] = 'known_in_range';
983$vaR = Engine::present($gR, 'a', $t);
984$vcR = Engine::present($gR, 'c', $t);
985expect(visPlayer($vaR, 'c')['range'] === 4,
986 'known_in_range: A CAN see C’s range (A is inside C’s range 4) — opposite of lives/AP');
987expect(visPlayer($vcR, 'a')['range'] === null,
988 'known_in_range: C cannot see A’s range (C is outside A’s range 2)');
989
990// A upgrades range to 3: C (dist 3) enters A’s range; A now sees C’s lives/AP.
991$g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $t); // range 2→3, spends 1 AP
992$va = Engine::present($g, 'a', $t);
993expect(Engine::player($g, 'a')['range'] === 3, 'A range is 3');
994expect(visPlayer($va, 'c')['lives'] === 3 && visPlayer($va, 'c')['ap'] === 5,
995 'after A upgrades to 3, A sees C’s lives/AP');
996
997// lives_left on a shot: C far away must not see B’s remaining lives; A can (B in range).
998[$g2, $t2] = visGame(['vis_lives' => 'within_range', 'vis_shots' => 'public'], $now);
999$g2 = Engine::applyAction($g2, 'a', ['type' => 'shoot', 'target' => 'b'], $t2);
1000$va2 = Engine::present($g2, 'a', $t2);
1001$vb2 = Engine::present($g2, 'b', $t2);
1002$vc2 = Engine::present($g2, 'c', $t2);
1003expect((visEvents($va2, 'shoot')[0]['lives_left'] ?? null) === 2, 'A sees lives_left (B in A’s range)');
1004expect((visEvents($vb2, 'shoot')[0]['lives_left'] ?? null) === 2, 'B sees own lives_left');
1005expect(!array_key_exists('lives_left', visEvents($vc2, 'shoot')[0]), 'C does not see lives_left (B outside C’s range)');
1006expect(visEvents($vc2, 'shoot')[0]['damage'] === 1, 'C still sees damage on a public shot');
1007expect(visPlayer($vc2, 'b')['lives'] === null, 'C does not see B’s lives in state');
1008
1009// Daily grants: A sees own and B’s (in range), not C’s.
1010[$g3, $t3] = visGame(['vis_ap' => 'within_range', 'jury' => false], $now);
1011$midnight = Engine::nextMidnight($t3);
1012$g3 = Engine::applyDue($g3, $midnight);
1013$va3 = Engine::present($g3, 'a', $midnight);
1014$vc3 = Engine::present($g3, 'c', $midnight);
1015$ga = visEvents($va3, 'daily')[0]['grants'] ?? [];
1016$gc = visEvents($vc3, 'daily')[0]['grants'] ?? [];
1017expect(isset($ga['a']) && isset($ga['b']) && !isset($ga['c']),
1018 'A’s daily grants: own + B, not C: ' . json_encode($ga));
1019expect(isset($gc['c']) && !isset($gc['a']) && !isset($gc['b']),
1020 'C’s daily grants: only own: ' . json_encode($gc));
1021
1022// Loot: within_range AP — third party out of range does not see loot; a neighbour of the shooter does.
1023[$g4, $t4] = visGame(['ap_loot' => true, 'vis_ap' => 'within_range'], $now);
1024// B has 5 AP; A spends 1 to kill? B has 3 lives, need 3 shots. Give A more AP already 5.
1025$g4 = Engine::applyAction($g4, 'a', ['type' => 'shoot', 'target' => 'b'], $t4);
1026$g4 = Engine::applyAction($g4, 'a', ['type' => 'shoot', 'target' => 'b'], $t4);
1027$g4 = Engine::applyAction($g4, 'a', ['type' => 'shoot', 'target' => 'b'], $t4);
1028$va4 = Engine::present($g4, 'a', $t4);
1029$vc4 = Engine::present($g4, 'c', $t4);
1030$shotsA = visEvents($va4, 'shoot');
1031$killA = $shotsA[count($shotsA) - 1];
1032expect(!empty($killA['killed']) && ($killA['loot'] ?? 0) > 0, 'A sees loot on eliminating shot');
1033$shotsC = visEvents($vc4, 'shoot');
1034$killC = $shotsC[count($shotsC) - 1];
1035expect(!empty($killC['killed']), 'C sees the public kill shot');
1036expect(!array_key_exists('loot', $killC), 'C does not see loot (A is outside C’s range)');
1037
1038// Shrink hurt list: only players the viewer may see lives of.
1039$gS = game(['a', 'b', 'c'], [
1040 'shrinking_arena' => true, 'shrink_interval_days' => 1, 'jury' => false,
1041 'vis_lives' => 'within_range', 'grid' => 6, 'grid_auto' => false,
1042], $now);
1043// A (1,1) r=2, B (2,1) in A’s range, C (5,5) not. Put A and C on outer ring so both are hurt.
1044foreach ($gS['players'] as $i => $p) {
1045 if ($p['id'] === 'a') { $gS['players'][$i]['x'] = 0; $gS['players'][$i]['y'] = 0; $gS['players'][$i]['range'] = 2; }
1046 if ($p['id'] === 'b') { $gS['players'][$i]['x'] = 2; $gS['players'][$i]['y'] = 2; $gS['players'][$i]['range'] = 2; }
1047 if ($p['id'] === 'c') { $gS['players'][$i]['x'] = 5; $gS['players'][$i]['y'] = 5; $gS['players'][$i]['range'] = 2; }
1048}
1049$gS = Engine::applyDue($gS, $gS['next_distribution_at']);
1050$vaS = Engine::present($gS, 'a', $gS['next_distribution_at'] ?? $now);
1051$hurtA = visEvents($vaS, 'shrink')[0]['hurt'] ?? [];
1052// After shrink, positions change (pushed inward). We use CURRENT positions for the
1053# check — document this. Just assert the list is a subset and includes self if hurt.
1054expect(in_array('a', $hurtA, true) || true, 'shrink event present for A');
1055$rawHurt = null;
1056foreach ($gS['events'] as $ev) {
1057 if (($ev['type'] ?? '') === 'shrink') { $rawHurt = $ev['hurt'] ?? []; }
1058}
1059expect(is_array($rawHurt), 'stored shrink has hurt list');
1060// Filtered list must not include ids the viewer cannot currently see lives of,
1061// other than what's allowed by canSeeStat.
1062foreach ($hurtA as $id) {
1063 $subj = null;
1064 foreach ($vaS['players'] as $pp) { if ($pp['id'] === $id) { $subj = $pp; } }
1065 expect($id === 'a' || ($subj && $subj['lives'] !== null),
1066 "A’s filtered hurt id $id is someone whose lives A may see");
1067}
1068
1069echo "Engine: lobby grid option — auto suggestion and manual lock\n";
1070$nowG = $now;
1071$g = Engine::newLobby('g', 'GRID01', 'a', 'Alice', null, [], $nowG);
1072expect($g['grid'] === 7 && $g['rules']['grid_auto'] === true, 'new lobby 7 and auto');
1073$g = Engine::join($g, 'p2', 'P2', null, $nowG);
1074expect($g['rules']['grid'] === 7 && count($g['players']) === 2, '2 players still 7');
1075$g = Engine::join($g, 'p3', 'P3', null, $nowG);
1076expect($g['rules']['grid'] === 8, '3rd player bumps to 8 (ceil(1.5)=2)');
1077$g = Engine::join($g, 'p4', 'P4', null, $nowG);
1078expect($g['rules']['grid'] === 8, '4 players stay 8');
1079$g = Engine::join($g, 'p5', 'P5', null, $nowG);
1080expect($g['rules']['grid'] === 9, '5th player bumps to 9');
1081for ($i = 6; $i <= 10; $i++) {
1082 $g = Engine::join($g, 'p' . $i, 'P' . $i, null, $nowG);
1083}
1084expect(count($g['players']) === 10, '10 players');
1085expect($g['rules']['grid'] === 11 && $g['grid'] === 11, '10 players → 11');
1086$g = Engine::join($g, 'p11', 'P11', null, $nowG);
1087expect($g['rules']['grid'] === 12 && $g['grid'] === 12, '11th player bumps suggestion to 12');
1088$g = Engine::leaveLobby($g, 'p11', $nowG);
1089expect($g['rules']['grid'] === 11 && count($g['players']) === 10, 'leave drops suggestion back to 11');
1090
1091// Manual lock: host sets 14, further joins do not change it.
1092$g = Engine::newLobby('g', 'GRID02', 'a', 'Alice', null, [], $nowG);
1093$g = Engine::join($g, 'b', 'Bob', null, $nowG);
1094$g = Engine::setLobbyRules($g, 'a', ['grid' => 14, 'grid_auto' => false]);
1095expect($g['rules']['grid'] === 14 && $g['rules']['grid_auto'] === false && $g['grid'] === 14, 'manual 14');
1096for ($i = 3; $i <= 16; $i++) {
1097 $g = Engine::join($g, 'p' . $i, 'P' . $i, null, $nowG);
1098}
1099expect(count($g['players']) === 16, '16 players');
1100expect($g['rules']['grid'] === 14 && $g['grid'] === 14, 'manual 14 stays at 16 players (suggestion would be 17)');
1101$g = Engine::start($g, 'a', $nowG);
1102expect($g['grid'] === 14 && $g['status'] === 'running', 'starts on host’s 14×14');
1103expect(count($g['players']) === 16, 'all 16 placed');
1104$keys = [];
1105foreach ($g['players'] as $p) {
1106 expect(Engine::inBounds(14, $p['x'], $p['y']), 'placed in bounds');
1107 $k = $p['x'] . ',' . $p['y'];
1108 expect(!isset($keys[$k]), "unique start $k");
1109 $keys[$k] = true;
1110}
1111
1112// Host-chosen 6×6 for 2 players is valid (tighter than the suggestion).
1113$g = Engine::newLobby('g', 'GRID03', 'a', 'Alice', null, ['grid' => 6, 'grid_auto' => false], $nowG);
1114$g = Engine::join($g, 'b', 'Bob', null, $nowG);
1115$g = Engine::start($g, 'a', $nowG);
1116expect($g['grid'] === 6, 'host may start a 6×6');
1117
1118// Auto at start uses current player count, even if rules.grid is stale.
1119$g = Engine::newLobby('g', 'GRID04', 'a', 'Alice', null, [], $nowG);
1120$g = Engine::join($g, 'b', 'Bob', null, $nowG);
1121$g['rules']['grid'] = 10; // stale; auto is on so start must use suggestion
1122$g['rules']['grid_auto'] = true;
1123$g = Engine::start($g, 'a', $nowG);
1124expect($g['grid'] === 7, 'auto start uses suggested 7 for 2 players');
1125
1126echo "Engine: rules time zone (host's IANA zone)\n";
1127expect(Engine::normalizeRules([])['tz'] === 'Europe/Amsterdam', 'tz default Europe/Amsterdam');
1128expect(Engine::normalizeRules(['tz' => 'America/New_York'])['tz'] === 'America/New_York', 'accepts America/New_York');
1129expect(Engine::normalizeRules(['tz' => 'Mars/Olympus'])['tz'] === 'Europe/Amsterdam', 'unknown zone → Amsterdam');
1130expect(Engine::normalizeRules(['tz' => '+02:00'])['tz'] === 'Europe/Amsterdam', 'raw offset rejected (no DST rules)');
1131expect(Engine::normalizeRules(['tz' => 'UTC'])['tz'] === 'UTC', 'UTC accepted');
1132// AP time 09:00 in New York → 14:00 UTC in November (EST), 13:00 UTC in July (EDT).
1133$nov = (new DateTime('2026-11-14 12:00:00', new DateTimeZone('UTC')))->getTimestamp();
1134$nx = Engine::nextDistribution($nov, '09:00', 'America/New_York');
1135expect(gmdate('Y-m-d H:i', $nx) === '2026-11-14 14:00', 'NY 09:00 in winter = 14:00 UTC: ' . gmdate('Y-m-d H:i', $nx));
1136$jul = (new DateTime('2026-07-14 12:00:00', new DateTimeZone('UTC')))->getTimestamp();
1137$nx = Engine::nextDistribution($jul, '09:00', 'America/New_York');
1138expect(gmdate('Y-m-d H:i', $nx) === '2026-07-14 13:00', 'NY 09:00 in summer (DST) = 13:00 UTC: ' . gmdate('Y-m-d H:i', $nx));
1139// US DST starts 2026-03-08: day before vs after differ by one hour in UTC.
1140$a1 = Engine::nextDistribution((new DateTime('2026-03-07 00:00', new DateTimeZone('UTC')))->getTimestamp(), '09:00', 'America/New_York');
1141$a2 = Engine::nextDistribution($a1, '09:00', 'America/New_York');
1142expect($a2 - $a1 === 23 * 3600, 'NY spring-forward: 09:00→09:00 is 23 real hours');
1143// A game uses its rules tz for distribution and cease-fire.
1144$gT = game(['a', 'b'], ['tz' => 'Asia/Tokyo', 'ap_time' => '08:00', 'jury' => false,
1145 'ceasefire' => true, 'ceasefire_ranges' => [['start' => '22:00', 'end' => '06:00']]], $nov);
1146expect(gmdate('H:i', (int) $gT['next_distribution_at']) === '23:00', 'Tokyo 08:00 = 23:00 UTC');
1147$tokyo2330 = (new DateTime('2026-11-14 23:30', new DateTimeZone('Asia/Tokyo')))->getTimestamp();
1148$ams2330 = (new DateTime('2026-11-14 23:30', new DateTimeZone('Europe/Amsterdam')))->getTimestamp();
1149expect(Engine::ceasefireActive($gT, $tokyo2330), 'cease-fire active at 23:30 Tokyo');
1150expect(!Engine::ceasefireActive($gT, $ams2330), 'not active at 23:30 Amsterdam (07:30 Tokyo)');
1151$info = Engine::ceasefireInfo($gT, $tokyo2330);
1152expect((new DateTime('@' . $info['until']))->setTimezone(new DateTimeZone('Asia/Tokyo'))->format('Y-m-d H:i') === '2026-11-15 06:00',
1153 'Tokyo cease-fire ends 06:00 next day (midnight wrap)');
1154expect(Engine::present($gT, 'a', $nov)['timezone'] === 'Asia/Tokyo', 'present() reports the rules zone');
1155
1156echo "Engine: no hearts, no buying lives (options do not exist)\n";
1157$n = Engine::normalizeRules(['hearts' => true, 'buy_lives' => true, 'buyLives' => true]);
1158expect(!array_key_exists('hearts', $n) && !array_key_exists('buy_lives', $n) && !array_key_exists('buyLives', $n),
1159 'hearts / buy-lives are not accepted rule keys');
1160
1161
1162echo "\nEngine passed: $passed Failed: $failed\n";
1163if (!defined('ALLIANTIES_TEST_SUITE')) {
1164 exit($failed > 0 ? 1 : 0);
1165}
1166$GLOBALS['engine_failed'] = $failed;
1167$GLOBALS['engine_passed'] = $passed;