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

Commit

Permalink
feat: Explicit state retrieval
Browse files Browse the repository at this point in the history
This also fixes some issues where UI state was not persisted to the global options object.
  • Loading branch information
oliversalzburg committed Jul 18, 2021
1 parent 55c26a2 commit 109485d
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 51 deletions.
5 changes: 4 additions & 1 deletion packages/userscript/source/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BulkManager } from "./BulkManager";
import { CacheManager } from "./CacheManager";
import { CraftManager } from "./CraftManager";
import { BonfireSettingsItem, BuildItem } from "./options/BonfireSettings";
import { PolicySettings } from "./options/PolicySettings";
import { FaithItem, ReligionSettingsItem } from "./options/ReligionSettings";
import { SettingsStorage } from "./options/SettingsStorage";
import { SpaceItem } from "./options/SpaceSettings";
Expand Down Expand Up @@ -1111,7 +1112,9 @@ export class Engine {
var policies = this._host.gamePage.science.policies;
var toResearch = [];

for (const [policy] of objectEntries(this._host.options.auto.policies.items)) {
for (const [policy] of objectEntries(
(this._host.options.auto.unlock.items.policies as PolicySettings).items
)) {
const targetPolicy = policies.find(policy => policy.name === policy.name);
if (isNil(targetPolicy)) {
cerror(`Policy '${policy}' not found in game!`);
Expand Down
1 change: 1 addition & 0 deletions packages/userscript/source/UserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class UserScript {
if (updater) {
updater(this.options);
}
this.options = this._userInterface?.getState();
this._optionsDirty = true;
}

Expand Down
102 changes: 52 additions & 50 deletions packages/userscript/source/options/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,124 +77,126 @@ export class Options {
karmaTotal: 0,
};

asLegacyOptions(): KittenStorageType {
asLegacyOptions(optionsObject: Options = this): KittenStorageType {
const subject = {} as KittenStorageType;

subject.toggles = {
build: this.auto.build.enabled,
space: this.auto.space.enabled,
craft: this.auto.craft.enabled,
upgrade: this.auto.unlock.enabled,
trade: this.auto.trade.enabled,
faith: this.auto.religion.enabled,
time: this.auto.time.enabled,
timeCtrl: this.auto.timeCtrl.enabled,
distribute: this.auto.distribute.enabled,
options: this.auto.options.enabled,
filter: this.auto.filters.enabled,
build: optionsObject.auto.build.enabled,
space: optionsObject.auto.space.enabled,
craft: optionsObject.auto.craft.enabled,
upgrade: optionsObject.auto.unlock.enabled,
trade: optionsObject.auto.trade.enabled,
faith: optionsObject.auto.religion.enabled,
time: optionsObject.auto.time.enabled,
timeCtrl: optionsObject.auto.timeCtrl.enabled,
distribute: optionsObject.auto.distribute.enabled,
options: optionsObject.auto.options.enabled,
filter: optionsObject.auto.filters.enabled,
};

subject.triggers = {
faith: this.auto.religion.trigger,
time: this.auto.time.trigger,
build: this.auto.build.trigger,
space: this.auto.space.trigger,
craft: this.auto.craft.trigger,
trade: this.auto.trade.trigger,
faith: optionsObject.auto.religion.trigger,
time: optionsObject.auto.time.trigger,
build: optionsObject.auto.build.trigger,
space: optionsObject.auto.space.trigger,
craft: optionsObject.auto.craft.trigger,
trade: optionsObject.auto.trade.trigger,
};

subject.reset = {
reset: this.reset.reset,
times: this.reset.times,
paragonLastTime: this.reset.paragonLastTime,
pargonTotal: this.reset.paragonTotal,
karmaLastTime: this.reset.karmaLastTime,
karmaTotal: this.reset.karmaTotal,
reset: optionsObject.reset.reset,
times: optionsObject.reset.times,
paragonLastTime: optionsObject.reset.paragonLastTime,
pargonTotal: optionsObject.reset.paragonTotal,
karmaLastTime: optionsObject.reset.karmaLastTime,
karmaTotal: optionsObject.reset.karmaTotal,
};

subject.items = {};
for (const [name, item] of objectEntries(this.auto.build.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.build.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
subject.items[`set-${name}-max` as const] = item.max;
}
for (const [name, item] of objectEntries(this.auto.craft.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.craft.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
subject.items[`toggle-limited-${name}` as const] = item.limited;
}
for (const [name, item] of objectEntries(this.auto.distribute.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.distribute.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
subject.items[`toggle-limited-${name}` as const] = item.limited;
subject.items[`set-${name}-max` as const] = item.max;
}
for (const [name, item] of objectEntries(this.auto.filters.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.filters.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
}
for (const [name, item] of objectEntries(this.auto.options.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.options.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
subject.items[`set-${name}-subTrigger` as const] = item.subTrigger;
}
for (const [name, item] of objectEntries(this.auto.religion.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.religion.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
}
for (const [name, item] of objectEntries(this.auto.space.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.space.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
subject.items[`set-${name}-max` as const] = item.max;
}

subject.items["toggle-accelerateTime"] = this.auto.timeCtrl.items.accelerateTime.enabled;
subject.items["toggle-accelerateTime"] =
optionsObject.auto.timeCtrl.items.accelerateTime.enabled;
subject.items["set-accelerateTime-subTrigger"] =
this.auto.timeCtrl.items.accelerateTime.subTrigger;
optionsObject.auto.timeCtrl.items.accelerateTime.subTrigger;

subject.items["toggle-reset"] = this.auto.timeCtrl.items.reset.enabled;
subject.items["toggle-reset"] = optionsObject.auto.timeCtrl.items.reset.enabled;

subject.items["toggle-timeSkip"] = this.auto.timeCtrl.items.timeSkip.enabled;
subject.items["set-timeSkip-subTrigger"] = this.auto.timeCtrl.items.timeSkip.subTrigger;
subject.items["toggle-timeSkip-autumn"] = this.auto.timeCtrl.items.timeSkip.autumn;
subject.items["toggle-timeSkip-spring"] = this.auto.timeCtrl.items.timeSkip.spring;
subject.items["toggle-timeSkip-summer"] = this.auto.timeCtrl.items.timeSkip.summer;
subject.items["toggle-timeSkip-winter"] = this.auto.timeCtrl.items.timeSkip.winter;
subject.items["toggle-timeSkip"] = optionsObject.auto.timeCtrl.items.timeSkip.enabled;
subject.items["set-timeSkip-subTrigger"] =
optionsObject.auto.timeCtrl.items.timeSkip.subTrigger;
subject.items["toggle-timeSkip-autumn"] = optionsObject.auto.timeCtrl.items.timeSkip.autumn;
subject.items["toggle-timeSkip-spring"] = optionsObject.auto.timeCtrl.items.timeSkip.spring;
subject.items["toggle-timeSkip-summer"] = optionsObject.auto.timeCtrl.items.timeSkip.summer;
subject.items["toggle-timeSkip-winter"] = optionsObject.auto.timeCtrl.items.timeSkip.winter;

for (let cycleIndex = 0; cycleIndex < 10; ++cycleIndex) {
subject.items[`toggle-timeSkip-${cycleIndex as CycleIndices}` as const] =
this.auto.timeCtrl.items.timeSkip[cycleIndex as CycleIndices];
optionsObject.auto.timeCtrl.items.timeSkip[cycleIndex as CycleIndices];
}

for (const [name, item] of objectEntries(this.auto.timeCtrl.buildItems)) {
for (const [name, item] of objectEntries(optionsObject.auto.timeCtrl.buildItems)) {
subject.items[`toggle-reset-build-${name}` as const] = item.checkForReset;
subject.items[`set-reset-build-${name}-min` as const] = item.triggerForReset;
}
for (const [name, item] of objectEntries(this.auto.timeCtrl.religionItems)) {
for (const [name, item] of objectEntries(optionsObject.auto.timeCtrl.religionItems)) {
subject.items[`toggle-reset-faith-${name}` as const] = item.checkForReset;
subject.items[`set-reset-faith-${name}-min` as const] = item.triggerForReset;
}
for (const [name, item] of objectEntries(this.auto.timeCtrl.spaceItems)) {
for (const [name, item] of objectEntries(optionsObject.auto.timeCtrl.spaceItems)) {
subject.items[`toggle-reset-space-${name}` as const] = item.checkForReset;
subject.items[`set-reset-space-${name}-min` as const] = item.triggerForReset;
}
for (const [name, item] of objectEntries(this.auto.timeCtrl.timeItems)) {
for (const [name, item] of objectEntries(optionsObject.auto.timeCtrl.timeItems)) {
subject.items[`toggle-reset-time-${name}` as const] = item.checkForReset;
subject.items[`set-reset-time-${name}-min` as const] = item.triggerForReset;
}

for (const [name, item] of objectEntries(this.auto.trade.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.trade.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
subject.items[`toggle-limited-${name}` as const] = item.limited;
subject.items[`toggle-${name}-autumn` as const] = item.autumn;
subject.items[`toggle-${name}-spring` as const] = item.spring;
subject.items[`toggle-${name}-summer` as const] = item.summer;
subject.items[`toggle-${name}-winter` as const] = item.winter;
}
for (const [name, item] of objectEntries(this.auto.unlock.items)) {
for (const [name, item] of objectEntries(optionsObject.auto.unlock.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
}
for (const [name, item] of objectEntries(
(this.auto.unlock.items.policies as PolicySettings).items
(optionsObject.auto.unlock.items.policies as PolicySettings).items
)) {
subject.items[`toggle-${name}` as const] = item.enabled;
}

subject.resources = {};
for (const [name, item] of objectEntries(this.auto.craft.resources)) {
for (const [name, item] of objectEntries(optionsObject.auto.craft.resources)) {
subject.resources[name] = {
checkForReset: false,
stockForReset: 0,
Expand All @@ -203,7 +205,7 @@ export class Options {
stock: item.stock,
};
}
for (const [name, item] of objectEntries(this.auto.timeCtrl.resources)) {
for (const [name, item] of objectEntries(optionsObject.auto.timeCtrl.resources)) {
subject.resources[name] = {
checkForReset: item.checkForReset,
stockForReset: item.stockForReset,
Expand Down
8 changes: 8 additions & 0 deletions packages/userscript/source/ui/BonfireSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ export class BonfireSettingsUi extends SettingsSectionUi<BonfireSettings> {
return element;
}

getState(): BonfireSettings {
return {
enabled: this._options.enabled,
trigger: this._options.trigger,
items: this._options.items,
};
}

setState(state: BonfireSettings): void {
this._options.enabled = state.enabled;
this._options.trigger = state.trigger;
Expand Down
9 changes: 9 additions & 0 deletions packages/userscript/source/ui/CraftSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ export class CraftSettingsUi extends SettingsSectionUi<CraftSettings> {
return this._resourcesList;
}

getState(): CraftSettings {
return {
enabled: this._options.enabled,
trigger: this._options.trigger,
items: this._options.items,
resources: this._options.resources,
};
}

setState(state: CraftSettings): void {
this._options.enabled = state.enabled;
this._options.trigger = state.trigger;
Expand Down
7 changes: 7 additions & 0 deletions packages/userscript/source/ui/DistributeSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ export class DistributeSettingsUi extends SettingsSectionUi<DistributeSettings>
return element;
}

getState(): DistributeSettings {
return {
enabled: this._options.enabled,
items: this._options.items,
};
}

setState(state: DistributeSettings): void {
mustExist(this._options.$enabled).prop("checked", state.enabled);
this._options.enabled = state.enabled;
Expand Down
6 changes: 6 additions & 0 deletions packages/userscript/source/ui/EngineSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export class EngineSettingsUi extends SettingsSectionUi<EngineSettings> {
this.element = element;
}

getState(): EngineSettings {
return {
enabled: this._options.enabled,
};
}

setState(state: EngineSettings): void {
this._options.enabled = state.enabled;
}
Expand Down
7 changes: 7 additions & 0 deletions packages/userscript/source/ui/FilterSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ export class FiltersSettingsUi extends SettingsSectionUi<FilterSettings> {
this.element = element;
}

getState(): FilterSettings {
return {
enabled: this._options.enabled,
items: this._options.items,
};
}

setState(state: FilterSettings): void {
this._options.enabled = state.enabled;

Expand Down
7 changes: 7 additions & 0 deletions packages/userscript/source/ui/OptionsSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ export class OptionsSettingsUi extends SettingsSectionUi<OptionsSettings> {
return element;
}

getState(): OptionsSettings {
return {
enabled: this._options.enabled,
items: this._options.items,
};
}

setState(state: OptionsSettings): void {
this._options.enabled = state.enabled;

Expand Down
9 changes: 9 additions & 0 deletions packages/userscript/source/ui/ReligionSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@ export class ReligionSettingsUi extends SettingsSectionUi<ReligionSettings> {
return [nodeHeader, nodeBestUnicornBuilding, nodeAutoPraise, nodeAdore, nodeTranscend];
}

getState(): ReligionSettings {
return {
enabled: this._options.enabled,
trigger: this._options.trigger,
addition: this._options.addition,
items: this._options.items,
};
}

setState(state: ReligionSettings): void {
this._options.enabled = state.enabled;
this._options.trigger = state.trigger;
Expand Down
1 change: 1 addition & 0 deletions packages/userscript/source/ui/SettingsSectionUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export abstract class SettingsSectionUi<TState> {
this._host = host;
}

abstract getState(): TState;
abstract setState(state: TState): void;
abstract refreshUi(): void;

Expand Down
8 changes: 8 additions & 0 deletions packages/userscript/source/ui/SpaceSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ export class SpaceSettingsUi extends SettingsSectionUi<SpaceSettings> {
return element;
}

getState(): SpaceSettings {
return {
enabled: this._options.enabled,
trigger: this._options.trigger,
items: this._options.items,
};
}

setState(state: SpaceSettings): void {
this._options.enabled = state.enabled;
this._options.trigger = state.trigger;
Expand Down
12 changes: 12 additions & 0 deletions packages/userscript/source/ui/TimeControlSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,18 @@ export class TimeControlSettingsUi extends SettingsSectionUi<TimeControlSettings
return this._resourcesList;
}

getState(): TimeControlSettings {
return {
enabled: this._options.enabled,
items: this._options.items,
buildItems: this._options.buildItems,
religionItems: this._options.religionItems,
resources: this._options.resources,
spaceItems: this._options.spaceItems,
timeItems: this._options.timeItems,
};
}

setState(state: TimeControlSettings): void {
this._options.enabled = state.enabled;

Expand Down
8 changes: 8 additions & 0 deletions packages/userscript/source/ui/TimeSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ export class TimeSettingsUi extends SettingsSectionUi<TimeSettings> {
this.element = element;
}

getState(): TimeSettings {
return {
enabled: this._options.enabled,
trigger: this._options.trigger,
items: this._options.items,
};
}

setState(state: TimeSettings): void {
this._options.enabled = state.enabled;
this._options.trigger = state.trigger;
Expand Down
8 changes: 8 additions & 0 deletions packages/userscript/source/ui/TradingSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ export class TradingSettingsUi extends SettingsSectionUi<TradingSettings> {
return element;
}

getState(): TradingSettings {
return {
enabled: this._options.enabled,
trigger: this._options.trigger,
items: this._options.items,
};
}

setState(state: TradingSettings): void {
this._options.enabled = state.enabled;
this._options.trigger = state.trigger;
Expand Down
7 changes: 7 additions & 0 deletions packages/userscript/source/ui/UnlockingSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ export class UnlockingSettingsUi extends SettingsSectionUi<UnlockingSettings> {
this.element = element;
}

getState(): UnlockingSettings {
return {
enabled: this._options.enabled,
items: this._options.items,
};
}

setState(state: UnlockingSettings): void {
this._options.enabled = state.enabled;

Expand Down
Loading

0 comments on commit 109485d

Please sign in to comment.