@kitten-science/kitten-scientists
Version:
Add-on for the wonderful incremental browser game: https://kittensgame.com/web/
130 lines • 5.76 kB
JavaScript
import { mustExist } from "@oliversalzburg/js-utils/data/nil.js";
import { BulkPurchaseHelper } from "./helper/BulkPurchaseHelper.js";
import { TimeSettings } from "./settings/TimeSettings.js";
import { cl } from "./tools/Log.js";
import { TimeItemVariant } from "./types/index.js";
export class TimeManager {
_host;
settings;
_bulkManager;
_workshopManager;
constructor(host, workshopManager, settings = new TimeSettings()) {
this._host = host;
this.settings = settings;
this._bulkManager = new BulkPurchaseHelper(this._host, workshopManager);
this._workshopManager = workshopManager;
}
tick(context) {
if (!this.settings.enabled) {
return;
}
this._bulkManager.resetPriceCache();
this.autoBuild(context);
if (this.settings.fixCryochambers.enabled) {
this.fixCryochambers();
}
}
/**
* 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 Time tab.
*
* @param builds The buildings to build.
*/
autoBuild(context, builds = this.settings.buildings) {
const sectionTrigger = this.settings.trigger;
// Get the current metadata for all the referenced buildings.
const metaData = {};
for (const build of Object.values(builds)) {
const buildMeta = build.variant === TimeItemVariant.Chronoforge
? this._host.game.time.getCFU(build.building)
: this._host.game.time.getVSU(build.building);
metaData[build.building] = mustExist(buildMeta);
const buildButton = build.variant === TimeItemVariant.Chronoforge
? this._host.game.time.queue.getQueueElementControllerAndModel({
name: build.building,
type: "chronoforge",
})
: this._host.game.time.queue.getQueueElementControllerAndModel({
name: build.building,
type: "voidSpace",
});
const panelVisible = build.variant === TimeItemVariant.Chronoforge
? this._host.game.workshop.get("chronoforge").researched
: this._host.game.science.get("voidSpace").researched ||
this._host.game.time.getVSU("usedCryochambers").val > 0;
const model = buildButton.model;
const buildingMetaData = mustExist(metaData[build.building]);
buildingMetaData.tHidden = !model.metadata.unlocked || !panelVisible;
}
const builder = (build) => {
this.build(build.id, build.variant, build.count);
};
context.purchaseOrders.push({ builder, builds, metaData, sectionTrigger });
}
build(name, variant, amount) {
let amountConstructed = 0;
let label;
if (variant === TimeItemVariant.Chronoforge) {
const itemMetaRaw = game.getUnlockByName(name, "chronoforge");
const controller = new classes.ui.time.ChronoforgeBtnController(this._host.game);
const model = controller.fetchModel({ controller, id: itemMetaRaw.name });
amountConstructed = this._bulkManager.construct(model, controller, amount);
label = itemMetaRaw.label;
}
else {
const itemMetaRaw = game.getUnlockByName(name, "voidSpace");
const controller = new classes.ui.time.VoidSpaceBtnController(this._host.game);
const model = controller.fetchModel({ controller, id: itemMetaRaw.name });
amountConstructed = this._bulkManager.construct(model, controller, amount);
label = itemMetaRaw.label;
}
if (amount !== amountConstructed) {
console.warn(...cl(`${label} Amount ordered: ${amount} Amount Constructed: ${amountConstructed}`));
// Bail out to not flood the log with garbage.
if (amountConstructed === 0) {
return;
}
}
this._host.engine.storeForSummary(label, amountConstructed, "build");
if (amountConstructed === 1) {
this._host.engine.iactivity("act.build", [label], "ks-build");
}
else {
this._host.engine.iactivity("act.builds", [label, this._host.renderAbsolute(amountConstructed)], "ks-build");
}
}
getBuild(name, variant) {
if (variant === TimeItemVariant.Chronoforge) {
return this._host.game.time.getCFU(name);
}
return this._host.game.time.getVSU(name);
}
fixCryochambers() {
if (this._host.game.time.getVSU("usedCryochambers").val < 1) {
return;
}
const prices = mustExist(this._host.game.time.getVSU("usedCryochambers").fixPrices);
for (const price of prices) {
const available = this._workshopManager.getValueAvailable(price.name);
if (available < price.val) {
return;
}
}
const controller = new classes.ui.time.FixCryochamberBtnController(this._host.game);
const model = controller.fetchModel({});
let fixed = 0;
let fixHappened;
do {
fixHappened = false;
const buyResult = controller.buyItem(model);
fixHappened = buyResult.itemBought;
fixed += fixHappened ? 1 : 0;
} while (fixHappened);
if (0 < fixed) {
this._host.engine.iactivity("act.fix.cry", [this._host.renderAbsolute(fixed)], "ks-fixCry");
this._host.engine.storeForSummary("fix.cry", fixed);
}
}
}
//# sourceMappingURL=TimeManager.js.map