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

Commit

Permalink
feat(core): Prepare loading from KG game state
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Oct 16, 2022
1 parent a37e16a commit 2e334cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
37 changes: 35 additions & 2 deletions packages/userscript/source/UserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare global {
interface Window {
dojo: {
clone: <T>(subject: T) => T;
subscribe: (event: string, handler: () => void) => void;
subscribe: (event: string, handler: (...args: any[]) => void) => void;
};
gamePage?: Maybe<GamePage>;
$: JQuery;
Expand Down Expand Up @@ -70,6 +70,8 @@ export class UserScript {
private static _gameStartSignal: Promise<boolean>;
private static _gameStartSignalResolver: undefined | ((value: boolean) => void);

private static _possibleEngineState: EngineState | undefined = undefined;

private _userInterface: UserInterface;
engine: Engine;

Expand Down Expand Up @@ -190,10 +192,15 @@ export class UserScript {
getSettings() {
return this.engine.stateSerialize();
}
setSetting(settings: EngineState) {
setSettings(settings: EngineState) {
this.engine.stateLoad(settings);
}

installSaveManager() {
clearInterval(this._intervalSaveSettings);
this.gamePage.managers.push(this.saveManager);
}

/**
* Experimental save manager for Kitten Game.
* It can be injected manually into the game to cause KS settings to be
Expand Down Expand Up @@ -242,6 +249,29 @@ export class UserScript {
cdebug("`game/start` signal caught. Fast-tracking script load...");
mustExist(UserScript._gameStartSignalResolver)(true);
});

UserScript.window.dojo.subscribe(
"server/load",
(saveData: { ks?: { state?: Array<EngineState> } }) => {
cwarn("`server/load` signal caught.");
if ("ks" in saveData === false) {
return;
}

const ksData = saveData.ks as { state?: Array<EngineState> };
if ("state" in ksData === false) {
return;
}

const state = ksData.state;
if (!Array.isArray(state)) {
return;
}

cdebug("Using provided save data as seed for next userscript instance.");
UserScript._possibleEngineState = mustExist(mustExist(saveData.ks).state)[0];
}
);
}

if (!isNil(UserScript._gameStartSignal)) {
Expand All @@ -268,6 +298,9 @@ export class UserScript {
mustExist(UserScript.window.$I),
localStorage["com.nuclearunicorn.kittengame.language"] as SupportedLanguages | undefined
);
if (!isNil(UserScript._possibleEngineState)) {
instance.setSettings(UserScript._possibleEngineState);
}
return instance;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/userscript/source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ const devSavegame = KG_SAVEGAME ?? null;
(async () => {
const kittenGame = await UserScript.waitForGame();

const userScript = UserScript.getDefaultInstance();
userScript.installSaveManager();

// For development convenience, load a lategame save to give us more test options.
if (!isNil(devSavegame)) {
await new SavegameLoader(kittenGame).load(devSavegame);
}

const userScript = UserScript.getDefaultInstance();

// @ts-expect-error Manipulating global containers is naughty, be we want to expose the script host.
window.kittenScientists = userScript;

Expand Down
4 changes: 4 additions & 0 deletions packages/userscript/source/types/gamePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export type GamePage = {
* Are we in iron will mode?
*/
ironWill: boolean;
managers: Array<{
load: (saveData: Record<string, unknown>) => void;
save: (saveData: Record<string, unknown>) => void;
}>;
msg: (...args: Array<number | string>) => { span: HTMLElement };
opts: {
disableCMBR: boolean;
Expand Down

0 comments on commit 2e334cf

Please sign in to comment.