refactor(state): fix code quality findings for runtime and story log
This commit is contained in:
+25
-21
@@ -41,6 +41,16 @@ export class GameRuntime {
|
||||
private lastSaveAt = 0;
|
||||
private booted = false;
|
||||
|
||||
private readonly visibilityChangeListener = (): void => {
|
||||
if (document.visibilityState === 'hidden') {
|
||||
void this.save();
|
||||
}
|
||||
};
|
||||
|
||||
private readonly beforeUnloadListener = (): void => {
|
||||
void this.save();
|
||||
};
|
||||
|
||||
async boot(): Promise<void> {
|
||||
if (this.booted) {
|
||||
return;
|
||||
@@ -75,6 +85,8 @@ export class GameRuntime {
|
||||
cancelAnimationFrame(this.rafId);
|
||||
this.rafId = null;
|
||||
}
|
||||
document.removeEventListener('visibilitychange', this.visibilityChangeListener);
|
||||
window.removeEventListener('beforeunload', this.beforeUnloadListener);
|
||||
}
|
||||
|
||||
performAction(actionId: string): void {
|
||||
@@ -88,6 +100,11 @@ export class GameRuntime {
|
||||
const events = performActionEngine(state, content, actionId);
|
||||
const store = useGameStore.getState();
|
||||
|
||||
// Append any custom log outcomes
|
||||
for (const event of events.filter((e) => e.kind === 'log')) {
|
||||
store.appendLog(event.prose);
|
||||
}
|
||||
|
||||
if (isStory) {
|
||||
const entries = storyEventsToLogEntries(events);
|
||||
for (const entry of entries) {
|
||||
@@ -114,19 +131,6 @@ export class GameRuntime {
|
||||
store.setActivePanel(panel);
|
||||
if (panel === 'story') {
|
||||
store.setStoryHasUnread(false);
|
||||
const state = this.state;
|
||||
if (state && state.currentStoryNodeId === 'boot_intro') {
|
||||
const events = enterStoryNode(state, content, 'fork_choice');
|
||||
const entries = storyEventsToLogEntries(events);
|
||||
for (const entry of entries) {
|
||||
store.appendStoryLog(entry);
|
||||
}
|
||||
for (const entry of entries) {
|
||||
const nodeProse = content.storyNodesById[entry.nodeId]?.prose ?? '';
|
||||
store.appendLog(`Story: ${nodeProse.slice(0, 40)}…`);
|
||||
}
|
||||
this.publish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +149,12 @@ export class GameRuntime {
|
||||
const events = engineApplyChoice(state, content, choiceId);
|
||||
const entries = storyEventsToLogEntries(events);
|
||||
const store = useGameStore.getState();
|
||||
|
||||
// Append any custom log outcomes
|
||||
for (const event of events.filter((e) => e.kind === 'log')) {
|
||||
store.appendLog(event.prose);
|
||||
}
|
||||
|
||||
for (const entry of entries) store.appendStoryLog(entry);
|
||||
const choiceLabel = entries.at(-1)?.choiceLabel;
|
||||
if (choiceLabel) store.appendLog(`Story: ${choiceLabel}`);
|
||||
@@ -279,14 +289,8 @@ export class GameRuntime {
|
||||
}
|
||||
|
||||
private installLifecycleHooks(): void {
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'hidden') {
|
||||
void this.save();
|
||||
}
|
||||
});
|
||||
window.addEventListener('beforeunload', () => {
|
||||
void this.save();
|
||||
});
|
||||
document.addEventListener('visibilitychange', this.visibilityChangeListener);
|
||||
window.addEventListener('beforeunload', this.beforeUnloadListener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user