Private
Public Access
1
0
Files
lua-prometheus-exporter/research-stats.lua
2026-01-04 01:37:25 +01:00

90 lines
2.7 KiB
Lua

local labBaseSpeed = 1
local biolabBaseSpeed = 2
function ScanLabs()
storage.labs = {}
for _, surface in pairs(game.surfaces) do
local labs = surface.find_entities_filtered({type="lab"})
for _, lab in pairs(labs) do
storage.labs[lab.unit_number] = lab
end
end
end
function UpdateLabs(event)
local lab = event.entity
if lab and lab.valid then
storage.labs[lab.unit_number] = lab
end
end
function RemoveLab(event)
local lab = event.entity
if lab and lab.valid then
storage.labs[lab.unit_number] = nil
end
end
function GetEstimatedResearchTime()
-- Base time in seconds for research
local playerForce = game.forces["player"]
local currentResearch = playerForce.current_research
if not currentResearch then
return
end
local researchName = currentResearch.name.."-"..currentResearch.level
local researchTotalCost = currentResearch.research_unit_count * (currentResearch.research_unit_energy/60)
local totalSpeed = storage.totalResearchSpeed
local remainingPercentage = 1-playerForce.research_progress
local remainingUnits = remainingPercentage*researchTotalCost
local estimatedSeconds = remainingUnits/totalSpeed
local returnSpeed = "---research-speed---\n"..totalSpeed.."\n"
local returnTime = "---research-time---\n"..estimatedSeconds.."\n"
local returnNameID = "---research-info---\n"..researchName.."\n"
local returnProgress = "---research-progress---\n"..playerForce.research_progress.."\n"
log("Reseach remaining "..returnTime)
return returnSpeed..returnTime..returnNameID..returnProgress
end
function UpdateLabInfos()
local totalLabs = 0
local totalSpeed = 0
totalLabs = #storage.labs
for _, lab in pairs(storage.labs) do
if lab.valid then
if lab.status == defines.entity_status.working then
local labBase
if lab.name == "biolab" then
labBase = biolabBaseSpeed
else
labBase = labBaseSpeed
end
local labSpeed = (labBase + (labBase * game.forces["player"].laboratory_speed_modifier)) * (lab.effects.speed or 1)
totalSpeed = totalSpeed + (labSpeed* (1+(lab.effects.productivity or 0)))
end
end
end
storage.totalLabCount = totalLabs
storage.totalResearchSpeed = totalSpeed
end
function SendResearchStats()
if options.enableResearch == true then
UpdateLabInfos()
local researchTimeInfo = GetEstimatedResearchTime()
if researchTimeInfo then
helpers.send_udp(udpAddress, researchTimeInfo, serverIndex)
end
end
end