UNPKG

@kitten-science/kitten-scientists

Version:

Add-on for the wonderful incremental browser game: https://kittensgame.com/web/

136 lines 5.42 kB
import { isNil, mustExist } from "@oliversalzburg/js-utils/data/nil.js"; import { TabManager } from "./TabManager.js"; import { BulkPurchaseHelper } from "./helper/BulkPurchaseHelper.js"; import { SpaceSettings } from "./settings/SpaceSettings.js"; import { cwarn } from "./tools/Log.js"; export class SpaceManager { _host; settings; manager; _bulkManager; _workshopManager; constructor(host, workshopManager, settings = new SpaceSettings()) { this._host = host; this.settings = settings; this.manager = new TabManager(this._host, "Space"); this._workshopManager = workshopManager; this._bulkManager = new BulkPurchaseHelper(this._host, this._workshopManager); } tick(context) { if (!this.settings.enabled) { return; } // We must call `.render()` here, because evaluation of availability of our options // is only performed when the game renders the contents of that tab. this.manager.render(); this.autoBuild(context); if (this.settings.unlockMissions.enabled) { this.autoUnlock(context); } } /** * 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 Space tab. * * @param builds The buildings to build. */ autoBuild(context, builds = this.settings.buildings) { const bulkManager = this._bulkManager; const sectionTrigger = this.settings.trigger; // Get the current metadata for all the referenced buildings. const metaData = {}; for (const build of Object.values(builds)) { metaData[build.building] = this.getBuild(build.building); } // Let the bulkmanager determine the builds we can make. const buildList = bulkManager.bulk(builds, metaData, sectionTrigger, "Space"); // Build all entries in the build list, where we can build any items. for (const build of buildList) { if (build.count <= 0) { continue; } if (0 === this.build(build.id, build.count)) { continue; } context.requestGameUiRefresh = true; } } autoUnlock(context) { if (!this._host.game.tabs[6].visible) { return; } const missions = this._host.game.space.meta[0].meta; missionLoop: for (let i = 0; i < missions.length; i++) { // If the mission is already purchased or not available yet, continue with the next one. if (0 < missions[i].val || !missions[i].unlocked || !this.settings.unlockMissions.missions[missions[i].name].enabled) { continue; } const model = this.manager.tab.GCPanel?.children[i]; if (isNil(model)) { return; } const prices = mustExist(model.model?.prices); for (const resource of prices) { // If we can't afford this resource price, continue with the next mission. if (this._workshopManager.getValueAvailable(resource.name) < resource.val) { continue missionLoop; } } // Start the mission by clicking the button. // TODO: Move this into the SpaceManager? model.domNode.click(); context.requestGameUiRefresh = true; if (i === 7 || i === 12) { this._host.engine.iactivity("upgrade.space.mission", [missions[i].label], "ks-upgrade"); } else { this._host.engine.iactivity("upgrade.space", [missions[i].label], "ks-upgrade"); } } } build(name, amount) { let amountCalculated = amount; const build = this.getBuild(name); const button = this._getBuildButton(name); if (!build.unlocked || !button?.model || !this.settings.buildings[name].enabled) { return 0; } if (!button.model.enabled) { return 0; } const amountTemp = amountCalculated; const label = build.label; amountCalculated = this._bulkManager.construct(button.model, button, amountCalculated); if (amountCalculated !== amountTemp) { cwarn(`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amountCalculated}`); } this._host.engine.storeForSummary(label, amountCalculated, "build"); if (amountCalculated === 1) { this._host.engine.iactivity("act.build", [label], "ks-build"); } else { this._host.engine.iactivity("act.builds", [label, this._host.renderAbsolute(amountCalculated)], "ks-build"); } return amountCalculated; } getBuild(name) { return this._host.game.space.getBuilding(name); } _getBuildButton(id) { const panels = this.manager.tab.planetPanels; if (isNil(panels)) { return null; } for (const panel of panels) { const button = panel.children.find(child => child.id === id); if (!isNil(button)) { return button; } } return null; } } //# sourceMappingURL=SpaceManager.js.map