| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | // php server/tests/run.php |
| 4 | define('METEORS_NO_SESSION', true); |
| 5 | require dirname(__DIR__) . '/lib/bootstrap.php'; |
| 6 | require dirname(__DIR__) . '/lib/Store.php'; |
| 7 | |
| 8 | $failed = 0; |
| 9 | $passed = 0; |
| 10 | |
| 11 | function expect(bool $cond, string $msg): void |
| 12 | { |
| 13 | global $failed, $passed; |
| 14 | if ($cond) { |
| 15 | $passed++; |
| 16 | echo " ok $msg\n"; |
| 17 | } else { |
| 18 | $failed++; |
| 19 | echo " FAIL $msg\n"; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | function almostEqual(float $a, float $b, float $eps = 1e-6): bool |
| 24 | { |
| 25 | return abs($a - $b) <= $eps; |
| 26 | } |
| 27 | |
| 28 | $tmp = sys_get_temp_dir() . '/meteors-test-' . bin2hex(random_bytes(6)); |
| 29 | mkdir($tmp, 0775, true); |
| 30 | putenv('METEORS_DATA_DIR=' . $tmp); |
| 31 | |
| 32 | echo "Store: lobby and game\n"; |
| 33 | $store = Store::open($tmp); |
| 34 | |
| 35 | $a = $store->createLobby('local:test_user_1', 'test_user_1', 0.30); |
| 36 | expect(isset($a['id'], $a['code']), 'create lobby returns id and code'); |
| 37 | expect($a['status'] === 'waiting', 'new lobby is waiting'); |
| 38 | expect(strlen($a['code']) === 4, 'code is 4 characters'); |
| 39 | expect(almostEqual((float) $a['miss_chance'], 0.30), 'default miss chance 0.30'); |
| 40 | |
| 41 | $view = $store->viewFor('local:test_user_1'); |
| 42 | expect($view['lobby']['players'][0]['is_host'] === true, 'creator is host'); |
| 43 | expect($view['lobby']['can_start'] === false, 'cannot start alone'); |
| 44 | |
| 45 | try { |
| 46 | $store->startGame('local:test_user_1'); |
| 47 | expect(false, 'start alone should throw'); |
| 48 | } catch (RuntimeException $e) { |
| 49 | expect(str_contains($e->getMessage(), 'twee'), 'start alone is refused: ' . $e->getMessage()); |
| 50 | } |
| 51 | |
| 52 | $joined = $store->joinLobby('local:test_user_2', 'test_user_2', $a['code']); |
| 53 | expect($joined['id'] === $a['id'], 'second player joins by code'); |
| 54 | $view = $store->viewFor('local:test_user_1'); |
| 55 | expect(count($view['lobby']['players']) === 2, 'two players in lobby'); |
| 56 | expect($view['lobby']['can_start'] === false, 'cannot start until both ready'); |
| 57 | |
| 58 | $store->setReady('local:test_user_1', true); |
| 59 | $store->setReady('local:test_user_2', true); |
| 60 | $view = $store->viewFor('local:test_user_1'); |
| 61 | expect($view['lobby']['all_ready'] === true, 'all ready'); |
| 62 | expect($view['lobby']['can_start'] === true, 'host can start when all ready and 2+'); |
| 63 | |
| 64 | $view2 = $store->viewFor('local:test_user_2'); |
| 65 | expect($view2['lobby']['can_start'] === false, 'non-host cannot start'); |
| 66 | |
| 67 | try { |
| 68 | $store->startGame('local:test_user_2'); |
| 69 | expect(false, 'non-host start should throw'); |
| 70 | } catch (RuntimeException $e) { |
| 71 | expect(str_contains($e->getMessage(), 'host'), 'non-host start refused'); |
| 72 | } |
| 73 | |
| 74 | $started = $store->startGame('local:test_user_1'); |
| 75 | expect($started['status'] === 'playing', 'game starts'); |
| 76 | |
| 77 | // Shots: force miss by using miss_chance 1.0 on a fresh lobby later. |
| 78 | // Here miss is 0.30 — fire many shots and check some hits and some misses, or set chance. |
| 79 | $results = $store->recordShots('local:test_user_1', [ |
| 80 | ['client_id' => 'c1', 'color' => 2], |
| 81 | ['client_id' => 'c2', 'color' => -1], |
| 82 | ]); |
| 83 | expect(count($results) === 2, 'two shot results'); |
| 84 | expect(isset($results[0]['miss'], $results[0]['flight_duration']), 'result has miss and duration'); |
| 85 | expect(almostEqual($results[0]['flight_duration'], METEORS_FLIGHT_DURATION), 'flight duration 1.2'); |
| 86 | |
| 87 | // Incoming is delayed until deliver_at. |
| 88 | $inc = $store->viewFor('local:test_user_2'); |
| 89 | $hits = array_filter($results, fn ($r) => !$r['miss']); |
| 90 | if ($hits) { |
| 91 | expect(count($inc['incoming']) === 0, 'incoming not delivered before flight duration'); |
| 92 | } else { |
| 93 | expect(true, 'all missed — no incoming expected'); |
| 94 | } |
| 95 | |
| 96 | // Fast-forward: rewrite deliver_at in the sqlite file. |
| 97 | $pdo = new PDO('sqlite:' . $tmp . '/meteors.sqlite'); |
| 98 | $pdo->exec('UPDATE shots SET deliver_at = 0 WHERE to_user = "local:test_user_2"'); |
| 99 | $inc = $store->viewFor('local:test_user_2'); |
| 100 | $expectedHits = count($hits); |
| 101 | expect(count($inc['incoming']) === $expectedHits, 'incoming delivered after deliver_at (' . count($inc['incoming']) . " vs $expectedHits)"); |
| 102 | $inc2 = $store->viewFor('local:test_user_2'); |
| 103 | expect(count($inc2['incoming']) === 0, 'incoming is delivered only once'); |
| 104 | |
| 105 | // Elimination and podium. |
| 106 | $store->updateSnapshot('local:test_user_2', ['score' => 3, 'blocks' => []], 3, true); |
| 107 | $view = $store->viewFor('local:test_user_1'); |
| 108 | expect($view['lobby']['status'] === 'finished', 'game finishes when one remains'); |
| 109 | $ranks = $view['lobby']['rankings']; |
| 110 | expect(count($ranks) >= 2, 'rankings present'); |
| 111 | $first = $ranks[0]; |
| 112 | expect($first['place'] === 1 && $first['id'] === 'local:test_user_1', 'survivor is 1st'); |
| 113 | $second = $ranks[1]; |
| 114 | expect($second['place'] === 2 && $second['id'] === 'local:test_user_2', 'eliminated is 2nd'); |
| 115 | |
| 116 | // Podium wait: not yet reset. |
| 117 | $view = $store->viewFor('local:test_user_1'); |
| 118 | expect($view['lobby']['status'] === 'finished', 'still finished during podium'); |
| 119 | |
| 120 | $pdo->exec('UPDATE lobbies SET finished = ' . (time() - 6)); |
| 121 | $view = $store->viewFor('local:test_user_1'); |
| 122 | expect($view['lobby']['status'] === 'waiting', 'back to lobby after podium seconds'); |
| 123 | expect($view['lobby']['players'][0]['ready'] === false, 'ready flags reset'); |
| 124 | |
| 125 | // Host leave transfers host. |
| 126 | $store->leaveLobby('local:test_user_1'); |
| 127 | $view = $store->viewFor('local:test_user_2'); |
| 128 | expect($view['lobby']['host_id'] === 'local:test_user_2', 'host transfers to remaining player'); |
| 129 | |
| 130 | // Mid-game leave counts as elimination. |
| 131 | $store->leaveLobby('local:test_user_2'); |
| 132 | $b = $store->createLobby('local:test_user_1', 'test_user_1', 0.0); |
| 133 | $store->joinLobby('local:test_user_2', 'test_user_2', $b['code']); |
| 134 | $store->setReady('local:test_user_1', true); |
| 135 | $store->setReady('local:test_user_2', true); |
| 136 | $store->startGame('local:test_user_1'); |
| 137 | $store->leaveLobby('local:test_user_2'); |
| 138 | $view = $store->viewFor('local:test_user_1'); |
| 139 | expect($view['lobby']['status'] === 'finished', 'leave mid-game finishes if one remains'); |
| 140 | expect($view['lobby']['rankings'][0]['id'] === 'local:test_user_1', 'stayer wins'); |
| 141 | |
| 142 | // Miss chance 1.0: all miss. Need a fresh game with 2 players. |
| 143 | // Leave leftover memberships. |
| 144 | $store->leaveLobby('local:test_user_1'); |
| 145 | $c = $store->createLobby('local:test_user_1', 'test_user_1', 1.0); |
| 146 | $store->joinLobby('local:test_user_2', 'test_user_2', $c['code']); |
| 147 | $store->setReady('local:test_user_1', true); |
| 148 | $store->setReady('local:test_user_2', true); |
| 149 | $store->startGame('local:test_user_1'); |
| 150 | $store->testMissOverride = 1.0; |
| 151 | $allMiss = $store->recordShots('local:test_user_1', [ |
| 152 | ['client_id' => 'm1', 'color' => 0], |
| 153 | ['client_id' => 'm2', 'color' => 1], |
| 154 | ['client_id' => 'm3', 'color' => 2], |
| 155 | ]); |
| 156 | $store->testMissOverride = null; |
| 157 | expect(count($allMiss) === 3 && array_reduce($allMiss, fn ($ok, $r) => $ok && $r['miss'], true), 'forced miss 1.0 → all miss'); |
| 158 | |
| 159 | $d = null; |
| 160 | $store->leaveLobby('local:test_user_1'); |
| 161 | $store->leaveLobby('local:test_user_2'); |
| 162 | $d = $store->createLobby('local:test_user_1', 'test_user_1', 0.0); |
| 163 | $store->joinLobby('local:test_user_2', 'test_user_2', $d['code']); |
| 164 | $store->setReady('local:test_user_1', true); |
| 165 | $store->setReady('local:test_user_2', true); |
| 166 | $store->startGame('local:test_user_1'); |
| 167 | $store->testMissOverride = 0.0; |
| 168 | $allHit = $store->recordShots('local:test_user_1', [ |
| 169 | ['client_id' => 'h1', 'color' => 0], |
| 170 | ['client_id' => 'h2', 'color' => 1], |
| 171 | ]); |
| 172 | $store->testMissOverride = null; |
| 173 | expect(count($allHit) === 2 && array_reduce($allHit, fn ($ok, $r) => $ok && !$r['miss'] && $r['target_id'] === 'local:test_user_2', true), 'forced miss 0 → all hit the other player'); |
| 174 | |
| 175 | // Eliminated player is not a target. |
| 176 | $store->updateSnapshot('local:test_user_2', null, 0, true); |
| 177 | $none = $store->recordShots('local:test_user_1', [['client_id' => 'x', 'color' => 0]]); |
| 178 | // Game may already be finished; recordShots on finished returns []. |
| 179 | expect($none === [] || $none[0]['miss'] === true, 'no living target after opponent is out'); |
| 180 | |
| 181 | |
| 182 | echo "\nPlanets and per-sender Power miss\n"; |
| 183 | expect(count(Planets::all()) >= 8 && count(Planets::all()) <= 12, 'catalog has 8–12 planets'); |
| 184 | expect(Planets::known('terra') && Planets::known('cinder') && Planets::known('aurel'), 'terra, cinder, aurel exist'); |
| 185 | expect(almostEqual(Planets::power(Planets::byId('aurel')), 0.0), 'Aurel power 0'); |
| 186 | expect(almostEqual(Planets::missChance(Planets::byId('aurel')), 0.60), 'Aurel miss 60%'); |
| 187 | expect(almostEqual(Planets::power(Planets::byId('cinder')), 1.0), 'Cinder power 1'); |
| 188 | expect(almostEqual(Planets::missChance(Planets::byId('cinder')), 0.05), 'Cinder miss 5%'); |
| 189 | $td = Planets::displayValues(Planets::byId('terra')); |
| 190 | expect(almostEqual($td['gravity'], 9.81) && almostEqual($td['density'], 1.05) && (int) round($td['newtons']) === 104 && $td['power'] === 44, 'Terra display g/d/F/Power'); |
| 191 | expect(Planets::displayPower(Planets::byId('aurel')) === 0, 'Aurel displayed Power 0'); |
| 192 | expect(Planets::displayPower(Planets::byId('cinder')) === 92, 'Cinder displayed Power 92 (5% miss)'); |
| 193 | expect(Planets::displayValues(Planets::byId('cinder'))['power'] === 92, 'Cinder displayValues Power 92'); |
| 194 | |
| 195 | $store->leaveLobby('local:test_user_1'); |
| 196 | $store->leaveLobby('local:test_user_2'); |
| 197 | $pl = $store->createLobby('local:test_user_1', 'test_user_1', 0.30, 'aurel'); |
| 198 | $store->joinLobby('local:test_user_2', 'test_user_2', $pl['code'], 'cinder'); |
| 199 | $presented = $store->presentLobby($pl, 'local:test_user_1'); |
| 200 | $byId = []; |
| 201 | foreach ($presented['players'] as $plr) { $byId[$plr['id']] = $plr; } |
| 202 | expect($byId['local:test_user_1']['planet']['id'] === 'aurel', 'host planet aurel'); |
| 203 | expect($byId['local:test_user_2']['planet']['id'] === 'cinder', 'guest planet cinder'); |
| 204 | expect(almostEqual($byId['local:test_user_1']['planet']['miss_chance'], 0.60), 'host miss from Aurel Power'); |
| 205 | expect(almostEqual($byId['local:test_user_2']['planet']['miss_chance'], 0.05), 'guest miss from Cinder Power'); |
| 206 | |
| 207 | $store->setPlanet('local:test_user_1', 'wisp'); |
| 208 | $presented = $store->presentLobby($store->getLobby($pl['id']), 'local:test_user_1'); |
| 209 | foreach ($presented['players'] as $plr) { |
| 210 | if ($plr['is_me']) { |
| 211 | expect($plr['planet']['id'] === 'wisp', 'setPlanet updates host to Wisp'); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | try { |
| 216 | $store->setPlanet('local:test_user_1', 'no-such-world'); |
| 217 | expect(false, 'unknown planet must throw'); |
| 218 | } catch (RuntimeException $e) { |
| 219 | expect(true, 'unknown planet rejected'); |
| 220 | } |
| 221 | |
| 222 | $store->setReady('local:test_user_1', true); |
| 223 | $store->setReady('local:test_user_2', true); |
| 224 | $store->startGame('local:test_user_1'); |
| 225 | $me = $store->member($pl['id'], 'local:test_user_1'); |
| 226 | expect(almostEqual($store->missChanceForMember($me), Planets::missChanceForId('wisp')), 'in-game miss uses the sender planet'); |
| 227 | $guest = $store->member($pl['id'], 'local:test_user_2'); |
| 228 | expect(almostEqual($store->missChanceForMember($guest), 0.05), 'guest still uses Cinder Power'); |
| 229 | try { |
| 230 | $store->setPlanet('local:test_user_1', 'terra'); |
| 231 | expect(false, 'setPlanet during play must throw'); |
| 232 | } catch (RuntimeException $e) { |
| 233 | expect(true, 'planet locked once the match starts'); |
| 234 | } |
| 235 | $store->leaveLobby('local:test_user_1'); |
| 236 | $store->leaveLobby('local:test_user_2'); |
| 237 | |
| 238 | echo "\nshot_flights (t1125)\n"; |
| 239 | $e = $store->createLobby('local:test_user_1', 'test_user_1', 0.0); |
| 240 | $store->joinLobby('local:test_user_2', 'test_user_2', $e['code']); |
| 241 | $store->joinLobby('local:test_user_3', 'test_user_3', $e['code']); |
| 242 | $store->setReady('local:test_user_1', true); |
| 243 | $store->setReady('local:test_user_2', true); |
| 244 | $store->setReady('local:test_user_3', true); |
| 245 | $store->startGame('local:test_user_1'); |
| 246 | $store->testMissOverride = 0.0; |
| 247 | $shots = $store->recordShots('local:test_user_1', [ |
| 248 | ['client_id' => 'f1', 'color' => 2, 'col' => 6, 'y' => 12.4], |
| 249 | ]); |
| 250 | $store->testMissOverride = null; |
| 251 | expect(count($shots) === 1, 'one recorded shot'); |
| 252 | expect(($shots[0]['from_id'] ?? null) === 'local:test_user_1', 'shot_results includes from_id'); |
| 253 | expect(isset($shots[0]['age']) && almostEqual((float) $shots[0]['age'], 0.0), 'own result age is 0'); |
| 254 | |
| 255 | $view3 = $store->viewFor('local:test_user_3'); |
| 256 | expect(isset($view3['shot_flights']) && is_array($view3['shot_flights']), 'viewFor exposes shot_flights'); |
| 257 | $found = null; |
| 258 | foreach ($view3['shot_flights'] as $f) { |
| 259 | if (($f['client_id'] ?? '') === 'f1') { |
| 260 | $found = $f; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | expect($found !== null, 'third player sees the shot in shot_flights'); |
| 265 | if ($found) { |
| 266 | expect($found['from_id'] === 'local:test_user_1', 'shot_flights.from_id is the shooter'); |
| 267 | expect($found['target_id'] === 'local:test_user_2' || $found['target_id'] === 'local:test_user_3', 'hit lands on a living opponent'); |
| 268 | expect($found['miss'] === false, 'forced hit is not a miss'); |
| 269 | expect($found['age'] >= 0 && $found['age'] < 2.0, 'age is seconds since created'); |
| 270 | expect(almostEqual((float) $found['flight_duration'], METEORS_FLIGHT_DURATION), 'flight_duration on shot_flights'); |
| 271 | expect(isset($found['land_col']) && $found['land_col'] >= 0 && $found['land_col'] <= 9, 'shot_flights has land_col 0–9'); |
| 272 | expect(($shots[0]['land_col'] ?? null) === $found['land_col'], 'recordShots and shot_flights share land_col'); |
| 273 | expect(($shots[0]['col'] ?? null) === 6, 'shot_results echoes launch col'); |
| 274 | expect(isset($shots[0]['y']) && abs((float) $shots[0]['y'] - 12.4) < 0.01, 'shot_results echoes launch y'); |
| 275 | expect(($found['col'] ?? null) === 6, 'shot_flights has launch col (t1136)'); |
| 276 | expect(isset($found['y']) && abs((float) $found['y'] - 12.4) < 0.01, 'shot_flights has launch y (t1136)'); |
| 277 | } |
| 278 | // Incoming carries the same land_col (t1126). |
| 279 | $targetId = $found['target_id'] ?? $shots[0]['target_id']; |
| 280 | $pdoLand = new PDO('sqlite:' . $tmp . '/meteors.sqlite'); |
| 281 | $pdoLand->exec('UPDATE shots SET deliver_at = 0 WHERE client_id = "f1"'); |
| 282 | $incLand = $store->viewFor($targetId); |
| 283 | $incHit = null; |
| 284 | foreach ($incLand['incoming'] as $row) { |
| 285 | if (($row['client_id'] ?? '') === 'f1') { |
| 286 | $incHit = $row; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | expect($incHit !== null, 'target receives incoming for the shot'); |
| 291 | if ($incHit) { |
| 292 | expect(($incHit['land_col'] ?? null) === $found['land_col'], 'incoming.land_col matches the flight'); |
| 293 | } |
| 294 | $view1 = $store->viewFor('local:test_user_1'); |
| 295 | expect(count($view1['shot_flights']) >= 1, 'shooter also sees shot_flights (client de-dupes)'); |
| 296 | $store->leaveLobby('local:test_user_1'); |
| 297 | $store->leaveLobby('local:test_user_2'); |
| 298 | $store->leaveLobby('local:test_user_3'); |
| 299 | |
| 300 | |
| 301 | echo "\nBlocks registry and unlock thresholds\n"; |
| 302 | require_once dirname(__DIR__) . '/lib/Blocks.php'; |
| 303 | $allBlocks = Blocks::all(); |
| 304 | expect(count($allBlocks) >= 48, 'registry has Terra + new icons (got ' . count($allBlocks) . ')'); |
| 305 | for ($i = 0; $i < 5; $i++) { |
| 306 | expect(Blocks::threshold($i) === 0, "terra index $i is free"); |
| 307 | } |
| 308 | expect(Blocks::threshold(5) === 500, 'first unlock is 500 points'); |
| 309 | $prev = 500; |
| 310 | for ($i = 6; $i < count($allBlocks); $i++) { |
| 311 | $t = Blocks::threshold($i); |
| 312 | if ($t <= $prev) { |
| 313 | expect(false, "threshold at $i should be > $prev, got $t"); |
| 314 | break; |
| 315 | } |
| 316 | $prev = $t; |
| 317 | } |
| 318 | expect($prev > 500, 'later thresholds grow'); |
| 319 | expect(Blocks::unlocked(0, 0) === true, 'terra unlocked at 0 points'); |
| 320 | expect(Blocks::unlocked(5, 499) === false, 'fehu locked at 499'); |
| 321 | expect(Blocks::unlocked(5, 500) === true, 'fehu unlocked at 500'); |
| 322 | $terraIds = []; |
| 323 | foreach ($allBlocks as $i => $b) { |
| 324 | if ($i >= 5) break; |
| 325 | $terraIds[] = $b['id']; |
| 326 | } |
| 327 | |
| 328 | echo "\nLifetime points, anti-cheat, custom planets\n"; |
| 329 | $ptsStore = Store::open($tmp); |
| 330 | $u = 'local:designer_a'; |
| 331 | $v = 'local:designer_b'; |
| 332 | expect($ptsStore->lifetimePoints($u) === 0, 'new profile starts at 0'); |
| 333 | $t1 = $ptsStore->awardPoints($u, 200, 'k-one', 'solo'); |
| 334 | expect($t1 === 200, 'first award adds 200'); |
| 335 | $t2 = $ptsStore->awardPoints($u, 200, 'k-one', 'solo'); |
| 336 | expect($t2 === 200, 'same award_key is idempotent'); |
| 337 | $t3 = $ptsStore->awardPoints($u, 50, 'k-two', 'offline'); |
| 338 | expect($t3 === 250, 'second key adds 50'); |
| 339 | |
| 340 | $ptsStore->testNow = 1_000_000; |
| 341 | $ses = $ptsStore->playBegin($u, 'solo'); |
| 342 | expect(isset($ses['token']) && strlen($ses['token']) === 32, 'playBegin returns a token'); |
| 343 | $ptsStore->testNow = 1_000_001; |
| 344 | $fin = $ptsStore->playFinish($u, $ses['token'], 9000); |
| 345 | expect($fin['awarded'] === 0, 'score with duration under 3s awards 0'); |
| 346 | expect($fin['lifetime_points'] === 250, 'lifetime unchanged after clamped zero'); |
| 347 | $again = $ptsStore->playFinish($u, $ses['token'], 9000); |
| 348 | expect(!empty($again['already']), 'second finish of same token is a no-op'); |
| 349 | |
| 350 | $ptsStore->testNow = 2_000_000; |
| 351 | $ses2 = $ptsStore->playBegin($u, 'offline'); |
| 352 | $ptsStore->testNow = 2_000_010; |
| 353 | $fin2 = $ptsStore->playFinish($u, $ses2['token'], 9000); |
| 354 | expect($fin2['awarded'] === 40, 'cap is 4 points/s over 10s (got ' . $fin2['awarded'] . ')'); |
| 355 | expect($fin2['lifetime_points'] === 290, 'capped award is added'); |
| 356 | |
| 357 | $ptsStore->testNow = 3_000_000; |
| 358 | $ses3 = $ptsStore->playBegin($u, 'solo'); |
| 359 | $ptsStore->testNow = 3_000_020; |
| 360 | $fin3 = $ptsStore->playFinish($u, $ses3['token'], 17); |
| 361 | expect($fin3['awarded'] === 17, 'honest score under the cap is kept'); |
| 362 | |
| 363 | try { |
| 364 | $ptsStore->saveCustomPlanet($u, [ |
| 365 | 'name' => 'Lockworld', 'people' => 'Lockers', |
| 366 | 'jumpsToClear' => 2, 'fallTime' => 3, 'riseTime' => 1.5, |
| 367 | 'colors' => array_merge($terraIds, ['rune-fehu']), |
| 368 | ]); |
| 369 | // 6 items? no that's 6. Use 4 terra + fehu |
| 370 | expect(false, 'should not reach (wrong colors count would throw first)'); |
| 371 | } catch (RuntimeException $e) { |
| 372 | expect(str_contains($e->getMessage(), 'vijf') || str_contains($e->getMessage(), 'ontgrendeld') || str_contains($e->getMessage(), 'Onbekend'), |
| 373 | 'invalid colors refused: ' . $e->getMessage()); |
| 374 | } |
| 375 | |
| 376 | try { |
| 377 | $ptsStore->saveCustomPlanet($u, [ |
| 378 | 'name' => 'Lockworld', 'people' => 'Lockers', |
| 379 | 'colors' => [$terraIds[0], $terraIds[1], $terraIds[2], $terraIds[3], 'rune-fehu'], |
| 380 | ]); |
| 381 | expect(false, 'locked block should throw'); |
| 382 | } catch (RuntimeException $e) { |
| 383 | expect(str_contains($e->getMessage(), 'ontgrendeld'), 'locked block refused: ' . $e->getMessage()); |
| 384 | } |
| 385 | |
| 386 | try { |
| 387 | $ptsStore->saveCustomPlanet($u, [ |
| 388 | 'name' => 'Ghost', 'people' => 'Ghosts', |
| 389 | 'colors' => [$terraIds[0], $terraIds[1], $terraIds[2], $terraIds[3], 'no-such-block'], |
| 390 | ]); |
| 391 | expect(false, 'unknown block should throw'); |
| 392 | } catch (RuntimeException $e) { |
| 393 | expect(str_contains($e->getMessage(), 'Onbekend'), 'unknown block refused: ' . $e->getMessage()); |
| 394 | } |
| 395 | |
| 396 | try { |
| 397 | $ptsStore->saveCustomPlanet($u, [ |
| 398 | 'name' => 'Twins', 'people' => 'Twins', |
| 399 | 'colors' => [$terraIds[0], $terraIds[1], $terraIds[2], $terraIds[3], $terraIds[0]], |
| 400 | ]); |
| 401 | expect(false, 'duplicate block should throw'); |
| 402 | } catch (RuntimeException $e) { |
| 403 | expect(str_contains($e->getMessage(), 'één vak') || str_contains($e->getMessage(), 'vak'), |
| 404 | 'duplicate block refused: ' . $e->getMessage()); |
| 405 | } |
| 406 | |
| 407 | $snap = $ptsStore->saveCustomPlanet($u, [ |
| 408 | 'name' => 'My World', 'people' => 'My People', |
| 409 | 'jumpsToClear' => 2.5, 'fallTime' => 2, 'riseTime' => 1, |
| 410 | 'colors' => $terraIds, |
| 411 | ]); |
| 412 | expect(!empty($snap['id']) && str_starts_with($snap['id'], 'c_'), 'custom id is c_ + hex'); |
| 413 | expect($snap['custom'] === true, 'snapshot is marked custom'); |
| 414 | expect($snap['name'] === 'My World', 'name stored'); |
| 415 | expect(count($snap['colors']) === 5, 'five colors in snapshot'); |
| 416 | expect($snap['colors'][0]['block'] === $terraIds[0], 'color has registry block id'); |
| 417 | expect($snap['owner_id'] === $u, 'owner_id set'); |
| 418 | |
| 419 | $listed = $ptsStore->listCustomPlanets($u); |
| 420 | expect(count($listed) === 1, 'owner lists the planet'); |
| 421 | expect($ptsStore->listCustomPlanets($v) === [], 'other user has no planets'); |
| 422 | |
| 423 | try { |
| 424 | $ptsStore->setPlanet($u, $snap['id']); |
| 425 | expect(false, 'setPlanet without a lobby should throw'); |
| 426 | } catch (RuntimeException $e) { |
| 427 | expect(true, 'setPlanet requires a lobby: ' . $e->getMessage()); |
| 428 | } |
| 429 | |
| 430 | $lobby = $ptsStore->createLobby($u, 'A', 0.3, $snap['id']); |
| 431 | $view = $ptsStore->viewFor($u); |
| 432 | $meP = $view['lobby']['players'][0]['planet'] ?? null; |
| 433 | expect($meP && $meP['id'] === $snap['id'], 'createLobby stores custom planet'); |
| 434 | expect(!empty($meP['custom']), 'presented planet is custom'); |
| 435 | expect(count($meP['colors']) === 5 && $meP['colors'][0]['image'] !== '', 'presented colors include images'); |
| 436 | expect(isset($meP['miss_chance']) && is_float($meP['miss_chance']), 'server sends miss_chance for custom'); |
| 437 | |
| 438 | $ptsStore->joinLobby($v, 'B', $lobby['code']); |
| 439 | $viewB = $ptsStore->viewFor($v); |
| 440 | $seen = null; |
| 441 | foreach ($viewB['lobby']['players'] as $plr) { |
| 442 | if ($plr['planet']['id'] === $snap['id']) { $seen = $plr['planet']; } |
| 443 | } |
| 444 | expect($seen !== null, 'other player sees the custom planet snapshot'); |
| 445 | expect(($seen['colors'][2]['block'] ?? '') === $terraIds[2], 'opponent sees block ids'); |
| 446 | |
| 447 | try { |
| 448 | $ptsStore->setPlanet($v, $snap['id']); |
| 449 | expect(false, 'cannot pick someone else\'s custom planet'); |
| 450 | } catch (RuntimeException $e) { |
| 451 | expect(str_contains($e->getMessage(), 'Onbekende'), 'foreign custom refused: ' . $e->getMessage()); |
| 452 | } |
| 453 | |
| 454 | $ptsStore->leaveLobby($u); |
| 455 | $ptsStore->leaveLobby($v); |
| 456 | $ptsStore->deleteCustomPlanet($u, $snap['id']); |
| 457 | expect($ptsStore->listCustomPlanets($u) === [], 'deleted planet is gone'); |
| 458 | |
| 459 | // Snapshot survives mid-match edits: create, join, start, then edit/delete, opponent still sees original. |
| 460 | $snap2 = $ptsStore->saveCustomPlanet($u, [ |
| 461 | 'name' => 'Frozen', 'people' => 'Ice', |
| 462 | 'jumpsToClear' => 3, 'fallTime' => 2, 'riseTime' => 1, |
| 463 | 'colors' => $terraIds, |
| 464 | ]); |
| 465 | $lobby2 = $ptsStore->createLobby($u, 'A', 0.3, $snap2['id']); |
| 466 | $ptsStore->joinLobby($v, 'B', $lobby2['code']); |
| 467 | $ptsStore->setReady($u, true); |
| 468 | $ptsStore->setReady($v, true); |
| 469 | $ptsStore->startGame($u); |
| 470 | $ptsStore->saveCustomPlanet($u, [ |
| 471 | 'id' => $snap2['id'], |
| 472 | 'name' => 'Changed', 'people' => 'Ice', |
| 473 | 'jumpsToClear' => 1, 'fallTime' => 5, 'riseTime' => 3, |
| 474 | 'colors' => $terraIds, |
| 475 | ]); |
| 476 | $viewMid = $ptsStore->viewFor($v); |
| 477 | $midP = null; |
| 478 | foreach ($viewMid['lobby']['players'] as $plr) { |
| 479 | if (($plr['planet']['id'] ?? '') === $snap2['id']) { $midP = $plr['planet']; } |
| 480 | } |
| 481 | expect($midP && $midP['name'] === 'Frozen', 'in-match snapshot keeps the original name (got ' . ($midP['name'] ?? '?') . ')'); |
| 482 | expect($midP && almostEqual((float) $midP['jumpsToClear'], 3.0), 'in-match snapshot keeps original gravity'); |
| 483 | $ptsStore->leaveLobby($u); |
| 484 | $ptsStore->leaveLobby($v); |
| 485 | |
| 486 | // Online award is idempotent and server-side. |
| 487 | $u3 = 'local:online_award_a'; |
| 488 | $v3 = 'local:online_award_b'; |
| 489 | $lob = $ptsStore->createLobby($u3, 'A', 0.0, 'terra'); |
| 490 | $ptsStore->joinLobby($v3, 'B', $lob['code']); |
| 491 | $ptsStore->setReady($u3, true); |
| 492 | $ptsStore->setReady($v3, true); |
| 493 | $ptsStore->startGame($u3); |
| 494 | $ptsStore->viewFor($u3, null, 80, false, []); |
| 495 | $ptsStore->viewFor($u3, null, 80, true, []); |
| 496 | expect($ptsStore->lifetimePoints($u3) === 80, 'online elimination awards the score'); |
| 497 | $ptsStore->viewFor($u3, null, 999, true, []); |
| 498 | expect($ptsStore->lifetimePoints($u3) === 80, 'online award_key is idempotent'); |
| 499 | |
| 500 | $ptsStore->setLocale($u, 'nl'); |
| 501 | expect($ptsStore->locale($u) === 'nl', 'setLocale stores nl'); |
| 502 | try { |
| 503 | $ptsStore->setLocale($u, 'xx'); |
| 504 | expect(false, 'unknown locale should throw'); |
| 505 | } catch (RuntimeException $e) { |
| 506 | expect(true, 'unknown locale refused'); |
| 507 | } |
| 508 | |
| 509 | echo "\nConfig helpers\n"; |
| 510 | |
| 511 | |
| 512 | $cfg = config(); |
| 513 | expect(isset($cfg['client_id'], $cfg['client_secret']), 'config() creates a file'); |
| 514 | expect(is_file($tmp . '/config.php'), 'config.php written to the data dir'); |
| 515 | expect($cfg['client_secret'] === '', 'secret starts empty (Tim must fill it in)'); |
| 516 | expect(authConfigured() === false, 'auth is unconfigured without a secret'); |
| 517 | putenv('METEORS_CLIENT_SECRET=test-secret-value'); |
| 518 | // config file empty secret falls back to env |
| 519 | $cfg2 = config(); |
| 520 | expect($cfg2['client_secret'] === 'test-secret-value', 'empty file field falls back to env'); |
| 521 | putenv('METEORS_CLIENT_SECRET'); |
| 522 | |
| 523 | echo "\n$passed passed, $failed failed\n"; |
| 524 | |
| 525 | // cleanup |
| 526 | $files = new RecursiveIteratorIterator( |
| 527 | new RecursiveDirectoryIterator($tmp, FilesystemIterator::SKIP_DOTS), |
| 528 | RecursiveIteratorIterator::CHILD_FIRST |
| 529 | ); |
| 530 | foreach ($files as $f) { |
| 531 | $f->isDir() ? @rmdir($f->getPathname()) : @unlink($f->getPathname()); |
| 532 | } |
| 533 | @rmdir($tmp); |
| 534 | |
| 535 | exit($failed === 0 ? 0 : 1); |
projects-timfalken-com meteors