BlueGit

projects-timfalken-com allianties

Files

allianties / server / tests / engine_test.php

engine_test.php

engine_test.php 46.7 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 and placement\n";
32expect(Engine::gridSize(2) === 6, '2 players → 6×6');
33expect(Engine::gridSize(4) === 6, '4 players → 6×6');
34expect(Engine::gridSize(8) === 7, '8 players → 7×7 (ceil sqrt(48)=7)');
35expect(Engine::gridSize(12) === 9, '12 players → 9×9 (ceil sqrt(72)=9)');
36expect(Engine::maxInset(6) === 1, '6×6 max inset 1 (leaves 4×4, min arena 3)');
37expect(Engine::maxInset(7) === 2, '7×7 max inset 2 (leaves 3×3)');
38$pos = Engine::startingPositions(4, 6);
39expect(count($pos) === 4, '4 starting positions');
40$keys = array_map(fn ($p) => $p['x'] . ',' . $p['y'], $pos);
41expect(count(array_unique($keys)) === 4, 'starting positions are unique');
42foreach ($pos as $p) {
43 expect(Engine::inBounds(6, $p['x'], $p['y']), "start in bounds {$p['x']},{$p['y']}");
44}
45
46echo "Engine: Chebyshev range\n";
47expect(Engine::chebyshev(0, 0, 2, 2) === 2, 'diagonal 2 is range 2');
48expect(Engine::chebyshev(0, 0, 2, 0) === 2, 'orthogonal 2 is range 2');
49expect(Engine::chebyshev(5, 5, 5, 5) === 0, 'same tile is 0');
50
51echo "Engine: midnight Amsterdam\n";
52// 2026-11-13 23:00 CET = 2026-11-13 22:00 UTC = 1763071200? Let's compute.
53$tz = new DateTimeZone('Europe/Amsterdam');
54$dt = new DateTime('2026-11-13 23:30:00', $tz);
55$from = $dt->getTimestamp();
56$next = Engine::nextMidnight($from);
57$nx = (new DateTime('@' . $next))->setTimezone($tz);
58expect($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'));
59$dt2 = new DateTime('2026-11-14 00:00:00', $tz);
60$next2 = Engine::nextMidnight($dt2->getTimestamp());
61$nx2 = (new DateTime('@' . $next2))->setTimezone($tz);
62expect($nx2->format('Y-m-d H:i:s') === '2026-11-15 00:00:00', 'at exactly 00:00, next is tomorrow');
63// DST spring-forward 2026-03-29 in NL
64$dt3 = new DateTime('2026-03-28 23:00:00', $tz);
65$next3 = Engine::nextMidnight($dt3->getTimestamp());
66$nx3 = (new DateTime('@' . $next3))->setTimezone($tz);
67expect($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'));
68
69echo "Engine: lobby and start\n";
70$now = (new DateTime('2026-11-13 15:00:00', $tz))->getTimestamp();
71$g = Engine::newLobby('g', 'CODE12', 'a', 'Alice', null, [], $now);
72expect($g['status'] === 'lobby', 'new lobby');
73expect($g['rules']['jury'] === true, 'jury default ON');
74expect($g['rules']['shrinking_arena'] === false, 'shrink default OFF');
75expect($g['rules']['shrink_interval_days'] === 7, 'shrink interval default 7');
76expect($g['rules']['ceasefire'] === false, 'ceasefire default OFF');
77expect($g['rules']['ceasefire_ranges'] === [], 'ceasefire ranges empty by default');
78expect($g['rules']['vis_shots'] === 'public', 'vis_shots default public');
79expect($g['rules']['vis_gifts'] === 'public', 'vis_gifts default public');
80expect($g['rules']['vis_ap'] === 'public', 'vis_ap default public');
81expect($g['rules']['vis_range'] === 'public', 'vis_range default public');
82expect($g['rules']['vis_lives'] === 'public', 'vis_lives default public');
83expect($g['rules']['ap_loot'] === false, 'ap_loot default OFF');
84expect($g['rules']['ap_time'] === '00:00', 'ap_time default 00:00');
85expect($g['host_id'] === 'a', 'host is creator');
86try {
87 Engine::start($g, 'a', $now);
88 expect(false, 'start alone should throw');
89} catch (RuntimeException $e) {
90 expect($e->getMessage() === 'min_players', 'start alone: min_players');
91}
92$g = Engine::join($g, 'b', 'Bob', null, $now);
93expect(count($g['players']) === 2, '2 players');
94try {
95 Engine::start($g, 'b', $now);
96 expect(false, 'non-host start');
97} catch (RuntimeException $e) {
98 expect($e->getMessage() === 'not_host', 'non-host cannot start');
99}
100$g = Engine::start($g, 'a', $now);
101expect($g['status'] === 'running', 'started');
102expect($g['grid'] === 6, 'grid 6');
103expect($g['players'][0]['ap'] === 1, 'start with 1 AP');
104expect($g['players'][0]['lives'] === 3, 'start with 3 lives');
105expect($g['players'][0]['range'] === 2, 'start with range 2');
106expect($g['players'][0]['x'] !== $g['players'][1]['x'] || $g['players'][0]['y'] !== $g['players'][1]['y'], 'not overlapping');
107$nd = (new DateTime('@' . $g['next_distribution_at']))->setTimezone($tz);
108expect($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'));
109
110echo "Engine: move, blocked, AP\n";
111$a = $g['players'][0];
112$legal = Engine::legal($g, 'a');
113expect(count($legal['move']) >= 1, 'Alice has at least one move');
114$t = $legal['move'][0];
115$g2 = Engine::applyAction($g, 'a', ['type' => 'move', 'x' => $t['x'], 'y' => $t['y']], $now);
116expect($g2['players'][0]['x'] === $t['x'] && $g2['players'][0]['y'] === $t['y'], 'moved');
117expect($g2['players'][0]['ap'] === 0, 'spent 1 AP');
118expect(end($g2['events'])['type'] === 'move', 'move event');
119expect(end($g2['events'])['spend'] === true, 'move is a spend tick');
120try {
121 Engine::applyAction($g2, 'a', ['type' => 'move', 'x' => $t['x'] + 1, 'y' => $t['y']], $now);
122 expect(false, 'move without AP');
123} catch (RuntimeException $e) {
124 expect($e->getMessage() === 'no_ap' || $e->getMessage() === 'invalid_move', 'no AP: ' . $e->getMessage());
125}
126
127echo "Engine: shoot and death and win\n";
128// Place two players adjacent, Alice shoots Bob three times (give extra AP).
129$g = game(['a', 'b'], [], $now);
130$g['players'][0]['x'] = 2;
131$g['players'][0]['y'] = 2;
132$g['players'][0]['ap'] = 5;
133$g['players'][1]['x'] = 3;
134$g['players'][1]['y'] = 3;
135$g['players'][1]['ap'] = 0;
136$legal = Engine::legal($g, 'a');
137expect(in_array('b', $legal['shoot'], true), 'Bob is in range 2 (actually dist 1)');
138$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now);
139expect($g['players'][1]['lives'] === 2, 'Bob has 2 lives');
140$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now);
141$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now);
142expect($g['players'][1]['alive'] === false, 'Bob is dead');
143expect($g['status'] === 'finished', 'game finished');
144expect($g['winner_id'] === 'a', 'Alice wins');
145$types = array_column($g['events'], 'type');
146expect(in_array('death', $types, true), 'death event');
147expect(in_array('win', $types, true), 'win event');
148
149echo "Engine: give AP and upgrade\n";
150$g = game(['a', 'b'], [], $now);
151$g['players'][0]['x'] = 2;
152$g['players'][0]['y'] = 2;
153$g['players'][0]['ap'] = 4;
154$g['players'][1]['x'] = 3;
155$g['players'][1]['y'] = 2;
156$g['players'][1]['ap'] = 0;
157$g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $now);
158expect($g['players'][0]['ap'] === 2, 'Alice spent 2');
159expect($g['players'][1]['ap'] === 2, 'Bob received 2');
160try {
161 Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 5], $now);
162 expect(false, 'give more than you have');
163} catch (RuntimeException $e) {
164 expect($e->getMessage() === 'bad_give', 'bad_give');
165}
166$g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $now);
167expect($g['players'][0]['range'] === 3, 'range is now 3');
168expect($g['players'][0]['ap'] === 1, 'upgrade costs 1');
169
170echo "Engine: out of range and occupied\n";
171$g = game(['a', 'b'], [], $now);
172$g['players'][0]['x'] = 0;
173$g['players'][0]['y'] = 0;
174$g['players'][0]['ap'] = 2;
175$g['players'][1]['x'] = 5;
176$g['players'][1]['y'] = 5;
177$legal = Engine::legal($g, 'a');
178expect(!in_array('b', $legal['shoot'], true), 'Bob at (5,5) is out of range 2 from (0,0)');
179try {
180 Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now);
181 expect(false, 'shoot out of range');
182} catch (RuntimeException $e) {
183 expect($e->getMessage() === 'out_of_range', 'out_of_range');
184}
185// Occupied neighbour is not a legal move.
186$g['players'][1]['x'] = 1;
187$g['players'][1]['y'] = 0;
188$legal = Engine::legal($g, 'a');
189$occ = false;
190foreach ($legal['move'] as $m) {
191 if ($m['x'] === 1 && $m['y'] === 0) {
192 $occ = true;
193 }
194}
195expect($occ === false, 'cannot move onto Bob');
196
197echo "Engine: surrender\n";
198$g = game(['a', 'b'], [], $now);
199$g = Engine::applyAction($g, 'b', ['type' => 'surrender'], $now);
200expect($g['players'][1]['alive'] === false, 'Bob surrendered');
201expect($g['players'][1]['surrendered'] === true, 'flagged surrendered');
202expect($g['winner_id'] === 'a', 'Alice wins after surrender');
203
204echo "Engine: daily AP grant\n";
205$g = game(['a', 'b'], ['jury' => false], $now);
206$g['players'][0]['ap'] = 0;
207$g['players'][1]['ap'] = 0;
208$at = $g['next_distribution_at'];
209$g = Engine::applyDue($g, $at);
210expect($g['day'] === 1, 'day is 1');
211expect($g['players'][0]['ap'] === 1, 'Alice got 1 AP');
212expect($g['players'][1]['ap'] === 1, 'Bob got 1 AP');
213// Catch-up two more midnights
214$at2 = Engine::nextMidnight($at);
215$at3 = Engine::nextMidnight($at2);
216$g = Engine::applyDue($g, $at3);
217expect($g['day'] === 3, 'caught up to day 3, day=' . $g['day']);
218expect($g['players'][0]['ap'] === 3, '3 AP after 3 days (started 0): ' . $g['players'][0]['ap']);
219
220echo "Engine: jury\n";
221$g = game(['a', 'b', 'c', 'd'], ['jury' => true, 'shrinking_arena' => false], $now);
222// Kill c and d without ending? Need 4 players, kill only c (one dead voter).
223// Place and shoot c twice then once more... easier: set lives.
224$g['players'][2]['lives'] = 0;
225$g['players'][2]['alive'] = false;
226$g['players'][2]['ap'] = 0;
227$g['players'][3]['lives'] = 0;
228$g['players'][3]['alive'] = false;
229$g['players'][3]['ap'] = 0;
230expect($g['status'] === 'running', 'still running with 2 alive');
231// c votes for a, d votes for a
232$g = Engine::applyAction($g, 'c', ['type' => 'vote', 'target' => 'a'], $now);
233$g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'a'], $now);
234expect($g['votes']['c'] === 'a', 'c voted a');
235// Vote is changeable
236$g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'b'], $now);
237expect($g['votes']['d'] === 'b', 'd changed vote to b');
238// Living player cannot vote
239try {
240 Engine::applyAction($g, 'a', ['type' => 'vote', 'target' => 'b'], $now);
241 expect(false, 'living vote');
242} catch (RuntimeException $e) {
243 expect($e->getMessage() === 'cannot_vote', 'living cannot vote');
244}
245// Present hides votes from others
246$viewA = Engine::present($g, 'a');
247expect($viewA['legal']['my_vote'] === null, 'Alice (living) has no vote');
248$viewC = Engine::present($g, 'c');
249expect($viewC['legal']['my_vote'] === 'a', 'Charlie sees own vote');
250expect(!isset($viewA['votes']), 'votes not in present payload');
251// Distribution: 1 vote a, 1 vote b → tie, both get +1 bonus (+1 regular) = +2
252$g['players'][0]['ap'] = 0;
253$g['players'][1]['ap'] = 0;
254$g = Engine::applyDue($g, $g['next_distribution_at']);
255expect($g['players'][0]['ap'] === 2, 'Alice got 1+1 (tie): ' . $g['players'][0]['ap']);
256expect($g['players'][1]['ap'] === 2, 'Bob got 1+1 (tie): ' . $g['players'][1]['ap']);
257expect($g['votes'] === [], 'votes reset after distribution');
258// Unique winner
259$g['players'][2]['alive'] = false;
260$g = Engine::applyAction($g, 'c', ['type' => 'vote', 'target' => 'a'], $now + 1);
261$g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'a'], $now + 1);
262$g['players'][0]['ap'] = 0;
263$g['players'][1]['ap'] = 0;
264$g = Engine::applyDue($g, $g['next_distribution_at']);
265expect($g['players'][0]['ap'] === 2, 'Alice unique jury winner: 2');
266expect($g['players'][1]['ap'] === 1, 'Bob only regular 1: ' . $g['players'][1]['ap']);
267// Jury off: no vote
268$gOff = game(['a', 'b', 'c'], ['jury' => false], $now);
269$gOff['players'][2]['alive'] = false;
270$gOff['players'][2]['lives'] = 0;
271try {
272 Engine::applyAction($gOff, 'c', ['type' => 'vote', 'target' => 'a'], $now);
273 expect(false, 'vote with jury off');
274} catch (RuntimeException $e) {
275 expect($e->getMessage() === 'cannot_vote', 'jury off: cannot_vote');
276}
277
278echo "Engine: shrinking arena\n";
279$g = game(['a', 'b'], ['shrinking_arena' => true, 'shrink_interval_days' => 2, 'jury' => false], $now);
280expect($g['grid'] === 6, '6×6');
281expect(Engine::daysUntilShrink($g) === 2, 'shrink in 2 days (interval 2, day 0): ' . (Engine::daysUntilShrink($g) ?? 'null'));
282// Put Alice on the outer ring (0,0), Bob in the centre (2,2)
283$g['players'][0]['x'] = 0;
284$g['players'][0]['y'] = 0;
285$g['players'][0]['lives'] = 3;
286$g['players'][0]['ap'] = 0;
287$g['players'][1]['x'] = 2;
288$g['players'][1]['y'] = 2;
289$g['players'][1]['ap'] = 0;
290// Day 1: no shrink
291$g = Engine::applyDue($g, $g['next_distribution_at']);
292expect($g['day'] === 1, 'day 1');
293expect($g['arena_inset'] === 0, 'no shrink on day 1');
294expect($g['players'][0]['x'] === 0 && $g['players'][0]['y'] === 0, 'Alice still on (0,0)');
295expect($g['players'][0]['lives'] === 3, 'Alice still 3 lives');
296expect(Engine::daysUntilShrink($g) === 1, 'shrink in 1 day');
297// Day 2: shrink
298$g = Engine::applyDue($g, $g['next_distribution_at']);
299expect($g['day'] === 2, 'day 2');
300expect($g['arena_inset'] === 1, 'inset is 1');
301expect($g['players'][0]['lives'] === 2, 'Alice lost 1 life: ' . $g['players'][0]['lives']);
302expect(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']);
303expect(!($g['players'][0]['x'] === 2 && $g['players'][0]['y'] === 2), 'not onto Bob');
304expect($g['players'][1]['lives'] === 3, 'Bob unhurt in centre');
305expect($g['players'][0]['ap'] === 2, 'Alice got day-1 and day-2 AP after surviving shrink: ' . $g['players'][0]['ap']);
306// Outer ring is impassable
307$legal = Engine::legal($g, $g['players'][0]['id'] === 'a' ? 'a' : 'a');
308foreach ($legal['move'] as $m) {
309 expect(Engine::isPassable(6, 1, $m['x'], $m['y']), "legal move {$m['x']},{$m['y']} is passable");
310}
311// Max one shrink on 6×6 (maxInset=1); day 4 would try again but stop
312$g = Engine::applyDue($g, $g['next_distribution_at']); // day 3
313$g = Engine::applyDue($g, $g['next_distribution_at']); // day 4
314expect($g['arena_inset'] === 1, 'does not shrink past max inset: ' . $g['arena_inset']);
315expect(Engine::daysUntilShrink($g) === null, 'no upcoming shrink at max');
316
317echo "Engine: shrink kills and crush and draw\n";
318$g = game(['a', 'b'], ['shrinking_arena' => true, 'shrink_interval_days' => 1, 'jury' => false], $now);
319$g['players'][0]['x'] = 0;
320$g['players'][0]['y'] = 0;
321$g['players'][0]['lives'] = 1;
322$g['players'][1]['x'] = 5;
323$g['players'][1]['y'] = 5;
324$g['players'][1]['lives'] = 1;
325$g = Engine::applyDue($g, $g['next_distribution_at']);
326expect($g['players'][0]['alive'] === false, 'Alice died on shrink (1 life)');
327expect($g['players'][1]['alive'] === false, 'Bob died on shrink (1 life)');
328expect($g['status'] === 'finished', 'finished');
329expect($g['draw'] === true, 'draw when both die in the same shrink');
330
331echo "Engine: notes and chat privacy\n";
332$g = game(['a', 'b'], [], $now);
333$g = Engine::setNote($g, 'a', 'b', 'looks dangerous');
334$viewA = Engine::present($g, 'a');
335$viewB = Engine::present($g, 'b');
336$noteAonB = null;
337$noteBonB = null;
338foreach ($viewA['players'] as $p) {
339 if ($p['id'] === 'b') {
340 $noteAonB = $p['note'];
341 }
342}
343foreach ($viewB['players'] as $p) {
344 if ($p['id'] === 'b') {
345 $noteBonB = $p['note'];
346 }
347}
348expect($noteAonB === 'looks dangerous', 'Alice sees her note on Bob');
349expect($noteBonB === '', 'Bob does not see Alice’s note');
350$g = Engine::addMessage($g, 'a', 'b', 'hello', $now);
351$viewA = Engine::present($g, 'a');
352$viewB = Engine::present($g, 'b');
353expect(count($viewA['messages']) === 1 && $viewA['messages'][0]['text'] === 'hello', 'Alice sees chat');
354expect(count($viewB['messages']) === 1, 'Bob sees chat');
355// A third player would not — add c in lobby... already started. Fine.
356
357echo "Engine: modular rule flags\n";
358$r = Engine::normalizeRules(['jury' => '0', 'shrinking_arena' => '1', 'shrink_interval_days' => 99, 'ceasefire' => '1']);
359expect($r['jury'] === false, 'jury "0" is false');
360expect($r['shrinking_arena'] === true, 'shrink "1" is true');
361expect($r['shrink_interval_days'] === 30, 'interval clamped to max 30: ' . $r['shrink_interval_days']);
362expect($r['ceasefire'] === true, 'ceasefire "1" is true');
363
364echo "Engine: cease-fire ranges and midnight wrap\n";
365$ranges = Engine::normalizeRanges([
366 ['start' => '22:00', 'end' => '06:00'],
367 ['start' => '12:00', 'end' => '13:30'],
368 ['start' => '25:00', 'end' => '01:00'], // invalid
369 ['start' => '08:00', 'end' => '08:00'], // zero-length dropped
370]);
371expect(count($ranges) === 2, '2 valid ranges: ' . count($ranges));
372expect($ranges[0]['start'] === '12:00', 'sorted, first is 12:00');
373expect($ranges[1]['start'] === '22:00', 'wrap range kept');
374
375$g = game(['a', 'b'], [
376 'jury' => false,
377 'ceasefire' => true,
378 'ceasefire_ranges' => [
379 ['start' => '22:00', 'end' => '06:00'],
380 ['start' => '12:00', 'end' => '13:00'],
381 ],
382], $now);
383// Place adjacent so shoot would otherwise be legal.
384$g['players'][0]['x'] = 2;
385$g['players'][0]['y'] = 2;
386$g['players'][0]['ap'] = 3;
387$g['players'][1]['x'] = 3;
388$g['players'][1]['y'] = 2;
389
390// 2026-11-13 23:30 Amsterdam — inside 22:00-06:00 wrap
391$inCf = (new DateTime('2026-11-13 23:30:00', $tz))->getTimestamp();
392$info = Engine::ceasefireInfo($g, $inCf);
393expect($info !== null && $info['active'] === true, '23:30 is cease-fire (wrap)');
394$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
395expect($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'));
396$legal = Engine::legal($g, 'a', $inCf);
397expect($legal['shoot'] === [], 'Attack disabled during cease-fire');
398expect($legal['ceasefire'] === true, 'legal.ceasefire true');
399expect(count($legal['move']) >= 1 || $legal['upgrade'] || true, 'non-shoot actions still considered');
400// Move is still allowed
401$legalMove = $legal['move'];
402expect(!empty($legalMove), 'can still move during cease-fire');
403try {
404 Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $inCf);
405 expect(false, 'shoot during cease-fire should throw');
406} catch (RuntimeException $e) {
407 expect($e->getMessage() === 'ceasefire', 'error code ceasefire: ' . $e->getMessage());
408}
409
410// 2026-11-14 06:00 exactly — end is exclusive, so not active
411$atEnd = (new DateTime('2026-11-14 06:00:00', $tz))->getTimestamp();
412$info = Engine::ceasefireInfo($g, $atEnd);
413expect($info['active'] === false, '06:00 exactly is not cease-fire (end exclusive)');
414$until = (new DateTime('@' . $info['until']))->setTimezone($tz);
415expect($until->format('H:i') === '12:00', 'next cease-fire at 12:00: ' . $until->format('H:i'));
416
417// 12:30 — second range
418$noon = (new DateTime('2026-11-14 12:30:00', $tz))->getTimestamp();
419$info = Engine::ceasefireInfo($g, $noon);
420expect($info['active'] === true, '12:30 is cease-fire');
421$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
422expect($end->format('H:i') === '13:00', 'ends 13:00: ' . $end->format('H:i'));
423
424// 15:00 — free, next is 22:00 same day
425$afternoon = (new DateTime('2026-11-14 15:00:00', $tz))->getTimestamp();
426$info = Engine::ceasefireInfo($g, $afternoon);
427expect($info['active'] === false, '15:00 is free');
428$until = (new DateTime('@' . $info['until']))->setTimezone($tz);
429expect($until->format('Y-m-d H:i') === '2026-11-14 22:00', 'until 22:00: ' . $until->format('Y-m-d H:i'));
430$legal = Engine::legal($g, 'a', $afternoon);
431expect(in_array('b', $legal['shoot'], true), 'can shoot when not in cease-fire');
432$g2 = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $afternoon);
433expect($g2['players'][1]['lives'] === 2, 'shot landed outside cease-fire');
434
435// Just before midnight wrap: 21:59 — until 22:00
436$before = (new DateTime('2026-11-14 21:59:00', $tz))->getTimestamp();
437$info = Engine::ceasefireInfo($g, $before);
438expect($info['active'] === false, '21:59 is free');
439expect($info['seconds'] === 60, '60 seconds until cease-fire: ' . $info['seconds']);
440
441// 05:59 next morning: 60 seconds left IN cease-fire
442$late = (new DateTime('2026-11-15 05:59:00', $tz))->getTimestamp();
443$info = Engine::ceasefireInfo($g, $late);
444expect($info['active'] === true, '05:59 still cease-fire');
445expect($info['seconds'] === 60, '60 seconds left in cease-fire: ' . $info['seconds']);
446
447echo "Engine: cease-fire DST spring-forward (2026-03-29 02:00→03:00)\n";
448// Range 01:00-04:00 on spring-forward day: 02:00 does not exist, period is 2 hours of real time.
449$gD = game(['a', 'b'], [
450 'ceasefire' => true,
451 'ceasefire_ranges' => [['start' => '01:00', 'end' => '04:00']],
452 'jury' => false,
453], $now);
454$start = (new DateTime('2026-03-29 01:00:00', $tz))->getTimestamp();
455$info = Engine::ceasefireInfo($gD, $start);
456expect($info['active'] === true, '01:00 CEST-transition day is cease-fire');
457$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
458expect($end->format('Y-m-d H:i') === '2026-03-29 04:00', 'ends 04:00: ' . $end->format('Y-m-d H:i'));
459// Real duration should be 2 hours (01:00 CET + skipped 02:00 hour → 04:00 CEST)
460// 01:00 on that day is still CET (UTC+1), 04:00 is CEST (UTC+2).
461$dur = $info['until'] - $start;
462expect($dur === 2 * 3600, 'spring-forward 01:00-04:00 is 2 real hours: ' . $dur);
463// 02:30 does not exist; 03:00 should still be inside
464$three = (new DateTime('2026-03-29 03:00:00', $tz))->getTimestamp();
465$info = Engine::ceasefireInfo($gD, $three);
466expect($info['active'] === true, '03:00 (after the jump) is still cease-fire');
467expect($info['seconds'] === 3600, '1 hour left at 03:00: ' . $info['seconds']);
468// Before: 00:30, until 01:00 = 30 min
469$half = (new DateTime('2026-03-29 00:30:00', $tz))->getTimestamp();
470$info = Engine::ceasefireInfo($gD, $half);
471expect($info['active'] === false, '00:30 is free');
472expect($info['seconds'] === 1800, '30 min until 01:00: ' . $info['seconds']);
473
474echo "Engine: cease-fire DST fall-back (2026-10-25 03:00→02:00)\n";
475// Range 01:00-03:30. The 02:00 hour happens twice. amsterdamWallclock adds minutes
476// from midnight, so 03:30 is unambiguous (after the fallback). Duration:
477// midnight CEST 00:00 = UTC 22:00 previous; +3.5h = 03:30 CET? Let's assert via DateTime.
478$gF = game(['a', 'b'], [
479 'ceasefire' => true,
480 'ceasefire_ranges' => [['start' => '01:00', 'end' => '03:30']],
481 'jury' => false,
482], $now);
483$fbStart = (new DateTime('2026-10-25 01:00:00', $tz))->getTimestamp();
484$info = Engine::ceasefireInfo($gF, $fbStart);
485expect($info['active'] === true, '01:00 fall-back day is cease-fire');
486$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
487expect($end->format('Y-m-d H:i') === '2026-10-25 03:30', 'ends 03:30: ' . $end->format('Y-m-d H:i'));
488$dur = $info['until'] - $fbStart;
489// 01:00 CEST (UTC 23:00) to 03:30 CET (UTC 02:30) = 3.5 hours (the 02:00 hour repeats).
490expect($dur === (int) (3.5 * 3600), 'fall-back 01:00-03:30 is 3.5 real hours: ' . $dur);
491// A moment during the repeated 02:xx should still be active.
492// 02:15 CEST (first) = 01:00 CEST + 75 min.
493$first215 = $fbStart + 75 * 60;
494$info = Engine::ceasefireInfo($gF, $first215);
495expect($info['active'] === true, 'first 02:15 (still DST) is cease-fire');
496// 02:15 CET (second) = 01:00 + 75 min + 60 min (the repeated hour)
497$second215 = $fbStart + (75 + 60) * 60;
498$info = Engine::ceasefireInfo($gF, $second215);
499expect($info['active'] === true, 'second 02:15 (CET) is still cease-fire');
500// After 03:30 CET: free
501$after = (new DateTime('2026-10-25 03:30:00', $tz))->getTimestamp();
502$info = Engine::ceasefireInfo($gF, $after);
503expect($info['active'] === false, '03:30 exactly is free (end exclusive)');
504
505echo "Engine: cease-fire off or empty ranges\n";
506$gN = game(['a', 'b'], ['ceasefire' => false, 'ceasefire_ranges' => [['start' => '00:00', 'end' => '23:59']]], $now);
507expect(Engine::ceasefireInfo($gN, $now) === null, 'rule off → no info (ranges ignored)');
508$gE = game(['a', 'b'], ['ceasefire' => true, 'ceasefire_ranges' => []], $now);
509expect(Engine::ceasefireInfo($gE, $now) === null, 'on but empty ranges → treated as no cease-fire');
510
511echo "Engine: overlapping ranges merge for countdown\n";
512$gM = game(['a', 'b'], [
513 'ceasefire' => true,
514 'ceasefire_ranges' => [
515 ['start' => '10:00', 'end' => '12:00'],
516 ['start' => '11:00', 'end' => '13:00'],
517 ],
518 'jury' => false,
519], $now);
520$t = (new DateTime('2026-11-14 10:30:00', $tz))->getTimestamp();
521$info = Engine::ceasefireInfo($gM, $t);
522$end = (new DateTime('@' . $info['until']))->setTimezone($tz);
523expect($end->format('H:i') === '13:00', 'merged overlapping ends at 13:00: ' . $end->format('H:i'));
524
525
526echo "Engine: visibility defaults and invalid values fall back to public\n";
527$norm = Engine::normalizeRules([
528 'vis_shots' => 'nope',
529 'vis_gifts' => 123,
530 'vis_ap' => 'PUBLIC',
531 'vis_range' => '',
532 'vis_lives' => null,
533]);
534expect($norm['vis_shots'] === 'public', 'bad vis_shots → public');
535expect($norm['vis_gifts'] === 'public', 'bad vis_gifts → public');
536expect($norm['vis_ap'] === 'public', 'bad vis_ap → public');
537expect($norm['vis_range'] === 'public', 'bad vis_range → public');
538expect($norm['vis_lives'] === 'public', 'bad vis_lives → public');
539$ok = Engine::normalizeRules([
540 'vis_shots' => 'elim_known_target',
541 'vis_gifts' => 'anonymous',
542 'vis_ap' => 'hidden',
543 'vis_range' => 'known_in_range',
544 'vis_lives' => 'hidden',
545]);
546expect($ok['vis_shots'] === 'elim_known_target', 'accepts elim_known_target');
547expect($ok['vis_gifts'] === 'anonymous', 'accepts anonymous');
548expect($ok['vis_ap'] === 'hidden', 'accepts hidden AP');
549expect($ok['vis_range'] === 'known_in_range', 'accepts known_in_range');
550expect($ok['vis_lives'] === 'hidden', 'accepts hidden lives');
551
552/**
553 * Three-player game with A next to B (in starting range 2) and C far away.
554 * Extra AP so we can shoot/give/upgrade more than once.
555 *
556 * @return array{0:array,1:int}
557 */
558function visGame(array $vis, int $now): array
559{
560 $g = game(['a', 'b', 'c'], array_merge(['jury' => false], $vis), $now);
561 // Place: A at (2,2), B at (3,2) dist 1, C at (5,5) dist 3 from A.
562 foreach ($g['players'] as $i => $p) {
563 if ($p['id'] === 'a') {
564 $g['players'][$i]['x'] = 2;
565 $g['players'][$i]['y'] = 2;
566 $g['players'][$i]['ap'] = 5;
567 $g['players'][$i]['range'] = 2;
568 } elseif ($p['id'] === 'b') {
569 $g['players'][$i]['x'] = 3;
570 $g['players'][$i]['y'] = 2;
571 $g['players'][$i]['ap'] = 5;
572 $g['players'][$i]['range'] = 2;
573 } else {
574 $g['players'][$i]['x'] = 5;
575 $g['players'][$i]['y'] = 5;
576 $g['players'][$i]['ap'] = 5;
577 $g['players'][$i]['range'] = 2;
578 }
579 }
580 return [$g, $now];
581}
582
583function visEvents(array $view, string $type): array
584{
585 return array_values(array_filter($view['events'] ?? [], fn ($e) => ($e['type'] ?? '') === $type));
586}
587
588function visPlayer(array $view, string $id): ?array
589{
590 foreach ($view['players'] as $p) {
591 if ($p['id'] === $id) {
592 return $p;
593 }
594 }
595 return null;
596}
597
598echo "Engine: vis shots — public (everyone sees, including lives_left when lives public)\n";
599[$g, $t] = visGame(['vis_shots' => 'public'], $now);
600$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
601$va = Engine::present($g, 'a', $t);
602$vb = Engine::present($g, 'b', $t);
603$vc = Engine::present($g, 'c', $t);
604expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees public shot');
605expect(count(visEvents($vb, 'shoot')) === 1, 'target sees public shot');
606expect(count(visEvents($vc, 'shoot')) === 1, 'third party sees public shot');
607expect(($vc['events'] === visEvents($vc, 'shoot') ? true : isset(visEvents($vc, 'shoot')[0]['player'])) && visEvents($vc, 'shoot')[0]['player'] === 'a', 'third party sees shooter id');
608expect(visEvents($vc, 'shoot')[0]['lives_left'] === 2, 'public lives_left on shot');
609expect(visEvents($vc, 'shoot')[0]['damage'] === 1, 'damage is 1');
610expect(visPlayer($vc, 'b')['lives'] === 2, 'public lives in state');
611
612echo "Engine: vis shots — elim_public (non-elim hidden from third party, visible to target)\n";
613[$g, $t] = visGame(['vis_shots' => 'elim_public'], $now);
614$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
615$va = Engine::present($g, 'a', $t);
616$vb = Engine::present($g, 'b', $t);
617$vc = Engine::present($g, 'c', $t);
618expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees non-elim');
619expect(count(visEvents($vb, 'shoot')) === 1, 'target sees non-elim under elim_public');
620expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see non-elim');
621expect(count(visEvents($vc, 'death')) === 0, 'no death on non-elim');
622// Two more shots to eliminate (B started with 3, now 2).
623$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
624$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
625$vc = Engine::present($g, 'c', $t);
626$vb = Engine::present($g, 'b', $t);
627$shotsC = visEvents($vc, 'shoot');
628expect(count($shotsC) === 1, 'third party sees only the eliminating shot: ' . count($shotsC));
629expect(!empty($shotsC[0]['killed']), 'the visible shot is the kill');
630expect(count(visEvents($vc, 'death')) === 1, 'third party sees death');
631expect((visEvents($vc, 'death')[0]['by'] ?? null) === 'a', 'elim_public death includes killer');
632expect((visEvents($vb, 'death')[0]['by'] ?? null) === 'a', 'target sees killer');
633expect(visPlayer($vc, 'b')['alive'] === false, 'death/alive is always public');
634
635echo "Engine: vis shots — known_target (others never see the shot, death without by)\n";
636[$g, $t] = visGame(['vis_shots' => 'known_target'], $now);
637$g['players'][array_key_first(array_filter($g['players'], fn ($p) => $p['id'] === 'b'))]['lives'] = 1;
638// find b index properly
639foreach ($g['players'] as $i => $p) {
640 if ($p['id'] === 'b') {
641 $g['players'][$i]['lives'] = 1;
642 }
643}
644$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
645$va = Engine::present($g, 'a', $t);
646$vb = Engine::present($g, 'b', $t);
647$vc = Engine::present($g, 'c', $t);
648expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees known_target shot');
649expect(count(visEvents($vb, 'shoot')) === 1, 'target sees known_target shot');
650expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see known_target shot');
651$deaths = visEvents($vc, 'death');
652expect(count($deaths) === 1, 'third party still sees that B died');
653expect(!array_key_exists('by', $deaths[0]), 'killer stripped for third party under known_target');
654expect((visEvents($vb, 'death')[0]['by'] ?? null) === 'a', 'target still sees who shot them');
655
656echo "Engine: vis shots — elim_known_target (non-elim hidden even from target)\n";
657[$g, $t] = visGame(['vis_shots' => 'elim_known_target'], $now);
658$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
659$va = Engine::present($g, 'a', $t);
660$vb = Engine::present($g, 'b', $t);
661$vc = Engine::present($g, 'c', $t);
662expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees non-elim under elim_known_target');
663expect(count(visEvents($vb, 'shoot')) === 0, 'target does NOT see non-elim shot (must deduce)');
664expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see non-elim');
665expect(visPlayer($vb, 'b')['lives'] === 2, 'target still sees own lives drop');
666expect(visPlayer($vc, 'b')['lives'] === 2, 'lives still public in this game (default)');
667// Eliminate
668$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
669$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
670$vb = Engine::present($g, 'b', $t);
671$vc = Engine::present($g, 'c', $t);
672expect(count(visEvents($vb, 'shoot')) === 1, 'target sees only the eliminating shot');
673expect((visEvents($vb, 'shoot')[0]['player'] ?? null) === 'a', 'elim shot names the shooter for the target');
674expect(count(visEvents($vc, 'shoot')) === 0, 'third party never sees shots under elim_known_target');
675expect(!array_key_exists('by', visEvents($vc, 'death')[0]), 'third party death has no by');
676
677echo "Engine: vis shots + hidden lives — lives_left stripped except for the target\n";
678[$g, $t] = visGame(['vis_shots' => 'public', 'vis_lives' => 'hidden'], $now);
679$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
680$va = Engine::present($g, 'a', $t);
681$vb = Engine::present($g, 'b', $t);
682$vc = Engine::present($g, 'c', $t);
683expect(!array_key_exists('lives_left', visEvents($va, 'shoot')[0]), 'shooter does not get target lives_left');
684expect((visEvents($vb, 'shoot')[0]['lives_left'] ?? null) === 2, 'target sees own lives_left');
685expect(!array_key_exists('lives_left', visEvents($vc, 'shoot')[0]), 'third party does not get lives_left');
686expect(visEvents($va, 'shoot')[0]['damage'] === 1, 'shooter still sees damage 1');
687expect(visPlayer($va, 'a')['lives'] === 3, 'own lives always visible');
688expect(visPlayer($va, 'b')['lives'] === null, 'other lives hidden from shooter');
689expect(visPlayer($vc, 'b')['lives'] === null, 'other lives hidden from third party');
690expect(visPlayer($vb, 'b')['lives'] === 2, 'target sees own lives');
691expect(is_bool(visPlayer($vc, 'b')['alive']), 'alive flag still present when lives hidden');
692
693echo "Engine: vis gifts — public, known_recipient, anonymous\n";
694[$g, $t] = visGame(['vis_gifts' => 'public'], $now);
695$g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t);
696$vc = Engine::present($g, 'c', $t);
697$gives = visEvents($vc, 'give');
698expect(count($gives) === 1 && $gives[0]['player'] === 'a' && $gives[0]['amount'] === 2, 'public gift visible to third party with giver and amount');
699
700[$g, $t] = visGame(['vis_gifts' => 'known_recipient'], $now);
701$g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t);
702$va = Engine::present($g, 'a', $t);
703$vb = Engine::present($g, 'b', $t);
704$vc = Engine::present($g, 'c', $t);
705expect(count(visEvents($va, 'give')) === 1 && visEvents($va, 'give')[0]['player'] === 'a', 'giver sees own gift');
706expect(count(visEvents($vb, 'give')) === 1 && visEvents($vb, 'give')[0]['player'] === 'a', 'recipient sees giver');
707expect(count(visEvents($vc, 'give')) === 0, 'third party does not see known_recipient gift');
708
709[$g, $t] = visGame(['vis_gifts' => 'anonymous'], $now);
710$beforeB = Engine::player($g, 'b')['ap'];
711$g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t);
712$va = Engine::present($g, 'a', $t);
713$vb = Engine::present($g, 'b', $t);
714$vc = Engine::present($g, 'c', $t);
715expect(count(visEvents($va, 'give')) === 1 && visEvents($va, 'give')[0]['player'] === 'a', 'giver sees full anonymous gift in their own log');
716$rec = visEvents($vb, 'give');
717expect(count($rec) === 1, 'recipient sees an anonymous gift event');
718expect(!empty($rec[0]['anonymous']), 'event marked anonymous');
719expect(!array_key_exists('player', $rec[0]), 'giver id stripped for recipient');
720expect(($rec[0]['amount'] ?? 0) === 2, 'recipient sees the AP amount');
721expect(($rec[0]['target'] ?? '') === 'b', 'recipient is named as target');
722expect(count(visEvents($vc, 'give')) === 0, 'third party does not see anonymous gift');
723expect(visPlayer($vb, 'b')['ap'] === $beforeB + 2, 'recipient AP actually increased');
724// Stored (unfiltered) event still has the giver — present() must not mutate.
725$rawGive = null;
726foreach ($g['events'] as $e) {
727 if (($e['type'] ?? '') === 'give') {
728 $rawGive = $e;
729 }
730}
731expect($rawGive !== null && ($rawGive['player'] ?? null) === 'a', 'stored event still has the giver (present does not mutate)');
732expect(!array_key_exists('player', $rec[0]) || $rec[0]['player'] !== 'a' || true, 'sanity');
733expect(($rawGive['player'] ?? null) === 'a' && !array_key_exists('player', $rec[0]), 'clone is independent of stored event');
734
735echo "Engine: vis AP totals hidden\n";
736[$g, $t] = visGame(['vis_ap' => 'hidden'], $now);
737$va = Engine::present($g, 'a', $t);
738$vc = Engine::present($g, 'c', $t);
739expect(visPlayer($va, 'a')['ap'] === 5, 'own AP visible when hidden rule is on');
740expect(visPlayer($va, 'b')['ap'] === null, 'other AP hidden');
741expect(visPlayer($vc, 'a')['ap'] === null, 'third party does not see A’s AP');
742// Daily grants: only the viewer’s own grant is included.
743$midnight = Engine::nextMidnight($t);
744$g = Engine::applyDue($g, $midnight);
745$va = Engine::present($g, 'a', $midnight);
746$dailies = visEvents($va, 'daily');
747expect(count($dailies) === 1, 'daily event present');
748expect(array_keys($dailies[0]['grants']) === ['a'], 'hidden AP: only own grant in daily event: ' . json_encode($dailies[0]['grants']));
749expect(!isset($dailies[0]['jury']['counts']), 'jury vote counts stripped');
750
751echo "Engine: vis range — hidden and known_in_range\n";
752[$g, $t] = visGame(['vis_range' => 'hidden'], $now);
753$g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $t);
754$va = Engine::present($g, 'a', $t);
755$vc = Engine::present($g, 'c', $t);
756expect(visPlayer($va, 'a')['range'] === 3, 'own range after upgrade');
757expect(visPlayer($vc, 'a')['range'] === null, 'other range hidden');
758expect(count(visEvents($va, 'upgrade')) === 1, 'upgrader sees own upgrade');
759expect(count(visEvents($vc, 'upgrade')) === 0, 'third party does not see hidden upgrade');
760
761[$g, $t] = visGame(['vis_range' => 'known_in_range'], $now);
762$va = Engine::present($g, 'a', $t);
763$vb = Engine::present($g, 'b', $t);
764$vc = Engine::present($g, 'c', $t);
765// B is at dist 1 from A (range 2) so A is in B’s range and B is in A’s range.
766expect(visPlayer($vb, 'a')['range'] === 2, 'B is within A’s range, sees A’s range');
767expect(visPlayer($va, 'b')['range'] === 2, 'A is within B’s range, sees B’s range');
768expect(visPlayer($vc, 'a')['range'] === null, 'C is outside A’s range (dist 3 > 2)');
769expect(visPlayer($va, 'c')['range'] === null, 'A is outside C’s range (dist 3 > 2)');
770expect(visPlayer($vc, 'c')['range'] === 2, 'C always sees own range');
771// Upgrade A to range 3: C (dist 3) is now in A’s range.
772$g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $t);
773$vc = Engine::present($g, 'c', $t);
774$va = Engine::present($g, 'a', $t);
775expect(visPlayer($vc, 'a')['range'] === 3, 'after upgrade, C is within A’s range and sees it');
776expect(count(visEvents($vc, 'upgrade')) === 1, 'C now also sees the upgrade event');
777expect(visPlayer($va, 'c')['range'] === null, 'A is still outside C’s range 2');
778
779echo "Engine: movement is always public, even with everything hidden\n";
780[$g, $t] = visGame([
781 'vis_shots' => 'elim_known_target',
782 'vis_gifts' => 'anonymous',
783 'vis_ap' => 'hidden',
784 'vis_range' => 'hidden',
785 'vis_lives' => 'hidden',
786], $now);
787// A moves to (2,3) — adjacent empty.
788$g = Engine::applyAction($g, 'a', ['type' => 'move', 'x' => 2, 'y' => 3], $t);
789$vc = Engine::present($g, 'c', $t);
790$moves = visEvents($vc, 'move');
791expect(count($moves) === 1, 'move is public');
792expect($moves[0]['player'] === 'a' && $moves[0]['to']['x'] === 2 && $moves[0]['to']['y'] === 3, 'move names the player and destination');
793expect(visPlayer($vc, 'a')['x'] === 2 && visPlayer($vc, 'a')['y'] === 3, 'new position is in the public state');
794expect(visPlayer($vc, 'a')['ap'] === null, 'AP still hidden after the move');
795expect(visPlayer($vc, 'a')['lives'] === null, 'lives still hidden');
796expect(visPlayer($vc, 'a')['range'] === null, 'range still hidden');
797
798echo "Engine: present() does not leak hidden keys with real values\n";
799[$g, $t] = visGame(['vis_ap' => 'hidden', 'vis_lives' => 'hidden', 'vis_range' => 'hidden'], $now);
800$vc = Engine::present($g, 'c', $t);
801$encoded = json_encode($vc);
802expect(visPlayer($vc, 'a')['ap'] === null, 'JSON-facing ap is null');
803expect(visPlayer($vc, 'a')['lives'] === null, 'JSON-facing lives is null');
804expect(visPlayer($vc, 'a')['range'] === null, 'JSON-facing range is null');
805// The raw numbers for A (ap=5, lives=3, range=2) must not appear attached to A in a leaky way
806// — we only require the structured fields are null, which json_encode will emit as null.
807$decoded = json_decode($encoded, true);
808foreach ($decoded['players'] as $p) {
809 if ($p['id'] !== 'c') {
810 expect($p['ap'] === null && $p['lives'] === null && $p['range'] === null, "no numeric secrets for {$p['id']}");
811 }
812}
813
814
815
816echo "Engine: nextDistribution (per-lobby AP time) and DST\n";
817$tz = new DateTimeZone('Europe/Amsterdam');
818$from = (new DateTime('2026-11-14 14:00:00', $tz))->getTimestamp();
819$nx = Engine::nextDistribution($from, '15:00');
820$got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s');
821expect($got === '2026-11-14 15:00:00', "14:00 → today 15:00: $got");
822$at = (new DateTime('2026-11-14 15:00:00', $tz))->getTimestamp();
823$nx = Engine::nextDistribution($at, '15:00');
824$got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s');
825expect($got === '2026-11-15 15:00:00', "exactly 15:00 → tomorrow 15:00: $got");
826$from2 = (new DateTime('2026-11-14 16:00:00', $tz))->getTimestamp();
827$nx = Engine::nextDistribution($from2, '15:00');
828$got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s');
829expect($got === '2026-11-15 15:00:00', "16:00 → tomorrow 15:00: $got");
830// 00:00 matches nextMidnight
831$eve = (new DateTime('2026-11-13 23:30:00', $tz))->getTimestamp();
832expect(Engine::nextDistribution($eve, '00:00') === Engine::nextMidnight($eve), '00:00 path equals nextMidnight');
833expect(Engine::normalizeRules(['ap_time' => '7:05'])['ap_time'] === '07:05', 'ap_time normalised to HH:MM');
834expect(Engine::normalizeRules(['ap_time' => 'nope'])['ap_time'] === '00:00', 'bad ap_time → 00:00');
835expect(Engine::normalizeRules(['ap_time' => '24:00'])['ap_time'] === '00:00', '24:00 invalid → 00:00');
836
837// DST spring-forward 2026-03-29 02:00–03:00 skipped.
838$before = (new DateTime('2026-03-29 01:30:00', $tz))->getTimestamp();
839$nx = Engine::nextDistribution($before, '02:30');
840$gotDt = (new DateTime('@' . $nx))->setTimezone($tz);
841// 02:30 does not exist; PHP setTime lands on or after the gap (03:30 CEST typically).
842expect($gotDt->getTimestamp() > $before, 'DST spring: 02:30 candidate is strictly after 01:30');
843expect($gotDt->format('Y-m-d') === '2026-03-29', 'DST spring: still that calendar day: ' . $gotDt->format('Y-m-d H:i:s T'));
844// A legal time after the gap is exact.
845$nx = Engine::nextDistribution($before, '04:00');
846$got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s');
847expect($got === '2026-03-29 04:00:00', "DST spring: 04:00 exact: $got");
848// Fall-back 2026-10-25: 02:30 exists twice; we just need a valid instant that day after 01:30.
849$fb = (new DateTime('2026-10-25 01:30:00', $tz))->getTimestamp();
850$nx = Engine::nextDistribution($fb, '02:30');
851$gotDt = (new DateTime('@' . $nx))->setTimezone($tz);
852expect($gotDt->format('Y-m-d') === '2026-10-25' && (int) $gotDt->format('H') === 2 && (int) $gotDt->format('i') === 30,
853 'DST fall-back: 02:30 that day: ' . $gotDt->format('Y-m-d H:i:s T'));
854
855echo "Engine: AP at a non-midnight lobby time\n";
856$t15 = (new DateTime('2026-11-14 14:00:00', $tz))->getTimestamp();
857$g = game(['a', 'b'], ['ap_time' => '15:00', 'jury' => false], $t15);
858expect((int) $g['next_distribution_at'] === Engine::nextDistribution($t15, '15:00'), 'start schedules 15:00');
859$apA = Engine::player($g, 'a')['ap'];
860$g2 = Engine::applyDue($g, Engine::nextDistribution($t15, '15:00'));
861expect((int) $g2['day'] === 1, 'day increments at 15:00');
862expect((int) Engine::player($g2, 'a')['ap'] === $apA + 1, 'AP granted at 15:00');
863// A tick just before 15:00 must not grant.
864$g3 = Engine::applyDue($g, Engine::nextDistribution($t15, '15:00') - 1);
865expect((int) $g3['day'] === 0, 'one second before 15:00: no grant');
866
867echo "Engine: AP loot default off; on only for eliminating shots\n";
868[$g, $t] = visGame(['ap_loot' => false], $now);
869foreach ($g['players'] as $i => $p) {
870 if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; }
871 if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; }
872}
873$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
874$a = Engine::player($g, 'a');
875$b = Engine::player($g, 'b');
876expect(empty($b['alive']), 'B is dead');
877expect((int) $b['ap'] === 0, 'dead B has 0 AP');
878expect((int) $a['ap'] === 2, 'loot off: A spent 1, did not receive B’s 4 (has 2)');
879$shoot = null;
880foreach ($g['events'] as $e) { if (($e['type'] ?? '') === 'shoot' && !empty($e['killed'])) { $shoot = $e; } }
881expect(!isset($shoot['loot']), 'loot off: no loot field on the kill shot');
882
883[$g, $t] = visGame(['ap_loot' => true], $now);
884foreach ($g['players'] as $i => $p) {
885 if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; }
886 if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; }
887}
888$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
889$a = Engine::player($g, 'a');
890$b = Engine::player($g, 'b');
891expect(empty($b['alive']) && (int) $b['ap'] === 0, 'loot on: B dead, AP zeroed');
892expect((int) $a['ap'] === 2 + 4, 'loot on: A spent 1 (3→2) then looted 4 → 6: ' . $a['ap']);
893$va = Engine::present($g, 'a', $t);
894$vc = Engine::present($g, 'c', $t);
895$sa = visEvents($va, 'shoot')[0] ?? [];
896$sc = visEvents($vc, 'shoot')[0] ?? [];
897expect(($sa['loot'] ?? 0) === 4, 'shooter sees loot amount');
898expect(($sc['loot'] ?? 0) === 4, 'public AP: third party sees loot');
899
900// Hidden AP: third party does not see loot amount; shooter does.
901[$g, $t] = visGame(['ap_loot' => true, 'vis_ap' => 'hidden'], $now);
902foreach ($g['players'] as $i => $p) {
903 if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; }
904 if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; }
905}
906$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
907$va = Engine::present($g, 'a', $t);
908$vc = Engine::present($g, 'c', $t);
909expect((visEvents($va, 'shoot')[0]['loot'] ?? 0) === 4, 'shooter sees loot even if AP totals hidden');
910expect(!array_key_exists('loot', visEvents($vc, 'shoot')[0]), 'third party does not see loot amount when AP is hidden');
911expect(visPlayer($va, 'a')['ap'] === 6, 'shooter sees own AP including loot');
912expect(visPlayer($vc, 'a')['ap'] === null, 'third party still does not see A’s total');
913
914// Non-elim shot does not loot; surrender does not loot A’s AP to anyone.
915[$g, $t] = visGame(['ap_loot' => true], $now);
916foreach ($g['players'] as $i => $p) {
917 if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 3; $g['players'][$i]['ap'] = 4; }
918 if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; }
919}
920$g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t);
921expect((int) Engine::player($g, 'a')['ap'] === 2, 'non-elim: no loot');
922expect((int) Engine::player($g, 'b')['ap'] === 4, 'non-elim: B keeps AP');
923$g = Engine::applyAction($g, 'b', ['type' => 'surrender'], $t);
924expect(empty(Engine::player($g, 'b')['alive']), 'B surrendered');
925expect((int) Engine::player($g, 'a')['ap'] === 2, 'surrender does not transfer AP');
926expect((int) Engine::player($g, 'b')['ap'] === 0, 'surrendered AP is zeroed, not looted');
927
928echo "Engine: no hearts, no buying lives (options do not exist)\n";
929$n = Engine::normalizeRules(['hearts' => true, 'buy_lives' => true, 'buyLives' => true]);
930expect(!array_key_exists('hearts', $n) && !array_key_exists('buy_lives', $n) && !array_key_exists('buyLives', $n),
931 'hearts / buy-lives are not accepted rule keys');
932
933
934echo "\nEngine passed: $passed Failed: $failed\n";
935if (!defined('ALLIANTIES_TEST_SUITE')) {
936 exit($failed > 0 ? 1 : 0);
937}
938$GLOBALS['engine_failed'] = $failed;
939$GLOBALS['engine_passed'] = $passed;