Add storage-backed story history
This commit is contained in:
@@ -109,12 +109,27 @@ export class InkEngine {
|
||||
if (!this.story) {
|
||||
throw new Error('No active Ink story to save');
|
||||
}
|
||||
return this.story.state.toJson();
|
||||
return JSON.stringify({
|
||||
inkState: this.story.state.toJson(),
|
||||
nextTurnId: this.nextTurnId,
|
||||
});
|
||||
}
|
||||
|
||||
loadGame(savedState: string): TurnResult {
|
||||
this.story = this.loadStory();
|
||||
this.story.state.LoadJson(savedState);
|
||||
let inkState = savedState;
|
||||
try {
|
||||
const parsed = JSON.parse(savedState);
|
||||
if (parsed && typeof parsed.inkState === 'string') {
|
||||
inkState = parsed.inkState;
|
||||
if (Number.isInteger(parsed.nextTurnId)) {
|
||||
this.nextTurnId = Math.max(1, parsed.nextTurnId);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Backward compatibility with raw Ink state JSON.
|
||||
}
|
||||
this.story.state.LoadJson(inkState);
|
||||
return this.continueStory();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user