BlueGit

projects-timfalken-com meteors

t1196: remember the last chosen planet on the profile (localStorage when logged out)

timfa Grok Bot · pushed by timfa

Authored 2026-11-15 · 2 days ago

Hash
67e019fe5f6ecc4f250df970837aa55cf0b6a6e0
Parent
0fb77079
5 files changed +90 −5
server/api.php +7 −0
@@ -21,6 +21,7 @@ try {
21 21 $profile = [
22 22 'lifetime_points' => 0,
23 23 'locale' => null,
24 'last_planet' => null,
24 25 'blocks' => [],
25 26 'planets' => [],
26 27 ];
@@ -45,6 +46,7 @@ try {
45 46 'logout_url' => 'logout.php',
46 47 'lifetime_points' => $profile['lifetime_points'],
47 48 'locale' => $profile['locale'],
49 'last_planet' => $profile['last_planet'],
48 50 'blocks' => $profile['blocks'],
49 51 'planets' => $profile['planets'],
50 52 ]);
@@ -120,6 +122,7 @@ try {
120 122 'shot_flights' => $view['shot_flights'] ?? [],
121 123 'lifetime_points' => $profile['lifetime_points'],
122 124 'locale' => $profile['locale'],
125 'last_planet' => $profile['last_planet'],
123 126 'blocks' => $profile['blocks'],
124 127 'planets' => $profile['planets'],
125 128 ]);
@@ -159,6 +162,10 @@ try {
159 162 'planets' => $store->listCustomPlanets($userId),
160 163 ]);
161 164
165 case 'set_last_planet':
166 $pid = $store->setLastPlanet($userId, (string) ($body['planet_id'] ?? ''));
167 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'last_planet' => $pid]);
168
162 169 case 'set_locale':
163 170 $locale = $store->setLocale($userId, (string) ($body['locale'] ?? ''));
164 171 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'locale' => $locale]);
@@ -21,6 +21,7 @@ try {
21 $profile = [
21 $profile = [
22 'lifetime_points' => 0,
22 'lifetime_points' => 0,
23 'locale' => null,
23 'locale' => null,
24 'last_planet' => null,
24 'blocks' => [],
25 'blocks' => [],
25 'planets' => [],
26 'planets' => [],
26 ];
27 ];
@@ -45,6 +46,7 @@ try {
45 'logout_url' => 'logout.php',
46 'logout_url' => 'logout.php',
46 'lifetime_points' => $profile['lifetime_points'],
47 'lifetime_points' => $profile['lifetime_points'],
47 'locale' => $profile['locale'],
48 'locale' => $profile['locale'],
49 'last_planet' => $profile['last_planet'],
48 'blocks' => $profile['blocks'],
50 'blocks' => $profile['blocks'],
49 'planets' => $profile['planets'],
51 'planets' => $profile['planets'],
50 ]);
52 ]);
@@ -120,6 +122,7 @@ try {
120 'shot_flights' => $view['shot_flights'] ?? [],
122 'shot_flights' => $view['shot_flights'] ?? [],
121 'lifetime_points' => $profile['lifetime_points'],
123 'lifetime_points' => $profile['lifetime_points'],
122 'locale' => $profile['locale'],
124 'locale' => $profile['locale'],
125 'last_planet' => $profile['last_planet'],
123 'blocks' => $profile['blocks'],
126 'blocks' => $profile['blocks'],
124 'planets' => $profile['planets'],
127 'planets' => $profile['planets'],
125 ]);
128 ]);
@@ -159,6 +162,10 @@ try {
159 'planets' => $store->listCustomPlanets($userId),
162 'planets' => $store->listCustomPlanets($userId),
160 ]);
163 ]);
161
164
165 case 'set_last_planet':
166 $pid = $store->setLastPlanet($userId, (string) ($body['planet_id'] ?? ''));
167 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'last_planet' => $pid]);
168
162 case 'set_locale':
169 case 'set_locale':
163 $locale = $store->setLocale($userId, (string) ($body['locale'] ?? ''));
170 $locale = $store->setLocale($userId, (string) ($body['locale'] ?? ''));
164 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'locale' => $locale]);
171 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'locale' => $locale]);
server/lib/Store.php +32 −0
@@ -115,6 +115,7 @@ CREATE TABLE IF NOT EXISTS custom_planets (
115 115 );
116 116 CREATE INDEX IF NOT EXISTS custom_planets_user ON custom_planets (user_id);
117 117 SQL);
118 $this->ensureColumn('profiles', 'last_planet', 'TEXT');
118 119 }
119 120
120 121 private function ensureColumn(string $table, string $column, string $ddl): void
@@ -864,6 +865,36 @@ SQL);
864 865 return $locale;
865 866 }
866 867
868 /**
869 * Last chosen planet (t1196): a standard planet id or one of the
870 * user's own custom planets. Returns null when nothing is stored or the
871 * stored custom planet no longer exists (graceful fallback).
872 */
873 public function lastPlanet(string $userId): ?string
874 {
875 $v = $this->ensureProfile($userId)['last_planet'] ?? null;
876 if (!is_string($v) || $v === '') {
877 return null;
878 }
879 if (Planets::known($v) || $this->ownedCustom($userId, $v)) {
880 return $v;
881 }
882 return null;
883 }
884
885 public function setLastPlanet(string $userId, string $planetId): string
886 {
887 $planetId = trim($planetId);
888 if ($planetId === '' || strlen($planetId) > 64
889 || !(Planets::known($planetId) || $this->ownedCustom($userId, $planetId))) {
890 throw new RuntimeException('Onbekende planeet.');
891 }
892 $this->ensureProfile($userId);
893 $this->pdo->prepare('UPDATE profiles SET last_planet = ?, updated = ? WHERE user_id = ?')
894 ->execute([$planetId, $this->now(), $userId]);
895 return $planetId;
896 }
897
867 898 /**
868 899 * Add points once for a unique award_key. Returns the new lifetime total.
869 900 * $points is clamped to [0, MAX_GAME_POINTS]. A repeat key is a no-op.
@@ -1107,6 +1138,7 @@ SQL);
1107 1138 return [
1108 1139 'lifetime_points' => $points,
1109 1140 'locale' => $this->locale($userId),
1141 'last_planet' => $this->lastPlanet($userId),
1110 1142 'blocks' => Blocks::present($points),
1111 1143 'planets' => $this->listCustomPlanets($userId),
1112 1144 ];
@@ -115,6 +115,7 @@ CREATE TABLE IF NOT EXISTS custom_planets (
115);
115);
116CREATE INDEX IF NOT EXISTS custom_planets_user ON custom_planets (user_id);
116CREATE INDEX IF NOT EXISTS custom_planets_user ON custom_planets (user_id);
117SQL);
117SQL);
118 $this->ensureColumn('profiles', 'last_planet', 'TEXT');
118 }
119 }
119
120
120 private function ensureColumn(string $table, string $column, string $ddl): void
121 private function ensureColumn(string $table, string $column, string $ddl): void
@@ -864,6 +865,36 @@ SQL);
864 return $locale;
865 return $locale;
865 }
866 }
866
867
868 /**
869 * Last chosen planet (t1196): a standard planet id or one of the
870 * user's own custom planets. Returns null when nothing is stored or the
871 * stored custom planet no longer exists (graceful fallback).
872 */
873 public function lastPlanet(string $userId): ?string
874 {
875 $v = $this->ensureProfile($userId)['last_planet'] ?? null;
876 if (!is_string($v) || $v === '') {
877 return null;
878 }
879 if (Planets::known($v) || $this->ownedCustom($userId, $v)) {
880 return $v;
881 }
882 return null;
883 }
884
885 public function setLastPlanet(string $userId, string $planetId): string
886 {
887 $planetId = trim($planetId);
888 if ($planetId === '' || strlen($planetId) > 64
889 || !(Planets::known($planetId) || $this->ownedCustom($userId, $planetId))) {
890 throw new RuntimeException('Onbekende planeet.');
891 }
892 $this->ensureProfile($userId);
893 $this->pdo->prepare('UPDATE profiles SET last_planet = ?, updated = ? WHERE user_id = ?')
894 ->execute([$planetId, $this->now(), $userId]);
895 return $planetId;
896 }
897
867 /**
898 /**
868 * Add points once for a unique award_key. Returns the new lifetime total.
899 * Add points once for a unique award_key. Returns the new lifetime total.
869 * $points is clamped to [0, MAX_GAME_POINTS]. A repeat key is a no-op.
900 * $points is clamped to [0, MAX_GAME_POINTS]. A repeat key is a no-op.
@@ -1107,6 +1138,7 @@ SQL);
1107 return [
1138 return [
1108 'lifetime_points' => $points,
1139 'lifetime_points' => $points,
1109 'locale' => $this->locale($userId),
1140 'locale' => $this->locale($userId),
1141 'last_planet' => $this->lastPlanet($userId),
1110 'blocks' => Blocks::present($points),
1142 'blocks' => Blocks::present($points),
1111 'planets' => $this->listCustomPlanets($userId),
1143 'planets' => $this->listCustomPlanets($userId),
1112 ];
1144 ];
server/tests/run.php +17 −0
@@ -433,6 +433,22 @@ expect($snap['owner_id'] === $u, 'owner_id set');
433 433
434 434 $listed = $ptsStore->listCustomPlanets($u);
435 435 expect(count($listed) === 1, 'owner lists the planet');
436
437 echo "Last chosen planet (t1196)\n";
438 expect($ptsStore->lastPlanet($u) === null, 'no last planet stored yet');
439 expect($ptsStore->profilePayload($u)['last_planet'] === null, 'payload last_planet null');
440 expect($ptsStore->setLastPlanet($u, 'terra') === 'terra', 'standard planet saved');
441 expect($ptsStore->lastPlanet($u) === 'terra', 'standard planet read back');
442 $ptsStore->setLastPlanet($u, $snap['id']);
443 expect($ptsStore->profilePayload($u)['last_planet'] === $snap['id'], 'own custom planet saved and in payload');
444 foreach (['', 'nope', str_repeat('x', 80)] as $bad) {
445 try { $ptsStore->setLastPlanet($u, $bad); expect(false, 'bad planet accepted: ' . $bad); }
446 catch (RuntimeException $e) { expect(true, 'bad planet refused: ' . substr($bad, 0, 8)); }
447 }
448 try { $ptsStore->setLastPlanet($v, $snap['id']); expect(false, 'other user may not pick my custom planet'); }
449 catch (RuntimeException $e) { expect(true, "someone else's custom planet refused"); }
450 expect($ptsStore->lastPlanet($u) === $snap['id'], 'refused writes keep the old value');
451
436 452 expect($ptsStore->listCustomPlanets($v) === [], 'other user has no planets');
437 453
438 454 try {
@@ -469,6 +485,7 @@ try {
469 485 $ptsStore->leaveLobby($u);
470 486 $ptsStore->leaveLobby($v);
471 487 $ptsStore->deleteCustomPlanet($u, $snap['id']);
488 expect($ptsStore->lastPlanet($u) === null, "deleted custom planet → last_planet falls back to null (t1196)");
472 489 expect($ptsStore->listCustomPlanets($u) === [], 'deleted planet is gone');
473 490
474 491 // Snapshot survives mid-match edits: create, join, start, then edit/delete, opponent still sees original.
@@ -433,6 +433,22 @@ expect($snap['owner_id'] === $u, 'owner_id set');
433
433
434$listed = $ptsStore->listCustomPlanets($u);
434$listed = $ptsStore->listCustomPlanets($u);
435expect(count($listed) === 1, 'owner lists the planet');
435expect(count($listed) === 1, 'owner lists the planet');
436
437echo "Last chosen planet (t1196)\n";
438expect($ptsStore->lastPlanet($u) === null, 'no last planet stored yet');
439expect($ptsStore->profilePayload($u)['last_planet'] === null, 'payload last_planet null');
440expect($ptsStore->setLastPlanet($u, 'terra') === 'terra', 'standard planet saved');
441expect($ptsStore->lastPlanet($u) === 'terra', 'standard planet read back');
442$ptsStore->setLastPlanet($u, $snap['id']);
443expect($ptsStore->profilePayload($u)['last_planet'] === $snap['id'], 'own custom planet saved and in payload');
444foreach (['', 'nope', str_repeat('x', 80)] as $bad) {
445 try { $ptsStore->setLastPlanet($u, $bad); expect(false, 'bad planet accepted: ' . $bad); }
446 catch (RuntimeException $e) { expect(true, 'bad planet refused: ' . substr($bad, 0, 8)); }
447}
448try { $ptsStore->setLastPlanet($v, $snap['id']); expect(false, 'other user may not pick my custom planet'); }
449catch (RuntimeException $e) { expect(true, "someone else's custom planet refused"); }
450expect($ptsStore->lastPlanet($u) === $snap['id'], 'refused writes keep the old value');
451
436expect($ptsStore->listCustomPlanets($v) === [], 'other user has no planets');
452expect($ptsStore->listCustomPlanets($v) === [], 'other user has no planets');
437
453
438try {
454try {
@@ -469,6 +485,7 @@ try {
469$ptsStore->leaveLobby($u);
485$ptsStore->leaveLobby($u);
470$ptsStore->leaveLobby($v);
486$ptsStore->leaveLobby($v);
471$ptsStore->deleteCustomPlanet($u, $snap['id']);
487$ptsStore->deleteCustomPlanet($u, $snap['id']);
488expect($ptsStore->lastPlanet($u) === null, "deleted custom planet → last_planet falls back to null (t1196)");
472expect($ptsStore->listCustomPlanets($u) === [], 'deleted planet is gone');
489expect($ptsStore->listCustomPlanets($u) === [], 'deleted planet is gone');
473
490
474// Snapshot survives mid-match edits: create, join, start, then edit/delete, opponent still sees original.
491// Snapshot survives mid-match edits: create, join, start, then edit/delete, opponent still sees original.
src/main.js +25 −5
@@ -2321,13 +2321,32 @@ function selectedPlanetId ()
2321 2321 return 'terra';
2322 2322 }
2323 2323
2324 /**
2325 * Pre-select the last chosen planet (t1196): logged in the profile value
2326 * (server), otherwise / as fallback localStorage, otherwise Terra. A
2327 * deleted custom planet falls back to the default (setSelectedPlanet).
2328 */
2324 2329 function restoreSelectedPlanet ()
2325 2330 {
2326 var id = 'terra';
2327 try
2328 { id = localStorage.getItem('meteors-planet') || 'terra'; }
2329 catch (e) {}
2330 setSelectedPlanet(id, true);
2331 var id = null;
2332 if (typeof Online !== 'undefined' && Online.me && Online.lastPlanet)
2333 { id = Online.lastPlanet; }
2334 if (!id)
2335 {
2336 try
2337 { id = localStorage.getItem('meteors-planet'); }
2338 catch (e) {}
2339 }
2340 setSelectedPlanet(id || 'terra', true);
2341 }
2342
2343 function rememberPlanetChoice (id)
2344 {
2345 if (typeof Online === 'undefined' || !Online.me || !Online.setLastPlanet)
2346 { return; }
2347 if (Online.lastPlanet === id)
2348 { return; }
2349 Promise.resolve(Online.setLastPlanet(id)).then(null, function () {});
2331 2350 }
2332 2351
2333 2352 function setSelectedPlanet (id, silent)
@@ -2347,6 +2366,7 @@ function setSelectedPlanet (id, silent)
2347 2366 catch (e) {}
2348 2367 if (!silent)
2349 2368 {
2369 rememberPlanetChoice(found.id);
2350 2370 var grid = document.getElementById('planet-grid');
2351 2371 if (grid && !grid.hidden && grid.children.length)
2352 2372 { renderPlanetGrid('planet-grid', false); }
@@ -2321,13 +2321,32 @@ function selectedPlanetId ()
2321 return 'terra';
2321 return 'terra';
2322}
2322}
2323
2323
2324/**
2325 * Pre-select the last chosen planet (t1196): logged in the profile value
2326 * (server), otherwise / as fallback localStorage, otherwise Terra. A
2327 * deleted custom planet falls back to the default (setSelectedPlanet).
2328 */
2324function restoreSelectedPlanet ()
2329function restoreSelectedPlanet ()
2325{
2330{
2326 var id = 'terra';
2331 var id = null;
2327 try
2332 if (typeof Online !== 'undefined' && Online.me && Online.lastPlanet)
2328 { id = localStorage.getItem('meteors-planet') || 'terra'; }
2333 { id = Online.lastPlanet; }
2329 catch (e) {}
2334 if (!id)
2330 setSelectedPlanet(id, true);
2335 {
2336 try
2337 { id = localStorage.getItem('meteors-planet'); }
2338 catch (e) {}
2339 }
2340 setSelectedPlanet(id || 'terra', true);
2341}
2342
2343function rememberPlanetChoice (id)
2344{
2345 if (typeof Online === 'undefined' || !Online.me || !Online.setLastPlanet)
2346 { return; }
2347 if (Online.lastPlanet === id)
2348 { return; }
2349 Promise.resolve(Online.setLastPlanet(id)).then(null, function () {});
2331}
2350}
2332
2351
2333function setSelectedPlanet (id, silent)
2352function setSelectedPlanet (id, silent)
@@ -2347,6 +2366,7 @@ function setSelectedPlanet (id, silent)
2347 catch (e) {}
2366 catch (e) {}
2348 if (!silent)
2367 if (!silent)
2349 {
2368 {
2369 rememberPlanetChoice(found.id);
2350 var grid = document.getElementById('planet-grid');
2370 var grid = document.getElementById('planet-grid');
2351 if (grid && !grid.hidden && grid.children.length)
2371 if (grid && !grid.hidden && grid.children.length)
2352 { renderPlanetGrid('planet-grid', false); }
2372 { renderPlanetGrid('planet-grid', false); }
src/online.js +9 −0
@@ -77,6 +77,8 @@ Online._apply = function (data)
77 77 { this.lifetimePoints = data.lifetime_points; }
78 78 if (data.locale !== undefined)
79 79 { this.locale = data.locale; }
80 if (data.last_planet !== undefined)
81 { this.lastPlanet = data.last_planet; }
80 82 if (data.blocks)
81 83 { this.blocks = data.blocks; }
82 84 if (data.planets)
@@ -184,6 +186,13 @@ Online.deletePlanet = function (id)
184 186 return this.request('planet_delete', { id: id });
185 187 };
186 188
189 /** Remember the last chosen planet on the profile (t1196). */
190 Online.setLastPlanet = function (planetId)
191 {
192 this.lastPlanet = planetId;
193 return this.request('set_last_planet', { planet_id: planetId });
194 };
195
187 196 Online.setLocale = function (locale)
188 197 {
189 198 return this.request('set_locale', { locale: locale });
@@ -77,6 +77,8 @@ Online._apply = function (data)
77 { this.lifetimePoints = data.lifetime_points; }
77 { this.lifetimePoints = data.lifetime_points; }
78 if (data.locale !== undefined)
78 if (data.locale !== undefined)
79 { this.locale = data.locale; }
79 { this.locale = data.locale; }
80 if (data.last_planet !== undefined)
81 { this.lastPlanet = data.last_planet; }
80 if (data.blocks)
82 if (data.blocks)
81 { this.blocks = data.blocks; }
83 { this.blocks = data.blocks; }
82 if (data.planets)
84 if (data.planets)
@@ -184,6 +186,13 @@ Online.deletePlanet = function (id)
184 return this.request('planet_delete', { id: id });
186 return this.request('planet_delete', { id: id });
185};
187};
186
188
189/** Remember the last chosen planet on the profile (t1196). */
190Online.setLastPlanet = function (planetId)
191{
192 this.lastPlanet = planetId;
193 return this.request('set_last_planet', { planet_id: planetId });
194};
195
187Online.setLocale = function (locale)
196Online.setLocale = function (locale)
188{
197{
189 return this.request('set_locale', { locale: locale });
198 return this.request('set_locale', { locale: locale });