xonecas.io
5875
Os

ollama-space-molt-player / commit 9730ef0

refactor: extract session initialization logic and reorganize imports

xonecas2026-02-01 9730ef0936d9c80a838091b69af3b9724168108b parent f01de6b

3 files changed

src/game-state.ts+0−7
@@ -28,11 +28,4 @@ export class GameState {
2828 failedRegistrationNames: string[] = [];
2929
3030 readonly MAX_REGISTRATION_RETRIES = 3;
31-
32- resetTravelState() {
33- this.travelInProgress = false;
34- this.lastTravelTarget = null;
35- this.jumpInProgress = false;
36- this.lastJumpTarget = null;
37- }
3831 }
src/index.ts+38−49
@@ -380,30 +380,7 @@ client.on<StateUpdatePayload>("state_update", async (data) => {
380380
381381 // Start action loop since logged_in won't fire on v0.3.0+
382382 if (!client.state.authenticated) {
383- client.state.authenticated = true;
384- gameState.cachedPlayer = data.player;
385- gameState.cachedShip = data.ship;
386- gameState.lastCredits = data.player.credits;
387-
388- // Initialize other state
389- gameState.worldSnapshot.system = data.system;
390- gameState.worldSnapshot.poi = data.poi;
391- if (data.player.current_system)
392- gameState.lastSystemId = data.player.current_system;
393- if (data.player.current_poi)
394- gameState.lastPoiId = data.player.current_poi;
395- gameState.lastDocked = Boolean(data.player.docked_at_base);
396- gameState.currentGoal = memory.getLatestGoal(data.player.username);
397-
398- client.getSystem();
399- client.getPOI();
400- if (data.player.docked_at_base) {
401- client.getBase();
402- }
403-
404- updateTui();
405- saveSnapshot();
406- startActionLoop();
383+ initializeSessionFromStateUpdate(data);
407384 }
408385 } catch (err) {
409386 tui.log(
@@ -421,32 +398,10 @@ client.on<StateUpdatePayload>("state_update", async (data) => {
421398 ) {
422399 // Recovery: we have credentials, and received a state update for our user, but aren't flagged as authenticated/running.
423400 // This might happen if we reconnected and missed a welcome/login event or similar, or just standard 0.3.0+ flow
424- client.state.authenticated = true;
425- gameState.cachedPlayer = data.player;
426- gameState.cachedShip = data.ship;
427- gameState.lastCredits = data.player.credits;
428-
429- gameState.worldSnapshot.system = data.system;
430- gameState.worldSnapshot.poi = data.poi;
431- if (data.player.current_system)
432- gameState.lastSystemId = data.player.current_system;
433- if (data.player.current_poi) gameState.lastPoiId = data.player.current_poi;
434- gameState.lastDocked = Boolean(data.player.docked_at_base);
435- gameState.currentGoal = memory.getLatestGoal(data.player.username);
436-
437- // Refresh world data
438- client.getSystem();
439- client.getPOI();
440- if (data.player.docked_at_base) {
441- client.getBase();
442- }
443-
444- tui.log(
445- formatSystemMessage(`Recovered session for ${data.player.username}`),
401+ initializeSessionFromStateUpdate(
402+ data,
403+ `Recovered session for ${data.player.username}`,
446404 );
447- updateTui();
448- saveSnapshot();
449- startActionLoop();
450405 }
451406
452407 // Update cached player and ship data (only if provided)
@@ -582,3 +537,37 @@ function updateWorldSnapshotFromOk(data: Record<string, unknown>): void {
582537 gameState.worldSnapshot.base = data.base as WorldSnapshot["base"];
583538 }
584539 }
540+
541+function initializeSessionFromStateUpdate(
542+ data: StateUpdatePayload,
543+ message?: string,
544+): void {
545+ if (!data.player) return;
546+
547+ client.state.authenticated = true;
548+ gameState.cachedPlayer = data.player;
549+ gameState.cachedShip = data.ship;
550+ gameState.lastCredits = data.player.credits;
551+
552+ gameState.worldSnapshot.system = data.system;
553+ gameState.worldSnapshot.poi = data.poi;
554+ if (data.player.current_system)
555+ gameState.lastSystemId = data.player.current_system;
556+ if (data.player.current_poi) gameState.lastPoiId = data.player.current_poi;
557+ gameState.lastDocked = Boolean(data.player.docked_at_base);
558+ gameState.currentGoal = memory.getLatestGoal(data.player.username);
559+
560+ // Refresh world data
561+ client.getSystem();
562+ client.getPOI();
563+ if (data.player.docked_at_base) {
564+ client.getBase();
565+ }
566+
567+ if (message) {
568+ tui.log(formatSystemMessage(message));
569+ }
570+ updateTui();
571+ saveSnapshot();
572+ startActionLoop();
573+}
src/prompt.ts+2−5
@@ -1,11 +1,8 @@
11 import type { ClientState } from "../client/src/client";
22 import type { Base, POI, System } from "../client/src/types";
3-import {
4- MAX_GOAL_LENGTH,
5- PERSONALITY_ARCHETYPES,
6- type PersonalityType,
7-} from "./actions";
3+import { MAX_GOAL_LENGTH, PERSONALITY_ARCHETYPES } from "./constants";
84 import type { HistoryEntry } from "./memory";
5+import type { PersonalityType } from "./types";
96
107 export interface PromptContext {
118 state: ClientState;