Skip to content

Commit

Permalink
fix loading an earlier save
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-vorobiov committed Oct 10, 2024
1 parent 2c481d9 commit b6fa973
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
2 changes: 1 addition & 1 deletion evolve_analytics.meta.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Evolve Analytics
// @namespace http://tampermonkey.net/
// @version 0.6.5
// @version 0.6.6
// @description Track and see detailed information about your runs
// @author Sneed
// @match https://pmotschmann.github.io/Evolve/
Expand Down
37 changes: 21 additions & 16 deletions src/runTracking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { saveCurrentRun, loadLatestRun, discardLatestRun } from "./database";
import { makeMilestoneChecker, type MilestoneChecker } from "./milestones";
import { filterMap } from "./utils/map";
import type { resets, universes } from "./enums";
import type { Game } from "./game";
import type { ConfigManager } from "./config";
Expand All @@ -25,36 +26,40 @@ export function inferResetType(runStats: LatestRun, game: Game) {
return reset ?? "unknown";
}

export function isCurrentRun(runStats: LatestRun, game: Game) {
return runStats.run === game.runNumber && runStats.totalDays <= game.day;
function isCurrentRun(runStats: LatestRun, game: Game) {
return runStats.run === game.runNumber;
}

export function isPreviousRun(runStats: LatestRun, game: Game) {
function isPreviousRun(runStats: LatestRun, game: Game) {
return runStats.run === game.runNumber - 1;
}

export function restoreToDay(run: LatestRun, day: number) {
run.milestones = filterMap(run.milestones, ([, timestamp]) => timestamp <= day);
run.totalDays = day;
}

export function processLatestRun(game: Game, config: ConfigManager, history: HistoryManager) {
const latestRun = loadLatestRun();

if (latestRun === null) {
return;
}

// Don't commit the last run if history is paused
if (!config.recordRuns) {
discardLatestRun();
return;
if (isCurrentRun(latestRun, game)) {
// If it is the current run, check if we leaded an earlier save - discard any milestones "from the future"
restoreToDay(latestRun, game.day);
saveCurrentRun(latestRun);
}

// If it's not the current run, discard it so that we can start tracking from scratch
if (!isCurrentRun(latestRun, game)) {
else {
// If it's not the current run, discard it so that we can start tracking from scratch
discardLatestRun();
}

// The game refreshes the page after a reset
// Thus, if the latest run is the previous one, it can be comitted to history
if (isPreviousRun(latestRun, game)) {
history.commitRun(latestRun);
// The game refreshes the page after a reset
// Thus, if the latest run is the previous one, it can be comitted to history
if (isPreviousRun(latestRun, game) && config.recordRuns) {
history.commitRun(latestRun);
}
}
}

Expand All @@ -63,7 +68,7 @@ function makeNewRunStats(game: Game): LatestRun {
run: game.runNumber,
universe: game.universe,
resets: game.resetCounts,
totalDays: 0,
totalDays: game.day,
milestones: {}
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export function transformMap<K extends keyof any, V, KNew extends keyof any, VNe
return Object.fromEntries(Object.entries(obj).map(([k, v]) => fn([k as K, v as V]))) as Record<KNew, VNew>;
}

export function filterMap<K extends string, V>(obj: Record<K, V>, fn: (kv: [K, V]) => boolean): Record<K, V> {
return Object.fromEntries(Object.entries(obj).filter(fn)) as Record<K, V>;
}

export function rotateMap<K extends keyof any, V extends keyof any>(obj: Record<K, V>): Record<V, K> {
return transformMap(obj, ([k, v]) => [v, k]);
}
Expand Down
24 changes: 1 addition & 23 deletions test/latestRunInference.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from "@jest/globals";
import { makeGameState } from "./fixture";

import { inferResetType, isCurrentRun, isPreviousRun, type LatestRun } from "../src/runTracking";
import { inferResetType, type LatestRun } from "../src/runTracking";
import { Game } from "../src/game";

function makeRunStats(info: Partial<LatestRun>): LatestRun {
Expand Down Expand Up @@ -41,26 +41,4 @@ describe("Latest run info", () => {
expect(inferResetType(run, game)).toBe("unknown");
});
});

describe("Run order inference", () => {
it("should detect current run", () => {
const game = new Game(makeGameState({ reset: 123, days: 456 }));

expect(isCurrentRun(makeRunStats({ run: 124, totalDays: 455 }), game)).toBe(true);
expect(isCurrentRun(makeRunStats({ run: 124, totalDays: 456 }), game)).toBe(true);

expect(isCurrentRun(makeRunStats({ run: 124, totalDays: 457 }), game)).toBe(false);
expect(isCurrentRun(makeRunStats({ run: 123, totalDays: 455 }), game)).toBe(false);
expect(isCurrentRun(makeRunStats({ run: 125, totalDays: 455 }), game)).toBe(false);
});

it("should detect previous run", () => {
const game = new Game(makeGameState({ reset: 123 }));

expect(isPreviousRun(makeRunStats({ run: 123 }), game)).toBe(true);

expect(isPreviousRun(makeRunStats({ run: 122 }), game)).toBe(false);
expect(isPreviousRun(makeRunStats({ run: 124 }), game)).toBe(false);
});
});
});
16 changes: 10 additions & 6 deletions test/latestRunProcessing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ describe("Latest run", () => {
});

describe("Current run", () => {
const run: LatestRun = { run: 123, universe: "standard", resets: {}, totalDays: 456, milestones: {} };
const run: LatestRun = { run: 123, universe: "standard", resets: {}, totalDays: 456, milestones: { foo: 123, bar: 234 } };

let game: Game;
let config: ConfigManager;
let history: HistoryManager;

beforeEach(() => {
game = new Game(makeGameState({ reset: 122, days: 456 }));
game = new Game(makeGameState({ reset: 122, days: 123 }));
config = makeConfig(game);
history = new HistoryManager(game, config, blankHistory());

Expand All @@ -70,14 +70,18 @@ describe("Latest run", () => {
processLatestRun(game, config, history);
});

it("should not discard the run", () => {
expect(loadLatestRun()).toEqual(run);
});

it("should not commit the run to history", () => {
expect(history.commitRun).not.toHaveBeenCalled();
expect(loadHistory()).toBe(null);
});

it("should discard future milestones", () => {
expect(loadLatestRun()).toEqual({
...run,
totalDays: 123,
milestones: { foo: 123 }
});
});
});

describe("Previous run", () => {
Expand Down

0 comments on commit b6fa973

Please sign in to comment.