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

Commit

Permalink
feat(bonfire): Select which buildings to upgrade
Browse files Browse the repository at this point in the history
Completes #105
  • Loading branch information
oliversalzburg committed Sep 23, 2022
1 parent f819bd6 commit 70685f7
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 16 deletions.
20 changes: 16 additions & 4 deletions packages/userscript/source/BonfireManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export class BonfireManager {

const pastureMeta = this._host.gamePage.bld.getBuildingExt("pasture").meta;
// If pastures haven't been upgraded to solar farms yet...
if (pastureMeta.stage === 0) {
if (
this._host.options.auto.bonfire.addition.upgradeBuildings.items.solarfarm.enabled &&
pastureMeta.stage === 0
) {
if (mustExist(pastureMeta.stages)[1].stageUnlocked) {
// If we would reduce our pastures to 0, by upgrading them, would we lose any catnip?
if (this._workshopManager.getPotentialCatnip(true, 0, aqueducts) > 0) {
Expand Down Expand Up @@ -110,7 +113,10 @@ export class BonfireManager {

const aqueductMeta = this._host.gamePage.bld.getBuildingExt("aqueduct").meta;
// If aqueducts haven't beeen upgraded to hydro plants yet...
if (aqueductMeta.stage === 0) {
if (
this._host.options.auto.bonfire.addition.upgradeBuildings.items.hydroplant.enabled &&
aqueductMeta.stage === 0
) {
if (mustExist(aqueductMeta.stages)[1].stageUnlocked) {
// If we would reduce our aqueducts to 0, by upgrading them, would we lose any catnip?
if (this._workshopManager.getPotentialCatnip(true, pastures, 0) > 0) {
Expand Down Expand Up @@ -139,7 +145,10 @@ export class BonfireManager {
}

const libraryMeta = this._host.gamePage.bld.getBuildingExt("library").meta;
if (libraryMeta.stage === 0) {
if (
this._host.options.auto.bonfire.addition.upgradeBuildings.items.dataCenter.enabled &&
libraryMeta.stage === 0
) {
if (mustExist(libraryMeta.stages)[1].stageUnlocked) {
let energyConsumptionRate = this._host.gamePage.workshop.get("cryocomputing").researched
? 1
Expand Down Expand Up @@ -194,7 +203,10 @@ export class BonfireManager {
const amphitheatreMeta = this._host.gamePage.bld.getBuildingExt("amphitheatre").meta;
// If amphitheathres haven't been upgraded to broadcast towers yet...
// This seems to be identical to the pasture upgrade.
if (amphitheatreMeta.stage === 0) {
if (
this._host.options.auto.bonfire.addition.upgradeBuildings.items.broadcasttower.enabled &&
amphitheatreMeta.stage === 0
) {
if (mustExist(amphitheatreMeta.stages)[1].stageUnlocked) {
// TODO: This is problematic. Upgrading from 50 amphitheatres to 1 broadcast tower sucks
// if you don't have enough resources to build several more.
Expand Down
13 changes: 11 additions & 2 deletions packages/userscript/source/options/BonfireSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { objectEntries } from "../tools/Entries";
import { Building } from "../types";
import { BuildingUpgradeSettings } from "./BuildingUpgradeSettings";
import { Requirement } from "./Options";
import { SettingLimit, SettingsSection, SettingToggle, SettingTrigger } from "./SettingsSection";
import { KittenStorageType } from "./SettingsStorage";
Expand Down Expand Up @@ -33,7 +34,7 @@ export type BonfireSettingsItem = SettingToggle &

export type BonfireAdditionSettings = {
turnOnSteamworks: SettingToggle;
upgradeBuildings: SettingToggle;
upgradeBuildings: BuildingUpgradeSettings;
};

export class BonfireSettings extends SettingsSection implements SettingTrigger {
Expand All @@ -42,7 +43,7 @@ export class BonfireSettings extends SettingsSection implements SettingTrigger {

addition: BonfireAdditionSettings = {
turnOnSteamworks: { enabled: true },
upgradeBuildings: { enabled: true },
upgradeBuildings: new BuildingUpgradeSettings(),
};

items: {
Expand Down Expand Up @@ -110,6 +111,10 @@ export class BonfireSettings extends SettingsSection implements SettingTrigger {

subject.items["toggle-buildings"] = settings.addition.upgradeBuildings.enabled;
subject.items["toggle-_steamworks"] = settings.addition.turnOnSteamworks.enabled;

for (const [name, item] of objectEntries(settings.addition.upgradeBuildings.items)) {
subject.items[`toggle-upgrade-${name}` as const] = item.enabled;
}
}

static fromLegacyOptions(subject: KittenStorageType) {
Expand All @@ -126,6 +131,10 @@ export class BonfireSettings extends SettingsSection implements SettingTrigger {
subject.items["toggle-buildings"] ?? options.addition.upgradeBuildings.enabled;
options.addition.turnOnSteamworks.enabled =
subject.items["toggle-_steamworks"] ?? options.addition.turnOnSteamworks.enabled;

for (const [name, item] of objectEntries(options.addition.upgradeBuildings.items)) {
item.enabled = subject.items[`toggle-upgrade-${name}` as const] ?? item.enabled;
}
return options;
}
}
14 changes: 14 additions & 0 deletions packages/userscript/source/options/BuildingUpgradeSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { StagedBuilding } from "../types";
import { SettingsSection, SettingToggle } from "./SettingsSection";

export type BuildingUpgradeSettingsItem = SettingToggle;
export class BuildingUpgradeSettings extends SettingsSection {
items: {
[item in StagedBuilding]: BuildingUpgradeSettingsItem;
} = {
broadcasttower: { enabled: true },
dataCenter: { enabled: true },
hydroplant: { enabled: true },
solarfarm: { enabled: true },
};
}
16 changes: 14 additions & 2 deletions packages/userscript/source/options/SettingsStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Job, Missions, Policy, Race, Resource, Season, Technology, Upgrade } from "../types";
import {
Job,
Missions,
Policy,
Race,
Resource,
Season,
StagedBuilding,
Technology,
Upgrade,
} from "../types";
import { BonfireItem } from "./BonfireSettings";
import { FilterItem } from "./FilterSettings";
import { AllItems } from "./Options";
Expand Down Expand Up @@ -66,8 +76,9 @@ type ToggleUnlockItem = `toggle-${ScienceItem}`;
type ToggleUnlockRaces = "toggle-races";
type ToggleUnlockMissions = "toggle-missions";
type ToggleUnlockUpgrades = "toggle-upgrades";
type ToggleUpgradeItem = `toggle-upgrade-${Upgrade}`;
type ToggleUpgradeBuildings = "toggle-buildings";
type ToggleUpgradeBuildingItem = `toggle-upgrade-${StagedBuilding}`;
type ToggleUpgradeItem = `toggle-upgrade-${Upgrade}`;

export type KittenStorageType = {
version: number;
Expand Down Expand Up @@ -117,6 +128,7 @@ export type KittenStorageType = {
Partial<Record<ToggleUnlockRaces, boolean>> &
Partial<Record<ToggleUnlockUpgrades, boolean>> &
Partial<Record<ToggleUpgradeBuildings, boolean>> &
Partial<Record<ToggleUpgradeBuildingItem, boolean>> &
Partial<Record<ToggleUpgradeItem, boolean>>;
resources: Partial<
Record<
Expand Down
2 changes: 2 additions & 0 deletions packages/userscript/source/types/buildings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export type Building =
| "zebraWorkshop"
| "ziggurat";

export type StagedBuilding = "broadcasttower" | "dataCenter" | "hydroplant" | "solarfarm";

// Returned from gamePage.bld.getBuildingExt()
export type BuildingMeta = {
calculateEffects?: (model: unknown, game: GamePage) => void;
Expand Down
64 changes: 61 additions & 3 deletions packages/userscript/source/ui/BonfireSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class BonfireSettingsUi extends SettingsSectionUi<BonfireSettings> {

private readonly _options: BonfireSettings;

private _buildingUpgradesExpanded = false;

constructor(host: UserScript, options: BonfireSettings = host.options.auto.bonfire) {
super(host);

Expand Down Expand Up @@ -280,9 +282,9 @@ export class BonfireSettingsUi extends SettingsSectionUi<BonfireSettings> {
}

getAdditionOptions(addition: BonfireAdditionSettings): Array<JQuery<HTMLElement>> {
const nodeHeader = this._getHeader("Additional options");
const header = this._getHeader("Additional options");

const nodeUpgradeBuildings = this._getOption(
const upgradeBuildingsButton = this._getOption(
"buildings",
addition.upgradeBuildings,
this._host.i18n("ui.upgrade.buildings"),
Expand All @@ -299,6 +301,49 @@ export class BonfireSettingsUi extends SettingsSectionUi<BonfireSettings> {
}
);

const upgradeBuildingsList = $("<ul/>", {
id: "items-list-buildings",
css: { display: "none", paddingLeft: "20px" },
});

const upgradeBuildingsButtons = [];
for (const [upgradeName, upgrade] of objectEntries(
this._options.addition.upgradeBuildings.items
)) {
const label = this._host.i18n(`$buildings.${upgradeName}.label`);
const button = this._getOption(`building-${upgradeName}`, upgrade, label, false, {
onCheck: () => {
this._host.updateOptions(() => (upgrade.enabled = true));
this._host.imessage("status.auto.enable", [label]);
},
onUnCheck: () => {
this._host.updateOptions(() => (upgrade.enabled = false));
this._host.imessage("status.auto.disable", [label]);
},
});

upgradeBuildingsButtons.push({ label: label, button: button });
}
// Ensure buttons are added into UI with their labels alphabetized.
upgradeBuildingsButtons.sort((a, b) => a.label.localeCompare(b.label));
upgradeBuildingsButtons.forEach(button => upgradeBuildingsList.append(button.button));

const upgradeBuildingsItemsButton = this._getItemsToggle("buildings-show");
upgradeBuildingsItemsButton.on("click", () => {
upgradeBuildingsList.toggle();

this._buildingUpgradesExpanded = !this._buildingUpgradesExpanded;

upgradeBuildingsItemsButton.text(this._buildingUpgradesExpanded ? "-" : "+");
upgradeBuildingsItemsButton.prop(
"title",
this._buildingUpgradesExpanded
? this._host.i18n("ui.itemsHide")
: this._host.i18n("ui.itemsShow")
);
});
upgradeBuildingsButton.append(upgradeBuildingsItemsButton, upgradeBuildingsList);

const nodeTurnOnSteamworks = this._getOption(
"_steamworks",
addition.turnOnSteamworks,
Expand All @@ -316,7 +361,7 @@ export class BonfireSettingsUi extends SettingsSectionUi<BonfireSettings> {
}
);

return [nodeHeader, nodeUpgradeBuildings, nodeTurnOnSteamworks];
return [header, upgradeBuildingsButton, nodeTurnOnSteamworks];
}

getState(): BonfireSettings {
Expand All @@ -338,6 +383,11 @@ export class BonfireSettingsUi extends SettingsSectionUi<BonfireSettings> {
option.enabled = state.items[name].enabled;
option.max = state.items[name].max;
}

// Building upgrades.
for (const [name, option] of objectEntries(this._options.addition.upgradeBuildings.items)) {
option.enabled = state.addition.upgradeBuildings.items[name].enabled;
}
}

refreshUi(): void {
Expand All @@ -358,5 +408,13 @@ export class BonfireSettingsUi extends SettingsSectionUi<BonfireSettings> {
this._host.i18n("ui.max", [this._renderLimit(this._options.items[name].max)])
);
}

// Building upgrades.
for (const [name, option] of objectEntries(this._options.addition.upgradeBuildings.items)) {
mustExist(option.$enabled).prop(
"checked",
this._options.addition.upgradeBuildings.items[name].enabled
);
}
}
}
10 changes: 5 additions & 5 deletions packages/userscript/source/ui/ScienceSettingsUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ export class ScienceSettingsUi extends SettingsSectionUi<ScienceSettings> {
for (const [techName, tech] of objectEntries(
(this._options.items.techs as TechSettings).items
)) {
const techLabel = this._host.i18n(`$science.${techName}.label`);
const techButton = this._getOption(`tech-${techName}`, tech, techLabel, false, {
const label = this._host.i18n(`$science.${techName}.label`);
const button = this._getOption(`tech-${techName}`, tech, label, false, {
onCheck: () => {
this._host.updateOptions(() => (tech.enabled = true));
this._host.imessage("status.auto.enable", [techLabel]);
this._host.imessage("status.auto.enable", [label]);
},
onUnCheck: () => {
this._host.updateOptions(() => (tech.enabled = false));
this._host.imessage("status.auto.disable", [techLabel]);
this._host.imessage("status.auto.disable", [label]);
},
});

techButtons.push({ label: techLabel, button: techButton });
techButtons.push({ label: label, button: button });
}
// Ensure buttons are added into UI with their labels alphabetized.
techButtons.sort((a, b) => a.label.localeCompare(b.label));
Expand Down

0 comments on commit 70685f7

Please sign in to comment.