diff --git a/.vscode/settings.json b/.vscode/settings.json index 1bd2da8..c359285 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,11 @@ } ], "Lua.diagnostics.globals": [ - "create_stats_string" + "serverIndex", + "udpAddress", + "SendProductionStats", + "SendPollutionStats", + "SendKillStats", + "SendFluidProductionStats" ] } \ No newline at end of file diff --git a/control.lua b/control.lua index 9827f18..c7ca5ad 100644 --- a/control.lua +++ b/control.lua @@ -1,13 +1,27 @@ +require("game-stats") require("production-stats") require("pollution-stats") +require("research-stats") +require("power-stats") +require("storage") tickInterval = tonumber(settings.global["factorio-prometheus-exporter-tick-interval"].value) or 300 udpAddress = tonumber(settings.startup["factorio-prometheus-exporter-udp-address"].value) or 52555 - serverIndex = 1 -if game.is_multiplayer() then - serverIndex = 0 -end + +script.on_init(function () + storage.researchedTechnologies = {} + storage.playerDeathCount = {} +end) + +script.on_load(function () + log("factorio-prometheus-exporter: on_load") + log("tickInterval: "..tickInterval) + log("udpAddress: "..udpAddress) + +end) + + -- Register the handler for the player movement event --script.on_event(defines.events.on_player_changed_position, on_player_moved) @@ -30,13 +44,40 @@ script.on_event(defines.events.on_runtime_mod_setting_changed, function(event) end end) +function SendStats(event) + if(event.tick % (tickInterval) ~= 0) then + return + end + SendProductionStats() + SendFluidProductionStats() + SendPollutionStats() + SendKillStats() +end + + +function SendGameStats(event) + if(event.tick % (tickInterval*2) ~= 0) then + return + end + local returnParts = {} + returnParts[#returnParts+1] = GetMods() + returnParts[#returnParts+1] = GetMapSeed() + returnParts[#returnParts+1] = GetRocketsLaunched() + returnParts[#returnParts+1] = GetPlayerTime() + returnParts[#returnParts+1] = GetPlayerDeaths() + helpers.send_udp(udpAddress, table.concat(returnParts, "\n"), serverIndex) +end + +function SendAll(event) + SendStats(event) + SendGameStats(event) +end --script.on_event(defines.events.on_player_joined_game, on_player_joined) --script.on_nth_tick(300, SendSurfaceStats) --script.on_event(defines.events.on_tick, SendSurfaceStats) -script.on_nth_tick(tickInterval, SendProductionStats) -script.on_nth_tick(tickInterval, SendFluidProductionStats) -script.on_nth_tick(tickInterval, GetPollutionStats) +script.on_event(defines.events.on_tick, SendAll) +--script.on_nth_tick(tickInterval*2, SendGameStats) diff --git a/game-stats.lua b/game-stats.lua index edd8030..ea740c1 100644 --- a/game-stats.lua +++ b/game-stats.lua @@ -9,9 +9,13 @@ function GetMods() end function GetMapSeed() - helpers.send_udp(udpAddress, ("---map-seed---\n%d"):format(game.surfaces["nauvis"].map_gen_settings.seed),serverIndex) + return("---map-seed---\n%d"):format(game.surfaces["nauvis"].map_gen_settings.seed) end +function GetRocketsLaunched() + return("---rocket-launches---\n%d"):format(game.forces["player"].rockets_launched) + end + ---Concats all player online times into a single string ---Takes all players that ever visited the server into account ---@return string diff --git a/pollution-stats.lua b/pollution-stats.lua index be9ddf0..90c5849 100644 --- a/pollution-stats.lua +++ b/pollution-stats.lua @@ -1,4 +1,4 @@ -function GetPollutionStats() +function SendPollutionStats() if not (settings.global["factorio-prometheus-exporter-export_pollution_stats"].value) then return end @@ -15,12 +15,41 @@ function GetPollutionStats() end for name, stat in pairs(pollution_input) do - pollutionParts[#pollutionParts+1] = ("%s:in:%s:%d"):format(name,surface_name, stat) + pollutionParts[#pollutionParts+1] = ("%s:in:%s:%d"):format(surface_name,name, stat) end for name, stat in pairs(pollution_output) do - pollutionParts[#pollutionParts+1] = ("%s:out:%s:%d"):format(name,surface_name, stat) + pollutionParts[#pollutionParts+1] = ("%s:out:%s:%d"):format(surface_name,name, stat) end - helpers.send_udp(udpAddress, table.concat(pollutionParts,"\n"),serverIndex) + end + helpers.send_udp(udpAddress, table.concat(pollutionParts,"\n"),serverIndex) +end + +function SendKillStats() + if not (settings.global["factorio-prometheus-exporter-export_kill_stats"].value) then + return + end + local killParts = {} + killParts[#killParts+1] = "---kill-stats---\n" + for _,surface in pairs(game.surfaces) do + local surface_name = surface.name + + local kill_input = game.forces["player"].get_kill_count_statistics(surface_name).input_counts + local kill_output = game.forces["player"].get_kill_count_statistics(surface_name).output_counts + + if surface.platform ~= nil then + surface_name = surface.platform.name + end + + for name, stat in pairs(kill_input) do + killParts[#killParts+1] = ("%s:in:%s:%d"):format(surface_name,name, stat) + end + + for name, stat in pairs(kill_output) do + killParts[#killParts+1] = ("%s:out:%s:%d"):format(surface_name,name, stat) + end + + end + helpers.send_udp(udpAddress, table.concat(killParts,"\n"),serverIndex) end \ No newline at end of file diff --git a/power-stats.lua b/power-stats.lua new file mode 100644 index 0000000..4e8c4ac --- /dev/null +++ b/power-stats.lua @@ -0,0 +1,6 @@ +function GetGlobalGrids() + for _,surface in pairs(game.surfaces) do + local grids = surface.has_global_electric_network + --log("Surface: "..surface.name.." has global electric network: "..tostring(grids)) + end +end \ No newline at end of file diff --git a/research-stats.lua b/research-stats.lua index 62b3edb..da27aad 100644 --- a/research-stats.lua +++ b/research-stats.lua @@ -1,11 +1,11 @@ -local researches = {} + ---comment ---@param event EventData.on_research_finished function onResearchFinished(event) for id, tech in pairs(game.forces["player"].technologies) do - researches[id] = tech + storage.researched[id] = tech end end diff --git a/settings.lua b/settings.lua index 71f48c3..9e04d28 100644 --- a/settings.lua +++ b/settings.lua @@ -56,5 +56,12 @@ data:extend({ setting_type = "runtime-global", default_value = true, order = "h" + }, + { + type = "bool-setting", + name = "factorio-prometheus-exporter-export_kill_stats", + setting_type = "runtime-global", + default_value = true, + order = "h" } }) \ No newline at end of file diff --git a/storage.lua b/storage.lua index bb20628..7d091e6 100644 --- a/storage.lua +++ b/storage.lua @@ -1 +1,2 @@ -storage.playerDeathCount = {} \ No newline at end of file +storage.playerDeathCount = {} +storage.researchedTech = {} \ No newline at end of file