From fd70f74481a5f94aed43ffda8cfbb0985b5b9706 Mon Sep 17 00:00:00 2001 From: LeCodex <40335983+LeCodex@users.noreply.github.com> Date: Mon, 30 Dec 2024 14:55:51 +0100 Subject: [PATCH] [~] Fix reloading games overriding the save file --- src/oath/game/actions/manager.ts | 26 +++++++++++++------------- src/oath/game/game.ts | 6 +++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/oath/game/actions/manager.ts b/src/oath/game/actions/manager.ts index df053b1..c47ac19 100644 --- a/src/oath/game/actions/manager.ts +++ b/src/oath/game/actions/manager.ts @@ -18,7 +18,7 @@ export class HistoryNode { return this.events.map(e => JSON.stringify(e.serialize())).join("\n"); } - parse(data: string) { + parse(data: string, save: boolean = true) { if (data === "") return; const lines = data.split("\n"); for (const [i, line] of lines.entries()) { @@ -26,7 +26,7 @@ export class HistoryNode { const { name, player, data } = JSON.parse(line) as ReturnType; // This is a "dummy" event that just replays the actions. The replay takes care of adding the actual events back const event = new eventsIndex[name](this.manager, player, data); - event.replay(); + event.replay(save); } } } @@ -39,7 +39,7 @@ export abstract class HistoryEvent { public player: string, ) { } - abstract replay(): void; + abstract replay(save?: boolean): void; serialize() { return { @@ -56,8 +56,8 @@ export class StartEvent extends HistoryEvent { public actionName: string ) { super(manager, player); } - replay() { - this.manager.startAction(this.actionName); + replay(save: boolean = true) { + this.manager.startAction(this.actionName, save); } serialize() { @@ -74,8 +74,8 @@ export class ContinueEvent extends HistoryEvent { public values: Record ) { super(manager, player); } - replay() { - this.manager.continueAction(this.player, this.values); + replay(save: boolean = true) { + this.manager.continueAction(this.player, this.values, save); } serialize() { @@ -163,8 +163,8 @@ export class OathActionManager { if (continueNow) this.resolveTopAction(); } - defer(): ActionManagerReturn { - if (this.game.phase !== OathPhase.Over) this.game.save(); + defer(save: boolean = true): ActionManagerReturn { + if (save && this.game.phase !== OathPhase.Over) this.game.save(); let action = this.actionsStack[this.actionsStack.length - 1]; return { @@ -177,7 +177,7 @@ export class OathActionManager { }; } - startAction(actionName: string) { + startAction(actionName: string, save: boolean = true) { const action = this.startOptions[actionName]; if (!action) throw new InvalidActionResolution("Invalid starting action name"); @@ -193,7 +193,7 @@ export class OathActionManager { try { this.checkForNextAction(); event.oneWay = this.markEventAsOneWay; - return this.defer(); + return this.defer(save); } catch (e) { this.authorizeCancel(); throw e; @@ -206,7 +206,7 @@ export class OathActionManager { return player; } - continueAction(playerId: string, values: Record) { + continueAction(playerId: string, values: Record, save: boolean = true) { const action = this.actionsStack[this.actionsStack.length - 1]; if (!action) throw new InvalidActionResolution("No action to continue"); @@ -225,7 +225,7 @@ export class OathActionManager { try { this.resolveTopAction(); event.oneWay = this.markEventAsOneWay; - return this.defer(); + return this.defer(save); } catch (e) { this.authorizeCancel(); throw e; diff --git a/src/oath/game/game.ts b/src/oath/game/game.ts index 10dc74b..b1390a3 100644 --- a/src/oath/game/game.ts +++ b/src/oath/game/game.ts @@ -18,10 +18,10 @@ import { CardName, Citizenship, PlayerCitizenship } from "./parser/interfaces"; import { hasPowers, SourceType, WithPowers } from "./interfaces"; import { Favor, Warband, Secret } from "./resources"; import { Reliquary, ReliquarySlot } from "./reliquary"; -import classIndex from "./classIndex"; import { constant, times } from "lodash"; -import * as fs from "fs"; import { SiteName } from "./cards/sites"; +import classIndex from "./classIndex"; +import * as fs from "fs"; export class OathGame extends TreeRoot { @@ -404,7 +404,7 @@ export class OathGame extends TreeRoot { for (const [i, nodeData] of chunks.entries()) { console.log(`Resolving chunk ${i}`); const node = new HistoryNode(game.actionManager, game.serialize(true)); - node.parse(nodeData); + node.parse(nodeData, false); } return game;