Skip to content

Commit

Permalink
migrate bigbang
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-vorobiov committed Oct 18, 2024
1 parent 5633f3d commit c89f240
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ export class ConfigManager extends Subscribable {
}

export function getConfig(game: Game) {
const config = loadConfig() ?? { version: 6, recordRuns: true, views: [] };
const config = loadConfig() ?? { version: 7, recordRuns: true, views: [] };
return new ConfigManager(game, config);
}
5 changes: 3 additions & 2 deletions src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { inferResetType, type LatestRun } from "./runTracking";
import { shouldIncludeRun } from "./exports/historyFiltering";
import { Subscribable } from "./subscribable";
import { rotateMap } from "./utils"
import type { universes } from "./enums";
import type { Game } from "./game";
import type { ConfigManager } from "./config";

export type MilestoneReference = [number, number];

export type HistoryEntry = {
run: number,
universe: string,
universe: keyof typeof universes,
milestones: MilestoneReference[],
raceName?: string
}
Expand Down Expand Up @@ -65,7 +66,7 @@ export class HistoryManager extends Subscribable {

const entry: HistoryEntry = {
run: runStats.run,
universe: runStats.universe,
universe: runStats.universe!,
milestones
};

Expand Down
6 changes: 3 additions & 3 deletions src/migration/3.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { buildings, resets, techs, universes, viewModes } from "../enums";
import { milestoneName } from "../milestones";
import { rotateMap, transformMap, lazyLoad } from "../utils";
import type { Config4 } from "./4";
import type { HistoryEntry, RunHistory } from "../history";
import type { LatestRun as LatestRun4 } from "../runTracking";
import type { Config4 } from "./4";
import type { LatestRun6 as LatestRun4 } from "./6";

type BuiltMilestone = ["Built", /*tab*/ string, /*id*/ string, /*name*/ string, /*count*/ number];
type ResearchedMilestone = ["Researched", /*id*/ string, /*name*/ string];
Expand Down Expand Up @@ -165,7 +165,7 @@ export function migrateHistory(history: RunHistory, config: Config4): RunHistory

export type LatestRun3 = {
run: number,
universe: keyof typeof universes,
universe: keyof typeof universes | "bigbang",
resets: Record<string, number>,
totalDays: number,
milestones: Record<string, number>
Expand Down
47 changes: 47 additions & 0 deletions src/migration/6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { universes } from "../enums";
import type { LatestRun as LatestRun7 } from "../runTracking";

export type LatestRun6 = Omit<LatestRun7, "universe"> & {
universe: keyof typeof universes | "bigbang"
}

function migrateLatestRun(latestRun: any) {
if (latestRun.universe === "bigbang") {
delete latestRun.universe;
}
}

function migrateHistory(history: any) {
for (let i = 0; i !== history.runs.length; ++i) {
const run = history.runs[i];
const nextRun = history.runs[i + 1];

// The runs after a t3 reset may have gotten the "bigbang" universe as the page is refreshed into the universe selection
if (run.universe === "bigbang") {
if (nextRun === undefined) {
// The last run is broken - mark migration as failed and try after the next run
return false;
}
else if (nextRun.universe !== "bigbang") {
// If the next run has a valid universe, this means we stayed in the same universe
run.universe = nextRun.universe;
}
else {
// If there are multiple t3 runs in a row, assume DE farming, which is usually done in magic
run.universe = "magic";
}
}
}

return true;
}

export function migrate6(config: any, history: any, latestRun: any) {
if (latestRun !== null) {
migrateLatestRun(latestRun);
}

if (migrateHistory(history)) {
config.version = 7;
}
}
8 changes: 7 additions & 1 deletion src/migration/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as DB from "../database";
import { migrate3 } from "./3";
import { migrate4 } from "./4";
import { migrate6 } from "./6";

export function migrate() {
let config: any = DB.loadConfig();
Expand All @@ -14,7 +15,7 @@ export function migrate() {
let migrated = false;

if (config.version < 4) {
[config, history, latestRun] = migrate3(config, DB.loadHistory(), DB.loadLatestRun());
[config, history, latestRun] = migrate3(config, DB.loadHistory(), DB.loadLatestRun() as any);
migrated = true;
}

Expand All @@ -23,6 +24,11 @@ export function migrate() {
migrated = true;
}

if (config.version === 6) {
migrate6(config, history ?? DB.loadHistory(), latestRun ?? DB.loadLatestRun());
migrated = true;
}

if (migrated) {
DB.saveConfig(config);
history !== null && DB.saveHistory(history);
Expand Down
4 changes: 2 additions & 2 deletions test/migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("Migration", () => {
migrate();

expect(loadConfig()).toEqual({
version: 6,
version: 7,
recordRuns: true,
views: [
{
Expand Down Expand Up @@ -171,7 +171,7 @@ describe("Migration", () => {
migrate();

expect(loadConfig()).toEqual({
version: 6,
version: 7,
recordRuns: true,
views: []
});
Expand Down
150 changes: 150 additions & 0 deletions test/migration6.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { describe, expect, it } from "@jest/globals";

import { migrate6 } from "../src/migration/6";

describe("Migration", () => {
describe("6 -> 7", () => {
it("should normalize latest current run universe", () => {
const config = {
version: 6,
views: []
};

const history = {
milestones: {
"reset:whitehole": 0,
},
runs: []
};

const latestRun = {
run: 123,
universe: "bigbang",
resets: { "mad": 1 },
totalDays: 10,
milestones: {},
raceName: "Human"
};

migrate6(config, history, latestRun);

expect(config).toEqual({
version: 7,
views: []
});

expect(latestRun).toEqual({
run: 123,
universe: undefined,
resets: { "mad": 1 },
totalDays: 10,
milestones: {},
raceName: "Human"
});
});

it("should take the universe from the next run", () => {
const config = {
version: 6,
views: []
};

const history = {
milestones: {
"reset:whitehole": 0,
},
runs: [
{ "run": 123, "universe": "bigbang", "milestones": [[0, 123]] },
{ "run": 124, "universe": "heavy", "milestones": [[0, 456]] }
]
};

migrate6(config, history, null);

expect(config).toEqual({
version: 7,
views: []
});

expect(history).toEqual({
milestones: {
"reset:whitehole": 0,
},
runs: [
{ "run": 123, "universe": "heavy", "milestones": [[0, 123]] },
{ "run": 124, "universe": "heavy", "milestones": [[0, 456]] }
]
});
});

it("should assume magic if multiple bigbangs in a row", () => {
const config = {
version: 6,
views: []
};

const history = {
milestones: {
"reset:whitehole": 0,
},
runs: [
{ "run": 123, "universe": "bigbang", "milestones": [[0, 123]] },
{ "run": 124, "universe": "bigbang", "milestones": [[0, 456]] },
{ "run": 125, "universe": "heavy", "milestones": [[0, 789]] }
]
};

migrate6(config, history, null);

expect(config).toEqual({
version: 7,
views: []
});

expect(history).toEqual({
milestones: {
"reset:whitehole": 0,
},
runs: [
{ "run": 123, "universe": "magic", "milestones": [[0, 123]] },
{ "run": 124, "universe": "heavy", "milestones": [[0, 456]] },
{ "run": 125, "universe": "heavy", "milestones": [[0, 789]] }
]
});
});

it("should fail if the last run is bigbang", () => {
const config = {
version: 6,
views: []
};

const history = {
milestones: {
"reset:whitehole": 0,
},
runs: [
{ "run": 123, "universe": "bigbang", "milestones": [[0, 123]] },
{ "run": 124, "universe": "bigbang", "milestones": [[0, 456]] }
]
};

migrate6(config, history, null);

expect(config).toEqual({
version: 6,
views: []
});

expect(history).toEqual({
milestones: {
"reset:whitehole": 0,
},
runs: [
{ "run": 123, "universe": "magic", "milestones": [[0, 123]] },
{ "run": 124, "universe": "bigbang", "milestones": [[0, 456]] }
]
});
});
});
});

0 comments on commit c89f240

Please sign in to comment.