Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
feat(dev): Load savegame in dev container
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Jul 17, 2021
1 parent 3701d16 commit 9c07268
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/userscript/source/fixtures/lategame.ts

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion packages/userscript/source/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import lategame from "./fixtures/lategame";
import testConfig from "./fixtures/localstorage.json";
import { Options } from "./options/Options";
import { SettingsStorage } from "./options/SettingsStorage";
import { cinfo } from "./tools/Log";
import { SavegameLoader } from "./tools/SavegameLoader";
import { UserScript } from "./UserScript";

(async () => {
await UserScript.waitForGame();
const kittenGame = await UserScript.waitForGame();
// For development convenience, load a lategame save to give us more test options.
await new SavegameLoader(kittenGame).load(lategame);

const userScript = await UserScript.getDefaultInstance();

// @ts-expect-error Manipulating global containers is naughty, be we want to expose the script host.
Expand Down
3 changes: 3 additions & 0 deletions packages/userscript/source/tools/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export function clog(...args: Array<any>): void {
export function cwarn(...args: Array<any>): void {
console.warn("👩‍🔬", ...args);
}
export function cerror(...args: Array<any>): void {
console.error("👩‍🔬", ...args);
}
27 changes: 27 additions & 0 deletions packages/userscript/source/tools/SavegameLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GamePage } from "../types";

export class SavegameLoader {
private readonly _gamePage: GamePage;

constructor(gamePage: GamePage) {
this._gamePage = gamePage;
}

/**
* Conveniently wraps the savegame loading process in an async construct.
* @param data The savegame data to load.
* @returns Nothing
*/
load(data: string): Promise<void> {
return new Promise((resolve, reject) => {
this._gamePage.saveImportDropboxText(data, error => {
if (error) {
reject(error);
return;
}

resolve();
});
});
}
}
1 change: 1 addition & 0 deletions packages/userscript/source/types/gamePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export type GamePage = {
}>;
hasRes: (resources: Array<Price>) => boolean;
};
saveImportDropboxText(lzdata: string, callback: (error?: Error) => unknown): void;
science: {
get: (name: "civil" | "cryptotheology" | "drama" | "nuclearFission") => { researched: boolean };
getPolicy: (name: Policy) => {
Expand Down

0 comments on commit 9c07268

Please sign in to comment.