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

Commit

Permalink
feat(core): Support stateSerialize API
Browse files Browse the repository at this point in the history
We now have a single stable interface to serialize any settings structure we build. All settings that extend `Setting` are designed to only contain data values, which now facilitates this serialization process.
  • Loading branch information
oliversalzburg committed Oct 12, 2022
1 parent 63c62f3 commit e7d9387
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
23 changes: 16 additions & 7 deletions packages/userscript/source/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Engine {
readonly spaceManager: SpaceManager;
readonly timeControlManager: TimeControlManager;
readonly timeManager: TimeManager;
readonly tradingManager: TradeManager;
readonly tradeManager: TradeManager;
readonly villageManager: VillageManager;
readonly workshopManager: WorkshopManager;

Expand Down Expand Up @@ -82,7 +82,7 @@ export class Engine {
this.spaceManager
);
this.timeManager = new TimeManager(this._host, this.workshopManager);
this.tradingManager = new TradeManager(this._host, this.workshopManager);
this.tradeManager = new TradeManager(this._host, this.workshopManager);
this.villageManager = new VillageManager(this._host, this.workshopManager);
}

Expand All @@ -98,7 +98,7 @@ export class Engine {
space: SpaceSettings;
time: TimeSettings;
timeControl: TimeControlSettings;
trading: TradeSettings;
trade: TradeSettings;
village: VillageSettings;
workshop: WorkshopSettings;
}) {
Expand All @@ -109,14 +109,23 @@ export class Engine {
this.spaceManager.load(settings.space);
this.timeControlManager.load(settings.timeControl);
this.timeManager.load(settings.time);
this.tradingManager.load(settings.trading);
this.tradeManager.load(settings.trade);
this.villageManager.load(settings.village);
this.workshopManager.load(settings.workshop);
}

stateSerialize() {
return {
engine: this.settings.options,
engine: this.settings,
bonfire: this.bonfireManager.settings,
religion: this.religionManager.settings,
science: this.scienceManager.settings,
space: this.spaceManager.settings,
timeControl: this.timeControlManager.settings,
time: this.timeManager.settings,
trade: this.tradeManager.settings,
village: this.villageManager.settings,
workshop: this.workshopManager.settings,
};
}

Expand Down Expand Up @@ -189,7 +198,7 @@ export class Engine {
this.bonfireManager.tick(context);
this.spaceManager.tick(context);
await this.workshopManager.tick(context);
this.tradingManager.tick(context);
this.tradeManager.tick(context);
this.religionManager.tick(context);
this.timeManager.tick(context);
// Blackcoin trading.
Expand Down Expand Up @@ -232,7 +241,7 @@ export class Engine {
* Feed leviathans.
*/
autofeed(): void {
this.tradingManager.autoFeedElders();
this.tradeManager.autoFeedElders();
}

private waitForBestPrice = false;
Expand Down
4 changes: 2 additions & 2 deletions packages/userscript/source/UserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class UserScript {
space: SpaceSettings.fromLegacyOptions(source),
time: TimeSettings.fromLegacyOptions(source),
timeControl: TimeControlSettings.fromLegacyOptions(source),
trading: TradeSettings.fromLegacyOptions(source),
trade: TradeSettings.fromLegacyOptions(source),
village: VillageSettings.fromLegacyOptions(source),
workshop: WorkshopSettings.fromLegacyOptions(source),
});
Expand Down Expand Up @@ -174,7 +174,7 @@ export class UserScript {
space: this.engine.spaceManager.settings,
time: this.engine.timeManager.settings,
timeControl: this.engine.timeControlManager.settings,
trading: this.engine.tradingManager.settings,
trading: this.engine.tradeManager.settings,
village: this.engine.villageManager.settings,
workshop: this.engine.workshopManager.settings,
});
Expand Down
4 changes: 4 additions & 0 deletions packages/userscript/source/options/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class Setting {
load(setting: Setting) {
this.enabled = setting.enabled;
}

serialize() {
return this;
}
}

export class SettingLimited extends Setting {
Expand Down
2 changes: 1 addition & 1 deletion packages/userscript/source/ui/UserInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class UserInterface {
this._craftUi = new WorkshopSettingsUi(this._host, engine.workshopManager.settings);
this._resourcesUi = new ResourcesSettingsUi(this._host, engine.settings.resources);
this._unlockUi = new ScienceSettingsUi(this._host, engine.scienceManager.settings);
this._tradingUi = new TradeSettingsUi(this._host, engine.tradingManager.settings);
this._tradingUi = new TradeSettingsUi(this._host, engine.tradeManager.settings);
this._religionUi = new ReligionSettingsUi(this._host, engine.religionManager.settings);
this._timeUi = new TimeSettingsUi(this._host, engine.timeManager.settings);
this._timeCtrlUi = new TimeControlSettingsUi(this._host, engine.timeControlManager.settings);
Expand Down

0 comments on commit e7d9387

Please sign in to comment.