Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 45 additions & 36 deletions source/BonfireManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import {
type BonfireItem,
BonfireSettings,
} from "./settings/BonfireSettings.js";
import { cwarn } from "./tools/Log.js";
import { cdebug, cwarn } from "./tools/Log.js";
import type {
BuildButton,
Building,
BuildingExt,
BuildingMeta,
ButtonModernController,
ButtonModernModel,
GameTab,
} from "./types/index.js";

export type BonfireTab = GameTab;
BuildingsModern,
GatherCatnipButton,
GatherCatnipButtonController,
RefineCatnipButton,
UnsafeBuilding,
UnsafeBuildingExt,
} from "./types/buildings.js";
import type { ButtonModern, UnsafeButtonModernModel } from "./types/core.js";
import type { Building } from "./types/index.js";

export class BonfireManager implements Automation {
private readonly _host: KittenScientists;
readonly settings: BonfireSettings;
readonly manager: TabManager;
readonly manager: TabManager<BuildingsModern>;
private readonly _bulkManager: BulkPurchaseHelper;
private readonly _workshopManager: WorkshopManager;

Expand All @@ -36,7 +36,7 @@ export class BonfireManager implements Automation {
) {
this._host = host;
this.settings = settings;
this.manager = new TabManager<BonfireTab>(this._host, "Bonfire");
this.manager = new TabManager<BuildingsModern>(this._host, "Bonfire");

this._workshopManager = workshopManager;
this._bulkManager = new BulkPurchaseHelper(this._host, this._workshopManager);
Expand Down Expand Up @@ -69,7 +69,7 @@ export class BonfireManager implements Automation {
const bulkManager = this._bulkManager;

// Get the current metadata for all the referenced buildings.
const metaData: Partial<Record<BonfireItem, BuildingMeta>> = {};
const metaData: Partial<Record<BonfireItem, Required<UnsafeBuilding>>> = {};
for (const build of Object.values(builds)) {
metaData[build.building] = this.getBuild(
(build.baseBuilding ?? build.building) as Building,
Expand Down Expand Up @@ -125,7 +125,7 @@ export class BonfireManager implements Automation {
this._host.engine.iactivity("upgrade.building.pasture", [], "ks-upgrade");

// Upgrade the pasture.
this._host.game.ui.render();
this._host.game.ui?.render();
this.build("pasture", 1, 1);
context.requestGameUiRefresh = true;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ export class BonfireManager implements Automation {

this._host.engine.iactivity("upgrade.building.aqueduct", [], "ks-upgrade");

this._host.game.ui.render();
this._host.game.ui?.render();
this.build("aqueduct", 1, 1);
context.requestGameUiRefresh = true;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ export class BonfireManager implements Automation {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
libraryMeta.calculateEffects?.(libraryMeta, this._host.game);
this._host.engine.iactivity("upgrade.building.library", [], "ks-upgrade");
this._host.game.ui.render();
this._host.game.ui?.render();
this.build("library", 1, 1);
context.requestGameUiRefresh = true;
return;
Expand All @@ -241,7 +241,7 @@ export class BonfireManager implements Automation {

this._host.engine.iactivity("upgrade.building.warehouse", [], "ks-upgrade");

this._host.game.ui.render();
this._host.game.ui?.render();
this.build("warehouse", 1, 1);
context.requestGameUiRefresh = true;

Expand Down Expand Up @@ -272,7 +272,7 @@ export class BonfireManager implements Automation {

this._host.engine.iactivity("upgrade.building.amphitheatre", [], "ks-upgrade");

this._host.game.ui.render();
this._host.game.ui?.render();
this.build("amphitheatre", 1, 1);
context.requestGameUiRefresh = true;
}
Expand Down Expand Up @@ -333,9 +333,7 @@ export class BonfireManager implements Automation {
autoGather(): void {
const controller = new classes.game.ui.GatherCatnipButtonController(this._host.game);
for (let clicks = 0; clicks < Math.floor(this._host.engine.settings.interval / 20); ++clicks) {
controller.buyItem(null, null, () => {
/* intentionally left blank */
});
controller.buyItem(undefined, null);
}
}

Expand All @@ -354,7 +352,11 @@ export class BonfireManager implements Automation {

const amountTemp = amountCalculated;
const label = this._getBuildLabel(build.meta, stage);
amountCalculated = this._bulkManager.construct(button.model, button, amountCalculated);
amountCalculated = this._bulkManager.construct(
button.model,
button.controller,
amountCalculated,
);
if (amountCalculated !== amountTemp) {
cwarn(`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amountCalculated}`);
}
Expand All @@ -371,25 +373,32 @@ export class BonfireManager implements Automation {
}
}

private _getBuildLabel(meta: BuildingMeta, stage?: number): string {
private _getBuildLabel(meta: UnsafeBuilding, stage?: number): string {
return meta.stages && !isNil(stage) ? meta.stages[stage].label : mustExist(meta.label);
}

getBuild(name: Building): BuildingExt {
return this._host.game.bld.getBuildingExt(name);
getBuild(name: Building): BuildingMeta<Required<UnsafeBuilding>> {
return this._host.game.bld.getBuildingExt(name) as BuildingMeta<Required<UnsafeBuilding>>;
}

getBuildButton(
name: Building,
stage?: number,
): BuildButton<string, ButtonModernModel, ButtonModernController> | null {
getBuildButton(name: Building, stage?: number) {
const buttons = this.manager.tab.children;
const build = this.getBuild(name);
const label = this._getBuildLabel(build.meta, stage);
return (buttons.find(button => button.model?.name.startsWith(label)) ?? null) as BuildButton<
string,
ButtonModernModel,
ButtonModernController
> | null;

const button =
(
buttons.filter(
button =>
button instanceof com.nuclearunicorn.game.ui.ButtonModern === false &&
button instanceof classes.game.ui.RefineCatnipButton === false,
) as Array<Exclude<(typeof buttons)[number], GatherCatnipButton | RefineCatnipButton>>
).find(
button => button.model?.metadata.name === name && button.model?.metadata.stage === stage,
) ?? null;

if (button === null) {
cdebug(`Couldn't find button for ${name}! This will likely create problems.`);
}

return button;
}
}
6 changes: 3 additions & 3 deletions source/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ export class Engine {
private _printOutput(
cssClasses: "ks-activity" | `ks-activity ${ActivityTypeClass}` | "ks-default" | "ks-summary",
color: string,
...args: Array<number | string>
message: string,
): void {
if (this.settings.filters.enabled) {
for (const filterItem of Object.values(this.settings.filters.filters)) {
Expand All @@ -572,10 +572,10 @@ export class Engine {
}

// update the color of the message immediately after adding
const msg = this._host.game.msg(...args, cssClasses);
const msg = this._host.game.msg(message, cssClasses);
$(msg.span).css("color", color);

cdebug(...args);
cdebug(message);
}

static evaluateSubSectionTrigger(sectionTrigger: number, subSectionTrigger: number): number {
Expand Down
10 changes: 6 additions & 4 deletions source/KittenScientists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { ScienceSettings } from "./settings/ScienceSettings.js";
import { SpaceSettings } from "./settings/SpaceSettings.js";
import { WorkshopSettings } from "./settings/WorkshopSettings.js";
import { cdebug, cerror, cinfo, cwarn } from "./tools/Log.js";
import type { Game, I18nEngine } from "./types/index.js";
import type { ReleaseChannel, ReleaseInfoSchema } from "./types/releases.js";
import type { ReleaseChannel, ReleaseInfoSchema } from "./types/_releases.js";
import type { TabManager } from "./types/core.js";
import type { GamePage } from "./types/game.js";
import type { I18nEngine } from "./types/index.js";
import { UserInterface } from "./ui/UserInterface.js";

declare global {
Expand All @@ -25,7 +27,7 @@ export const ksVersion = (prefix = "") => {
};

export class KittenScientists {
readonly game: Game;
readonly game: GamePage;

/**
* A function in the game that allows to retrieve translated messages.
Expand All @@ -42,7 +44,7 @@ export class KittenScientists {
private _serverLoadHandle: ["server/load", number] | undefined;

constructor(
game: Game,
game: GamePage,
i18nEngine: I18nEngine,
gameLanguage: GameLanguage = "en",
engineState?: EngineState,
Expand Down
Loading