Skip to content

Commit

Permalink
[~] Fix reloading games overriding the save file
Browse files Browse the repository at this point in the history
  • Loading branch information
LeCodex committed Dec 30, 2024
1 parent 89a32bb commit fd70f74
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 13 additions & 13 deletions src/oath/game/actions/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ export class HistoryNode<T extends OathActionManager> {
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()) {
console.log(` Resolving event ${i}: ${line}`);
const { name, player, data } = JSON.parse(line) as ReturnType<HistoryEvent["serialize"]>;
// 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);
}
}
}
Expand All @@ -39,7 +39,7 @@ export abstract class HistoryEvent {
public player: string,
) { }

abstract replay(): void;
abstract replay(save?: boolean): void;

serialize() {
return {
Expand All @@ -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() {
Expand All @@ -74,8 +74,8 @@ export class ContinueEvent extends HistoryEvent {
public values: Record<string, string[]>
) { super(manager, player); }

replay() {
this.manager.continueAction(this.player, this.values);
replay(save: boolean = true) {
this.manager.continueAction(this.player, this.values, save);
}

serialize() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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");

Expand All @@ -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;
Expand All @@ -206,7 +206,7 @@ export class OathActionManager {
return player;
}

continueAction(playerId: string, values: Record<string, string[]>) {
continueAction(playerId: string, values: Record<string, string[]>, save: boolean = true) {
const action = this.actionsStack[this.actionsStack.length - 1];
if (!action) throw new InvalidActionResolution("No action to continue");

Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/oath/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OathGame> {
Expand Down Expand Up @@ -404,7 +404,7 @@ export class OathGame extends TreeRoot<OathGame> {
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;
Expand Down

0 comments on commit fd70f74

Please sign in to comment.