BlueGit

projects-timfalken-com meteors

feat: load custom planets into every mode's picker (t1140)

The catalog picker includes the player's own planets. Online sends the
id; the server snapshot is remembered on every client so opponents
simulate the same physics, miss chance and sprites.

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
2f1cc9c1df41770e6de70ad90fdf99cfa834d521
Parent
40f2cf58
2 files changed +115 −0
src/online.js +49 −0
@@ -14,6 +14,10 @@ var Online = {
14 14 local: false,
15 15 csrf: '',
16 16 me: null,
17 lifetimePoints: 0,
18 locale: null,
19 blocks: [],
20 planets: [],
17 21 lobby: null,
18 22 incoming: [],
19 23 shotResults: [],
@@ -65,6 +69,26 @@ Online._apply = function (data)
65 69 { this.error = data.error; }
66 70 else if (data.ok)
67 71 { this.error = ''; }
72 if (data.lifetime_points != null)
73 { this.lifetimePoints = data.lifetime_points; }
74 if (data.locale !== undefined)
75 { this.locale = data.locale; }
76 if (data.blocks)
77 { this.blocks = data.blocks; }
78 if (data.planets)
79 {
80 this.planets = data.planets;
81 if (typeof Planet !== 'undefined' && Planet.setCustomPlanets)
82 { Planet.setCustomPlanets(data.planets); }
83 }
84 if (data.lobby && data.lobby.players && typeof Planet !== 'undefined' && Planet.rememberPlanet)
85 {
86 for (var i = 0; i < data.lobby.players.length; i++)
87 {
88 if (data.lobby.players[i].planet)
89 { Planet.rememberPlanet(data.lobby.players[i].planet); }
90 }
91 }
68 92 };
69 93
70 94 Online.request = function (action, body)
@@ -136,6 +160,31 @@ Online.setPlanet = function (planetId)
136 160 return this.request('lobby_set_planet', { planet_id: planetId });
137 161 };
138 162
163 Online.playBegin = function (mode)
164 {
165 return this.request('play_begin', { mode: mode || 'solo' });
166 };
167
168 Online.playFinish = function (token, score)
169 {
170 return this.request('play_finish', { token: token, score: score || 0 });
171 };
172
173 Online.savePlanet = function (planet)
174 {
175 return this.request('planet_save', { planet: planet });
176 };
177
178 Online.deletePlanet = function (id)
179 {
180 return this.request('planet_delete', { id: id });
181 };
182
183 Online.setLocale = function (locale)
184 {
185 return this.request('set_locale', { locale: locale });
186 };
187
139 188 Online.leaveLobby = function ()
140 189 {
141 190 return this.request('lobby_leave');
@@ -14,6 +14,10 @@ var Online = {
14 local: false,
14 local: false,
15 csrf: '',
15 csrf: '',
16 me: null,
16 me: null,
17 lifetimePoints: 0,
18 locale: null,
19 blocks: [],
20 planets: [],
17 lobby: null,
21 lobby: null,
18 incoming: [],
22 incoming: [],
19 shotResults: [],
23 shotResults: [],
@@ -65,6 +69,26 @@ Online._apply = function (data)
65 { this.error = data.error; }
69 { this.error = data.error; }
66 else if (data.ok)
70 else if (data.ok)
67 { this.error = ''; }
71 { this.error = ''; }
72 if (data.lifetime_points != null)
73 { this.lifetimePoints = data.lifetime_points; }
74 if (data.locale !== undefined)
75 { this.locale = data.locale; }
76 if (data.blocks)
77 { this.blocks = data.blocks; }
78 if (data.planets)
79 {
80 this.planets = data.planets;
81 if (typeof Planet !== 'undefined' && Planet.setCustomPlanets)
82 { Planet.setCustomPlanets(data.planets); }
83 }
84 if (data.lobby && data.lobby.players && typeof Planet !== 'undefined' && Planet.rememberPlanet)
85 {
86 for (var i = 0; i < data.lobby.players.length; i++)
87 {
88 if (data.lobby.players[i].planet)
89 { Planet.rememberPlanet(data.lobby.players[i].planet); }
90 }
91 }
68};
92};
69
93
70Online.request = function (action, body)
94Online.request = function (action, body)
@@ -136,6 +160,31 @@ Online.setPlanet = function (planetId)
136 return this.request('lobby_set_planet', { planet_id: planetId });
160 return this.request('lobby_set_planet', { planet_id: planetId });
137};
161};
138
162
163Online.playBegin = function (mode)
164{
165 return this.request('play_begin', { mode: mode || 'solo' });
166};
167
168Online.playFinish = function (token, score)
169{
170 return this.request('play_finish', { token: token, score: score || 0 });
171};
172
173Online.savePlanet = function (planet)
174{
175 return this.request('planet_save', { planet: planet });
176};
177
178Online.deletePlanet = function (id)
179{
180 return this.request('planet_delete', { id: id });
181};
182
183Online.setLocale = function (locale)
184{
185 return this.request('set_locale', { locale: locale });
186};
187
139Online.leaveLobby = function ()
188Online.leaveLobby = function ()
140{
189{
141 return this.request('lobby_leave');
190 return this.request('lobby_leave');
src/planet.js +66 −0
@@ -105,6 +105,34 @@ function loadCatalog ()
105 105 return { planets: [] };
106 106 }
107 107
108 /** Player-owned custom planets (from the profile) and any remembered
109 * snapshots (opponents in a lobby), not part of the official catalog. */
110 var _customPlanets = [];
111 var _remembered = Object.create(null);
112
113 function setCustomPlanets (list)
114 {
115 _customPlanets = [];
116 if (!list || !list.length)
117 { return; }
118 for (var i = 0; i < list.length; i++)
119 { _customPlanets.push(planetFromData(list[i])); }
120 }
121
122 function rememberPlanet (data)
123 {
124 if (!data || !data.id)
125 { return null; }
126 var pl = planetFromData(data);
127 _remembered[pl.id] = pl;
128 return pl;
129 }
130
131 function forgetRemembered ()
132 {
133 _remembered = Object.create(null);
134 }
135
108 136 function planetFromData (data)
109 137 {
110 138 var planet = createPlanet({
@@ -116,6 +144,10 @@ function planetFromData (data)
116 144 colors: data.colors
117 145 });
118 146 planet.id = data.id;
147 if (data.custom)
148 { planet.custom = true; }
149 if (data.owner_id)
150 { planet.owner_id = data.owner_id; }
119 151 return planet;
120 152 }
121 153
@@ -127,6 +159,8 @@ function allPlanets ()
127 159 { out.push(planetFromData(raw[i])); }
128 160 if (out.length === 0)
129 161 { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; }
162 for (var c = 0; c < _customPlanets.length; c++)
163 { out.push(_customPlanets[c]); }
130 164 return out;
131 165 }
132 166
@@ -138,9 +172,30 @@ function planetById (id)
138 172 if (all[i].id === id)
139 173 { return all[i]; }
140 174 }
175 if (id && _remembered[id])
176 { return _remembered[id]; }
141 177 return defaultPlanet();
142 178 }
143 179
180 function officialPlanets ()
181 {
182 var raw = loadCatalog().planets || [];
183 var out = [];
184 for (var i = 0; i < raw.length; i++)
185 { out.push(planetFromData(raw[i])); }
186 return out;
187 }
188
189 function customPlanets ()
190 {
191 return _customPlanets.slice();
192 }
193
194 function pickerPlanets ()
195 {
196 return officialPlanets().concat(_customPlanets);
197 }
198
144 199 function defaultPlanet ()
145 200 {
146 201 var all = allPlanets();
@@ -167,6 +222,11 @@ function spriteKey (planet, colorIndex)
167 222 var c = colors && colors[colorIndex];
168 223 if (!c)
169 224 { return 'gray'; }
225 // Custom planets (and any color that names a registry block) share
226 // the preloaded Blocks.images() sprite, so late-loaded customs and
227 // opponent snapshots do not need a second preload.
228 if (c.block)
229 { return 'block:' + c.block; }
170 230 return (planet.id || 'planet') + '-' + (c.name || String(colorIndex));
171 231 }
172 232
@@ -264,6 +324,12 @@ var Planet = {
264 324 defaultPlanet: defaultPlanet,
265 325 defaultColors: defaultColors,
266 326 allPlanets: allPlanets,
327 officialPlanets: officialPlanets,
328 customPlanets: customPlanets,
329 pickerPlanets: pickerPlanets,
330 setCustomPlanets: setCustomPlanets,
331 rememberPlanet: rememberPlanet,
332 forgetRemembered: forgetRemembered,
267 333 planetById: planetById,
268 334 planetFromData: planetFromData,
269 335 formatStats: formatStats,
@@ -105,6 +105,34 @@ function loadCatalog ()
105 return { planets: [] };
105 return { planets: [] };
106}
106}
107
107
108/** Player-owned custom planets (from the profile) and any remembered
109 * snapshots (opponents in a lobby), not part of the official catalog. */
110var _customPlanets = [];
111var _remembered = Object.create(null);
112
113function setCustomPlanets (list)
114{
115 _customPlanets = [];
116 if (!list || !list.length)
117 { return; }
118 for (var i = 0; i < list.length; i++)
119 { _customPlanets.push(planetFromData(list[i])); }
120}
121
122function rememberPlanet (data)
123{
124 if (!data || !data.id)
125 { return null; }
126 var pl = planetFromData(data);
127 _remembered[pl.id] = pl;
128 return pl;
129}
130
131function forgetRemembered ()
132{
133 _remembered = Object.create(null);
134}
135
108function planetFromData (data)
136function planetFromData (data)
109{
137{
110 var planet = createPlanet({
138 var planet = createPlanet({
@@ -116,6 +144,10 @@ function planetFromData (data)
116 colors: data.colors
144 colors: data.colors
117 });
145 });
118 planet.id = data.id;
146 planet.id = data.id;
147 if (data.custom)
148 { planet.custom = true; }
149 if (data.owner_id)
150 { planet.owner_id = data.owner_id; }
119 return planet;
151 return planet;
120}
152}
121
153
@@ -127,6 +159,8 @@ function allPlanets ()
127 { out.push(planetFromData(raw[i])); }
159 { out.push(planetFromData(raw[i])); }
128 if (out.length === 0)
160 if (out.length === 0)
129 { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; }
161 { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; }
162 for (var c = 0; c < _customPlanets.length; c++)
163 { out.push(_customPlanets[c]); }
130 return out;
164 return out;
131}
165}
132
166
@@ -138,9 +172,30 @@ function planetById (id)
138 if (all[i].id === id)
172 if (all[i].id === id)
139 { return all[i]; }
173 { return all[i]; }
140 }
174 }
175 if (id && _remembered[id])
176 { return _remembered[id]; }
141 return defaultPlanet();
177 return defaultPlanet();
142}
178}
143
179
180function officialPlanets ()
181{
182 var raw = loadCatalog().planets || [];
183 var out = [];
184 for (var i = 0; i < raw.length; i++)
185 { out.push(planetFromData(raw[i])); }
186 return out;
187}
188
189function customPlanets ()
190{
191 return _customPlanets.slice();
192}
193
194function pickerPlanets ()
195{
196 return officialPlanets().concat(_customPlanets);
197}
198
144function defaultPlanet ()
199function defaultPlanet ()
145{
200{
146 var all = allPlanets();
201 var all = allPlanets();
@@ -167,6 +222,11 @@ function spriteKey (planet, colorIndex)
167 var c = colors && colors[colorIndex];
222 var c = colors && colors[colorIndex];
168 if (!c)
223 if (!c)
169 { return 'gray'; }
224 { return 'gray'; }
225 // Custom planets (and any color that names a registry block) share
226 // the preloaded Blocks.images() sprite, so late-loaded customs and
227 // opponent snapshots do not need a second preload.
228 if (c.block)
229 { return 'block:' + c.block; }
170 return (planet.id || 'planet') + '-' + (c.name || String(colorIndex));
230 return (planet.id || 'planet') + '-' + (c.name || String(colorIndex));
171}
231}
172
232
@@ -264,6 +324,12 @@ var Planet = {
264 defaultPlanet: defaultPlanet,
324 defaultPlanet: defaultPlanet,
265 defaultColors: defaultColors,
325 defaultColors: defaultColors,
266 allPlanets: allPlanets,
326 allPlanets: allPlanets,
327 officialPlanets: officialPlanets,
328 customPlanets: customPlanets,
329 pickerPlanets: pickerPlanets,
330 setCustomPlanets: setCustomPlanets,
331 rememberPlanet: rememberPlanet,
332 forgetRemembered: forgetRemembered,
267 planetById: planetById,
333 planetById: planetById,
268 planetFromData: planetFromData,
334 planetFromData: planetFromData,
269 formatStats: formatStats,
335 formatStats: formatStats,