feat: lobby planet picks and server-side miss from the sender's Power
Members store planet_id. Shots use the sender's planet, not a lobby flat rate, so the client cannot cheat. Players can change planet only while the lobby is waiting.
@@ -62,12 +62,19 @@ try {
62
62
switch ($action) {
63
63
case 'lobby_create':
64
64
$miss = isset($body['miss_chance']) ? (float) $body['miss_chance'] : METEORS_DEFAULT_MISS;
65
$lobby = $store->createLobby($userId, $name, $miss);
65
$planet = (string) ($body['planet_id'] ?? '');
66
$lobby = $store->createLobby($userId, $name, $miss, $planet);
66
67
jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
67
68
68
69
case 'lobby_join':
69
70
$code = (string) ($body['code'] ?? '');
70
$lobby = $store->joinLobby($userId, $name, $code);
71
$planet = (string) ($body['planet_id'] ?? '');
72
$lobby = $store->joinLobby($userId, $name, $code, $planet);
73
jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
74
75
case 'lobby_set_planet':
76
$planet = (string) ($body['planet_id'] ?? '');
77
$lobby = $store->setPlanet($userId, $planet);
71
78
jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
72
79
73
80
case 'lobby_leave':
@@ -62,12 +62,19 @@ try {62
switch ($action) {62
switch ($action) {63
case 'lobby_create':63
case 'lobby_create':64
$miss = isset($body['miss_chance']) ? (float) $body['miss_chance'] : METEORS_DEFAULT_MISS;64
$miss = isset($body['miss_chance']) ? (float) $body['miss_chance'] : METEORS_DEFAULT_MISS;65
$lobby = $store->createLobby($userId, $name, $miss);65
$planet = (string) ($body['planet_id'] ?? '');66
$lobby = $store->createLobby($userId, $name, $miss, $planet);66
jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);67
jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);67
68
68
case 'lobby_join':69
case 'lobby_join':69
$code = (string) ($body['code'] ?? '');70
$code = (string) ($body['code'] ?? '');70
$lobby = $store->joinLobby($userId, $name, $code);71
$planet = (string) ($body['planet_id'] ?? '');72
$lobby = $store->joinLobby($userId, $name, $code, $planet);73
jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);74
75
case 'lobby_set_planet':76
$planet = (string) ($body['planet_id'] ?? '');77
$lobby = $store->setPlanet($userId, $planet);71
jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);78
jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);72
79
73
case 'lobby_leave':80
case 'lobby_leave':@@ -1,6 +1,8 @@
1
1
<?php
2
2
declare(strict_types=1);
3
3
4
require_once __DIR__ . '/Planets.php';
5
4
6
final class Store
5
7
{
6
8
private PDO $pdo;
@@ -71,6 +73,18 @@ CREATE TABLE IF NOT EXISTS shots (
71
73
);
72
74
CREATE INDEX IF NOT EXISTS shots_deliver ON shots (lobby_id, to_user, delivered, deliver_at);
73
75
SQL);
76
$this->ensureColumn('members', 'planet_id', "TEXT NOT NULL DEFAULT 'terra'");
77
}
78
79
private function ensureColumn(string $table, string $column, string $ddl): void
80
{
81
$cols = $this->pdo->query('PRAGMA table_info(' . $table . ')')->fetchAll();
82
foreach ($cols as $c) {
83
if (($c['name'] ?? '') === $column) {
84
return;
85
}
86
}
87
$this->pdo->exec('ALTER TABLE ' . $table . ' ADD COLUMN ' . $column . ' ' . $ddl);
74
88
}
75
89
76
90
public function transaction(callable $fn): mixed
@@ -170,9 +184,9 @@ SQL);
170
184
return $stmt->fetchAll();
171
185
}
172
186
173
public function createLobby(string $userId, string $name, float $missChance = METEORS_DEFAULT_MISS): array
187
public function createLobby(string $userId, string $name, float $missChance = METEORS_DEFAULT_MISS, string $planetId = ''): array
174
188
{
175
return $this->transaction(function () use ($userId, $name, $missChance) {
189
return $this->transaction(function () use ($userId, $name, $missChance, $planetId) {
176
190
$existing = $this->lobbyForUser($userId);
177
191
if ($existing && $existing['status'] !== 'finished') {
178
192
throw new RuntimeException('Je zit al in een lobby. Verlaat die eerst.');
@@ -188,17 +202,18 @@ SQL);
188
202
'INSERT INTO lobbies (id, code, host_id, status, miss_chance, flight_duration, created)
189
203
VALUES (?,?,?,?,?,?,?)'
190
204
)->execute([$id, $code, $userId, 'waiting', $miss, METEORS_FLIGHT_DURATION, $now]);
205
$planet = Planets::known($planetId) ? $planetId : Planets::defaultId();
191
206
$this->pdo->prepare(
192
'INSERT INTO members (lobby_id, user_id, name, ready, joined, last_seen)
193
VALUES (?,?,?,0,?,?)'
194
)->execute([$id, $userId, $name, $now, $now]);
207
'INSERT INTO members (lobby_id, user_id, name, ready, joined, last_seen, planet_id)
208
VALUES (?,?,?,0,?,?,?)'
209
)->execute([$id, $userId, $name, $now, $now, $planet]);
195
210
return $this->getLobby($id);
196
211
});
197
212
}
198
213
199
public function joinLobby(string $userId, string $name, string $code): array
214
public function joinLobby(string $userId, string $name, string $code, string $planetId = ''): array
200
215
{
201
return $this->transaction(function () use ($userId, $name, $code) {
216
return $this->transaction(function () use ($userId, $name, $code, $planetId) {
202
217
$existing = $this->lobbyForUser($userId);
203
218
if ($existing && $existing['status'] !== 'finished') {
204
219
if (strcasecmp($existing['code'], $code) === 0) {
@@ -219,8 +234,8 @@ SQL);
219
234
}
220
235
$now = $this->now();
221
236
$this->pdo->prepare(
222
'INSERT INTO members (lobby_id, user_id, name, ready, eliminated, place, left_at, joined, last_seen, score, snapshot)
223
VALUES (?,?,?,0,0,NULL,NULL,?,?,0,NULL)
237
'INSERT INTO members (lobby_id, user_id, name, ready, eliminated, place, left_at, joined, last_seen, score, snapshot, planet_id)
238
VALUES (?,?,?,0,0,NULL,NULL,?,?,0,NULL,?)
224
239
ON CONFLICT(lobby_id, user_id) DO UPDATE SET
225
240
name = excluded.name,
226
241
ready = 0,
@@ -230,8 +245,9 @@ SQL);
230
245
joined = excluded.joined,
231
246
last_seen = excluded.last_seen,
232
247
score = 0,
233
snapshot = NULL'
234
)->execute([$lobby['id'], $userId, $name, $now, $now]);
248
snapshot = NULL,
249
planet_id = excluded.planet_id'
250
)->execute([$lobby['id'], $userId, $name, $now, $now, Planets::known($planetId) ? $planetId : Planets::defaultId()]);
235
251
return $this->getLobby($lobby['id']);
236
252
});
237
253
}
@@ -323,6 +339,39 @@ SQL);
323
339
}
324
340
325
341
/** @param list<array{client_id?:string,color?:int}> $shots */
342
public function setPlanet(string $userId, string $planetId): array
343
{
344
return $this->transaction(function () use ($userId, $planetId) {
345
$lobby = $this->lobbyForUser($userId);
346
if (!$lobby) {
347
throw new RuntimeException('Je zit in geen lobby.');
348
}
349
if ($lobby['status'] !== 'waiting') {
350
throw new RuntimeException('De planeet kun je alleen kiezen voordat het spel start.');
351
}
352
if (!Planets::known($planetId)) {
353
throw new RuntimeException('Onbekende planeet.');
354
}
355
$this->pdo->prepare(
356
'UPDATE members SET planet_id = ?, last_seen = ? WHERE lobby_id = ? AND user_id = ? AND left_at IS NULL'
357
)->execute([$planetId, $this->now(), $lobby['id'], $userId]);
358
return $this->getLobby($lobby['id']);
359
});
360
}
361
362
/** @param array<string,mixed> $member */
363
public function missChanceForMember(array $member): float
364
{
365
if (isset($this->testMissOverride) && is_float($this->testMissOverride)) {
366
return max(0.0, min(1.0, $this->testMissOverride));
367
}
368
$id = (string) ($member['planet_id'] ?? Planets::defaultId());
369
return Planets::missChanceForId($id);
370
}
371
372
/** @var ?float Tests may force a miss chance without changing the catalog. */
373
public ?float $testMissOverride = null;
374
326
375
public function recordShots(string $userId, array $shots): array
327
376
{
328
377
return $this->transaction(function () use ($userId, $shots) {
@@ -341,7 +390,7 @@ SQL);
341
390
}
342
391
}
343
392
$results = [];
344
$missChance = (float) $lobby['miss_chance'];
393
$missChance = $this->missChanceForMember($me);
345
394
$flight = (float) $lobby['flight_duration'];
346
395
$now = $this->nowf();
347
396
$ins = $this->pdo->prepare(
@@ -580,6 +629,7 @@ SQL);
580
629
'is_me' => $m['user_id'] === $userId,
581
630
'score' => (int) $m['score'],
582
631
'snapshot' => $m['user_id'] === $userId ? null : $snap,
632
'planet' => Planets::present((string) ($m['planet_id'] ?? Planets::defaultId())),
583
633
];
584
634
}
585
635
$rankings = [];
@@ -1,6 +1,8 @@1
<?php1
<?php2
declare(strict_types=1);2
declare(strict_types=1);3
3
4
require_once __DIR__ . '/Planets.php';5
4
final class Store6
final class Store5
{7
{6
private PDO $pdo;8
private PDO $pdo;@@ -71,6 +73,18 @@ CREATE TABLE IF NOT EXISTS shots (71
);73
);72
CREATE INDEX IF NOT EXISTS shots_deliver ON shots (lobby_id, to_user, delivered, deliver_at);74
CREATE INDEX IF NOT EXISTS shots_deliver ON shots (lobby_id, to_user, delivered, deliver_at);73
SQL);75
SQL);76
$this->ensureColumn('members', 'planet_id', "TEXT NOT NULL DEFAULT 'terra'");77
}78
79
private function ensureColumn(string $table, string $column, string $ddl): void80
{81
$cols = $this->pdo->query('PRAGMA table_info(' . $table . ')')->fetchAll();82
foreach ($cols as $c) {83
if (($c['name'] ?? '') === $column) {84
return;85
}86
}87
$this->pdo->exec('ALTER TABLE ' . $table . ' ADD COLUMN ' . $column . ' ' . $ddl);74
}88
}75
89
76
public function transaction(callable $fn): mixed90
public function transaction(callable $fn): mixed@@ -170,9 +184,9 @@ SQL);170
return $stmt->fetchAll();184
return $stmt->fetchAll();171
}185
}172
186
173
public function createLobby(string $userId, string $name, float $missChance = METEORS_DEFAULT_MISS): array187
public function createLobby(string $userId, string $name, float $missChance = METEORS_DEFAULT_MISS, string $planetId = ''): array174
{188
{175
return $this->transaction(function () use ($userId, $name, $missChance) {189
return $this->transaction(function () use ($userId, $name, $missChance, $planetId) {176
$existing = $this->lobbyForUser($userId);190
$existing = $this->lobbyForUser($userId);177
if ($existing && $existing['status'] !== 'finished') {191
if ($existing && $existing['status'] !== 'finished') {178
throw new RuntimeException('Je zit al in een lobby. Verlaat die eerst.');192
throw new RuntimeException('Je zit al in een lobby. Verlaat die eerst.');@@ -188,17 +202,18 @@ SQL);188
'INSERT INTO lobbies (id, code, host_id, status, miss_chance, flight_duration, created)202
'INSERT INTO lobbies (id, code, host_id, status, miss_chance, flight_duration, created)189
VALUES (?,?,?,?,?,?,?)'203
VALUES (?,?,?,?,?,?,?)'190
)->execute([$id, $code, $userId, 'waiting', $miss, METEORS_FLIGHT_DURATION, $now]);204
)->execute([$id, $code, $userId, 'waiting', $miss, METEORS_FLIGHT_DURATION, $now]);205
$planet = Planets::known($planetId) ? $planetId : Planets::defaultId();191
$this->pdo->prepare(206
$this->pdo->prepare(192
'INSERT INTO members (lobby_id, user_id, name, ready, joined, last_seen)207
'INSERT INTO members (lobby_id, user_id, name, ready, joined, last_seen, planet_id)193
VALUES (?,?,?,0,?,?)'208
VALUES (?,?,?,0,?,?,?)'194
)->execute([$id, $userId, $name, $now, $now]);209
)->execute([$id, $userId, $name, $now, $now, $planet]);195
return $this->getLobby($id);210
return $this->getLobby($id);196
});211
});197
}212
}198
213
199
public function joinLobby(string $userId, string $name, string $code): array214
public function joinLobby(string $userId, string $name, string $code, string $planetId = ''): array200
{215
{201
return $this->transaction(function () use ($userId, $name, $code) {216
return $this->transaction(function () use ($userId, $name, $code, $planetId) {202
$existing = $this->lobbyForUser($userId);217
$existing = $this->lobbyForUser($userId);203
if ($existing && $existing['status'] !== 'finished') {218
if ($existing && $existing['status'] !== 'finished') {204
if (strcasecmp($existing['code'], $code) === 0) {219
if (strcasecmp($existing['code'], $code) === 0) {@@ -219,8 +234,8 @@ SQL);219
}234
}220
$now = $this->now();235
$now = $this->now();221
$this->pdo->prepare(236
$this->pdo->prepare(222
'INSERT INTO members (lobby_id, user_id, name, ready, eliminated, place, left_at, joined, last_seen, score, snapshot)237
'INSERT INTO members (lobby_id, user_id, name, ready, eliminated, place, left_at, joined, last_seen, score, snapshot, planet_id)223
VALUES (?,?,?,0,0,NULL,NULL,?,?,0,NULL)238
VALUES (?,?,?,0,0,NULL,NULL,?,?,0,NULL,?)224
ON CONFLICT(lobby_id, user_id) DO UPDATE SET239
ON CONFLICT(lobby_id, user_id) DO UPDATE SET225
name = excluded.name,240
name = excluded.name,226
ready = 0,241
ready = 0,@@ -230,8 +245,9 @@ SQL);230
joined = excluded.joined,245
joined = excluded.joined,231
last_seen = excluded.last_seen,246
last_seen = excluded.last_seen,232
score = 0,247
score = 0,233
snapshot = NULL'248
snapshot = NULL,234
)->execute([$lobby['id'], $userId, $name, $now, $now]);249
planet_id = excluded.planet_id'250
)->execute([$lobby['id'], $userId, $name, $now, $now, Planets::known($planetId) ? $planetId : Planets::defaultId()]);235
return $this->getLobby($lobby['id']);251
return $this->getLobby($lobby['id']);236
});252
});237
}253
}@@ -323,6 +339,39 @@ SQL);323
}339
}324
340
325
/** @param list<array{client_id?:string,color?:int}> $shots */341
/** @param list<array{client_id?:string,color?:int}> $shots */342
public function setPlanet(string $userId, string $planetId): array343
{344
return $this->transaction(function () use ($userId, $planetId) {345
$lobby = $this->lobbyForUser($userId);346
if (!$lobby) {347
throw new RuntimeException('Je zit in geen lobby.');348
}349
if ($lobby['status'] !== 'waiting') {350
throw new RuntimeException('De planeet kun je alleen kiezen voordat het spel start.');351
}352
if (!Planets::known($planetId)) {353
throw new RuntimeException('Onbekende planeet.');354
}355
$this->pdo->prepare(356
'UPDATE members SET planet_id = ?, last_seen = ? WHERE lobby_id = ? AND user_id = ? AND left_at IS NULL'357
)->execute([$planetId, $this->now(), $lobby['id'], $userId]);358
return $this->getLobby($lobby['id']);359
});360
}361
362
/** @param array<string,mixed> $member */363
public function missChanceForMember(array $member): float364
{365
if (isset($this->testMissOverride) && is_float($this->testMissOverride)) {366
return max(0.0, min(1.0, $this->testMissOverride));367
}368
$id = (string) ($member['planet_id'] ?? Planets::defaultId());369
return Planets::missChanceForId($id);370
}371
372
/** @var ?float Tests may force a miss chance without changing the catalog. */373
public ?float $testMissOverride = null;374
326
public function recordShots(string $userId, array $shots): array375
public function recordShots(string $userId, array $shots): array327
{376
{328
return $this->transaction(function () use ($userId, $shots) {377
return $this->transaction(function () use ($userId, $shots) {@@ -341,7 +390,7 @@ SQL);341
}390
}342
}391
}343
$results = [];392
$results = [];344
$missChance = (float) $lobby['miss_chance'];393
$missChance = $this->missChanceForMember($me);345
$flight = (float) $lobby['flight_duration'];394
$flight = (float) $lobby['flight_duration'];346
$now = $this->nowf();395
$now = $this->nowf();347
$ins = $this->pdo->prepare(396
$ins = $this->pdo->prepare(@@ -580,6 +629,7 @@ SQL);580
'is_me' => $m['user_id'] === $userId,629
'is_me' => $m['user_id'] === $userId,581
'score' => (int) $m['score'],630
'score' => (int) $m['score'],582
'snapshot' => $m['user_id'] === $userId ? null : $snap,631
'snapshot' => $m['user_id'] === $userId ? null : $snap,632
'planet' => Planets::present((string) ($m['planet_id'] ?? Planets::defaultId())),583
];633
];584
}634
}585
$rankings = [];635
$rankings = [];@@ -147,12 +147,14 @@ $store->joinLobby('local:test_user_2', 'test_user_2', $c['code']);
147
147
$store->setReady('local:test_user_1', true);
148
148
$store->setReady('local:test_user_2', true);
149
149
$store->startGame('local:test_user_1');
150
$store->testMissOverride = 1.0;
150
151
$allMiss = $store->recordShots('local:test_user_1', [
151
152
['client_id' => 'm1', 'color' => 0],
152
153
['client_id' => 'm2', 'color' => 1],
153
154
['client_id' => 'm3', 'color' => 2],
154
155
]);
155
expect(count($allMiss) === 3 && array_reduce($allMiss, fn ($ok, $r) => $ok && $r['miss'], true), 'miss_chance 1.0 → all miss');
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');
156
158
157
159
$d = null;
158
160
$store->leaveLobby('local:test_user_1');
@@ -162,11 +164,13 @@ $store->joinLobby('local:test_user_2', 'test_user_2', $d['code']);
162
164
$store->setReady('local:test_user_1', true);
163
165
$store->setReady('local:test_user_2', true);
164
166
$store->startGame('local:test_user_1');
167
$store->testMissOverride = 0.0;
165
168
$allHit = $store->recordShots('local:test_user_1', [
166
169
['client_id' => 'h1', 'color' => 0],
167
170
['client_id' => 'h2', 'color' => 1],
168
171
]);
169
expect(count($allHit) === 2 && array_reduce($allHit, fn ($ok, $r) => $ok && !$r['miss'] && $r['target_id'] === 'local:test_user_2', true), 'miss_chance 0 → all hit the other player');
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');
170
174
171
175
// Eliminated player is not a target.
172
176
$store->updateSnapshot('local:test_user_2', null, 0, true);
@@ -174,7 +178,62 @@ $none = $store->recordShots('local:test_user_1', [['client_id' => 'x', 'color' =
174
178
// Game may already be finished; recordShots on finished returns [].
175
179
expect($none === [] || $none[0]['miss'] === true, 'no living target after opponent is out');
176
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'] === 48, 'Terra display g/d/F/Power');
191
192
$store->leaveLobby('local:test_user_1');
193
$store->leaveLobby('local:test_user_2');
194
$pl = $store->createLobby('local:test_user_1', 'test_user_1', 0.30, 'aurel');
195
$store->joinLobby('local:test_user_2', 'test_user_2', $pl['code'], 'cinder');
196
$presented = $store->presentLobby($pl, 'local:test_user_1');
197
$byId = [];
198
foreach ($presented['players'] as $plr) { $byId[$plr['id']] = $plr; }
199
expect($byId['local:test_user_1']['planet']['id'] === 'aurel', 'host planet aurel');
200
expect($byId['local:test_user_2']['planet']['id'] === 'cinder', 'guest planet cinder');
201
expect(almostEqual($byId['local:test_user_1']['planet']['miss_chance'], 0.60), 'host miss from Aurel Power');
202
expect(almostEqual($byId['local:test_user_2']['planet']['miss_chance'], 0.05), 'guest miss from Cinder Power');
203
204
$store->setPlanet('local:test_user_1', 'wisp');
205
$presented = $store->presentLobby($store->getLobby($pl['id']), 'local:test_user_1');
206
foreach ($presented['players'] as $plr) {
207
if ($plr['is_me']) {
208
expect($plr['planet']['id'] === 'wisp', 'setPlanet updates host to Wisp');
209
}
210
}
211
212
try {
213
$store->setPlanet('local:test_user_1', 'no-such-world');
214
expect(false, 'unknown planet must throw');
215
} catch (RuntimeException $e) {
216
expect(true, 'unknown planet rejected');
217
}
218
219
$store->setReady('local:test_user_1', true);
220
$store->setReady('local:test_user_2', true);
221
$store->startGame('local:test_user_1');
222
$me = $store->member($pl['id'], 'local:test_user_1');
223
expect(almostEqual($store->missChanceForMember($me), Planets::missChanceForId('wisp')), 'in-game miss uses the sender planet');
224
$guest = $store->member($pl['id'], 'local:test_user_2');
225
expect(almostEqual($store->missChanceForMember($guest), 0.05), 'guest still uses Cinder Power');
226
try {
227
$store->setPlanet('local:test_user_1', 'terra');
228
expect(false, 'setPlanet during play must throw');
229
} catch (RuntimeException $e) {
230
expect(true, 'planet locked once the match starts');
231
}
232
$store->leaveLobby('local:test_user_1');
233
$store->leaveLobby('local:test_user_2');
234
177
235
echo "\nConfig helpers\n";
236
178
237
$cfg = config();
179
238
expect(isset($cfg['client_id'], $cfg['client_secret']), 'config() creates a file');
180
239
expect(is_file($tmp . '/config.php'), 'config.php written to the data dir');
@@ -147,12 +147,14 @@ $store->joinLobby('local:test_user_2', 'test_user_2', $c['code']);147
$store->setReady('local:test_user_1', true);147
$store->setReady('local:test_user_1', true);148
$store->setReady('local:test_user_2', true);148
$store->setReady('local:test_user_2', true);149
$store->startGame('local:test_user_1');149
$store->startGame('local:test_user_1');150
$store->testMissOverride = 1.0;150
$allMiss = $store->recordShots('local:test_user_1', [151
$allMiss = $store->recordShots('local:test_user_1', [151
['client_id' => 'm1', 'color' => 0],152
['client_id' => 'm1', 'color' => 0],152
['client_id' => 'm2', 'color' => 1],153
['client_id' => 'm2', 'color' => 1],153
['client_id' => 'm3', 'color' => 2],154
['client_id' => 'm3', 'color' => 2],154
]);155
]);155
expect(count($allMiss) === 3 && array_reduce($allMiss, fn ($ok, $r) => $ok && $r['miss'], true), 'miss_chance 1.0 → all miss');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');156
158
157
$d = null;159
$d = null;158
$store->leaveLobby('local:test_user_1');160
$store->leaveLobby('local:test_user_1');@@ -162,11 +164,13 @@ $store->joinLobby('local:test_user_2', 'test_user_2', $d['code']);162
$store->setReady('local:test_user_1', true);164
$store->setReady('local:test_user_1', true);163
$store->setReady('local:test_user_2', true);165
$store->setReady('local:test_user_2', true);164
$store->startGame('local:test_user_1');166
$store->startGame('local:test_user_1');167
$store->testMissOverride = 0.0;165
$allHit = $store->recordShots('local:test_user_1', [168
$allHit = $store->recordShots('local:test_user_1', [166
['client_id' => 'h1', 'color' => 0],169
['client_id' => 'h1', 'color' => 0],167
['client_id' => 'h2', 'color' => 1],170
['client_id' => 'h2', 'color' => 1],168
]);171
]);169
expect(count($allHit) === 2 && array_reduce($allHit, fn ($ok, $r) => $ok && !$r['miss'] && $r['target_id'] === 'local:test_user_2', true), 'miss_chance 0 → all hit the other player');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');170
174
171
// Eliminated player is not a target.175
// Eliminated player is not a target.172
$store->updateSnapshot('local:test_user_2', null, 0, true);176
$store->updateSnapshot('local:test_user_2', null, 0, true);@@ -174,7 +178,62 @@ $none = $store->recordShots('local:test_user_1', [['client_id' => 'x', 'color' =174
// Game may already be finished; recordShots on finished returns [].178
// Game may already be finished; recordShots on finished returns [].175
expect($none === [] || $none[0]['miss'] === true, 'no living target after opponent is out');179
expect($none === [] || $none[0]['miss'] === true, 'no living target after opponent is out');176
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'] === 48, 'Terra display g/d/F/Power');191
192
$store->leaveLobby('local:test_user_1');193
$store->leaveLobby('local:test_user_2');194
$pl = $store->createLobby('local:test_user_1', 'test_user_1', 0.30, 'aurel');195
$store->joinLobby('local:test_user_2', 'test_user_2', $pl['code'], 'cinder');196
$presented = $store->presentLobby($pl, 'local:test_user_1');197
$byId = [];198
foreach ($presented['players'] as $plr) { $byId[$plr['id']] = $plr; }199
expect($byId['local:test_user_1']['planet']['id'] === 'aurel', 'host planet aurel');200
expect($byId['local:test_user_2']['planet']['id'] === 'cinder', 'guest planet cinder');201
expect(almostEqual($byId['local:test_user_1']['planet']['miss_chance'], 0.60), 'host miss from Aurel Power');202
expect(almostEqual($byId['local:test_user_2']['planet']['miss_chance'], 0.05), 'guest miss from Cinder Power');203
204
$store->setPlanet('local:test_user_1', 'wisp');205
$presented = $store->presentLobby($store->getLobby($pl['id']), 'local:test_user_1');206
foreach ($presented['players'] as $plr) {207
if ($plr['is_me']) {208
expect($plr['planet']['id'] === 'wisp', 'setPlanet updates host to Wisp');209
}210
}211
212
try {213
$store->setPlanet('local:test_user_1', 'no-such-world');214
expect(false, 'unknown planet must throw');215
} catch (RuntimeException $e) {216
expect(true, 'unknown planet rejected');217
}218
219
$store->setReady('local:test_user_1', true);220
$store->setReady('local:test_user_2', true);221
$store->startGame('local:test_user_1');222
$me = $store->member($pl['id'], 'local:test_user_1');223
expect(almostEqual($store->missChanceForMember($me), Planets::missChanceForId('wisp')), 'in-game miss uses the sender planet');224
$guest = $store->member($pl['id'], 'local:test_user_2');225
expect(almostEqual($store->missChanceForMember($guest), 0.05), 'guest still uses Cinder Power');226
try {227
$store->setPlanet('local:test_user_1', 'terra');228
expect(false, 'setPlanet during play must throw');229
} catch (RuntimeException $e) {230
expect(true, 'planet locked once the match starts');231
}232
$store->leaveLobby('local:test_user_1');233
$store->leaveLobby('local:test_user_2');234
177
echo "\nConfig helpers\n";235
echo "\nConfig helpers\n";236
178
$cfg = config();237
$cfg = config();179
expect(isset($cfg['client_id'], $cfg['client_secret']), 'config() creates a file');238
expect(isset($cfg['client_id'], $cfg['client_secret']), 'config() creates a file');180
expect(is_file($tmp . '/config.php'), 'config.php written to the data dir');239
expect(is_file($tmp . '/config.php'), 'config.php written to the data dir');