xonecas.io
5875
Os

ollama-space-molt-player / commit ce73a14

refactor: move auto-login logic from ws connection to welcome handler

xonecas2026-02-03 ce73a140a56df90222414ab30098a31865729452 parent e940c44

1 file changed

packages/client/src/game-client.ts+105−25
@@ -159,11 +159,11 @@ export class GameClient {
159159 payload: { username: activeUsername.username, token: activeUsername.token },
160160 });
161161 } else {
162- await this.processTick().catch((error) => {
163- console.error(`[${this.config.instanceId}] Initial tick processing error:`, error);
162+ await this.promptForRegistration().catch((error) => {
163+ console.error(`[${this.config.instanceId}] Registration prompt error:`, error);
164164 this.db.saveMessage(this.currentTick, "client", {
165- error: "Initial tick processing failed",
166- code: "TICK_PROCESSING_ERROR",
165+ error: "Registration prompt failed",
166+ code: "REGISTRATION_PROMPT_ERROR",
167167 });
168168 });
169169 }
@@ -218,9 +218,29 @@ export class GameClient {
218218
219219 console.log(`[${this.config.instanceId}] Registration successful`);
220220
221+ const characterPrompt = await this.generateCharacterPrompt(
222+ this.registrationData.username,
223+ this.registrationData.empire,
224+ );
225+
226+ this.db.saveUsername(
227+ this.registrationData.username,
228+ message.payload.token,
229+ characterPrompt,
230+ message.payload.player_id,
231+ this.currentTick,
232+ );
233+
234+ console.log(`[${this.config.instanceId}] Character prompt generated and saved`);
235+
236+ this.waitingForRegistration = false;
237+ this.registrationData = null;
238+ }
239+
240+ private async generateCharacterPrompt(username: string, empire: string): Promise<string> {
221241 const archetypeList = getArchetypeListText(this.config.archetype);
222242
223- const promptGeneration = `You are creating a character for a sci-fi themed online multiplayer game called SpaceMolt. The character's name is "${this.registrationData.username}" and they belong to the "${this.registrationData.empire}" empire.
243+ const promptGeneration = `You are creating a character for a sci-fi themed online multiplayer game called SpaceMolt. The character's name is "${username}" and they belong to the "${empire}" empire.
224244
225245 Choose a personality archetype from this list:
226246 ${archetypeList}
@@ -236,37 +256,97 @@ Character prompt:`;
236256 console.log(`[${this.config.instanceId}] Thinking: ${result.thinking}`);
237257 }
238258
239- this.db.saveUsername(
240- this.registrationData.username,
241- message.payload.token,
242- result.response,
243- message.payload.player_id,
244- this.currentTick,
245- );
246-
247- console.log(`[${this.config.instanceId}] Character prompt generated and saved`);
259+ return result.response;
248260 } catch (error) {
249261 console.error(`[${this.config.instanceId}] Failed to generate prompt:`, error);
262+ return `You are ${username}, a ${empire} player in SpaceMolt.`;
263+ }
264+ }
265+
266+ private async promptForRegistration(): Promise<void> {
267+ const registerApi = {
268+ register: PLAYER_API_REFERENCE.register,
269+ };
250270
251- this.db.saveUsername(
252- this.registrationData.username,
253- message.payload.token,
254- `You are ${this.registrationData.username}, a ${this.registrationData.empire} player in SpaceMolt.`,
255- message.payload.player_id,
256- this.currentTick,
271+ const minimalContext = {
272+ api: registerApi,
273+ note: "You must register before you can play. Choose a creative username prefix - the system will add a unique suffix.",
274+ };
275+
276+ const contextJson = JSON.stringify(minimalContext);
277+ const registrationPrompt = `You are playing a multiplayer space game called SpaceMolt. You need to register a username to play.
278+
279+Game Context:
280+${contextJson}
281+
282+Choose a creative username prefix (the system will add a unique suffix for you).
283+Choose an empire from: solarian, voidborn, crimson, nebula, outerrim
284+
285+CRITICAL: You must respond with ONLY valid JSON. No explanations, no markdown, just the JSON command.
286+
287+JSON Format:
288+{"type": "register", "payload": {"username": "YourChosenPrefix", "empire": "solarian"}}
289+
290+Your JSON response:`;
291+
292+ try {
293+ const result = await this.ollama.generate(registrationPrompt);
294+
295+ if (result.thinking) {
296+ console.log(`[${this.config.instanceId}] Thinking:\n${result.thinking}`);
297+ }
298+
299+ let command: BaseCommand;
300+ try {
301+ command = JSON.parse(result.response);
302+ } catch {
303+ command = this.extractJSON(result.response);
304+ }
305+
306+ if (command.type !== "register" || !command.payload) {
307+ throw new Error("Invalid registration command: missing type or payload");
308+ }
309+
310+ const payload = command.payload as { username: string; empire: string };
311+ if (!payload.username || !payload.empire) {
312+ throw new Error("Invalid registration payload: missing username or empire");
313+ }
314+
315+ const instanceSuffix = `-${this.config.instanceId}`;
316+ if (!payload.username.endsWith(instanceSuffix)) {
317+ payload.username = `${payload.username}${instanceSuffix}`;
318+ }
319+
320+ console.log(
321+ `[${this.config.instanceId}] → register (username: ${payload.username}, empire: ${payload.empire})`,
257322 );
258- }
259323
260- this.waitingForRegistration = false;
261- this.registrationData = null;
324+ this.waitingForRegistration = true;
325+ this.registrationData = {
326+ username: payload.username,
327+ empire: payload.empire,
328+ };
329+
330+ this.ws.send(command);
331+ this.db.saveMessage(this.currentTick, "client", command);
332+ } catch (error) {
333+ console.error(`[${this.config.instanceId}] Registration prompt failed:`, error);
334+ this.db.saveMessage(this.currentTick, "client", {
335+ error: error instanceof Error ? error.message : "Registration prompt failed",
336+ code: "REGISTRATION_PROMPT_FAILED",
337+ });
338+ }
262339 }
263340
264341 private handleLoggedIn(_message: LoggedInMessage): void {}
265342
266343 private handleServerError(message: ErrorMessage): void {
267344 if (this.waitingForRegistration && this.registrationData) {
268- console.log(`[${this.config.instanceId}] Registration failed, will retry on next tick`);
345+ console.log(`[${this.config.instanceId}] Registration failed, retrying...`);
269346 this.waitingForRegistration = false;
347+ this.promptForRegistration().catch((error) => {
348+ console.error(`[${this.config.instanceId}] Registration retry error:`, error);
349+ });
270350 }
271351 }
272352
@@ -310,7 +390,7 @@ JSON Format Rules:
310390 - Actions WITHOUT arguments: {"type": "action_name"}
311391
312392 Examples:
313-- {"type": "action_name"} ← Correct (no payload needed)
393+- {"type": "action_name_without_arguments"} ← Correct (no payload needed)
314394 - {"type": "action_name_with_arguments", "payload": {"target_poi": "sol_belt"}} ← Correct
315395
316396 NEVER write "undefined" or "null" as payload value. If an action has no arguments, omit the payload field entirely.