| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | require_once dirname(__DIR__) . '/lib/Engine.php'; |
| 4 | |
| 5 | $failed = 0; |
| 6 | $passed = 0; |
| 7 | |
| 8 | function 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 | |
| 20 | function 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 | |
| 31 | echo "Engine: grid and placement\n"; |
| 32 | expect(Engine::gridSize(2) === 6, '2 players → 6×6'); |
| 33 | expect(Engine::gridSize(4) === 6, '4 players → 6×6'); |
| 34 | expect(Engine::gridSize(8) === 7, '8 players → 7×7 (ceil sqrt(48)=7)'); |
| 35 | expect(Engine::gridSize(12) === 9, '12 players → 9×9 (ceil sqrt(72)=9)'); |
| 36 | expect(Engine::maxInset(6) === 1, '6×6 max inset 1 (leaves 4×4, min arena 3)'); |
| 37 | expect(Engine::maxInset(7) === 2, '7×7 max inset 2 (leaves 3×3)'); |
| 38 | $pos = Engine::startingPositions(4, 6); |
| 39 | expect(count($pos) === 4, '4 starting positions'); |
| 40 | $keys = array_map(fn ($p) => $p['x'] . ',' . $p['y'], $pos); |
| 41 | expect(count(array_unique($keys)) === 4, 'starting positions are unique'); |
| 42 | foreach ($pos as $p) { |
| 43 | expect(Engine::inBounds(6, $p['x'], $p['y']), "start in bounds {$p['x']},{$p['y']}"); |
| 44 | } |
| 45 | |
| 46 | echo "Engine: Chebyshev range\n"; |
| 47 | expect(Engine::chebyshev(0, 0, 2, 2) === 2, 'diagonal 2 is range 2'); |
| 48 | expect(Engine::chebyshev(0, 0, 2, 0) === 2, 'orthogonal 2 is range 2'); |
| 49 | expect(Engine::chebyshev(5, 5, 5, 5) === 0, 'same tile is 0'); |
| 50 | |
| 51 | echo "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); |
| 58 | 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')); |
| 59 | $dt2 = new DateTime('2026-11-14 00:00:00', $tz); |
| 60 | $next2 = Engine::nextMidnight($dt2->getTimestamp()); |
| 61 | $nx2 = (new DateTime('@' . $next2))->setTimezone($tz); |
| 62 | expect($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); |
| 67 | 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')); |
| 68 | |
| 69 | echo "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); |
| 72 | expect($g['status'] === 'lobby', 'new lobby'); |
| 73 | expect($g['rules']['jury'] === true, 'jury default ON'); |
| 74 | expect($g['rules']['shrinking_arena'] === false, 'shrink default OFF'); |
| 75 | expect($g['rules']['shrink_interval_days'] === 7, 'shrink interval default 7'); |
| 76 | expect($g['rules']['ceasefire'] === false, 'ceasefire default OFF'); |
| 77 | expect($g['rules']['ceasefire_ranges'] === [], 'ceasefire ranges empty by default'); |
| 78 | expect($g['rules']['vis_shots'] === 'public', 'vis_shots default public'); |
| 79 | expect($g['rules']['vis_gifts'] === 'public', 'vis_gifts default public'); |
| 80 | expect($g['rules']['vis_ap'] === 'public', 'vis_ap default public'); |
| 81 | expect($g['rules']['vis_range'] === 'public', 'vis_range default public'); |
| 82 | expect($g['rules']['vis_lives'] === 'public', 'vis_lives default public'); |
| 83 | expect($g['rules']['ap_loot'] === false, 'ap_loot default OFF'); |
| 84 | expect($g['rules']['ap_time'] === '00:00', 'ap_time default 00:00'); |
| 85 | expect($g['host_id'] === 'a', 'host is creator'); |
| 86 | try { |
| 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); |
| 93 | expect(count($g['players']) === 2, '2 players'); |
| 94 | try { |
| 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); |
| 101 | expect($g['status'] === 'running', 'started'); |
| 102 | expect($g['grid'] === 6, 'grid 6'); |
| 103 | expect($g['players'][0]['ap'] === 1, 'start with 1 AP'); |
| 104 | expect($g['players'][0]['lives'] === 3, 'start with 3 lives'); |
| 105 | expect($g['players'][0]['range'] === 2, 'start with range 2'); |
| 106 | expect($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); |
| 108 | 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')); |
| 109 | |
| 110 | echo "Engine: move, blocked, AP\n"; |
| 111 | $a = $g['players'][0]; |
| 112 | $legal = Engine::legal($g, 'a'); |
| 113 | expect(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); |
| 116 | expect($g2['players'][0]['x'] === $t['x'] && $g2['players'][0]['y'] === $t['y'], 'moved'); |
| 117 | expect($g2['players'][0]['ap'] === 0, 'spent 1 AP'); |
| 118 | expect(end($g2['events'])['type'] === 'move', 'move event'); |
| 119 | expect(end($g2['events'])['spend'] === true, 'move is a spend tick'); |
| 120 | try { |
| 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 | |
| 127 | echo "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'); |
| 137 | expect(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); |
| 139 | expect($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); |
| 142 | expect($g['players'][1]['alive'] === false, 'Bob is dead'); |
| 143 | expect($g['status'] === 'finished', 'game finished'); |
| 144 | expect($g['winner_id'] === 'a', 'Alice wins'); |
| 145 | $types = array_column($g['events'], 'type'); |
| 146 | expect(in_array('death', $types, true), 'death event'); |
| 147 | expect(in_array('win', $types, true), 'win event'); |
| 148 | |
| 149 | echo "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); |
| 158 | expect($g['players'][0]['ap'] === 2, 'Alice spent 2'); |
| 159 | expect($g['players'][1]['ap'] === 2, 'Bob received 2'); |
| 160 | try { |
| 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); |
| 167 | expect($g['players'][0]['range'] === 3, 'range is now 3'); |
| 168 | expect($g['players'][0]['ap'] === 1, 'upgrade costs 1'); |
| 169 | |
| 170 | echo "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'); |
| 178 | expect(!in_array('b', $legal['shoot'], true), 'Bob at (5,5) is out of range 2 from (0,0)'); |
| 179 | try { |
| 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; |
| 190 | foreach ($legal['move'] as $m) { |
| 191 | if ($m['x'] === 1 && $m['y'] === 0) { |
| 192 | $occ = true; |
| 193 | } |
| 194 | } |
| 195 | expect($occ === false, 'cannot move onto Bob'); |
| 196 | |
| 197 | echo "Engine: surrender\n"; |
| 198 | $g = game(['a', 'b'], [], $now); |
| 199 | $g = Engine::applyAction($g, 'b', ['type' => 'surrender'], $now); |
| 200 | expect($g['players'][1]['alive'] === false, 'Bob surrendered'); |
| 201 | expect($g['players'][1]['surrendered'] === true, 'flagged surrendered'); |
| 202 | expect($g['winner_id'] === 'a', 'Alice wins after surrender'); |
| 203 | |
| 204 | echo "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); |
| 210 | expect($g['day'] === 1, 'day is 1'); |
| 211 | expect($g['players'][0]['ap'] === 1, 'Alice got 1 AP'); |
| 212 | expect($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); |
| 217 | expect($g['day'] === 3, 'caught up to day 3, day=' . $g['day']); |
| 218 | expect($g['players'][0]['ap'] === 3, '3 AP after 3 days (started 0): ' . $g['players'][0]['ap']); |
| 219 | |
| 220 | echo "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; |
| 230 | expect($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); |
| 234 | expect($g['votes']['c'] === 'a', 'c voted a'); |
| 235 | // Vote is changeable |
| 236 | $g = Engine::applyAction($g, 'd', ['type' => 'vote', 'target' => 'b'], $now); |
| 237 | expect($g['votes']['d'] === 'b', 'd changed vote to b'); |
| 238 | // Living player cannot vote |
| 239 | try { |
| 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'); |
| 247 | expect($viewA['legal']['my_vote'] === null, 'Alice (living) has no vote'); |
| 248 | $viewC = Engine::present($g, 'c'); |
| 249 | expect($viewC['legal']['my_vote'] === 'a', 'Charlie sees own vote'); |
| 250 | expect(!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']); |
| 255 | expect($g['players'][0]['ap'] === 2, 'Alice got 1+1 (tie): ' . $g['players'][0]['ap']); |
| 256 | expect($g['players'][1]['ap'] === 2, 'Bob got 1+1 (tie): ' . $g['players'][1]['ap']); |
| 257 | expect($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']); |
| 265 | expect($g['players'][0]['ap'] === 2, 'Alice unique jury winner: 2'); |
| 266 | expect($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; |
| 271 | try { |
| 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 | |
| 278 | echo "Engine: shrinking arena\n"; |
| 279 | $g = game(['a', 'b'], ['shrinking_arena' => true, 'shrink_interval_days' => 2, 'jury' => false], $now); |
| 280 | expect($g['grid'] === 6, '6×6'); |
| 281 | expect(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']); |
| 292 | expect($g['day'] === 1, 'day 1'); |
| 293 | expect($g['arena_inset'] === 0, 'no shrink on day 1'); |
| 294 | expect($g['players'][0]['x'] === 0 && $g['players'][0]['y'] === 0, 'Alice still on (0,0)'); |
| 295 | expect($g['players'][0]['lives'] === 3, 'Alice still 3 lives'); |
| 296 | expect(Engine::daysUntilShrink($g) === 1, 'shrink in 1 day'); |
| 297 | // Day 2: shrink |
| 298 | $g = Engine::applyDue($g, $g['next_distribution_at']); |
| 299 | expect($g['day'] === 2, 'day 2'); |
| 300 | expect($g['arena_inset'] === 1, 'inset is 1'); |
| 301 | expect($g['players'][0]['lives'] === 2, 'Alice lost 1 life: ' . $g['players'][0]['lives']); |
| 302 | 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']); |
| 303 | expect(!($g['players'][0]['x'] === 2 && $g['players'][0]['y'] === 2), 'not onto Bob'); |
| 304 | expect($g['players'][1]['lives'] === 3, 'Bob unhurt in centre'); |
| 305 | expect($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'); |
| 308 | foreach ($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 |
| 314 | expect($g['arena_inset'] === 1, 'does not shrink past max inset: ' . $g['arena_inset']); |
| 315 | expect(Engine::daysUntilShrink($g) === null, 'no upcoming shrink at max'); |
| 316 | |
| 317 | echo "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']); |
| 326 | expect($g['players'][0]['alive'] === false, 'Alice died on shrink (1 life)'); |
| 327 | expect($g['players'][1]['alive'] === false, 'Bob died on shrink (1 life)'); |
| 328 | expect($g['status'] === 'finished', 'finished'); |
| 329 | expect($g['draw'] === true, 'draw when both die in the same shrink'); |
| 330 | |
| 331 | echo "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; |
| 338 | foreach ($viewA['players'] as $p) { |
| 339 | if ($p['id'] === 'b') { |
| 340 | $noteAonB = $p['note']; |
| 341 | } |
| 342 | } |
| 343 | foreach ($viewB['players'] as $p) { |
| 344 | if ($p['id'] === 'b') { |
| 345 | $noteBonB = $p['note']; |
| 346 | } |
| 347 | } |
| 348 | expect($noteAonB === 'looks dangerous', 'Alice sees her note on Bob'); |
| 349 | expect($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'); |
| 353 | expect(count($viewA['messages']) === 1 && $viewA['messages'][0]['text'] === 'hello', 'Alice sees chat'); |
| 354 | expect(count($viewB['messages']) === 1, 'Bob sees chat'); |
| 355 | // A third player would not — add c in lobby... already started. Fine. |
| 356 | |
| 357 | echo "Engine: modular rule flags\n"; |
| 358 | $r = Engine::normalizeRules(['jury' => '0', 'shrinking_arena' => '1', 'shrink_interval_days' => 99, 'ceasefire' => '1']); |
| 359 | expect($r['jury'] === false, 'jury "0" is false'); |
| 360 | expect($r['shrinking_arena'] === true, 'shrink "1" is true'); |
| 361 | expect($r['shrink_interval_days'] === 30, 'interval clamped to max 30: ' . $r['shrink_interval_days']); |
| 362 | expect($r['ceasefire'] === true, 'ceasefire "1" is true'); |
| 363 | |
| 364 | echo "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 | ]); |
| 371 | expect(count($ranges) === 2, '2 valid ranges: ' . count($ranges)); |
| 372 | expect($ranges[0]['start'] === '12:00', 'sorted, first is 12:00'); |
| 373 | expect($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); |
| 393 | expect($info !== null && $info['active'] === true, '23:30 is cease-fire (wrap)'); |
| 394 | $end = (new DateTime('@' . $info['until']))->setTimezone($tz); |
| 395 | 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')); |
| 396 | $legal = Engine::legal($g, 'a', $inCf); |
| 397 | expect($legal['shoot'] === [], 'Attack disabled during cease-fire'); |
| 398 | expect($legal['ceasefire'] === true, 'legal.ceasefire true'); |
| 399 | expect(count($legal['move']) >= 1 || $legal['upgrade'] || true, 'non-shoot actions still considered'); |
| 400 | // Move is still allowed |
| 401 | $legalMove = $legal['move']; |
| 402 | expect(!empty($legalMove), 'can still move during cease-fire'); |
| 403 | try { |
| 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); |
| 413 | expect($info['active'] === false, '06:00 exactly is not cease-fire (end exclusive)'); |
| 414 | $until = (new DateTime('@' . $info['until']))->setTimezone($tz); |
| 415 | expect($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); |
| 420 | expect($info['active'] === true, '12:30 is cease-fire'); |
| 421 | $end = (new DateTime('@' . $info['until']))->setTimezone($tz); |
| 422 | expect($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); |
| 427 | expect($info['active'] === false, '15:00 is free'); |
| 428 | $until = (new DateTime('@' . $info['until']))->setTimezone($tz); |
| 429 | expect($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); |
| 431 | expect(in_array('b', $legal['shoot'], true), 'can shoot when not in cease-fire'); |
| 432 | $g2 = Engine::applyAction($g, 'a', ['type' => 'shoot', 'target' => 'b'], $afternoon); |
| 433 | expect($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); |
| 438 | expect($info['active'] === false, '21:59 is free'); |
| 439 | expect($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); |
| 444 | expect($info['active'] === true, '05:59 still cease-fire'); |
| 445 | expect($info['seconds'] === 60, '60 seconds left in cease-fire: ' . $info['seconds']); |
| 446 | |
| 447 | echo "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); |
| 456 | expect($info['active'] === true, '01:00 CEST-transition day is cease-fire'); |
| 457 | $end = (new DateTime('@' . $info['until']))->setTimezone($tz); |
| 458 | expect($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; |
| 462 | expect($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); |
| 466 | expect($info['active'] === true, '03:00 (after the jump) is still cease-fire'); |
| 467 | expect($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); |
| 471 | expect($info['active'] === false, '00:30 is free'); |
| 472 | expect($info['seconds'] === 1800, '30 min until 01:00: ' . $info['seconds']); |
| 473 | |
| 474 | echo "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); |
| 485 | expect($info['active'] === true, '01:00 fall-back day is cease-fire'); |
| 486 | $end = (new DateTime('@' . $info['until']))->setTimezone($tz); |
| 487 | expect($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). |
| 490 | expect($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); |
| 495 | expect($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); |
| 499 | expect($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); |
| 503 | expect($info['active'] === false, '03:30 exactly is free (end exclusive)'); |
| 504 | |
| 505 | echo "Engine: cease-fire off or empty ranges\n"; |
| 506 | $gN = game(['a', 'b'], ['ceasefire' => false, 'ceasefire_ranges' => [['start' => '00:00', 'end' => '23:59']]], $now); |
| 507 | expect(Engine::ceasefireInfo($gN, $now) === null, 'rule off → no info (ranges ignored)'); |
| 508 | $gE = game(['a', 'b'], ['ceasefire' => true, 'ceasefire_ranges' => []], $now); |
| 509 | expect(Engine::ceasefireInfo($gE, $now) === null, 'on but empty ranges → treated as no cease-fire'); |
| 510 | |
| 511 | echo "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); |
| 523 | expect($end->format('H:i') === '13:00', 'merged overlapping ends at 13:00: ' . $end->format('H:i')); |
| 524 | |
| 525 | |
| 526 | echo "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 | ]); |
| 534 | expect($norm['vis_shots'] === 'public', 'bad vis_shots → public'); |
| 535 | expect($norm['vis_gifts'] === 'public', 'bad vis_gifts → public'); |
| 536 | expect($norm['vis_ap'] === 'public', 'bad vis_ap → public'); |
| 537 | expect($norm['vis_range'] === 'public', 'bad vis_range → public'); |
| 538 | expect($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 | ]); |
| 546 | expect($ok['vis_shots'] === 'elim_known_target', 'accepts elim_known_target'); |
| 547 | expect($ok['vis_gifts'] === 'anonymous', 'accepts anonymous'); |
| 548 | expect($ok['vis_ap'] === 'hidden', 'accepts hidden AP'); |
| 549 | expect($ok['vis_range'] === 'known_in_range', 'accepts known_in_range'); |
| 550 | expect($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 | */ |
| 558 | function 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 | |
| 583 | function visEvents(array $view, string $type): array |
| 584 | { |
| 585 | return array_values(array_filter($view['events'] ?? [], fn ($e) => ($e['type'] ?? '') === $type)); |
| 586 | } |
| 587 | |
| 588 | function 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 | |
| 598 | echo "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); |
| 604 | expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees public shot'); |
| 605 | expect(count(visEvents($vb, 'shoot')) === 1, 'target sees public shot'); |
| 606 | expect(count(visEvents($vc, 'shoot')) === 1, 'third party sees public shot'); |
| 607 | expect(($vc['events'] === visEvents($vc, 'shoot') ? true : isset(visEvents($vc, 'shoot')[0]['player'])) && visEvents($vc, 'shoot')[0]['player'] === 'a', 'third party sees shooter id'); |
| 608 | expect(visEvents($vc, 'shoot')[0]['lives_left'] === 2, 'public lives_left on shot'); |
| 609 | expect(visEvents($vc, 'shoot')[0]['damage'] === 1, 'damage is 1'); |
| 610 | expect(visPlayer($vc, 'b')['lives'] === 2, 'public lives in state'); |
| 611 | |
| 612 | echo "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); |
| 618 | expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees non-elim'); |
| 619 | expect(count(visEvents($vb, 'shoot')) === 1, 'target sees non-elim under elim_public'); |
| 620 | expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see non-elim'); |
| 621 | expect(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'); |
| 628 | expect(count($shotsC) === 1, 'third party sees only the eliminating shot: ' . count($shotsC)); |
| 629 | expect(!empty($shotsC[0]['killed']), 'the visible shot is the kill'); |
| 630 | expect(count(visEvents($vc, 'death')) === 1, 'third party sees death'); |
| 631 | expect((visEvents($vc, 'death')[0]['by'] ?? null) === 'a', 'elim_public death includes killer'); |
| 632 | expect((visEvents($vb, 'death')[0]['by'] ?? null) === 'a', 'target sees killer'); |
| 633 | expect(visPlayer($vc, 'b')['alive'] === false, 'death/alive is always public'); |
| 634 | |
| 635 | echo "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 |
| 639 | foreach ($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); |
| 648 | expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees known_target shot'); |
| 649 | expect(count(visEvents($vb, 'shoot')) === 1, 'target sees known_target shot'); |
| 650 | expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see known_target shot'); |
| 651 | $deaths = visEvents($vc, 'death'); |
| 652 | expect(count($deaths) === 1, 'third party still sees that B died'); |
| 653 | expect(!array_key_exists('by', $deaths[0]), 'killer stripped for third party under known_target'); |
| 654 | expect((visEvents($vb, 'death')[0]['by'] ?? null) === 'a', 'target still sees who shot them'); |
| 655 | |
| 656 | echo "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); |
| 662 | expect(count(visEvents($va, 'shoot')) === 1, 'shooter sees non-elim under elim_known_target'); |
| 663 | expect(count(visEvents($vb, 'shoot')) === 0, 'target does NOT see non-elim shot (must deduce)'); |
| 664 | expect(count(visEvents($vc, 'shoot')) === 0, 'third party does not see non-elim'); |
| 665 | expect(visPlayer($vb, 'b')['lives'] === 2, 'target still sees own lives drop'); |
| 666 | expect(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); |
| 672 | expect(count(visEvents($vb, 'shoot')) === 1, 'target sees only the eliminating shot'); |
| 673 | expect((visEvents($vb, 'shoot')[0]['player'] ?? null) === 'a', 'elim shot names the shooter for the target'); |
| 674 | expect(count(visEvents($vc, 'shoot')) === 0, 'third party never sees shots under elim_known_target'); |
| 675 | expect(!array_key_exists('by', visEvents($vc, 'death')[0]), 'third party death has no by'); |
| 676 | |
| 677 | echo "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); |
| 683 | expect(!array_key_exists('lives_left', visEvents($va, 'shoot')[0]), 'shooter does not get target lives_left'); |
| 684 | expect((visEvents($vb, 'shoot')[0]['lives_left'] ?? null) === 2, 'target sees own lives_left'); |
| 685 | expect(!array_key_exists('lives_left', visEvents($vc, 'shoot')[0]), 'third party does not get lives_left'); |
| 686 | expect(visEvents($va, 'shoot')[0]['damage'] === 1, 'shooter still sees damage 1'); |
| 687 | expect(visPlayer($va, 'a')['lives'] === 3, 'own lives always visible'); |
| 688 | expect(visPlayer($va, 'b')['lives'] === null, 'other lives hidden from shooter'); |
| 689 | expect(visPlayer($vc, 'b')['lives'] === null, 'other lives hidden from third party'); |
| 690 | expect(visPlayer($vb, 'b')['lives'] === 2, 'target sees own lives'); |
| 691 | expect(is_bool(visPlayer($vc, 'b')['alive']), 'alive flag still present when lives hidden'); |
| 692 | |
| 693 | echo "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'); |
| 698 | expect(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); |
| 705 | expect(count(visEvents($va, 'give')) === 1 && visEvents($va, 'give')[0]['player'] === 'a', 'giver sees own gift'); |
| 706 | expect(count(visEvents($vb, 'give')) === 1 && visEvents($vb, 'give')[0]['player'] === 'a', 'recipient sees giver'); |
| 707 | expect(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); |
| 715 | expect(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'); |
| 717 | expect(count($rec) === 1, 'recipient sees an anonymous gift event'); |
| 718 | expect(!empty($rec[0]['anonymous']), 'event marked anonymous'); |
| 719 | expect(!array_key_exists('player', $rec[0]), 'giver id stripped for recipient'); |
| 720 | expect(($rec[0]['amount'] ?? 0) === 2, 'recipient sees the AP amount'); |
| 721 | expect(($rec[0]['target'] ?? '') === 'b', 'recipient is named as target'); |
| 722 | expect(count(visEvents($vc, 'give')) === 0, 'third party does not see anonymous gift'); |
| 723 | expect(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; |
| 726 | foreach ($g['events'] as $e) { |
| 727 | if (($e['type'] ?? '') === 'give') { |
| 728 | $rawGive = $e; |
| 729 | } |
| 730 | } |
| 731 | expect($rawGive !== null && ($rawGive['player'] ?? null) === 'a', 'stored event still has the giver (present does not mutate)'); |
| 732 | expect(!array_key_exists('player', $rec[0]) || $rec[0]['player'] !== 'a' || true, 'sanity'); |
| 733 | expect(($rawGive['player'] ?? null) === 'a' && !array_key_exists('player', $rec[0]), 'clone is independent of stored event'); |
| 734 | |
| 735 | echo "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); |
| 739 | expect(visPlayer($va, 'a')['ap'] === 5, 'own AP visible when hidden rule is on'); |
| 740 | expect(visPlayer($va, 'b')['ap'] === null, 'other AP hidden'); |
| 741 | expect(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'); |
| 747 | expect(count($dailies) === 1, 'daily event present'); |
| 748 | expect(array_keys($dailies[0]['grants']) === ['a'], 'hidden AP: only own grant in daily event: ' . json_encode($dailies[0]['grants'])); |
| 749 | expect(!isset($dailies[0]['jury']['counts']), 'jury vote counts stripped'); |
| 750 | |
| 751 | echo "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); |
| 756 | expect(visPlayer($va, 'a')['range'] === 3, 'own range after upgrade'); |
| 757 | expect(visPlayer($vc, 'a')['range'] === null, 'other range hidden'); |
| 758 | expect(count(visEvents($va, 'upgrade')) === 1, 'upgrader sees own upgrade'); |
| 759 | expect(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. |
| 766 | expect(visPlayer($vb, 'a')['range'] === 2, 'B is within A’s range, sees A’s range'); |
| 767 | expect(visPlayer($va, 'b')['range'] === 2, 'A is within B’s range, sees B’s range'); |
| 768 | expect(visPlayer($vc, 'a')['range'] === null, 'C is outside A’s range (dist 3 > 2)'); |
| 769 | expect(visPlayer($va, 'c')['range'] === null, 'A is outside C’s range (dist 3 > 2)'); |
| 770 | expect(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); |
| 775 | expect(visPlayer($vc, 'a')['range'] === 3, 'after upgrade, C is within A’s range and sees it'); |
| 776 | expect(count(visEvents($vc, 'upgrade')) === 1, 'C now also sees the upgrade event'); |
| 777 | expect(visPlayer($va, 'c')['range'] === null, 'A is still outside C’s range 2'); |
| 778 | |
| 779 | echo "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'); |
| 791 | expect(count($moves) === 1, 'move is public'); |
| 792 | expect($moves[0]['player'] === 'a' && $moves[0]['to']['x'] === 2 && $moves[0]['to']['y'] === 3, 'move names the player and destination'); |
| 793 | expect(visPlayer($vc, 'a')['x'] === 2 && visPlayer($vc, 'a')['y'] === 3, 'new position is in the public state'); |
| 794 | expect(visPlayer($vc, 'a')['ap'] === null, 'AP still hidden after the move'); |
| 795 | expect(visPlayer($vc, 'a')['lives'] === null, 'lives still hidden'); |
| 796 | expect(visPlayer($vc, 'a')['range'] === null, 'range still hidden'); |
| 797 | |
| 798 | echo "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); |
| 802 | expect(visPlayer($vc, 'a')['ap'] === null, 'JSON-facing ap is null'); |
| 803 | expect(visPlayer($vc, 'a')['lives'] === null, 'JSON-facing lives is null'); |
| 804 | expect(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); |
| 808 | foreach ($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 | |
| 816 | echo "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'); |
| 821 | expect($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'); |
| 825 | expect($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'); |
| 829 | expect($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(); |
| 832 | expect(Engine::nextDistribution($eve, '00:00') === Engine::nextMidnight($eve), '00:00 path equals nextMidnight'); |
| 833 | expect(Engine::normalizeRules(['ap_time' => '7:05'])['ap_time'] === '07:05', 'ap_time normalised to HH:MM'); |
| 834 | expect(Engine::normalizeRules(['ap_time' => 'nope'])['ap_time'] === '00:00', 'bad ap_time → 00:00'); |
| 835 | expect(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). |
| 842 | expect($gotDt->getTimestamp() > $before, 'DST spring: 02:30 candidate is strictly after 01:30'); |
| 843 | expect($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'); |
| 847 | expect($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); |
| 852 | expect($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 | |
| 855 | echo "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); |
| 858 | expect((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')); |
| 861 | expect((int) $g2['day'] === 1, 'day increments at 15:00'); |
| 862 | expect((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); |
| 865 | expect((int) $g3['day'] === 0, 'one second before 15:00: no grant'); |
| 866 | |
| 867 | echo "Engine: AP loot default off; on only for eliminating shots\n"; |
| 868 | [$g, $t] = visGame(['ap_loot' => false], $now); |
| 869 | foreach ($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'); |
| 876 | expect(empty($b['alive']), 'B is dead'); |
| 877 | expect((int) $b['ap'] === 0, 'dead B has 0 AP'); |
| 878 | expect((int) $a['ap'] === 2, 'loot off: A spent 1, did not receive B’s 4 (has 2)'); |
| 879 | $shoot = null; |
| 880 | foreach ($g['events'] as $e) { if (($e['type'] ?? '') === 'shoot' && !empty($e['killed'])) { $shoot = $e; } } |
| 881 | expect(!isset($shoot['loot']), 'loot off: no loot field on the kill shot'); |
| 882 | |
| 883 | [$g, $t] = visGame(['ap_loot' => true], $now); |
| 884 | foreach ($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'); |
| 891 | expect(empty($b['alive']) && (int) $b['ap'] === 0, 'loot on: B dead, AP zeroed'); |
| 892 | expect((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] ?? []; |
| 897 | expect(($sa['loot'] ?? 0) === 4, 'shooter sees loot amount'); |
| 898 | expect(($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); |
| 902 | foreach ($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); |
| 909 | expect((visEvents($va, 'shoot')[0]['loot'] ?? 0) === 4, 'shooter sees loot even if AP totals hidden'); |
| 910 | expect(!array_key_exists('loot', visEvents($vc, 'shoot')[0]), 'third party does not see loot amount when AP is hidden'); |
| 911 | expect(visPlayer($va, 'a')['ap'] === 6, 'shooter sees own AP including loot'); |
| 912 | expect(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); |
| 916 | foreach ($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); |
| 921 | expect((int) Engine::player($g, 'a')['ap'] === 2, 'non-elim: no loot'); |
| 922 | expect((int) Engine::player($g, 'b')['ap'] === 4, 'non-elim: B keeps AP'); |
| 923 | $g = Engine::applyAction($g, 'b', ['type' => 'surrender'], $t); |
| 924 | expect(empty(Engine::player($g, 'b')['alive']), 'B surrendered'); |
| 925 | expect((int) Engine::player($g, 'a')['ap'] === 2, 'surrender does not transfer AP'); |
| 926 | expect((int) Engine::player($g, 'b')['ap'] === 0, 'surrendered AP is zeroed, not looted'); |
| 927 | |
| 928 | echo "Engine: no hearts, no buying lives (options do not exist)\n"; |
| 929 | $n = Engine::normalizeRules(['hearts' => true, 'buy_lives' => true, 'buyLives' => true]); |
| 930 | expect(!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 | |
| 934 | echo "\nEngine passed: $passed Failed: $failed\n"; |
| 935 | if (!defined('ALLIANTIES_TEST_SUITE')) { |
| 936 | exit($failed > 0 ? 1 : 0); |
| 937 | } |
| 938 | $GLOBALS['engine_failed'] = $failed; |
| 939 | $GLOBALS['engine_passed'] = $passed; |
projects-timfalken-com allianties