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

Commit

Permalink
refactor(core): Encapsulate settings in manager
Browse files Browse the repository at this point in the history
Step 2 moves most accesses to settings into the managers. The global options object has been removed.
  • Loading branch information
oliversalzburg committed Sep 27, 2022
1 parent d17034c commit 234ed5b
Show file tree
Hide file tree
Showing 31 changed files with 1,462 additions and 1,519 deletions.
37 changes: 23 additions & 14 deletions packages/userscript/source/BonfireManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BulkManager } from "./BulkManager";
import { BonfireItem, BonfireSettingsItem } from "./options/BonfireSettings";
import { Automation, TickContext } from "./Engine";
import { BonfireItem, BonfireSettings, BonfireSettingsItem } from "./options/BonfireSettings";
import { TabManager } from "./TabManager";
import { objectEntries } from "./tools/Entries";
import { isNil, mustExist } from "./tools/Maybe";
Expand All @@ -9,33 +10,41 @@ import { WorkshopManager } from "./WorkshopManager";

export type BonfireTab = GameTab;

export class BonfireManager {
export class BonfireManager implements Automation {
private readonly _host: UserScript;
settings: BonfireSettings;
readonly manager: TabManager<BonfireTab>;
private readonly _bulkManager: BulkManager;
private readonly _workshopManager: WorkshopManager;

constructor(host: UserScript) {
constructor(host: UserScript, settings = new BonfireSettings()) {
this._host = host;
this.settings = settings;
this.manager = new TabManager<BonfireTab>(this._host, "Bonfire");
this._bulkManager = new BulkManager(this._host);
this._workshopManager = new WorkshopManager(this._host);
}

tick(context: TickContext) {
if (!this.settings.enabled) {
return;
}

this.autoBuild();
this.autoMisc();
}

/**
* Try to build as many of the passed buildings as possible.
* Usually, this is called at each iteration of the automation engine to
* handle the building of items on the Bonfire tab.
*
* @param builds The buildings to build.
*/
autoBuild(
builds: Partial<Record<BonfireItem, BonfireSettingsItem>> = this._host.options.auto.bonfire
.items
) {
autoBuild(builds: Partial<Record<BonfireItem, BonfireSettingsItem>> = this.settings.items) {
// TODO: Refactor. See SpaceManager.autoBuild
const bulkManager = this._bulkManager;
const trigger = this._host.options.auto.bonfire.trigger;
const trigger = this.settings.trigger;

// Render the tab to make sure that the buttons actually exist in the DOM.
// TODO: Is this really required?
Expand Down Expand Up @@ -79,7 +88,7 @@ export class BonfireManager {
const pastureMeta = this._host.gamePage.bld.getBuildingExt("pasture").meta;
// If pastures haven't been upgraded to solar farms yet...
if (
this._host.options.auto.bonfire.addition.upgradeBuildings.items.solarfarm.enabled &&
this.settings.addition.upgradeBuildings.items.solarfarm.enabled &&
pastureMeta.stage === 0
) {
if (mustExist(pastureMeta.stages)[1].stageUnlocked) {
Expand Down Expand Up @@ -114,7 +123,7 @@ export class BonfireManager {
const aqueductMeta = this._host.gamePage.bld.getBuildingExt("aqueduct").meta;
// If aqueducts haven't beeen upgraded to hydro plants yet...
if (
this._host.options.auto.bonfire.addition.upgradeBuildings.items.hydroplant.enabled &&
this.settings.addition.upgradeBuildings.items.hydroplant.enabled &&
aqueductMeta.stage === 0
) {
if (mustExist(aqueductMeta.stages)[1].stageUnlocked) {
Expand Down Expand Up @@ -146,7 +155,7 @@ export class BonfireManager {

const libraryMeta = this._host.gamePage.bld.getBuildingExt("library").meta;
if (
this._host.options.auto.bonfire.addition.upgradeBuildings.items.dataCenter.enabled &&
this.settings.addition.upgradeBuildings.items.dataCenter.enabled &&
libraryMeta.stage === 0
) {
if (mustExist(libraryMeta.stages)[1].stageUnlocked) {
Expand Down Expand Up @@ -204,7 +213,7 @@ export class BonfireManager {
// If amphitheathres haven't been upgraded to broadcast towers yet...
// This seems to be identical to the pasture upgrade.
if (
this._host.options.auto.bonfire.addition.upgradeBuildings.items.broadcasttower.enabled &&
this.settings.addition.upgradeBuildings.items.broadcasttower.enabled &&
amphitheatreMeta.stage === 0
) {
if (mustExist(amphitheatreMeta.stages)[1].stageUnlocked) {
Expand Down Expand Up @@ -232,7 +241,7 @@ export class BonfireManager {

autoMisc() {
// Auto turn on steamworks
if (this._host.options.auto.bonfire.addition.turnOnSteamworks.enabled) {
if (this.settings.addition.turnOnSteamworks.enabled) {
const steamworks = this._host.gamePage.bld.getBuildingExt("steamworks");
if (steamworks.meta.val && steamworks.meta.on === 0) {
const button = mustExist(this.getBuildButton("steamworks"));
Expand All @@ -241,7 +250,7 @@ export class BonfireManager {
}

// If buildings (upgrades of bonfire items) are enabled...
if (this._host.options.auto.bonfire.addition.upgradeBuildings.enabled) {
if (this.settings.addition.upgradeBuildings.enabled) {
this.autoUpgrade();
}
}
Expand Down

0 comments on commit 234ed5b

Please sign in to comment.