$p['x'] . ',' . $p['y'], $pos); expect(count(array_unique($keys)) === 4, 'starting positions are unique'); foreach ($pos as $p) { expect(Engine::inBounds(6, $p['x'], $p['y']), "start in bounds {$p['x']},{$p['y']}"); } echo "Engine: Chebyshev range\n"; expect(Engine::chebyshev(0, 0, 2, 2) === 2, 'diagonal 2 is range 2'); expect(Engine::chebyshev(0, 0, 2, 0) === 2, 'orthogonal 2 is range 2'); expect(Engine::chebyshev(5, 5, 5, 5) === 0, 'same tile is 0'); echo "Engine: midnight Amsterdam\n"; // 2026-11-13 23:00 CET = 2026-11-13 22:00 UTC = 1763071200? Let's compute. $tz = new DateTimeZone('Europe/Amsterdam'); $dt = new DateTime('2026-11-13 23:30:00', $tz); $from = $dt->getTimestamp(); $next = Engine::nextMidnight($from); $nx = (new DateTime('@' . $next))->setTimezone($tz); expect($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')); $dt2 = new DateTime('2026-11-14 00:00:00', $tz); $next2 = Engine::nextMidnight($dt2->getTimestamp()); $nx2 = (new DateTime('@' . $next2))->setTimezone($tz); expect($nx2->format('Y-m-d H:i:s') === '2026-11-15 00:00:00', 'at exactly 00:00, next is tomorrow'); // DST spring-forward 2026-03-29 in NL $dt3 = new DateTime('2026-03-28 23:00:00', $tz); $next3 = Engine::nextMidnight($dt3->getTimestamp()); $nx3 = (new DateTime('@' . $next3))->setTimezone($tz); expect($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')); echo "Engine: lobby and start\n"; $now = (new DateTime('2026-11-13 15:00:00', $tz))->getTimestamp(); $g = Engine::newLobby('g', 'CODE12', 'a', 'Alice', null, [], $now); expect($g['status'] === 'lobby', 'new lobby'); expect($g['rules']['jury'] === true, 'jury default ON'); expect($g['rules']['shrinking_arena'] === false, 'shrink default OFF'); expect($g['rules']['shrink_interval_days'] === 7, 'shrink interval default 7'); expect($g['rules']['ceasefire'] === false, 'ceasefire default OFF'); expect($g['rules']['ceasefire_ranges'] === [], 'ceasefire ranges empty by default'); expect($g['rules']['vis_shots'] === 'public', 'vis_shots default public'); expect($g['rules']['vis_gifts'] === 'public', 'vis_gifts default public'); expect($g['rules']['vis_ap'] === 'public', 'vis_ap default public'); expect($g['rules']['vis_range'] === 'public', 'vis_range default public'); expect($g['rules']['vis_lives'] === 'public', 'vis_lives default public'); expect($g['rules']['ap_loot'] === false, 'ap_loot default OFF'); expect($g['rules']['ap_time'] === '00:00', 'ap_time default 00:00'); expect($g['rules']['grid'] === 7, 'lobby suggested grid 7 for 1 player'); expect($g['rules']['grid_auto'] === true, 'grid_auto default ON'); expect($g['grid'] === 7, 'game.grid mirrors suggestion in lobby'); expect($g['host_id'] === 'a', 'host is creator'); try { Engine::start($g, 'a', $now); expect(false, 'start alone should throw'); } catch (RuntimeException $e) { expect($e->getMessage() === 'min_players', 'start alone: min_players'); } $g = Engine::join($g, 'b', 'Bob', null, $now); expect(count($g['players']) === 2, '2 players'); try { Engine::start($g, 'b', $now); expect(false, 'non-host start'); } catch (RuntimeException $e) { expect($e->getMessage() === 'not_host', 'non-host cannot start'); } $g = Engine::start($g, 'a', $now); expect($g['status'] === 'running', 'started'); expect($g['grid'] === 7, 'auto grid for 2 players is 7'); expect($g['players'][0]['ap'] === 1, 'start with 1 AP'); expect($g['players'][0]['lives'] === 3, 'start with 3 lives'); expect($g['players'][0]['range'] === 2, 'start with range 2'); expect($g['players'][0]['x'] !== $g['players'][1]['x'] || $g['players'][0]['y'] !== $g['players'][1]['y'], 'not overlapping'); $nd = (new DateTime('@' . $g['next_distribution_at']))->setTimezone($tz); expect($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')); echo "Engine: move, blocked, AP\n"; $a = $g['players'][0]; $legal = Engine::legal($g, 'a'); expect(count($legal['move']) >= 1, 'Alice has at least one move'); $t = $legal['move'][0]; $g2 = Engine::applyAction($g, 'a', ['type' => 'move', 'x' => $t['x'], 'y' => $t['y']], $now); expect($g2['players'][0]['x'] === $t['x'] && $g2['players'][0]['y'] === $t['y'], 'moved'); expect($g2['players'][0]['ap'] === 0, 'spent 1 AP'); expect(end($g2['events'])['type'] === 'move', 'move event'); expect(end($g2['events'])['spend'] === true, 'move is a spend tick'); try { Engine::applyAction($g2, 'a', ['type' => 'move', 'x' => $t['x'] + 1, 'y' => $t['y']], $now); expect(false, 'move without AP'); } catch (RuntimeException $e) { expect($e->getMessage() === 'no_ap' || $e->getMessage() === 'invalid_move', 'no AP: ' . $e->getMessage()); } echo "Engine: shoot and death and win\n"; // Place two players adjacent, Alice shoots Bob three times (give extra AP). $g = game(['a', 'b'], [], $now); $g['players'][0]['x'] = 2; $g['players'][0]['y'] = 2; $g['players'][0]['ap'] = 5; $g['players'][1]['x'] = 3; $g['players'][1]['y'] = 3; $g['players'][1]['ap'] = 0; $legal = Engine::legal($g, 'a'); expect(in_array('b', $legal['shoot'], true), 'Bob is in range 2 (actually dist 1)'); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now); expect($g['players'][1]['lives'] === 2, 'Bob has 2 lives'); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now); expect($g['players'][1]['alive'] === false, 'Bob is dead'); expect($g['status'] === 'finished', 'game finished'); expect($g['winner_id'] === 'a', 'Alice wins'); $types = array_column($g['events'], 'type'); expect(in_array('death', $types, true), 'death event'); expect(in_array('win', $types, true), 'win event'); echo "Engine: give AP and upgrade\n"; $g = game(['a', 'b'], [], $now); $g['players'][0]['x'] = 2; $g['players'][0]['y'] = 2; $g['players'][0]['ap'] = 4; $g['players'][1]['x'] = 3; $g['players'][1]['y'] = 2; $g['players'][1]['ap'] = 0; $g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $now); expect($g['players'][0]['ap'] === 2, 'Alice spent 2'); expect($g['players'][1]['ap'] === 2, 'Bob received 2'); try { Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 5], $now); expect(false, 'give more than you have'); } catch (RuntimeException $e) { expect($e->getMessage() === 'bad_give', 'bad_give'); } $g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $now); expect($g['players'][0]['range'] === 3, 'range is now 3'); expect($g['players'][0]['ap'] === 1, 'upgrade costs 1'); echo "Engine: out of range and occupied\n"; $g = game(['a', 'b'], [], $now); $g['players'][0]['x'] = 0; $g['players'][0]['y'] = 0; $g['players'][0]['ap'] = 2; $g['players'][1]['x'] = 5; $g['players'][1]['y'] = 5; $legal = Engine::legal($g, 'a'); expect(!in_array('b', $legal['shoot'], true), 'Bob at (5,5) is out of range 2 from (0,0)'); try { Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $now); expect(false, 'shoot out of range'); } catch (RuntimeException $e) { expect($e->getMessage() === 'out_of_range', 'out_of_range'); } // Occupied neighbour is not a legal move. $g['players'][1]['x'] = 1; $g['players'][1]['y'] = 0; $legal = Engine::legal($g, 'a'); $occ = false; foreach ($legal['move'] as $m) { if ($m['x'] === 1 && $m['y'] === 0) { $occ = true; } } expect($occ === false, 'cannot move onto Bob'); echo "Engine: surrender\n"; $g = game(['a', 'b'], [], $now); $g = Engine::applyAction($g, 'b', ['type' => 'surrender'], $now); expect($g['players'][1]['alive'] === false, 'Bob surrendered'); expect($g['players'][1]['surrendered'] === true, 'flagged surrendered'); expect($g['winner_id'] === 'a', 'Alice wins after surrender'); echo "Engine: daily AP grant\n"; $g = game(['a', 'b'], ['jury' => false], $now); $g['players'][0]['ap'] = 0; $g['players'][1]['ap'] = 0; $at = $g['next_distribution_at']; $g = Engine::applyDue($g, $at); expect($g['day'] === 1, 'day is 1'); expect($g['players'][0]['ap'] === 1, 'Alice got 1 AP'); expect($g['players'][1]['ap'] === 1, 'Bob got 1 AP'); // Catch-up two more midnights $at2 = Engine::nextMidnight($at); $at3 = Engine::nextMidnight($at2); $g = Engine::applyDue($g, $at3); expect($g['day'] === 3, 'caught up to day 3, day=' . $g['day']); expect($g['players'][0]['ap'] === 3, '3 AP after 3 days (started 0): ' . $g['players'][0]['ap']); echo "Engine: jury\n"; $g = game(['a', 'b', 'c', 'd'], ['jury' => true, 'shrinking_arena' => false], $now); // Kill c and d without ending? Need 4 players, kill only c (one dead voter). // Place and shoot c twice then once more... easier: set lives. $g['players'][2]['lives'] = 0; $g['players'][2]['alive'] = false; $g['players'][2]['ap'] = 0; $g['players'][3]['lives'] = 0; $g['players'][3]['alive'] = false; $g['players'][3]['ap'] = 0; expect($g['status'] === 'running', 'still running with 2 alive'); // c votes for a, d votes for a $g = Engine::applyAction($g, 'c', ['type' => 'vote', 'target' => 'a'], $now); $g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'a'], $now); expect($g['votes']['c'] === 'a', 'c voted a'); // Vote is changeable $g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'b'], $now); expect($g['votes']['d'] === 'b', 'd changed vote to b'); // Living player cannot vote try { Engine::applyAction($g, 'a', ['type' => 'vote', 'target' => 'b'], $now); expect(false, 'living vote'); } catch (RuntimeException $e) { expect($e->getMessage() === 'cannot_vote', 'living cannot vote'); } // Present hides votes from others $viewA = Engine::present($g, 'a'); expect($viewA['legal']['my_vote'] === null, 'Alice (living) has no vote'); $viewC = Engine::present($g, 'c'); expect($viewC['legal']['my_vote'] === 'a', 'Charlie sees own vote'); expect(!isset($viewA['votes']), 'votes not in present payload'); // Distribution: 1 vote a, 1 vote b → tie, both get +1 bonus (+1 regular) = +2 $g['players'][0]['ap'] = 0; $g['players'][1]['ap'] = 0; $g = Engine::applyDue($g, $g['next_distribution_at']); expect($g['players'][0]['ap'] === 2, 'Alice got 1+1 (tie): ' . $g['players'][0]['ap']); expect($g['players'][1]['ap'] === 2, 'Bob got 1+1 (tie): ' . $g['players'][1]['ap']); expect($g['votes'] === [], 'votes reset after distribution'); // Unique winner $g['players'][2]['alive'] = false; $g = Engine::applyAction($g, 'c', ['type' => 'vote', 'target' => 'a'], $now + 1); $g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'a'], $now + 1); $g['players'][0]['ap'] = 0; $g['players'][1]['ap'] = 0; $g = Engine::applyDue($g, $g['next_distribution_at']); expect($g['players'][0]['ap'] === 2, 'Alice unique jury winner: 2'); expect($g['players'][1]['ap'] === 1, 'Bob only regular 1: ' . $g['players'][1]['ap']); // Jury off: no vote $gOff = game(['a', 'b', 'c'], ['jury' => false], $now); $gOff['players'][2]['alive'] = false; $gOff['players'][2]['lives'] = 0; try { Engine::applyAction($gOff, 'c', ['type' => 'vote', 'target' => 'a'], $now); expect(false, 'vote with jury off'); } catch (RuntimeException $e) { expect($e->getMessage() === 'cannot_vote', 'jury off: cannot_vote'); } echo "Engine: shrinking arena\n"; $g = game(['a', 'b'], ['shrinking_arena' => true, 'shrink_interval_days' => 2, 'jury' => false, 'grid' => 6, 'grid_auto' => false], $now); expect($g['grid'] === 6, 'host-chosen 6×6'); expect(Engine::daysUntilShrink($g) === 2, 'shrink in 2 days (interval 2, day 0): ' . (Engine::daysUntilShrink($g) ?? 'null')); // Put Alice on the outer ring (0,0), Bob in the centre (2,2) $g['players'][0]['x'] = 0; $g['players'][0]['y'] = 0; $g['players'][0]['lives'] = 3; $g['players'][0]['ap'] = 0; $g['players'][1]['x'] = 2; $g['players'][1]['y'] = 2; $g['players'][1]['ap'] = 0; // Day 1: no shrink $g = Engine::applyDue($g, $g['next_distribution_at']); expect($g['day'] === 1, 'day 1'); expect($g['arena_inset'] === 0, 'no shrink on day 1'); expect($g['players'][0]['x'] === 0 && $g['players'][0]['y'] === 0, 'Alice still on (0,0)'); expect($g['players'][0]['lives'] === 3, 'Alice still 3 lives'); expect(Engine::daysUntilShrink($g) === 1, 'shrink in 1 day'); // Day 2: shrink $g = Engine::applyDue($g, $g['next_distribution_at']); expect($g['day'] === 2, 'day 2'); expect($g['arena_inset'] === 1, 'inset is 1'); expect($g['players'][0]['lives'] === 2, 'Alice lost 1 life: ' . $g['players'][0]['lives']); expect(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']); expect(!($g['players'][0]['x'] === 2 && $g['players'][0]['y'] === 2), 'not onto Bob'); expect($g['players'][1]['lives'] === 3, 'Bob unhurt in centre'); expect($g['players'][0]['ap'] === 2, 'Alice got day-1 and day-2 AP after surviving shrink: ' . $g['players'][0]['ap']); // Outer ring is impassable $legal = Engine::legal($g, $g['players'][0]['id'] === 'a' ? 'a' : 'a'); foreach ($legal['move'] as $m) { expect(Engine::isPassable(6, 1, $m['x'], $m['y']), "legal move {$m['x']},{$m['y']} is passable"); } // Max one shrink on 6×6 (maxInset=1); day 4 would try again but stop $g = Engine::applyDue($g, $g['next_distribution_at']); // day 3 $g = Engine::applyDue($g, $g['next_distribution_at']); // day 4 expect($g['arena_inset'] === 1, 'does not shrink past max inset: ' . $g['arena_inset']); expect(Engine::daysUntilShrink($g) === null, 'no upcoming shrink at max'); echo "Engine: shrink kills and crush and draw\n"; $g = game(['a', 'b'], ['shrinking_arena' => true, 'shrink_interval_days' => 1, 'jury' => false, 'grid' => 6, 'grid_auto' => false], $now); $g['players'][0]['x'] = 0; $g['players'][0]['y'] = 0; $g['players'][0]['lives'] = 1; $g['players'][1]['x'] = 5; $g['players'][1]['y'] = 5; $g['players'][1]['lives'] = 1; $g = Engine::applyDue($g, $g['next_distribution_at']); expect($g['players'][0]['alive'] === false, 'Alice died on shrink (1 life)'); expect($g['players'][1]['alive'] === false, 'Bob died on shrink (1 life)'); expect($g['status'] === 'finished', 'finished'); expect($g['draw'] === true, 'draw when both die in the same shrink'); echo "Engine: notes and chat privacy\n"; $g = game(['a', 'b'], [], $now); $g = Engine::setNote($g, 'a', 'b', 'looks dangerous'); $viewA = Engine::present($g, 'a'); $viewB = Engine::present($g, 'b'); $noteAonB = null; $noteBonB = null; foreach ($viewA['players'] as $p) { if ($p['id'] === 'b') { $noteAonB = $p['note']; } } foreach ($viewB['players'] as $p) { if ($p['id'] === 'b') { $noteBonB = $p['note']; } } expect($noteAonB === 'looks dangerous', 'Alice sees her note on Bob'); expect($noteBonB === '', 'Bob does not see Alice’s note'); $g = Engine::addMessage($g, 'a', 'b', 'hello', $now); $viewA = Engine::present($g, 'a'); $viewB = Engine::present($g, 'b'); expect(count($viewA['messages']) === 1 && $viewA['messages'][0]['text'] === 'hello', 'Alice sees chat'); expect(count($viewB['messages']) === 1, 'Bob sees chat'); // A third player would not — add c in lobby... already started. Fine. echo "Engine: modular rule flags\n"; $r = Engine::normalizeRules(['jury' => '0', 'shrinking_arena' => '1', 'shrink_interval_days' => 99, 'ceasefire' => '1']); expect($r['jury'] === false, 'jury "0" is false'); expect($r['shrinking_arena'] === true, 'shrink "1" is true'); expect($r['shrink_interval_days'] === 30, 'interval clamped to max 30: ' . $r['shrink_interval_days']); expect($r['ceasefire'] === true, 'ceasefire "1" is true'); echo "Engine: cease-fire ranges and midnight wrap\n"; $ranges = Engine::normalizeRanges([ ['start' => '22:00', 'end' => '06:00'], ['start' => '12:00', 'end' => '13:30'], ['start' => '25:00', 'end' => '01:00'], // invalid ['start' => '08:00', 'end' => '08:00'], // zero-length dropped ]); expect(count($ranges) === 2, '2 valid ranges: ' . count($ranges)); expect($ranges[0]['start'] === '12:00', 'sorted, first is 12:00'); expect($ranges[1]['start'] === '22:00', 'wrap range kept'); $g = game(['a', 'b'], [ 'jury' => false, 'ceasefire' => true, 'ceasefire_ranges' => [ ['start' => '22:00', 'end' => '06:00'], ['start' => '12:00', 'end' => '13:00'], ], ], $now); // Place adjacent so shoot would otherwise be legal. $g['players'][0]['x'] = 2; $g['players'][0]['y'] = 2; $g['players'][0]['ap'] = 3; $g['players'][1]['x'] = 3; $g['players'][1]['y'] = 2; // 2026-11-13 23:30 Amsterdam — inside 22:00-06:00 wrap $inCf = (new DateTime('2026-11-13 23:30:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($g, $inCf); expect($info !== null && $info['active'] === true, '23:30 is cease-fire (wrap)'); $end = (new DateTime('@' . $info['until']))->setTimezone($tz); expect($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')); $legal = Engine::legal($g, 'a', $inCf); expect($legal['shoot'] === [], 'Attack disabled during cease-fire'); expect($legal['ceasefire'] === true, 'legal.ceasefire true'); expect(count($legal['move']) >= 1 || $legal['upgrade'] || true, 'non-shoot actions still considered'); // Move is still allowed $legalMove = $legal['move']; expect(!empty($legalMove), 'can still move during cease-fire'); try { Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $inCf); expect(false, 'shoot during cease-fire should throw'); } catch (RuntimeException $e) { expect($e->getMessage() === 'ceasefire', 'error code ceasefire: ' . $e->getMessage()); } // 2026-11-14 06:00 exactly — end is exclusive, so not active $atEnd = (new DateTime('2026-11-14 06:00:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($g, $atEnd); expect($info['active'] === false, '06:00 exactly is not cease-fire (end exclusive)'); $until = (new DateTime('@' . $info['until']))->setTimezone($tz); expect($until->format('H:i') === '12:00', 'next cease-fire at 12:00: ' . $until->format('H:i')); // 12:30 — second range $noon = (new DateTime('2026-11-14 12:30:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($g, $noon); expect($info['active'] === true, '12:30 is cease-fire'); $end = (new DateTime('@' . $info['until']))->setTimezone($tz); expect($end->format('H:i') === '13:00', 'ends 13:00: ' . $end->format('H:i')); // 15:00 — free, next is 22:00 same day $afternoon = (new DateTime('2026-11-14 15:00:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($g, $afternoon); expect($info['active'] === false, '15:00 is free'); $until = (new DateTime('@' . $info['until']))->setTimezone($tz); expect($until->format('Y-m-d H:i') === '2026-11-14 22:00', 'until 22:00: ' . $until->format('Y-m-d H:i')); $legal = Engine::legal($g, 'a', $afternoon); expect(in_array('b', $legal['shoot'], true), 'can shoot when not in cease-fire'); $g2 = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $afternoon); expect($g2['players'][1]['lives'] === 2, 'shot landed outside cease-fire'); // Just before midnight wrap: 21:59 — until 22:00 $before = (new DateTime('2026-11-14 21:59:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($g, $before); expect($info['active'] === false, '21:59 is free'); expect($info['seconds'] === 60, '60 seconds until cease-fire: ' . $info['seconds']); // 05:59 next morning: 60 seconds left IN cease-fire $late = (new DateTime('2026-11-15 05:59:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($g, $late); expect($info['active'] === true, '05:59 still cease-fire'); expect($info['seconds'] === 60, '60 seconds left in cease-fire: ' . $info['seconds']); echo "Engine: cease-fire DST spring-forward (2026-03-29 02:00→03:00)\n"; // Range 01:00-04:00 on spring-forward day: 02:00 does not exist, period is 2 hours of real time. $gD = game(['a', 'b'], [ 'ceasefire' => true, 'ceasefire_ranges' => [['start' => '01:00', 'end' => '04:00']], 'jury' => false, ], $now); $start = (new DateTime('2026-03-29 01:00:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($gD, $start); expect($info['active'] === true, '01:00 CEST-transition day is cease-fire'); $end = (new DateTime('@' . $info['until']))->setTimezone($tz); expect($end->format('Y-m-d H:i') === '2026-03-29 04:00', 'ends 04:00: ' . $end->format('Y-m-d H:i')); // Real duration should be 2 hours (01:00 CET + skipped 02:00 hour → 04:00 CEST) // 01:00 on that day is still CET (UTC+1), 04:00 is CEST (UTC+2). $dur = $info['until'] - $start; expect($dur === 2 * 3600, 'spring-forward 01:00-04:00 is 2 real hours: ' . $dur); // 02:30 does not exist; 03:00 should still be inside $three = (new DateTime('2026-03-29 03:00:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($gD, $three); expect($info['active'] === true, '03:00 (after the jump) is still cease-fire'); expect($info['seconds'] === 3600, '1 hour left at 03:00: ' . $info['seconds']); // Before: 00:30, until 01:00 = 30 min $half = (new DateTime('2026-03-29 00:30:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($gD, $half); expect($info['active'] === false, '00:30 is free'); expect($info['seconds'] === 1800, '30 min until 01:00: ' . $info['seconds']); echo "Engine: cease-fire DST fall-back (2026-10-25 03:00→02:00)\n"; // Range 01:00-03:30. The 02:00 hour happens twice. amsterdamWallclock adds minutes // from midnight, so 03:30 is unambiguous (after the fallback). Duration: // midnight CEST 00:00 = UTC 22:00 previous; +3.5h = 03:30 CET? Let's assert via DateTime. $gF = game(['a', 'b'], [ 'ceasefire' => true, 'ceasefire_ranges' => [['start' => '01:00', 'end' => '03:30']], 'jury' => false, ], $now); $fbStart = (new DateTime('2026-10-25 01:00:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($gF, $fbStart); expect($info['active'] === true, '01:00 fall-back day is cease-fire'); $end = (new DateTime('@' . $info['until']))->setTimezone($tz); expect($end->format('Y-m-d H:i') === '2026-10-25 03:30', 'ends 03:30: ' . $end->format('Y-m-d H:i')); $dur = $info['until'] - $fbStart; // 01:00 CEST (UTC 23:00) to 03:30 CET (UTC 02:30) = 3.5 hours (the 02:00 hour repeats). expect($dur === (int) (3.5 * 3600), 'fall-back 01:00-03:30 is 3.5 real hours: ' . $dur); // A moment during the repeated 02:xx should still be active. // 02:15 CEST (first) = 01:00 CEST + 75 min. $first215 = $fbStart + 75 * 60; $info = Engine::ceasefireInfo($gF, $first215); expect($info['active'] === true, 'first 02:15 (still DST) is cease-fire'); // 02:15 CET (second) = 01:00 + 75 min + 60 min (the repeated hour) $second215 = $fbStart + (75 + 60) * 60; $info = Engine::ceasefireInfo($gF, $second215); expect($info['active'] === true, 'second 02:15 (CET) is still cease-fire'); // After 03:30 CET: free $after = (new DateTime('2026-10-25 03:30:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($gF, $after); expect($info['active'] === false, '03:30 exactly is free (end exclusive)'); echo "Engine: cease-fire off or empty ranges\n"; $gN = game(['a', 'b'], ['ceasefire' => false, 'ceasefire_ranges' => [['start' => '00:00', 'end' => '23:59']]], $now); expect(Engine::ceasefireInfo($gN, $now) === null, 'rule off → no info (ranges ignored)'); $gE = game(['a', 'b'], ['ceasefire' => true, 'ceasefire_ranges' => []], $now); expect(Engine::ceasefireInfo($gE, $now) === null, 'on but empty ranges → treated as no cease-fire'); echo "Engine: overlapping ranges merge for countdown\n"; $gM = game(['a', 'b'], [ 'ceasefire' => true, 'ceasefire_ranges' => [ ['start' => '10:00', 'end' => '12:00'], ['start' => '11:00', 'end' => '13:00'], ], 'jury' => false, ], $now); $t = (new DateTime('2026-11-14 10:30:00', $tz))->getTimestamp(); $info = Engine::ceasefireInfo($gM, $t); $end = (new DateTime('@' . $info['until']))->setTimezone($tz); expect($end->format('H:i') === '13:00', 'merged overlapping ends at 13:00: ' . $end->format('H:i')); echo "Engine: visibility defaults and invalid values fall back to public\n"; $norm = Engine::normalizeRules([ 'vis_shots' => 'nope', 'vis_gifts' => 123, 'vis_ap' => 'PUBLIC', 'vis_range' => '', 'vis_lives' => null, ]); expect($norm['vis_shots'] === 'public', 'bad vis_shots → public'); expect($norm['vis_gifts'] === 'public', 'bad vis_gifts → public'); expect($norm['vis_ap'] === 'public', 'bad vis_ap → public'); expect($norm['vis_range'] === 'public', 'bad vis_range → public'); expect($norm['vis_lives'] === 'public', 'bad vis_lives → public'); $ok = Engine::normalizeRules([ 'vis_shots' => 'elim_known_target', 'vis_gifts' => 'anonymous', 'vis_ap' => 'within_range', 'vis_range' => 'known_in_range', 'vis_lives' => 'within_range', 'grid' => 14, 'grid_auto' => false, ]); expect($ok['vis_shots'] === 'elim_known_target', 'accepts elim_known_target'); expect($ok['vis_gifts'] === 'anonymous', 'accepts anonymous'); expect($ok['vis_ap'] === 'within_range', 'accepts vis_ap within_range'); expect($ok['vis_range'] === 'known_in_range', 'accepts known_in_range'); expect($ok['vis_lives'] === 'within_range', 'accepts vis_lives within_range'); expect($ok['grid'] === 14 && $ok['grid_auto'] === false, 'accepts manual grid 14'); $ok2 = Engine::normalizeRules(['vis_ap' => 'hidden', 'vis_lives' => 'hidden']); expect($ok2['vis_ap'] === 'hidden' && $ok2['vis_lives'] === 'hidden', 'still accepts hidden'); $badG = Engine::normalizeRules(['vis_ap' => 'in_range', 'vis_lives' => 'known_in_range']); expect($badG['vis_ap'] === 'public' && $badG['vis_lives'] === 'public', 'unknown vis_ap/lives values → public'); /** * Three-player game with A next to B (in starting range 2) and C far away. * Extra AP so we can shoot/give/upgrade more than once. * * @return array{0:array,1:int} */ function visGame(array $vis, int $now): array { $g = game(['a', 'b', 'c'], array_merge(['jury' => false], $vis), $now); // Place: A at (2,2), B at (3,2) dist 1, C at (5,5) dist 3 from A. foreach ($g['players'] as $i => $p) { if ($p['id'] === 'a') { $g['players'][$i]['x'] = 2; $g['players'][$i]['y'] = 2; $g['players'][$i]['ap'] = 5; $g['players'][$i]['range'] = 2; } elseif ($p['id'] === 'b') { $g['players'][$i]['x'] = 3; $g['players'][$i]['y'] = 2; $g['players'][$i]['ap'] = 5; $g['players'][$i]['range'] = 2; } else { $g['players'][$i]['x'] = 5; $g['players'][$i]['y'] = 5; $g['players'][$i]['ap'] = 5; $g['players'][$i]['range'] = 2; } } return [$g, $now]; } function visEvents(array $view, string $type): array { return array_values(array_filter($view['events'] ?? [], fn ($e) => ($e['type'] ?? '') === $type)); } function visPlayer(array $view, string $id): ?array { foreach ($view['players'] as $p) { if ($p['id'] === $id) { return $p; } } return null; } echo "Engine: vis shots — public (everyone sees, including lives_left when lives public)\n"; [$g, $t] = visGame(['vis_shots' => 'public'], $now); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees public shot'); expect(count(visEvents($vb, 'shoot')) === 1, 'target sees public shot'); expect(count(visEvents($vc, 'shoot')) === 1, 'third party sees public shot'); expect(($vc['events'] === visEvents($vc, 'shoot') ? true : isset(visEvents($vc, 'shoot')[0]['player'])) && visEvents($vc, 'shoot')[0]['player'] === 'a', 'third party sees shooter id'); expect(visEvents($vc, 'shoot')[0]['lives_left'] === 2, 'public lives_left on shot'); expect(visEvents($vc, 'shoot')[0]['damage'] === 1, 'damage is 1'); expect(visPlayer($vc, 'b')['lives'] === 2, 'public lives in state'); echo "Engine: vis shots — elim_public (non-elim hidden from third party, visible to target)\n"; [$g, $t] = visGame(['vis_shots' => 'elim_public'], $now); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees non-elim'); expect(count(visEvents($vb, 'shoot')) === 1, 'target sees non-elim under elim_public'); expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see non-elim'); expect(count(visEvents($vc, 'death')) === 0, 'no death on non-elim'); // Two more shots to eliminate (B started with 3, now 2). $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $vc = Engine::present($g, 'c', $t); $vb = Engine::present($g, 'b', $t); $shotsC = visEvents($vc, 'shoot'); expect(count($shotsC) === 1, 'third party sees only the eliminating shot: ' . count($shotsC)); expect(!empty($shotsC[0]['killed']), 'the visible shot is the kill'); expect(count(visEvents($vc, 'death')) === 1, 'third party sees death'); expect((visEvents($vc, 'death')[0]['by'] ?? null) === 'a', 'elim_public death includes killer'); expect((visEvents($vb, 'death')[0]['by'] ?? null) === 'a', 'target sees killer'); expect(visPlayer($vc, 'b')['alive'] === false, 'death/alive is always public'); echo "Engine: vis shots — known_target (others never see the shot, death without by)\n"; [$g, $t] = visGame(['vis_shots' => 'known_target'], $now); $g['players'][array_key_first(array_filter($g['players'], fn ($p) => $p['id'] === 'b'))]['lives'] = 1; // find b index properly foreach ($g['players'] as $i => $p) { if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; } } $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees known_target shot'); expect(count(visEvents($vb, 'shoot')) === 1, 'target sees known_target shot'); expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see known_target shot'); $deaths = visEvents($vc, 'death'); expect(count($deaths) === 1, 'third party still sees that B died'); expect(!array_key_exists('by', $deaths[0]), 'killer stripped for third party under known_target'); expect((visEvents($vb, 'death')[0]['by'] ?? null) === 'a', 'target still sees who shot them'); echo "Engine: vis shots — elim_known_target (non-elim hidden even from target)\n"; [$g, $t] = visGame(['vis_shots' => 'elim_known_target'], $now); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees non-elim under elim_known_target'); expect(count(visEvents($vb, 'shoot')) === 0, 'target does NOT see non-elim shot (must deduce)'); expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see non-elim'); expect(visPlayer($vb, 'b')['lives'] === 2, 'target still sees own lives drop'); expect(visPlayer($vc, 'b')['lives'] === 2, 'lives still public in this game (default)'); // Eliminate $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(count(visEvents($vb, 'shoot')) === 1, 'target sees only the eliminating shot'); expect((visEvents($vb, 'shoot')[0]['player'] ?? null) === 'a', 'elim shot names the shooter for the target'); expect(count(visEvents($vc, 'shoot')) === 0, 'third party never sees shots under elim_known_target'); expect(!array_key_exists('by', visEvents($vc, 'death')[0]), 'third party death has no by'); echo "Engine: vis shots + hidden lives — lives_left stripped except for the target\n"; [$g, $t] = visGame(['vis_shots' => 'public', 'vis_lives' => 'hidden'], $now); $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(!array_key_exists('lives_left', visEvents($va, 'shoot')[0]), 'shooter does not get target lives_left'); expect((visEvents($vb, 'shoot')[0]['lives_left'] ?? null) === 2, 'target sees own lives_left'); expect(!array_key_exists('lives_left', visEvents($vc, 'shoot')[0]), 'third party does not get lives_left'); expect(visEvents($va, 'shoot')[0]['damage'] === 1, 'shooter still sees damage 1'); expect(visPlayer($va, 'a')['lives'] === 3, 'own lives always visible'); expect(visPlayer($va, 'b')['lives'] === null, 'other lives hidden from shooter'); expect(visPlayer($vc, 'b')['lives'] === null, 'other lives hidden from third party'); expect(visPlayer($vb, 'b')['lives'] === 2, 'target sees own lives'); expect(is_bool(visPlayer($vc, 'b')['alive']), 'alive flag still present when lives hidden'); echo "Engine: vis gifts — public, known_recipient, anonymous\n"; [$g, $t] = visGame(['vis_gifts' => 'public'], $now); $g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t); $vc = Engine::present($g, 'c', $t); $gives = visEvents($vc, 'give'); expect(count($gives) === 1 && $gives[0]['player'] === 'a' && $gives[0]['amount'] === 2, 'public gift visible to third party with giver and amount'); [$g, $t] = visGame(['vis_gifts' => 'known_recipient'], $now); $g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(count(visEvents($va, 'give')) === 1 && visEvents($va, 'give')[0]['player'] === 'a', 'giver sees own gift'); expect(count(visEvents($vb, 'give')) === 1 && visEvents($vb, 'give')[0]['player'] === 'a', 'recipient sees giver'); expect(count(visEvents($vc, 'give')) === 0, 'third party does not see known_recipient gift'); [$g, $t] = visGame(['vis_gifts' => 'anonymous'], $now); $beforeB = Engine::player($g, 'b')['ap']; $g = Engine::applyAction($g, 'a', ['type' => 'give', 'target' => 'b', 'amount' => 2], $t); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(count(visEvents($va, 'give')) === 1 && visEvents($va, 'give')[0]['player'] === 'a', 'giver sees full anonymous gift in their own log'); $rec = visEvents($vb, 'give'); expect(count($rec) === 1, 'recipient sees an anonymous gift event'); expect(!empty($rec[0]['anonymous']), 'event marked anonymous'); expect(!array_key_exists('player', $rec[0]), 'giver id stripped for recipient'); expect(($rec[0]['amount'] ?? 0) === 2, 'recipient sees the AP amount'); expect(($rec[0]['target'] ?? '') === 'b', 'recipient is named as target'); expect(count(visEvents($vc, 'give')) === 0, 'third party does not see anonymous gift'); expect(visPlayer($vb, 'b')['ap'] === $beforeB + 2, 'recipient AP actually increased'); // Stored (unfiltered) event still has the giver — present() must not mutate. $rawGive = null; foreach ($g['events'] as $e) { if (($e['type'] ?? '') === 'give') { $rawGive = $e; } } expect($rawGive !== null && ($rawGive['player'] ?? null) === 'a', 'stored event still has the giver (present does not mutate)'); expect(!array_key_exists('player', $rec[0]) || $rec[0]['player'] !== 'a' || true, 'sanity'); expect(($rawGive['player'] ?? null) === 'a' && !array_key_exists('player', $rec[0]), 'clone is independent of stored event'); echo "Engine: vis AP totals hidden\n"; [$g, $t] = visGame(['vis_ap' => 'hidden'], $now); $va = Engine::present($g, 'a', $t); $vc = Engine::present($g, 'c', $t); expect(visPlayer($va, 'a')['ap'] === 5, 'own AP visible when hidden rule is on'); expect(visPlayer($va, 'b')['ap'] === null, 'other AP hidden'); expect(visPlayer($vc, 'a')['ap'] === null, 'third party does not see A’s AP'); // Daily grants: only the viewer’s own grant is included. $midnight = Engine::nextMidnight($t); $g = Engine::applyDue($g, $midnight); $va = Engine::present($g, 'a', $midnight); $dailies = visEvents($va, 'daily'); expect(count($dailies) === 1, 'daily event present'); expect(array_keys($dailies[0]['grants']) === ['a'], 'hidden AP: only own grant in daily event: ' . json_encode($dailies[0]['grants'])); expect(!isset($dailies[0]['jury']['counts']), 'jury vote counts stripped'); echo "Engine: vis range — hidden and known_in_range\n"; [$g, $t] = visGame(['vis_range' => 'hidden'], $now); $g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $t); $va = Engine::present($g, 'a', $t); $vc = Engine::present($g, 'c', $t); expect(visPlayer($va, 'a')['range'] === 3, 'own range after upgrade'); expect(visPlayer($vc, 'a')['range'] === null, 'other range hidden'); expect(count(visEvents($va, 'upgrade')) === 1, 'upgrader sees own upgrade'); expect(count(visEvents($vc, 'upgrade')) === 0, 'third party does not see hidden upgrade'); [$g, $t] = visGame(['vis_range' => 'known_in_range'], $now); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); // B is at dist 1 from A (range 2) so A is in B’s range and B is in A’s range. expect(visPlayer($vb, 'a')['range'] === 2, 'B is within A’s range, sees A’s range'); expect(visPlayer($va, 'b')['range'] === 2, 'A is within B’s range, sees B’s range'); expect(visPlayer($vc, 'a')['range'] === null, 'C is outside A’s range (dist 3 > 2)'); expect(visPlayer($va, 'c')['range'] === null, 'A is outside C’s range (dist 3 > 2)'); expect(visPlayer($vc, 'c')['range'] === 2, 'C always sees own range'); // Upgrade A to range 3: C (dist 3) is now in A’s range. $g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $t); $vc = Engine::present($g, 'c', $t); $va = Engine::present($g, 'a', $t); expect(visPlayer($vc, 'a')['range'] === 3, 'after upgrade, C is within A’s range and sees it'); expect(count(visEvents($vc, 'upgrade')) === 1, 'C now also sees the upgrade event'); expect(visPlayer($va, 'c')['range'] === null, 'A is still outside C’s range 2'); echo "Engine: movement is always public, even with everything hidden\n"; [$g, $t] = visGame([ 'vis_shots' => 'elim_known_target', 'vis_gifts' => 'anonymous', 'vis_ap' => 'hidden', 'vis_range' => 'hidden', 'vis_lives' => 'hidden', ], $now); // A moves to (2,3) — adjacent empty. $g = Engine::applyAction($g, 'a', ['type' => 'move', 'x' => 2, 'y' => 3], $t); $vc = Engine::present($g, 'c', $t); $moves = visEvents($vc, 'move'); expect(count($moves) === 1, 'move is public'); expect($moves[0]['player'] === 'a' && $moves[0]['to']['x'] === 2 && $moves[0]['to']['y'] === 3, 'move names the player and destination'); expect(visPlayer($vc, 'a')['x'] === 2 && visPlayer($vc, 'a')['y'] === 3, 'new position is in the public state'); expect(visPlayer($vc, 'a')['ap'] === null, 'AP still hidden after the move'); expect(visPlayer($vc, 'a')['lives'] === null, 'lives still hidden'); expect(visPlayer($vc, 'a')['range'] === null, 'range still hidden'); echo "Engine: present() does not leak hidden keys with real values\n"; [$g, $t] = visGame(['vis_ap' => 'hidden', 'vis_lives' => 'hidden', 'vis_range' => 'hidden'], $now); $vc = Engine::present($g, 'c', $t); $encoded = json_encode($vc); expect(visPlayer($vc, 'a')['ap'] === null, 'JSON-facing ap is null'); expect(visPlayer($vc, 'a')['lives'] === null, 'JSON-facing lives is null'); expect(visPlayer($vc, 'a')['range'] === null, 'JSON-facing range is null'); // The raw numbers for A (ap=5, lives=3, range=2) must not appear attached to A in a leaky way // — we only require the structured fields are null, which json_encode will emit as null. $decoded = json_decode($encoded, true); foreach ($decoded['players'] as $p) { if ($p['id'] !== 'c') { expect($p['ap'] === null && $p['lives'] === null && $p['range'] === null, "no numeric secrets for {$p['id']}"); } } echo "Engine: nextDistribution (per-lobby AP time) and DST\n"; $tz = new DateTimeZone('Europe/Amsterdam'); $from = (new DateTime('2026-11-14 14:00:00', $tz))->getTimestamp(); $nx = Engine::nextDistribution($from, '15:00'); $got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s'); expect($got === '2026-11-14 15:00:00', "14:00 → today 15:00: $got"); $at = (new DateTime('2026-11-14 15:00:00', $tz))->getTimestamp(); $nx = Engine::nextDistribution($at, '15:00'); $got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s'); expect($got === '2026-11-15 15:00:00', "exactly 15:00 → tomorrow 15:00: $got"); $from2 = (new DateTime('2026-11-14 16:00:00', $tz))->getTimestamp(); $nx = Engine::nextDistribution($from2, '15:00'); $got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s'); expect($got === '2026-11-15 15:00:00', "16:00 → tomorrow 15:00: $got"); // 00:00 matches nextMidnight $eve = (new DateTime('2026-11-13 23:30:00', $tz))->getTimestamp(); expect(Engine::nextDistribution($eve, '00:00') === Engine::nextMidnight($eve), '00:00 path equals nextMidnight'); expect(Engine::normalizeRules(['ap_time' => '7:05'])['ap_time'] === '07:05', 'ap_time normalised to HH:MM'); expect(Engine::normalizeRules(['ap_time' => 'nope'])['ap_time'] === '00:00', 'bad ap_time → 00:00'); expect(Engine::normalizeRules(['ap_time' => '24:00'])['ap_time'] === '00:00', '24:00 invalid → 00:00'); // DST spring-forward 2026-03-29 02:00–03:00 skipped. $before = (new DateTime('2026-03-29 01:30:00', $tz))->getTimestamp(); $nx = Engine::nextDistribution($before, '02:30'); $gotDt = (new DateTime('@' . $nx))->setTimezone($tz); // 02:30 does not exist; PHP setTime lands on or after the gap (03:30 CEST typically). expect($gotDt->getTimestamp() > $before, 'DST spring: 02:30 candidate is strictly after 01:30'); expect($gotDt->format('Y-m-d') === '2026-03-29', 'DST spring: still that calendar day: ' . $gotDt->format('Y-m-d H:i:s T')); // A legal time after the gap is exact. $nx = Engine::nextDistribution($before, '04:00'); $got = (new DateTime('@' . $nx))->setTimezone($tz)->format('Y-m-d H:i:s'); expect($got === '2026-03-29 04:00:00', "DST spring: 04:00 exact: $got"); // Fall-back 2026-10-25: 02:30 exists twice; we just need a valid instant that day after 01:30. $fb = (new DateTime('2026-10-25 01:30:00', $tz))->getTimestamp(); $nx = Engine::nextDistribution($fb, '02:30'); $gotDt = (new DateTime('@' . $nx))->setTimezone($tz); expect($gotDt->format('Y-m-d') === '2026-10-25' && (int) $gotDt->format('H') === 2 && (int) $gotDt->format('i') === 30, 'DST fall-back: 02:30 that day: ' . $gotDt->format('Y-m-d H:i:s T')); echo "Engine: AP at a non-midnight lobby time\n"; $t15 = (new DateTime('2026-11-14 14:00:00', $tz))->getTimestamp(); $g = game(['a', 'b'], ['ap_time' => '15:00', 'jury' => false], $t15); expect((int) $g['next_distribution_at'] === Engine::nextDistribution($t15, '15:00'), 'start schedules 15:00'); $apA = Engine::player($g, 'a')['ap']; $g2 = Engine::applyDue($g, Engine::nextDistribution($t15, '15:00')); expect((int) $g2['day'] === 1, 'day increments at 15:00'); expect((int) Engine::player($g2, 'a')['ap'] === $apA + 1, 'AP granted at 15:00'); // A tick just before 15:00 must not grant. $g3 = Engine::applyDue($g, Engine::nextDistribution($t15, '15:00') - 1); expect((int) $g3['day'] === 0, 'one second before 15:00: no grant'); echo "Engine: AP loot default off; on only for eliminating shots\n"; [$g, $t] = visGame(['ap_loot' => false], $now); foreach ($g['players'] as $i => $p) { if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; } if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; } } $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $a = Engine::player($g, 'a'); $b = Engine::player($g, 'b'); expect(empty($b['alive']), 'B is dead'); expect((int) $b['ap'] === 0, 'dead B has 0 AP'); expect((int) $a['ap'] === 2, 'loot off: A spent 1, did not receive B’s 4 (has 2)'); $shoot = null; foreach ($g['events'] as $e) { if (($e['type'] ?? '') === 'shoot' && !empty($e['killed'])) { $shoot = $e; } } expect(!isset($shoot['loot']), 'loot off: no loot field on the kill shot'); [$g, $t] = visGame(['ap_loot' => true], $now); foreach ($g['players'] as $i => $p) { if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; } if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; } } $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $a = Engine::player($g, 'a'); $b = Engine::player($g, 'b'); expect(empty($b['alive']) && (int) $b['ap'] === 0, 'loot on: B dead, AP zeroed'); expect((int) $a['ap'] === 2 + 4, 'loot on: A spent 1 (3→2) then looted 4 → 6: ' . $a['ap']); $va = Engine::present($g, 'a', $t); $vc = Engine::present($g, 'c', $t); $sa = visEvents($va, 'shoot')[0] ?? []; $sc = visEvents($vc, 'shoot')[0] ?? []; expect(($sa['loot'] ?? 0) === 4, 'shooter sees loot amount'); expect(($sc['loot'] ?? 0) === 4, 'public AP: third party sees loot'); // Hidden AP: third party does not see loot amount; shooter does. [$g, $t] = visGame(['ap_loot' => true, 'vis_ap' => 'hidden'], $now); foreach ($g['players'] as $i => $p) { if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 1; $g['players'][$i]['ap'] = 4; } if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; } } $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); $va = Engine::present($g, 'a', $t); $vc = Engine::present($g, 'c', $t); expect((visEvents($va, 'shoot')[0]['loot'] ?? 0) === 4, 'shooter sees loot even if AP totals hidden'); expect(!array_key_exists('loot', visEvents($vc, 'shoot')[0]), 'third party does not see loot amount when AP is hidden'); expect(visPlayer($va, 'a')['ap'] === 6, 'shooter sees own AP including loot'); expect(visPlayer($vc, 'a')['ap'] === null, 'third party still does not see A’s total'); // Non-elim shot does not loot; surrender does not loot A’s AP to anyone. [$g, $t] = visGame(['ap_loot' => true], $now); foreach ($g['players'] as $i => $p) { if ($p['id'] === 'b') { $g['players'][$i]['lives'] = 3; $g['players'][$i]['ap'] = 4; } if ($p['id'] === 'a') { $g['players'][$i]['ap'] = 3; } } $g = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $t); expect((int) Engine::player($g, 'a')['ap'] === 2, 'non-elim: no loot'); expect((int) Engine::player($g, 'b')['ap'] === 4, 'non-elim: B keeps AP'); $g = Engine::applyAction($g, 'b', ['type' => 'surrender'], $t); expect(empty(Engine::player($g, 'b')['alive']), 'B surrendered'); expect((int) Engine::player($g, 'a')['ap'] === 2, 'surrender does not transfer AP'); expect((int) Engine::player($g, 'b')['ap'] === 0, 'surrendered AP is zeroed, not looted'); echo "Engine: vis lives+AP within_range uses the VIEWER's range\n"; // A (2,2) r=2; B (3,2) dist 1; C (5,5) dist 3 from A, dist 3 from B. [$g, $t] = visGame(['vis_lives' => 'within_range', 'vis_ap' => 'within_range', 'vis_range' => 'hidden'], $now); $va = Engine::present($g, 'a', $t); $vb = Engine::present($g, 'b', $t); $vc = Engine::present($g, 'c', $t); expect(visPlayer($va, 'a')['lives'] === 3 && visPlayer($va, 'a')['ap'] === 5, 'A always sees own lives/AP'); expect(visPlayer($va, 'b')['lives'] === 3 && visPlayer($va, 'b')['ap'] === 5, 'A sees B (dist 1 ≤ A’s range 2)'); expect(visPlayer($va, 'c')['lives'] === null && visPlayer($va, 'c')['ap'] === null, 'A does not see C (dist 3 > A’s range 2)'); expect(visPlayer($vb, 'a')['lives'] === 3 && visPlayer($vb, 'a')['ap'] === 5, 'B sees A (dist 1 ≤ B’s range 2)'); expect(visPlayer($vb, 'c')['lives'] === null && visPlayer($vb, 'c')['ap'] === null, 'B does not see C (dist 3 > 2)'); expect(visPlayer($vc, 'a')['lives'] === null && visPlayer($vc, 'a')['ap'] === null, 'C sees neither A'); expect(visPlayer($vc, 'b')['lives'] === null && visPlayer($vc, 'b')['ap'] === null, 'C sees neither B'); expect(visPlayer($vc, 'c')['lives'] === 3, 'C sees own lives'); // Asymmetry vs vis_range known_in_range: give C range 4 so A is in C’s range // (dist 3 ≤ 4) but C is still not in A’s range (3 > 2). foreach ($g['players'] as $i => $p) { if ($p['id'] === 'c') { $g['players'][$i]['range'] = 4; } } $va = Engine::present($g, 'a', $t); $vc = Engine::present($g, 'c', $t); expect(visPlayer($va, 'c')['lives'] === null && visPlayer($va, 'c')['ap'] === null, 'within_range lives/AP: A still cannot see C (C is outside A’s range)'); expect(visPlayer($vc, 'a')['lives'] === 3 && visPlayer($vc, 'a')['ap'] === 5, 'within_range lives/AP: C can see A (A is inside C’s range 4)'); // Same positions, vis_range = known_in_range: opposite criterion (SUBJECT’s range). $gR = $g; $gR['rules']['vis_range'] = 'known_in_range'; $vaR = Engine::present($gR, 'a', $t); $vcR = Engine::present($gR, 'c', $t); expect(visPlayer($vaR, 'c')['range'] === 4, 'known_in_range: A CAN see C’s range (A is inside C’s range 4) — opposite of lives/AP'); expect(visPlayer($vcR, 'a')['range'] === null, 'known_in_range: C cannot see A’s range (C is outside A’s range 2)'); // A upgrades range to 3: C (dist 3) enters A’s range; A now sees C’s lives/AP. $g = Engine::applyAction($g, 'a', ['type' => 'upgrade'], $t); // range 2→3, spends 1 AP $va = Engine::present($g, 'a', $t); expect(Engine::player($g, 'a')['range'] === 3, 'A range is 3'); expect(visPlayer($va, 'c')['lives'] === 3 && visPlayer($va, 'c')['ap'] === 5, 'after A upgrades to 3, A sees C’s lives/AP'); // lives_left on a shot: C far away must not see B’s remaining lives; A can (B in range). [$g2, $t2] = visGame(['vis_lives' => 'within_range', 'vis_shots' => 'public'], $now); $g2 = Engine::applyAction($g2, 'a', ['type' => 'shoot', 'target' => 'b'], $t2); $va2 = Engine::present($g2, 'a', $t2); $vb2 = Engine::present($g2, 'b', $t2); $vc2 = Engine::present($g2, 'c', $t2); expect((visEvents($va2, 'shoot')[0]['lives_left'] ?? null) === 2, 'A sees lives_left (B in A’s range)'); expect((visEvents($vb2, 'shoot')[0]['lives_left'] ?? null) === 2, 'B sees own lives_left'); expect(!array_key_exists('lives_left', visEvents($vc2, 'shoot')[0]), 'C does not see lives_left (B outside C’s range)'); expect(visEvents($vc2, 'shoot')[0]['damage'] === 1, 'C still sees damage on a public shot'); expect(visPlayer($vc2, 'b')['lives'] === null, 'C does not see B’s lives in state'); // Daily grants: A sees own and B’s (in range), not C’s. [$g3, $t3] = visGame(['vis_ap' => 'within_range', 'jury' => false], $now); $midnight = Engine::nextMidnight($t3); $g3 = Engine::applyDue($g3, $midnight); $va3 = Engine::present($g3, 'a', $midnight); $vc3 = Engine::present($g3, 'c', $midnight); $ga = visEvents($va3, 'daily')[0]['grants'] ?? []; $gc = visEvents($vc3, 'daily')[0]['grants'] ?? []; expect(isset($ga['a']) && isset($ga['b']) && !isset($ga['c']), 'A’s daily grants: own + B, not C: ' . json_encode($ga)); expect(isset($gc['c']) && !isset($gc['a']) && !isset($gc['b']), 'C’s daily grants: only own: ' . json_encode($gc)); // Loot: within_range AP — third party out of range does not see loot; a neighbour of the shooter does. [$g4, $t4] = visGame(['ap_loot' => true, 'vis_ap' => 'within_range'], $now); // B has 5 AP; A spends 1 to kill? B has 3 lives, need 3 shots. Give A more AP already 5. $g4 = Engine::applyAction($g4, 'a', ['type' => 'shoot', 'target' => 'b'], $t4); $g4 = Engine::applyAction($g4, 'a', ['type' => 'shoot', 'target' => 'b'], $t4); $g4 = Engine::applyAction($g4, 'a', ['type' => 'shoot', 'target' => 'b'], $t4); $va4 = Engine::present($g4, 'a', $t4); $vc4 = Engine::present($g4, 'c', $t4); $shotsA = visEvents($va4, 'shoot'); $killA = $shotsA[count($shotsA) - 1]; expect(!empty($killA['killed']) && ($killA['loot'] ?? 0) > 0, 'A sees loot on eliminating shot'); $shotsC = visEvents($vc4, 'shoot'); $killC = $shotsC[count($shotsC) - 1]; expect(!empty($killC['killed']), 'C sees the public kill shot'); expect(!array_key_exists('loot', $killC), 'C does not see loot (A is outside C’s range)'); // Shrink hurt list: only players the viewer may see lives of. $gS = game(['a', 'b', 'c'], [ 'shrinking_arena' => true, 'shrink_interval_days' => 1, 'jury' => false, 'vis_lives' => 'within_range', 'grid' => 6, 'grid_auto' => false, ], $now); // 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. foreach ($gS['players'] as $i => $p) { if ($p['id'] === 'a') { $gS['players'][$i]['x'] = 0; $gS['players'][$i]['y'] = 0; $gS['players'][$i]['range'] = 2; } if ($p['id'] === 'b') { $gS['players'][$i]['x'] = 2; $gS['players'][$i]['y'] = 2; $gS['players'][$i]['range'] = 2; } if ($p['id'] === 'c') { $gS['players'][$i]['x'] = 5; $gS['players'][$i]['y'] = 5; $gS['players'][$i]['range'] = 2; } } $gS = Engine::applyDue($gS, $gS['next_distribution_at']); $vaS = Engine::present($gS, 'a', $gS['next_distribution_at'] ?? $now); $hurtA = visEvents($vaS, 'shrink')[0]['hurt'] ?? []; // After shrink, positions change (pushed inward). We use CURRENT positions for the # check — document this. Just assert the list is a subset and includes self if hurt. expect(in_array('a', $hurtA, true) || true, 'shrink event present for A'); $rawHurt = null; foreach ($gS['events'] as $ev) { if (($ev['type'] ?? '') === 'shrink') { $rawHurt = $ev['hurt'] ?? []; } } expect(is_array($rawHurt), 'stored shrink has hurt list'); // Filtered list must not include ids the viewer cannot currently see lives of, // other than what's allowed by canSeeStat. foreach ($hurtA as $id) { $subj = null; foreach ($vaS['players'] as $pp) { if ($pp['id'] === $id) { $subj = $pp; } } expect($id === 'a' || ($subj && $subj['lives'] !== null), "A’s filtered hurt id $id is someone whose lives A may see"); } echo "Engine: lobby grid option — auto suggestion and manual lock\n"; $nowG = $now; $g = Engine::newLobby('g', 'GRID01', 'a', 'Alice', null, [], $nowG); expect($g['grid'] === 7 && $g['rules']['grid_auto'] === true, 'new lobby 7 and auto'); $g = Engine::join($g, 'p2', 'P2', null, $nowG); expect($g['rules']['grid'] === 7 && count($g['players']) === 2, '2 players still 7'); $g = Engine::join($g, 'p3', 'P3', null, $nowG); expect($g['rules']['grid'] === 8, '3rd player bumps to 8 (ceil(1.5)=2)'); $g = Engine::join($g, 'p4', 'P4', null, $nowG); expect($g['rules']['grid'] === 8, '4 players stay 8'); $g = Engine::join($g, 'p5', 'P5', null, $nowG); expect($g['rules']['grid'] === 9, '5th player bumps to 9'); for ($i = 6; $i <= 10; $i++) { $g = Engine::join($g, 'p' . $i, 'P' . $i, null, $nowG); } expect(count($g['players']) === 10, '10 players'); expect($g['rules']['grid'] === 11 && $g['grid'] === 11, '10 players → 11'); $g = Engine::join($g, 'p11', 'P11', null, $nowG); expect($g['rules']['grid'] === 12 && $g['grid'] === 12, '11th player bumps suggestion to 12'); $g = Engine::leaveLobby($g, 'p11', $nowG); expect($g['rules']['grid'] === 11 && count($g['players']) === 10, 'leave drops suggestion back to 11'); // Manual lock: host sets 14, further joins do not change it. $g = Engine::newLobby('g', 'GRID02', 'a', 'Alice', null, [], $nowG); $g = Engine::join($g, 'b', 'Bob', null, $nowG); $g = Engine::setLobbyRules($g, 'a', ['grid' => 14, 'grid_auto' => false]); expect($g['rules']['grid'] === 14 && $g['rules']['grid_auto'] === false && $g['grid'] === 14, 'manual 14'); for ($i = 3; $i <= 16; $i++) { $g = Engine::join($g, 'p' . $i, 'P' . $i, null, $nowG); } expect(count($g['players']) === 16, '16 players'); expect($g['rules']['grid'] === 14 && $g['grid'] === 14, 'manual 14 stays at 16 players (suggestion would be 17)'); $g = Engine::start($g, 'a', $nowG); expect($g['grid'] === 14 && $g['status'] === 'running', 'starts on host’s 14×14'); expect(count($g['players']) === 16, 'all 16 placed'); $keys = []; foreach ($g['players'] as $p) { expect(Engine::inBounds(14, $p['x'], $p['y']), 'placed in bounds'); $k = $p['x'] . ',' . $p['y']; expect(!isset($keys[$k]), "unique start $k"); $keys[$k] = true; } // Host-chosen 6×6 for 2 players is valid (tighter than the suggestion). $g = Engine::newLobby('g', 'GRID03', 'a', 'Alice', null, ['grid' => 6, 'grid_auto' => false], $nowG); $g = Engine::join($g, 'b', 'Bob', null, $nowG); $g = Engine::start($g, 'a', $nowG); expect($g['grid'] === 6, 'host may start a 6×6'); // Auto at start uses current player count, even if rules.grid is stale. $g = Engine::newLobby('g', 'GRID04', 'a', 'Alice', null, [], $nowG); $g = Engine::join($g, 'b', 'Bob', null, $nowG); $g['rules']['grid'] = 10; // stale; auto is on so start must use suggestion $g['rules']['grid_auto'] = true; $g = Engine::start($g, 'a', $nowG); expect($g['grid'] === 7, 'auto start uses suggested 7 for 2 players'); echo "Engine: rules time zone (host's IANA zone)\n"; expect(Engine::normalizeRules([])['tz'] === 'Europe/Amsterdam', 'tz default Europe/Amsterdam'); expect(Engine::normalizeRules(['tz' => 'America/New_York'])['tz'] === 'America/New_York', 'accepts America/New_York'); expect(Engine::normalizeRules(['tz' => 'Mars/Olympus'])['tz'] === 'Europe/Amsterdam', 'unknown zone → Amsterdam'); expect(Engine::normalizeRules(['tz' => '+02:00'])['tz'] === 'Europe/Amsterdam', 'raw offset rejected (no DST rules)'); expect(Engine::normalizeRules(['tz' => 'UTC'])['tz'] === 'UTC', 'UTC accepted'); // AP time 09:00 in New York → 14:00 UTC in November (EST), 13:00 UTC in July (EDT). $nov = (new DateTime('2026-11-14 12:00:00', new DateTimeZone('UTC')))->getTimestamp(); $nx = Engine::nextDistribution($nov, '09:00', 'America/New_York'); expect(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)); $jul = (new DateTime('2026-07-14 12:00:00', new DateTimeZone('UTC')))->getTimestamp(); $nx = Engine::nextDistribution($jul, '09:00', 'America/New_York'); expect(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)); // US DST starts 2026-03-08: day before vs after differ by one hour in UTC. $a1 = Engine::nextDistribution((new DateTime('2026-03-07 00:00', new DateTimeZone('UTC')))->getTimestamp(), '09:00', 'America/New_York'); $a2 = Engine::nextDistribution($a1, '09:00', 'America/New_York'); expect($a2 - $a1 === 23 * 3600, 'NY spring-forward: 09:00→09:00 is 23 real hours'); // A game uses its rules tz for distribution and cease-fire. $gT = game(['a', 'b'], ['tz' => 'Asia/Tokyo', 'ap_time' => '08:00', 'jury' => false, 'ceasefire' => true, 'ceasefire_ranges' => [['start' => '22:00', 'end' => '06:00']]], $nov); expect(gmdate('H:i', (int) $gT['next_distribution_at']) === '23:00', 'Tokyo 08:00 = 23:00 UTC'); $tokyo2330 = (new DateTime('2026-11-14 23:30', new DateTimeZone('Asia/Tokyo')))->getTimestamp(); $ams2330 = (new DateTime('2026-11-14 23:30', new DateTimeZone('Europe/Amsterdam')))->getTimestamp(); expect(Engine::ceasefireActive($gT, $tokyo2330), 'cease-fire active at 23:30 Tokyo'); expect(!Engine::ceasefireActive($gT, $ams2330), 'not active at 23:30 Amsterdam (07:30 Tokyo)'); $info = Engine::ceasefireInfo($gT, $tokyo2330); expect((new DateTime('@' . $info['until']))->setTimezone(new DateTimeZone('Asia/Tokyo'))->format('Y-m-d H:i') === '2026-11-15 06:00', 'Tokyo cease-fire ends 06:00 next day (midnight wrap)'); expect(Engine::present($gT, 'a', $nov)['timezone'] === 'Asia/Tokyo', 'present() reports the rules zone'); echo "Engine: no hearts, no buying lives (options do not exist)\n"; $n = Engine::normalizeRules(['hearts' => true, 'buy_lives' => true, 'buyLives' => true]); expect(!array_key_exists('hearts', $n) && !array_key_exists('buy_lives', $n) && !array_key_exists('buyLives', $n), 'hearts / buy-lives are not accepted rule keys'); echo "\nEngine passed: $passed Failed: $failed\n"; if (!defined('ALLIANTIES_TEST_SUITE')) { exit($failed > 0 ? 1 : 0); } $GLOBALS['engine_failed'] = $failed; $GLOBALS['engine_passed'] = $passed;