Skip to content

Commit 8c2e732

Browse files
feat: Export pollution metrics
1 parent a10b676 commit 8c2e732

File tree

7 files changed

+143
-23
lines changed

7 files changed

+143
-23
lines changed

packages/kitten-analysts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"ka:build": "node build.js && vite --config vite.config.inject.js build",
2525
"ka:compose:build": "docker-compose build",
2626
"ka:compose:down": "docker-compose down",
27+
"ka:compose:refresh": "yarn run ka:compose:build; yarn run ka:compose:down; yarn run ka:compose:up -d",
2728
"ka:compose:up": "docker-compose up",
2829
"ka:preview": "DEV_BUILD=true vite --config vite.config.userscript.js build",
2930
"ka:release": "MINIFY=true vite --config vite.config.userscript.js build; vite --config vite.config.userscript.js build",

packages/kitten-analysts/source/KittenAnalysts.ts

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
KGNetSavePersisted,
66
TabId,
77
} from "@kitten-science/kitten-scientists/types/index.js";
8+
import { isNil } from "@oliversalzburg/js-utils/data/nil.js";
89
import { redirectErrorsToConsole } from "@oliversalzburg/js-utils/errors/console.js";
910
import { cdebug, cinfo, cwarn } from "./tools/Log.js";
1011
import { identifyExchange } from "./tools/MessageFormat.js";
@@ -20,6 +21,7 @@ export type KittenAnalystsMessageId =
2021
| "connected"
2122
| "getBuildings"
2223
| "getCalendar"
24+
| "getPollution"
2325
| "getRaces"
2426
| "getResourcePool"
2527
| "getStatistics"
@@ -49,6 +51,11 @@ export type PayloadCalendar = Array<{
4951
year: number;
5052
yearsPerCycle: number;
5153
}>;
54+
export type PayloadPollution = Array<{
55+
label: string;
56+
name: string;
57+
pollution: number;
58+
}>;
5259
export type PayloadRaces = Array<{
5360
embassyLevel: number;
5461
energy: number;
@@ -85,21 +92,23 @@ export interface KittenAnalystsMessage<
8592
? PayloadBuildings
8693
: TMessage extends "getCalendar"
8794
? PayloadCalendar
88-
: TMessage extends "getRaces"
89-
? PayloadRaces
90-
: TMessage extends "getResourcePool"
91-
? PayloadResources
92-
: TMessage extends "getStatistics"
93-
? PayloadStatistics
94-
: TMessage extends "getTechnologies"
95-
? PayloadTechnologies
96-
: TMessage extends "reportFrame"
97-
? unknown
98-
: TMessage extends "reportSavegame"
95+
: TMessage extends "getPollution"
96+
? PayloadPollution
97+
: TMessage extends "getRaces"
98+
? PayloadRaces
99+
: TMessage extends "getResourcePool"
100+
? PayloadResources
101+
: TMessage extends "getStatistics"
102+
? PayloadStatistics
103+
: TMessage extends "getTechnologies"
104+
? PayloadTechnologies
105+
: TMessage extends "reportFrame"
99106
? unknown
100-
: TMessage extends "injectSavegame"
101-
? KGNetSavePersisted
102-
: never,
107+
: TMessage extends "reportSavegame"
108+
? unknown
109+
: TMessage extends "injectSavegame"
110+
? KGNetSavePersisted
111+
: never,
103112
> {
104113
/**
105114
* The payload of the message.
@@ -365,6 +374,44 @@ export class KittenAnalysts {
365374
};
366375
}
367376

377+
case "getPollution": {
378+
const producers = game.bld.meta[0].meta.filter(
379+
_ => !isNil(_.effects?.cathPollutionPerTickProd),
380+
);
381+
const consumers = game.bld.meta[0].meta.filter(
382+
_ => !isNil(_.effects?.cathPollutionPerTickCon),
383+
);
384+
385+
const data: PayloadPollution = [
386+
{
387+
label: "Total",
388+
name: "cathPollution",
389+
// Could be simplified, but this syntax matches the function getPollutionMod of the game.
390+
pollution: (game.bld.cathPollution / 10000000) * 100,
391+
},
392+
...producers.map(_ => ({
393+
label: _.label ?? "",
394+
name: _.name,
395+
pollution: (_.effects?.cathPollutionPerTickProd ?? 0) * _.on,
396+
})),
397+
...consumers.map(_ => ({
398+
label: _.label ?? "",
399+
name: _.name,
400+
pollution: (_.effects?.cathPollutionPerTickCon ?? 0) * _.on,
401+
})),
402+
];
403+
404+
return {
405+
client_type: this.location.includes("headless.html") ? "headless" : "browser",
406+
data,
407+
guid: game.telemetry.guid,
408+
location: this.location,
409+
responseId: message.responseId,
410+
411+
type: message.type,
412+
};
413+
}
414+
368415
case "getRaces": {
369416
const data: PayloadRaces = game.diplomacy.races.map(race => ({
370417
embassyLevel: race.embassyLevel,

packages/kitten-analysts/source/entrypoint-backend.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
KittenAnalystsMessageId,
2222
PayloadBuildings,
2323
PayloadCalendar,
24+
PayloadPollution,
2425
PayloadRaces,
2526
PayloadResources,
2627
PayloadStatistics,
@@ -40,6 +41,8 @@ import { kg_kittens_average } from "./metrics/kg_kittens_average.js";
4041
import { kg_kittens_dead } from "./metrics/kg_kittens_dead.js";
4142
import { kg_kittens_total } from "./metrics/kg_kittens_total.js";
4243
import { kg_paragon_total } from "./metrics/kg_paragon_total.js";
44+
import { kg_pollution_production } from "./metrics/kg_pollution_prodution.js";
45+
import { kg_pollution_total } from "./metrics/kg_pollution_total.js";
4346
import { kg_race_energy } from "./metrics/kg_race_energy.js";
4447
import { kg_race_standing } from "./metrics/kg_race_standing.js";
4548
import { kg_resets_total } from "./metrics/kg_resets_total.js";
@@ -94,6 +97,7 @@ const cache = new Map<
9497
KittenAnalystsMessageId,
9598
| PayloadBuildings
9699
| PayloadCalendar
100+
| PayloadPollution
97101
| PayloadRaces
98102
| PayloadResources
99103
| PayloadStatistics
@@ -121,6 +125,9 @@ register.registerMetric(kg_race_standing(cache, remote));
121125
register.registerMetric(kg_crypto_price(cache, remote));
122126
register.registerMetric(kg_festival_days(cache, remote));
123127

128+
register.registerMetric(kg_pollution_production(cache, remote));
129+
register.registerMetric(kg_pollution_total(cache, remote));
130+
124131
// Metrics from in-game Stats
125132

126133
register.registerMetric(kg_buildings_constructed(cache, remote));

packages/kitten-analysts/source/metrics/factory.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { MessageCache } from "../entrypoint-backend.js";
44
import {
55
PayloadBuildings,
66
PayloadCalendar,
7+
PayloadPollution,
78
PayloadRaces,
89
PayloadResources,
910
PayloadStatistics,
@@ -15,6 +16,7 @@ export const gaugeFactory = <
1516
TMessage extends
1617
| "getBuildings"
1718
| "getCalendar"
19+
| "getPollution"
1820
| "getRaces"
1921
| "getResourcePool"
2022
| "getStatistics"
@@ -23,22 +25,25 @@ export const gaugeFactory = <
2325
TData extends
2426
| PayloadBuildings
2527
| PayloadCalendar
28+
| PayloadPollution
2629
| PayloadRaces
2730
| PayloadResources
2831
| PayloadStatistics
2932
| PayloadTechnologies = TMessage extends "getBuildings"
3033
? PayloadBuildings
3134
: TMessage extends "getCalendar"
3235
? PayloadCalendar
33-
: TMessage extends "getRaces"
34-
? PayloadRaces
35-
: TMessage extends "getResourcePool"
36-
? PayloadResources
37-
: TMessage extends "getStatistics"
38-
? PayloadStatistics
39-
: TMessage extends "getTechnologies"
40-
? PayloadTechnologies
41-
: never,
36+
: TMessage extends "getPollution"
37+
? PayloadPollution
38+
: TMessage extends "getRaces"
39+
? PayloadRaces
40+
: TMessage extends "getResourcePool"
41+
? PayloadResources
42+
: TMessage extends "getStatistics"
43+
? PayloadStatistics
44+
: TMessage extends "getTechnologies"
45+
? PayloadTechnologies
46+
: never,
4247
>(instructions: {
4348
cache: MessageCache;
4449
remote: KittensGameRemote;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ucfirst } from "@kitten-science/kitten-scientists/tools/Format.js";
2+
import { MessageCache } from "../entrypoint-backend.js";
3+
import { KittensGameRemote } from "../network/KittensGameRemote.js";
4+
import { gaugeFactory } from "./factory.js";
5+
6+
export const kg_pollution_production = (cache: MessageCache, remote: KittensGameRemote) =>
7+
gaugeFactory({
8+
cache,
9+
remote,
10+
help: "How much pollution buildings currently generate.",
11+
name: "kg_pollution_production",
12+
labelNames: ["client_type", "guid", "label", "location", "name"] as const,
13+
require: "getPollution",
14+
extract(client_type, guid, location, element, subject) {
15+
// This is tracked in kg_pollution_total
16+
if (element.name === "cathPollution") {
17+
return;
18+
}
19+
20+
subject.set(
21+
{
22+
client_type,
23+
guid,
24+
label: ucfirst(element.label),
25+
location,
26+
name: element.name,
27+
},
28+
element.pollution,
29+
);
30+
},
31+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { MessageCache } from "../entrypoint-backend.js";
2+
import { KittensGameRemote } from "../network/KittensGameRemote.js";
3+
import { gaugeFactory } from "./factory.js";
4+
5+
export const kg_pollution_total = (cache: MessageCache, remote: KittensGameRemote) =>
6+
gaugeFactory({
7+
cache,
8+
remote,
9+
help: "The current pollution level on Cath.",
10+
name: "kg_pollution_total",
11+
labelNames: ["client_type", "guid", "location"] as const,
12+
require: "getPollution",
13+
extract(client_type, guid, location, element, subject) {
14+
// Contributors are tracked in kg_pollution_production
15+
if (element.name !== "cathPollution") {
16+
return;
17+
}
18+
19+
subject.set(
20+
{
21+
client_type,
22+
guid,
23+
location,
24+
},
25+
element.pollution,
26+
);
27+
},
28+
});

packages/kitten-scientists/source/types/game.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export type Game = {
130130
title: string;
131131
buildings: Array<Building>;
132132
}>;
133+
cathPollution: number;
133134
/** @deprecated Use `getBuildingExt()` instead. */
134135
get: (build: Building) => BuildingMeta;
135136
getBuildingExt: (building: Building) => BuildingExt;

0 commit comments

Comments
 (0)