Skip to content

Commit 0af5f5d

Browse files
feat(core): Implement entire game API surface
1 parent 0e8e848 commit 0af5f5d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+5386
-2045
lines changed

source/BonfireManager.ts

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import {
99
type BonfireItem,
1010
BonfireSettings,
1111
} from "./settings/BonfireSettings.js";
12-
import { cwarn } from "./tools/Log.js";
12+
import { cdebug, cwarn } from "./tools/Log.js";
1313
import type {
14-
BuildButton,
15-
Building,
16-
BuildingExt,
1714
BuildingMeta,
18-
ButtonModernController,
19-
ButtonModernModel,
20-
GameTab,
21-
} from "./types/index.js";
22-
23-
export type BonfireTab = GameTab;
15+
BuildingsModern,
16+
GatherCatnipButton,
17+
GatherCatnipButtonController,
18+
RefineCatnipButton,
19+
UnsafeBuilding,
20+
UnsafeBuildingExt,
21+
} from "./types/buildings.js";
22+
import type { ButtonModern, UnsafeButtonModernModel } from "./types/core.js";
23+
import type { Building } from "./types/index.js";
2424

2525
export class BonfireManager implements Automation {
2626
private readonly _host: KittenScientists;
2727
readonly settings: BonfireSettings;
28-
readonly manager: TabManager;
28+
readonly manager: TabManager<BuildingsModern>;
2929
private readonly _bulkManager: BulkPurchaseHelper;
3030
private readonly _workshopManager: WorkshopManager;
3131

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

4141
this._workshopManager = workshopManager;
4242
this._bulkManager = new BulkPurchaseHelper(this._host, this._workshopManager);
@@ -69,7 +69,7 @@ export class BonfireManager implements Automation {
6969
const bulkManager = this._bulkManager;
7070

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

127127
// Upgrade the pasture.
128-
this._host.game.ui.render();
128+
this._host.game.ui?.render();
129129
this.build("pasture", 1, 1);
130130
context.requestGameUiRefresh = true;
131131
}
@@ -158,7 +158,7 @@ export class BonfireManager implements Automation {
158158

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

161-
this._host.game.ui.render();
161+
this._host.game.ui?.render();
162162
this.build("aqueduct", 1, 1);
163163
context.requestGameUiRefresh = true;
164164
}
@@ -215,7 +215,7 @@ export class BonfireManager implements Automation {
215215
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
216216
libraryMeta.calculateEffects?.(libraryMeta, this._host.game);
217217
this._host.engine.iactivity("upgrade.building.library", [], "ks-upgrade");
218-
this._host.game.ui.render();
218+
this._host.game.ui?.render();
219219
this.build("library", 1, 1);
220220
context.requestGameUiRefresh = true;
221221
return;
@@ -241,7 +241,7 @@ export class BonfireManager implements Automation {
241241

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

244-
this._host.game.ui.render();
244+
this._host.game.ui?.render();
245245
this.build("warehouse", 1, 1);
246246
context.requestGameUiRefresh = true;
247247

@@ -272,7 +272,7 @@ export class BonfireManager implements Automation {
272272

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

275-
this._host.game.ui.render();
275+
this._host.game.ui?.render();
276276
this.build("amphitheatre", 1, 1);
277277
context.requestGameUiRefresh = true;
278278
}
@@ -333,7 +333,7 @@ export class BonfireManager implements Automation {
333333
autoGather(): void {
334334
const controller = new classes.game.ui.GatherCatnipButtonController(this._host.game);
335335
for (let clicks = 0; clicks < Math.floor(this._host.engine.settings.interval / 20); ++clicks) {
336-
controller.buyItem(null, null);
336+
controller.buyItem(undefined, null);
337337
}
338338
}
339339

@@ -352,7 +352,11 @@ export class BonfireManager implements Automation {
352352

353353
const amountTemp = amountCalculated;
354354
const label = this._getBuildLabel(build.meta, stage);
355-
amountCalculated = this._bulkManager.construct(button.model, button, amountCalculated);
355+
amountCalculated = this._bulkManager.construct(
356+
button.model,
357+
button.controller,
358+
amountCalculated,
359+
);
356360
if (amountCalculated !== amountTemp) {
357361
cwarn(`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amountCalculated}`);
358362
}
@@ -369,22 +373,32 @@ export class BonfireManager implements Automation {
369373
}
370374
}
371375

372-
private _getBuildLabel(meta: BuildingMeta, stage?: number): string {
376+
private _getBuildLabel(meta: UnsafeBuilding, stage?: number): string {
373377
return meta.stages && !isNil(stage) ? meta.stages[stage].label : mustExist(meta.label);
374378
}
375379

376-
getBuild(name: Building): BuildingExt {
377-
return this._host.game.bld.getBuildingExt(name);
380+
getBuild(name: Building): BuildingMeta<Required<UnsafeBuilding>> {
381+
return this._host.game.bld.getBuildingExt(name) as BuildingMeta<Required<UnsafeBuilding>>;
378382
}
379383

380-
getBuildButton(
381-
name: Building,
382-
stage?: number,
383-
): BuildButton<string, ButtonModernModel, ButtonModernController> | null {
384+
getBuildButton(name: Building, stage?: number) {
384385
const buttons = this.manager.tab.children;
385-
const build = this.getBuild(name);
386-
const label = this._getBuildLabel(build.meta, stage);
387-
return (buttons.find(button => (button.model?.options.name as string).startsWith(label)) ??
388-
null) as BuildButton<string, ButtonModernModel, ButtonModernController> | null;
386+
387+
const button =
388+
(
389+
buttons.filter(
390+
button =>
391+
button instanceof com.nuclearunicorn.game.ui.ButtonModern === false &&
392+
button instanceof classes.game.ui.RefineCatnipButton === false,
393+
) as Array<Exclude<(typeof buttons)[number], GatherCatnipButton | RefineCatnipButton>>
394+
).find(
395+
button => button.model?.metadata.name === name && button.model?.metadata.stage === stage,
396+
) ?? null;
397+
398+
if (button === null) {
399+
cdebug(`Couldn't find button for ${name}! This will likely create problems.`);
400+
}
401+
402+
return button;
389403
}
390404
}

source/Engine.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ export class Engine {
561561
private _printOutput(
562562
cssClasses: "ks-activity" | `ks-activity ${ActivityTypeClass}` | "ks-default" | "ks-summary",
563563
color: string,
564-
...args: Array<number | string>
564+
message: string,
565565
): void {
566566
if (this.settings.filters.enabled) {
567567
for (const filterItem of Object.values(this.settings.filters.filters)) {
@@ -572,10 +572,10 @@ export class Engine {
572572
}
573573

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

578-
cdebug(...args);
578+
cdebug(message);
579579
}
580580

581581
static evaluateSubSectionTrigger(sectionTrigger: number, subSectionTrigger: number): number {

source/KittenScientists.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { ScienceSettings } from "./settings/ScienceSettings.js";
77
import { SpaceSettings } from "./settings/SpaceSettings.js";
88
import { WorkshopSettings } from "./settings/WorkshopSettings.js";
99
import { cdebug, cerror, cinfo, cwarn } from "./tools/Log.js";
10-
import type { Game, I18nEngine } from "./types/index.js";
11-
import type { ReleaseChannel, ReleaseInfoSchema } from "./types/releases.js";
10+
import type { ReleaseChannel, ReleaseInfoSchema } from "./types/_releases.js";
11+
import type { TabManager } from "./types/core.js";
12+
import type { GamePage } from "./types/game.js";
13+
import type { I18nEngine } from "./types/index.js";
1214
import { UserInterface } from "./ui/UserInterface.js";
1315

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

2729
export class KittenScientists {
28-
readonly game: Game;
30+
readonly game: GamePage;
2931

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

4446
constructor(
45-
game: Game,
47+
game: GamePage,
4648
i18nEngine: I18nEngine,
4749
gameLanguage: GameLanguage = "en",
4850
engineState?: EngineState,

0 commit comments

Comments
 (0)