Skip to content

Commit

Permalink
refactor(time): Move fix cryochambers into time section
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Oct 19, 2022
1 parent d2fd689 commit 260c966
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 81 deletions.
23 changes: 0 additions & 23 deletions packages/userscript/source/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,30 +202,7 @@ export class Engine {
this.religionManager.tick(context);
this.timeManager.tick(context);
this.villageManager.tick(context);

// Time automations (Tempus Fugit & Shatter TC)
await this.timeControlManager.tick(context);

// Miscelaneous automations.
if (subOptions.enabled) {
// Fix used cryochambers
// If the option is enabled and we have used cryochambers...
if (
subOptions.items.fixCry.enabled &&
0 < this._host.gamePage.time.getVSU("usedCryochambers").val
) {
let fixed = 0;
const btn = this.timeManager.manager.tab.vsPanel.children[0].children[0]; //check?
// doFixCryochamber will check resources
while (btn.controller.doFixCryochamber(btn.model)) {
++fixed;
}
if (0 < fixed) {
this._host.engine.iactivity("act.fix.cry", [fixed], "ks-fixCry");
this._host.engine.storeForSummary("fix.cry", fixed);
}
}
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions packages/userscript/source/TimeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class TimeManager {
}

this.autoBuild();

if (this.settings.fixCryochambers.enabled) {
this.fixCryochambers();
}
}

load(settings: TimeSettings) {
Expand Down Expand Up @@ -153,4 +157,21 @@ export class TimeManager {

return null;
}

fixCryochambers() {
// Fix used cryochambers
// If the option is enabled and we have used cryochambers...
if (0 < this._host.gamePage.time.getVSU("usedCryochambers").val) {
let fixed = 0;
const btn = this.manager.tab.vsPanel.children[0].children[0]; //check?
// doFixCryochamber will check resources
while (btn.controller.doFixCryochamber(btn.model)) {
++fixed;
}
if (0 < fixed) {
this._host.engine.iactivity("act.fix.cry", [fixed], "ks-fixCry");
this._host.engine.storeForSummary("fix.cry", fixed);
}
}
}
}
6 changes: 3 additions & 3 deletions packages/userscript/source/TradeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class TradeManager implements Automation {
const season = this._host.gamePage.calendar.getCurSeason().name;

// Determine how many races we will trade with this cycle.
for (const trade of Object.values(this.settings.items)) {
for (const trade of Object.values(this.settings.races)) {
const race = this.getRace(trade.race);

// Check if the race is enabled, in season, unlocked, and we can actually afford it.
Expand Down Expand Up @@ -137,7 +137,7 @@ export class TradeManager implements Automation {
// with them.
for (let tradeIndex = 0; tradeIndex < trades.length; tradeIndex++) {
const race = trades[tradeIndex];
const tradeSettings = this.settings.items[race];
const tradeSettings = this.settings.races[race];
// Does this trade require a certain resource?
const require = !tradeSettings.require
? false
Expand Down Expand Up @@ -587,7 +587,7 @@ export class TradeManager implements Automation {
const race = this.getRace(name);
const button = this.getTradeButton(race.name);

if (!button.model.enabled || !this.settings.items[name].enabled) {
if (!button.model.enabled || !this.settings.races[name].enabled) {
cwarn(
"KS trade checks are not functioning properly, please create an issue on the github page."
);
Expand Down
10 changes: 3 additions & 7 deletions packages/userscript/source/settings/OptionsSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { objectEntries } from "../tools/Entries";
import { Setting, SettingTrigger } from "./Settings";
import { LegacyStorage } from "./SettingsStorage";

export type OptionsItem = "fixCry" | "observe";
export type OptionsItem = "observe";

export class OptionsSettingsItem extends SettingTrigger {}

Expand All @@ -11,13 +11,9 @@ export class OptionsSettings extends Setting {
[key in OptionsItem]: OptionsSettingsItem;
};

constructor(
enabled = false,
observe = new OptionsSettingsItem(true),
fixCry = new OptionsSettingsItem(false)
) {
constructor(enabled = false, observe = new OptionsSettingsItem(true)) {
super(enabled);
this.items = { fixCry, observe };
this.items = { observe };
}

load(settings: OptionsSettings) {
Expand Down
10 changes: 6 additions & 4 deletions packages/userscript/source/settings/SettingsStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ type SetMinResetUnicornItem = `set-reset-unicorn-${UnicornItem}-min`;
type SetSubtriggerOptionItem = `set-${OptionsItem}-trigger`;
type SetSubtriggerReligionItem = `set-${ReligionAdditionItem}-trigger`;
type SetSubtriggerTimeCtrlItem = `set-${"accelerateTime" | "timeSkip"}-trigger`;
type ToggleTradeBlackcoin = "toggle-crypto";
type ToggleFeedLeviathans = "toggle-autofeed";
type ToggleBuildEmbassies = "toggle-buildEmbassies";
type ToggleBuildEmbassiesItem = `toggle-build-${Race}`;
type ToggleBuildingItem = `toggle-${AllItems}`;
type ToggleFaithUnicornItem = `toggle-${FaithItem | UnicornItem}`;
type ToggleFeedLeviathans = "toggle-autofeed";
type ToggleFilterItem = `toggle-${FilterItem}`;
type ToggleFixCryochambers = `toggle-fixCry`;
type ToggleHoldFestivals = "toggle-festival";
type ToggleHunt = "toggle-hunt";
type ToggleJobItem = `toggle-${Job}`;
Expand All @@ -78,13 +78,14 @@ type ToggleTimeControlCycleItem = `toggle-timeSkip-${0 | 1 | 2 | 3 | 4 | 5 | 6 |
type ToggleTimeControlItem = `toggle-${TimeControlItem}`;
type ToggleTimeControlSeasonItem = `toggle-timeSkip-${Season}`;
type ToggleTimeItem = `toggle-${TimeItem}`;
type ToggleTradeBlackcoin = "toggle-crypto";
type ToggleTurnOnSteamworks = "toggle-_steamworks";
type ToggleUnlockItem = `toggle-${ScienceItem}`;
type ToggleUnlockRaces = "toggle-races";
type ToggleUnlockMissions = "toggle-missions";
type ToggleUnlockRaces = "toggle-races";
type ToggleUnlockUpgrades = "toggle-upgrades";
type ToggleUpgradeBuildings = "toggle-buildings";
type ToggleUpgradeBuildingItem = `toggle-upgrade-${StagedBuilding}`;
type ToggleUpgradeBuildings = "toggle-buildings";
type ToggleUpgradeItem = `toggle-upgrade-${Upgrade}`;

export type LegacyStorage = {
Expand All @@ -110,6 +111,7 @@ export type LegacyStorage = {
Partial<Record<ToggleFaithUnicornItem, boolean>> &
Partial<Record<ToggleFeedLeviathans, boolean>> &
Partial<Record<ToggleFilterItem, boolean>> &
Partial<Record<ToggleFixCryochambers, boolean>> &
Partial<Record<ToggleHoldFestivals, boolean>> &
Partial<Record<ToggleHunt, boolean>> &
Partial<Record<ToggleJobItem, boolean>> &
Expand Down
22 changes: 16 additions & 6 deletions packages/userscript/source/settings/TimeSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type TimeSettingsItems = {
export class TimeSettings extends SettingTrigger {
items: TimeSettingsItems;

fixCryochambers: SettingTrigger;

constructor(
enabled = false,
trigger = 1,
Expand All @@ -52,10 +54,12 @@ export class TimeSettings extends SettingTrigger {
voidHoover: new TimeSettingsItem("voidHoover", TimeItemVariant.VoidSpace, "antimatter"),
voidResonator: new TimeSettingsItem("voidResonator", TimeItemVariant.VoidSpace),
voidRift: new TimeSettingsItem("voidRift", TimeItemVariant.VoidSpace),
}
},
fixCryochambers = new SettingTrigger(false)
) {
super(enabled, trigger);
this.items = items;
this.fixCryochambers = fixCryochambers;
}

load(settings: TimeSettings) {
Expand All @@ -76,17 +80,23 @@ export class TimeSettings extends SettingTrigger {
subject.items[`toggle-${name}` as const] = item.enabled;
subject.items[`set-${name}-max` as const] = item.max;
}

subject.items[`toggle-fixCry`] = settings.fixCryochambers.enabled;
}

static fromLegacyOptions(subject: LegacyStorage) {
const options = new TimeSettings();
options.enabled = subject.toggles.time;
options.trigger = subject.triggers.time;
const settings = new TimeSettings();
settings.enabled = subject.toggles.time;
settings.trigger = subject.triggers.time;

for (const [name, item] of objectEntries(options.items)) {
for (const [name, item] of objectEntries(settings.items)) {
item.enabled = subject.items[`toggle-${name}` as const] ?? item.enabled;
item.max = subject.items[`set-${name}-max` as const] ?? item.max;
}
return options;

settings.fixCryochambers.enabled =
subject.items[`toggle-fixCry`] ?? settings.fixCryochambers.enabled;

return settings;
}
}
28 changes: 14 additions & 14 deletions packages/userscript/source/settings/TradeSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ export type TradeSettingsItems = {
};

export class TradeSettings extends SettingTrigger {
items: TradeSettingsItems;
races: TradeSettingsItems;

feedLeviathans: SettingTrigger;
feedLeviathans: Setting;
buildEmbassies: EmbassySettings;
tradeBlackcoin: SettingTrigger;
unlockRaces: Setting;

constructor(
enabled = false,
trigger = 1,
items = {
races = {
dragons: new TradeSettingsItem("dragons", true, true, true, true, true, true, "titanium"),
griffins: new TradeSettingsItem("griffins", true, true, false, true, false, false, "wood"),
leviathans: new TradeSettingsItem(
Expand All @@ -72,12 +72,12 @@ export class TradeSettings extends SettingTrigger {
zebras: new TradeSettingsItem("zebras", true, true, true, true, true, true),
},
buildEmbassies = new EmbassySettings(),
feedLeviathans = new SettingTrigger(false, 1),
feedLeviathans = new Setting(false),
tradeBlackcoin = new SettingTrigger(true, 10000),
unlockRaces = new Setting(true)
) {
super(enabled, trigger);
this.items = items;
this.races = races;

this.buildEmbassies = buildEmbassies;
this.feedLeviathans = feedLeviathans;
Expand All @@ -89,13 +89,13 @@ export class TradeSettings extends SettingTrigger {
this.enabled = settings.enabled;
this.trigger = settings.trigger;

for (const [name, item] of objectEntries(settings.items)) {
this.items[name].enabled = item.enabled;
this.items[name].limited = item.limited;
this.items[name].autumn = item.autumn;
this.items[name].spring = item.spring;
this.items[name].summer = item.summer;
this.items[name].winter = item.winter;
for (const [name, item] of objectEntries(settings.races)) {
this.races[name].enabled = item.enabled;
this.races[name].limited = item.limited;
this.races[name].autumn = item.autumn;
this.races[name].spring = item.spring;
this.races[name].summer = item.summer;
this.races[name].winter = item.winter;
}

this.buildEmbassies.load(settings.buildEmbassies);
Expand All @@ -108,7 +108,7 @@ export class TradeSettings extends SettingTrigger {
subject.toggles.trade = settings.enabled;
subject.triggers.trade = settings.trigger;

for (const [name, item] of objectEntries(settings.items)) {
for (const [name, item] of objectEntries(settings.races)) {
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.enabled;
Expand All @@ -129,7 +129,7 @@ export class TradeSettings extends SettingTrigger {
options.enabled = subject.toggles.trade;
options.trigger = subject.triggers.trade;

for (const [name, item] of objectEntries(options.items)) {
for (const [name, item] of objectEntries(options.races)) {
item.enabled = subject.items[`toggle-${name}` as const] ?? item.enabled;
item.limited = subject.items[`toggle-limited-${name}` as const] ?? item.limited;
item.autumn.enabled = subject.items[`toggle-${name}-autumn` as const] ?? item.autumn.enabled;
Expand Down
16 changes: 8 additions & 8 deletions packages/userscript/source/ui/OptionsSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ import { SettingTriggerListItem } from "./components/SettingTriggerListItem";
import { SettingsSectionUi } from "./SettingsSectionUi";

export class OptionsSettingsUi extends SettingsSectionUi<OptionsSettings> {
protected readonly _buildings: Array<SettingListItem>;
protected readonly _observeStars: SettingListItem;

constructor(host: UserScript, settings: OptionsSettings) {
const label = host.engine.i18n("ui.options");
super(host, label, settings);

this._list.addEventListener("enableAll", () => {
this._buildings.forEach(item => (item.setting.enabled = true));
this._observeStars.setting.enabled = true;
this.refreshUi();
});
this._list.addEventListener("disableAll", () => {
this._buildings.forEach(item => (item.setting.enabled = false));
this._observeStars.setting.enabled = false;
this.refreshUi();
});
this._list.addEventListener("reset", () => {
this.setting.load(new OptionsSettings());
this.refreshUi();
});

this._buildings = [
this._getOptionsOption(this.setting.items.observe, this._host.engine.i18n("option.observe")),
this._getOptionsOption(this.setting.items.fixCry, this._host.engine.i18n("option.fix.cry")),
];
this.addChildren(this._buildings);
this._observeStars = this._getOptionsOption(
this.setting.items.observe,
this._host.engine.i18n("option.observe")
);
this.addChild(this._observeStars);
}

private _getOptionsOption(option: OptionsSettingsItem, iname: string) {
Expand Down
27 changes: 24 additions & 3 deletions packages/userscript/source/ui/TimeSettingsUi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TimeSettings, TimeSettingsItem } from "../settings/TimeSettings";
import { UserScript } from "../UserScript";
import { HeaderListItem } from "./components/HeaderListItem";
import { SettingListItem } from "./components/SettingListItem";
import { SettingMaxListItem } from "./components/SettingMaxListItem";
import { TriggerButton } from "./components/TriggerButton";
Expand All @@ -8,6 +9,7 @@ import { SettingsSectionUi } from "./SettingsSectionUi";
export class TimeSettingsUi extends SettingsSectionUi<TimeSettings> {
private readonly _trigger: TriggerButton;
private readonly _buildings: Array<SettingListItem>;
private readonly _fixCryochamber: SettingListItem;

constructor(host: UserScript, settings: TimeSettings) {
const label = host.engine.i18n("ui.time");
Expand Down Expand Up @@ -75,10 +77,29 @@ export class TimeSettingsUi extends SettingsSectionUi<TimeSettings> {
),
this._getTimeSetting(
this.setting.items.voidResonator,
this._host.engine.i18n("$time.vsu.voidResonator.label")
this._host.engine.i18n("$time.vsu.voidResonator.label"),
true
),
];
this.addChildren(this._buildings);

this.addChild(new HeaderListItem(this._host, "Additional options"));
this._fixCryochamber = new SettingListItem(
this._host,
this._host.engine.i18n("option.fix.cry"),
this.setting.fixCryochambers,
{
onCheck: () =>
this._host.engine.imessage("status.sub.enable", [
this._host.engine.i18n("option.fix.cry"),
]),
onUnCheck: () =>
this._host.engine.imessage("status.sub.disable", [
this._host.engine.i18n("option.fix.cry"),
]),
}
);
this.addChild(this._fixCryochamber);
}

private _getTimeSetting(setting: TimeSettingsItem, label: string, delimiter = false) {
Expand All @@ -87,8 +108,8 @@ export class TimeSettingsUi extends SettingsSectionUi<TimeSettings> {
label,
setting,
{
onCheck: () => this._host.engine.imessage("status.auto.enable", [label]),
onUnCheck: () => this._host.engine.imessage("status.auto.disable", [label]),
onCheck: () => this._host.engine.imessage("status.sub.enable", [label]),
onUnCheck: () => this._host.engine.imessage("status.sub.disable", [label]),
},
delimiter
);
Expand Down
Loading

0 comments on commit 260c966

Please sign in to comment.