Add a production dashboard
Every minute, the mod diffs the force's production statistics for each
surface against the totals it sent last time (kept in
storage.productionTotals), and sends what was produced and consumed as a
ProductionUpdate.
events-server-web keeps the statistics in ring buffers (1440 minutes and
168 hours), and serves them at
/kafkatorio/data/servers/{id}/production, for a range of game time and
optionally one surface.
The web map has a production page (/servers/{id}/production, linked from
the header) with science packs used per minute (in total and per pack),
production per minute as a line chart, and a searchable table of
everything produced and consumed.
export_building_icons.py now also exports every item and fluid icon and
name (items.json, fluids.json), for the production page.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e31971c58e
commit
e1f8198b57
24 changed files with 1524 additions and 30 deletions
|
|
@ -20,6 +20,7 @@ export type KafkatorioPacketData =
|
|||
| KafkatorioPacketData.MapChunkResourceUpdate
|
||||
| KafkatorioPacketData.MapChunkTileUpdate
|
||||
| KafkatorioPacketData.PlayerUpdate
|
||||
| KafkatorioPacketData.ProductionUpdate
|
||||
| KafkatorioPacketData.PrototypesUpdate
|
||||
| KafkatorioPacketData.ResearchUpdate
|
||||
| KafkatorioPacketData.SurfaceUpdate;
|
||||
|
|
@ -29,6 +30,7 @@ export namespace KafkatorioPacketData {
|
|||
ConfigurationUpdate = "kafkatorio.packet.instant.ConfigurationUpdate",
|
||||
ConsoleChatUpdate = "kafkatorio.packet.instant.ConsoleChatUpdate",
|
||||
ConsoleCommandUpdate = "kafkatorio.packet.instant.ConsoleCommandUpdate",
|
||||
ProductionUpdate = "kafkatorio.packet.instant.ProductionUpdate",
|
||||
PrototypesUpdate = "kafkatorio.packet.instant.PrototypesUpdate",
|
||||
ResearchUpdate = "kafkatorio.packet.instant.ResearchUpdate",
|
||||
SurfaceUpdate = "kafkatorio.packet.instant.SurfaceUpdate",
|
||||
|
|
@ -61,6 +63,16 @@ export namespace KafkatorioPacketData {
|
|||
parameters: string;
|
||||
}
|
||||
|
||||
export interface ProductionUpdate {
|
||||
type: KafkatorioPacketData.Type.ProductionUpdate;
|
||||
forceName: string;
|
||||
surfaceIndex: SurfaceIndex;
|
||||
tick: Tick;
|
||||
periodTicks: UInt;
|
||||
items?: ProductionCounts;
|
||||
fluids?: ProductionCounts;
|
||||
}
|
||||
|
||||
export interface PrototypesUpdate {
|
||||
type: KafkatorioPacketData.Type.PrototypesUpdate;
|
||||
prototypes: FactorioPrototype[];
|
||||
|
|
@ -165,6 +177,11 @@ export interface ConfigurationUpdateGameData {
|
|||
newVersion?: string | null;
|
||||
}
|
||||
|
||||
export interface ProductionCounts {
|
||||
produced?: { [key: string]: number };
|
||||
consumed?: { [key: string]: number };
|
||||
}
|
||||
|
||||
export type Double = double;
|
||||
|
||||
export interface EntityUpdateKey {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,131 @@
|
|||
import PacketEmitter from "../emitting/PacketEmitter";
|
||||
import {KafkatorioPacketData, ProductionCounts, Tick, UInt} from "../../generated/kafkatorio-schema";
|
||||
import {LuaFlowStatistics, LuaForce, LuaSurface, OnTickEvent, SurfaceIndex} from "factorio:runtime"
|
||||
import Type = KafkatorioPacketData.Type;
|
||||
|
||||
|
||||
/** Send production statistics this often */
|
||||
const PRODUCTION_INTERVAL_TICKS = 60 * 60
|
||||
|
||||
/** Spread the surfaces over the interval, rather than sending them all on the same tick */
|
||||
const PRODUCTION_OFFSET_TICKS = 30
|
||||
|
||||
|
||||
/** Totals of a LuaFlowStatistics, by name. Produced = input, consumed = output. */
|
||||
interface FlowTotals {
|
||||
produced: Record<string, number>
|
||||
consumed: Record<string, number>
|
||||
}
|
||||
|
||||
interface ProductionTotals {
|
||||
tick: number
|
||||
items: FlowTotals
|
||||
fluids: FlowTotals
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send what the 'player' force produced and consumed on each surface since the last update.
|
||||
*
|
||||
* Factorio's statistics are running totals, so the totals of the last update are kept in
|
||||
* `storage`, and only the difference is sent.
|
||||
*/
|
||||
export function handleProductionTick(event: OnTickEvent) {
|
||||
const offset = event.tick % PRODUCTION_INTERVAL_TICKS
|
||||
if (offset % PRODUCTION_OFFSET_TICKS != 0) return
|
||||
|
||||
const force = game.forces["player"]
|
||||
if (force == undefined) return
|
||||
|
||||
// e.g. surfaces 1, 3, 5... on offset 0, surfaces 2, 4... on offset 30
|
||||
const slots = PRODUCTION_INTERVAL_TICKS / PRODUCTION_OFFSET_TICKS
|
||||
const slot = offset / PRODUCTION_OFFSET_TICKS
|
||||
for (const [, surface] of game.surfaces) {
|
||||
if (surface.index % slots == slot) {
|
||||
emitProductionUpdate(force, surface, event.tick)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function emitProductionUpdate(force: LuaForce, surface: LuaSurface, tick: number) {
|
||||
storage.productionTotals ??= {}
|
||||
const key = `${force.name}/${surface.index}`
|
||||
|
||||
const current: ProductionTotals = {
|
||||
tick: tick,
|
||||
items: flowTotals(force.get_item_production_statistics(surface)),
|
||||
fluids: flowTotals(force.get_fluid_production_statistics(surface)),
|
||||
}
|
||||
const previous = storage.productionTotals[key] as ProductionTotals | undefined
|
||||
storage.productionTotals[key] = current
|
||||
|
||||
// the first time, there's nothing to compare with
|
||||
if (previous == undefined) return
|
||||
|
||||
const items = difference(previous.items, current.items)
|
||||
const fluids = difference(previous.fluids, current.fluids)
|
||||
if (isEmpty(items) && isEmpty(fluids)) return
|
||||
|
||||
const update: KafkatorioPacketData.ProductionUpdate = {
|
||||
type: Type.ProductionUpdate,
|
||||
forceName: force.name,
|
||||
surfaceIndex: surface.index as unknown as SurfaceIndex,
|
||||
tick: tick as Tick,
|
||||
periodTicks: (tick - previous.tick) as UInt,
|
||||
items: items,
|
||||
fluids: fluids,
|
||||
}
|
||||
PacketEmitter.emitInstantPacket(update)
|
||||
}
|
||||
|
||||
|
||||
function flowTotals(statistics: LuaFlowStatistics): FlowTotals {
|
||||
const produced: Record<string, number> = {}
|
||||
const consumed: Record<string, number> = {}
|
||||
for (const [name, count] of pairs(statistics.input_counts)) {
|
||||
produced[name] = count as number
|
||||
}
|
||||
for (const [name, count] of pairs(statistics.output_counts)) {
|
||||
consumed[name] = count as number
|
||||
}
|
||||
return {produced: produced, consumed: consumed}
|
||||
}
|
||||
|
||||
|
||||
function difference(previous: FlowTotals, current: FlowTotals): ProductionCounts {
|
||||
return {
|
||||
produced: differenceOf(previous.produced, current.produced),
|
||||
consumed: differenceOf(previous.consumed, current.consumed),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function differenceOf(
|
||||
previous: Record<string, number>,
|
||||
current: Record<string, number>,
|
||||
): Record<string, number> {
|
||||
const result: Record<string, number> = {}
|
||||
for (const [name, count] of pairs(current)) {
|
||||
const change = count - (previous[name] ?? 0)
|
||||
// totals can go down if statistics are reset, so ignore negative changes
|
||||
if (change > 0) {
|
||||
result[name] = change
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
function isEmpty(counts: ProductionCounts): boolean {
|
||||
return isEmptyTable(counts.produced) && isEmptyTable(counts.consumed)
|
||||
}
|
||||
|
||||
|
||||
function isEmptyTable(table: Record<string, number> | null | undefined): boolean {
|
||||
if (table == undefined) return true
|
||||
for (const [,] of pairs(table)) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ import {isEventType} from "./eventTypeCheck";
|
|||
import {Converters} from "./converters";
|
||||
import {handleSurfaceUpdate} from "./surfaceUpdates";
|
||||
import {handleResearchTick} from "./researchUpdates";
|
||||
import {handleProductionTick} from "./productionUpdates";
|
||||
import EventCacheService from "../emitting/EventDataCache";
|
||||
import MapChunkUpdate from "./handlers/MapChunkUpdateHandler";
|
||||
import EntityUpdates from "./handlers/EntityUpdateHandler";
|
||||
|
|
@ -20,6 +21,7 @@ script.on_event(defines.events.on_tick, (event: OnTickEvent) => {
|
|||
}
|
||||
|
||||
handleResearchTick(event)
|
||||
handleProductionTick(event)
|
||||
|
||||
const kPacket: KafkatorioPacketData | null = KafkatorioPacketQueue.dequeue()
|
||||
if (kPacket != null) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
declare const storage: {
|
||||
MOD_VERSION: string
|
||||
FACTORIO_VERSION: string
|
||||
/** The production statistics totals that were last sent, by `force/surface` */
|
||||
productionTotals?: Record<string, unknown>
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue