@kitten-science/kitten-scientists
Version:
Add-on for the wonderful incremental browser game: https://kittensgame.com/web/
71 lines • 2.8 kB
JavaScript
export class UpgradeManager {
_host;
constructor(host) {
this._host = host;
}
upgrade(upgrade, variant) {
let success = false;
if (variant === "policy") {
const itemMetaRaw = game.getUnlockByName(upgrade.name, "policies");
const controller = new classes.ui.PolicyBtnController(this._host.game);
const model = controller.fetchModel({ controller, id: itemMetaRaw.name });
success = UpgradeManager.skipConfirm(() => controller.buyItem(model)).itemBought;
}
else if (variant === "science") {
const itemMetaRaw = game.getUnlockByName(upgrade.name, "tech");
const controller = new com.nuclearunicorn.game.ui.TechButtonController(this._host.game);
const model = controller.fetchModel({ controller, id: itemMetaRaw.name });
success = UpgradeManager.skipConfirm(() => controller.buyItem(model)).itemBought;
}
else {
const itemMetaRaw = game.getUnlockByName(upgrade.name, "upgrades");
const controller = new com.nuclearunicorn.game.ui.UpgradeButtonController(this._host.game);
const model = controller.fetchModel({ controller, id: itemMetaRaw.name });
success = UpgradeManager.skipConfirm(() => controller.buyItem(model)).itemBought;
}
if (!success) {
return false;
}
const label = upgrade.label;
if (variant === "workshop") {
this._host.engine.storeForSummary(label, 1, "upgrade");
this._host.engine.iactivity("upgrade.upgrade", [label], "ks-upgrade");
}
else if (variant === "policy") {
this._host.engine.iactivity("upgrade.policy", [label]);
}
else if (variant === "science") {
this._host.engine.storeForSummary(label, 1, "research");
this._host.engine.iactivity("upgrade.tech", [label], "ks-research");
}
return true;
}
/**
* Run a piece of code that might invoke UI confirmation and
* skip that UI confirmation.
*
* @param action The function to run without UI confirmation.
* @returns Whatever `action` returns.
*/
static async skipConfirmAsync(action) {
const originalConfirm = game.ui.confirm;
try {
game.ui.confirm = () => true;
return await action();
}
finally {
game.ui.confirm = originalConfirm;
}
}
static skipConfirm(action) {
const originalConfirm = game.ui.confirm;
try {
game.ui.confirm = () => true;
return action();
}
finally {
game.ui.confirm = originalConfirm;
}
}
}
//# sourceMappingURL=UpgradeManager.js.map