Add a research page, and keep the topics that views are rebuilt from
The mod sends the research state of a force (researched technologies,
levels, current research, progress and queue) whenever research changes,
and every minute for the progress. events-server-web keeps the latest state
and serves it at /kafkatorio/data/servers/{id}/research.
The web map has a research page (/servers/{id}/research, linked from the
header), with:
- progress, the current research and the queue
- the science packs needed for everything that's left
- every technology, filterable by status, science pack and name, with its
cost (including levelled technologies' count formulas)
- details: the total cost to reach a technology, its prerequisites, what it
leads to and what it unlocks
The technology tree comes from export_building_icons.py, which now also
exports technologies.json, items.json and technology/science pack icons.
Kafka deleted data older than 7 days, but the building, resource and
research views are rebuilt by reading their topics from the beginning, so
buildings disappeared after a restart. These topics are now kept forever.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4d02928081
commit
e31971c58e
23 changed files with 1517 additions and 11 deletions
|
|
@ -21,6 +21,7 @@ export type KafkatorioPacketData =
|
|||
| KafkatorioPacketData.MapChunkTileUpdate
|
||||
| KafkatorioPacketData.PlayerUpdate
|
||||
| KafkatorioPacketData.PrototypesUpdate
|
||||
| KafkatorioPacketData.ResearchUpdate
|
||||
| KafkatorioPacketData.SurfaceUpdate;
|
||||
|
||||
export namespace KafkatorioPacketData {
|
||||
|
|
@ -29,6 +30,7 @@ export namespace KafkatorioPacketData {
|
|||
ConsoleChatUpdate = "kafkatorio.packet.instant.ConsoleChatUpdate",
|
||||
ConsoleCommandUpdate = "kafkatorio.packet.instant.ConsoleCommandUpdate",
|
||||
PrototypesUpdate = "kafkatorio.packet.instant.PrototypesUpdate",
|
||||
ResearchUpdate = "kafkatorio.packet.instant.ResearchUpdate",
|
||||
SurfaceUpdate = "kafkatorio.packet.instant.SurfaceUpdate",
|
||||
EntityUpdate = "kafkatorio.packet.keyed.EntityUpdate",
|
||||
MapChunkEntityUpdate = "kafkatorio.packet.keyed.MapChunkEntityUpdate",
|
||||
|
|
@ -64,6 +66,17 @@ export namespace KafkatorioPacketData {
|
|||
prototypes: FactorioPrototype[];
|
||||
}
|
||||
|
||||
export interface ResearchUpdate {
|
||||
type: KafkatorioPacketData.Type.ResearchUpdate;
|
||||
forceName: string;
|
||||
researched: string[];
|
||||
levels?: { [key: string]: number };
|
||||
current?: string | null;
|
||||
progress?: Double | null;
|
||||
queue?: string[];
|
||||
technologyPriceMultiplier?: Double;
|
||||
}
|
||||
|
||||
export interface SurfaceUpdate {
|
||||
type: KafkatorioPacketData.Type.SurfaceUpdate;
|
||||
index: SurfaceIndex;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import {isEventType} from "./eventTypeCheck";
|
||||
import {Converters} from "./converters";
|
||||
import {handleSurfaceUpdate} from "./surfaceUpdates";
|
||||
import {handleResearchTick} from "./researchUpdates";
|
||||
import EventCacheService from "../emitting/EventDataCache";
|
||||
import MapChunkUpdate from "./handlers/MapChunkUpdateHandler";
|
||||
import EntityUpdates from "./handlers/EntityUpdateHandler";
|
||||
|
|
@ -18,6 +19,8 @@ script.on_event(defines.events.on_tick, (event: OnTickEvent) => {
|
|||
}
|
||||
}
|
||||
|
||||
handleResearchTick(event)
|
||||
|
||||
const kPacket: KafkatorioPacketData | null = KafkatorioPacketQueue.dequeue()
|
||||
if (kPacket != null) {
|
||||
PacketEmitter.emitInstantPacket(kPacket)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,99 @@
|
|||
import PacketEmitter from "../emitting/PacketEmitter";
|
||||
import {KafkatorioPacketData} from "../../generated/kafkatorio-schema";
|
||||
import {LuaForce, OnTickEvent} from "factorio:runtime"
|
||||
import Type = KafkatorioPacketData.Type;
|
||||
|
||||
|
||||
/** Research progress changes constantly, so send it at most this often */
|
||||
const PROGRESS_INTERVAL_TICKS = 60 * 60
|
||||
|
||||
/** Batch research events (e.g. queueing several technologies at once) */
|
||||
const EVENT_DELAY_TICKS = 60
|
||||
|
||||
|
||||
/** Ticks at which each force's research should be sent, by force name */
|
||||
const pendingForces: Record<string, number> = {}
|
||||
|
||||
|
||||
function scheduleResearchUpdate(force: LuaForce) {
|
||||
const due = game.tick + EVENT_DELAY_TICKS
|
||||
const pending = pendingForces[force.name]
|
||||
if (pending == undefined || pending > due) {
|
||||
pendingForces[force.name] = due
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Send the research state of the 'player' force, and of any forces with research events */
|
||||
export function handleResearchTick(event: OnTickEvent) {
|
||||
if (event.tick % PROGRESS_INTERVAL_TICKS == 0) {
|
||||
const player = game.forces["player"]
|
||||
if (player != undefined) {
|
||||
pendingForces[player.name] = event.tick
|
||||
}
|
||||
}
|
||||
|
||||
for (const [forceName, due] of pairs(pendingForces)) {
|
||||
if (due <= event.tick) {
|
||||
delete pendingForces[forceName]
|
||||
const force = game.forces[forceName]
|
||||
if (force != undefined) {
|
||||
emitResearchUpdate(force)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function emitResearchUpdate(force: LuaForce) {
|
||||
const researched: string[] = []
|
||||
const levels: Record<string, number> = {}
|
||||
|
||||
for (const [name, technology] of pairs(force.technologies)) {
|
||||
if (technology.researched) {
|
||||
researched[researched.length] = name
|
||||
}
|
||||
if (technology.prototype.max_level > 1) {
|
||||
levels[name] = technology.level
|
||||
}
|
||||
}
|
||||
|
||||
const queue: string[] = []
|
||||
for (const technology of force.research_queue ?? []) {
|
||||
queue[queue.length] = technology.name
|
||||
}
|
||||
|
||||
const current = force.current_research
|
||||
|
||||
const update: KafkatorioPacketData.ResearchUpdate = {
|
||||
type: Type.ResearchUpdate,
|
||||
forceName: force.name,
|
||||
researched: researched,
|
||||
levels: levels,
|
||||
current: current?.name,
|
||||
progress: current != undefined ? force.research_progress : undefined,
|
||||
queue: queue,
|
||||
technologyPriceMultiplier: game.difficulty_settings.technology_price_multiplier,
|
||||
}
|
||||
|
||||
PacketEmitter.emitInstantPacket(update)
|
||||
}
|
||||
|
||||
|
||||
script.on_event(
|
||||
[
|
||||
defines.events.on_research_started,
|
||||
defines.events.on_research_finished,
|
||||
defines.events.on_research_cancelled,
|
||||
defines.events.on_research_reversed,
|
||||
defines.events.on_research_queued,
|
||||
defines.events.on_research_moved,
|
||||
],
|
||||
(event) => {
|
||||
const e = event as unknown as { research?: { force: LuaForce }, force?: LuaForce }
|
||||
const force = e.force ?? e.research?.force
|
||||
if (force != undefined) {
|
||||
scheduleResearchUpdate(force)
|
||||
}
|
||||
},
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue