Private
Public Access
1
0

Initial Commit

This commit is contained in:
Jan Grießhaber
2025-12-28 18:48:24 +01:00
commit 149b085cb7
13 changed files with 296 additions and 0 deletions

36
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,36 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "factoriomod",
"request": "launch",
"name": "Factorio Mod Debug",
"factorioArgs": [
"--enable-lua-udp=55555"
]
},
{
"type": "factoriomod",
"request": "launch",
"name": "Factorio Mod Debug (Settings & Data)",
"hookSettings": true,
"hookData": true,
"factorioArgs": [
"--enable-lua-udp=55555"
]
},
{
"type": "factoriomod",
"request": "launch",
"name": "Factorio Mod Debug (Profile)",
"hookMode": "profile",
"factorioArgs": [
"--enable-lua-udp=55555"
]
}
]
}

15
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,15 @@
{
"Lua.workspace.userThirdParty": [
"c:\\Users\\jangr\\AppData\\Roaming\\Code\\User\\workspaceStorage\\8bef1fd407f1cda3acb8320784a68ee6\\justarandomgeek.factoriomod-debug\\sumneko-3rd"
],
"Lua.workspace.checkThirdParty": "ApplyInMemory",
"factorio.versions": [
{
"name": "Factorio 2.0.72 local",
"factorioPath": "d:\\SteamLibrary\\steamapps\\common\\Factorio\\bin\\x64\\factorio.exe"
}
],
"Lua.diagnostics.globals": [
"create_stats_string"
]
}

48
control.lua Normal file
View File

@@ -0,0 +1,48 @@
require("production-stats")
-- control.lua for factorio-prometheus-exporter
-- Adds a handler for player movement (on_player_changed_position)
---@param event EventData.on_tick
local function SendSurfaceStats(event)
if event.tick % 300 ~= 0 then return end
helpers.send_udp(52555, game.tick,1)
for _,surface in pairs(game.surfaces) do
local surface_name = surface.name
local productionStat = CreateItemStatisticsString(game.forces["player"].get_item_production_statistics(surface_name), surface)
local fluidStat = CreateFluidStatisticsString(game.forces["player"].get_fluid_production_statistics(surface_name), surface)
local deathStat = CreateDeathStatisticsString(game.forces["player"].get_kill_count_statistics(surface_name), surface)
helpers.send_udp(52555, productionStat..fluidStat..deathStat,1)
end
end
-- Register the handler for the player movement event
--script.on_event(defines.events.on_player_changed_position, on_player_moved)
script.on_event(defines.events.on_player_died, function(event)
local player = game.get_player(event.player_index)
if not player then return end
local index = event.player_index
helpers.send_udp(52555, ("player-death %s %d"):format(player.name, player.index),index)
end)
script.on_event(defines.events.on_player_joined_game, function(event)
local player = game.get_player(event.player_index)
if not player then return end
local index = event.player_index
helpers.send_udp(52555, ("player-join %s %d"):format(player.name, player.index),index)
end)
function GetAllPlayers()
for _,player in pairs(game.players) do
end
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)

0
data.lua Normal file
View File

22
game-stats.lua Normal file
View File

@@ -0,0 +1,22 @@
---Sends all mod infos for the current game, should only be called on the first load of the map since mods don't change during runtime
function GetMods()
local mods = script.active_mods
local modstring = "---mod-info---\n"
for k,v in pairs(mods) do
modstring = modstring .. ("mod-info:%s:%s\n"):format(k,v)
end
helpers.send_udp(52555, modstring,1)
end
---Concats all player online times into a single string
---Takes all players that ever visited the server into account
---@return string
function GetPlayerTime()
local timeParts = {}
timeParts[#timeParts+1] = "---player-times---\n"
for _,player in pairs(game.players) do
timeParts[#timeParts+1] = ("player-time:%s:%d:%d"):format(player.name, player.index, player.online_time)
end
return table.concat(timeParts, "\n")
end

10
implemetation-plans.md Normal file
View File

@@ -0,0 +1,10 @@
Production Statistics Input and Output
Player Kills|Deaths|Time
Power Grids| Power Stats
Rockets launched
Research Speed| Level| current queue, estimated time to completion

19
info.json Normal file
View File

@@ -0,0 +1,19 @@
{
"name": "factorio-prometheus-exporter",
"version": "0.1.0",
"title": "Prometheus Exporter",
"author": "Your Name",
"contact": "your.email@example.com",
"homepage": "",
"description": "Exports Factorio metrics for collection by Prometheus.",
"factorio_version": "2.0",
"dependencies": [
"base >= 2.0.0"
],
"license": "MIT",
"tags": [
"monitoring",
"metrics",
"prometheus"
]
}

0
locale/de-DE/locale.cfg Normal file
View File

0
locale/en/locale.cfg Normal file
View File

18
pollution-stats.lua Normal file
View File

@@ -0,0 +1,18 @@
function GetPollutionStats()
for _,surface in pairs(game.surfaces) do
local surface_name = surface.name
local pollutionParts = {}
local pollution_stats = game.surfaces[surface_name].pollution_statistics
local pollution_input = pollution_stats.input_counts
local pollution_output = pollution_stats.output_counts
pollutionParts[#pollutionParts+1] = ("---pollution-input---%s\n"):format(surface_name)
for name, stat in pairs(pollution_input) do
pollutionParts[#pollutionParts+1] = ("%s:%d"):format(name, stat)
end
pollutionParts[#pollutionParts+1] = ("---pollution-output---%s\n"):format(surface_name)
for name, stat in pairs(pollution_output) do
pollutionParts[#pollutionParts+1] = ("%s:%d"):format(name, stat)
end
helpers.send_udp(52555, table.concat(pollutionParts,"\n"),1)
end
end

56
production-stats.lua Normal file
View File

@@ -0,0 +1,56 @@
---@param productionStatsTable LuaFlowStatistics
---@param surface LuaSurface
---@return string
function CreateItemStatisticsString(productionStatsTable --[[LuaFlowStatistics]], surface --[[LuaSurface]])
local parts = {}
parts[#parts+1] = surface.name
parts[#parts+1] = "---input---"
for itemName, itemCount in pairs(productionStatsTable.input_counts) do
parts[#parts+1] = itemName .. ":" .. itemCount
end
parts[#parts+1] = "---output---"
for itemName, itemCount in pairs(productionStatsTable.output_counts) do
parts[#parts+1] = itemName .. ":" .. itemCount
end
parts[#parts+1] = surface.name
return table.concat(parts, "\n")
end
---comment
---@param fluidStatsTable LuaFlowStatistics
---@param surface LuaSurface
---@return string
function CreateFluidStatisticsString(fluidStatsTable --[[LuaFlowStatistics]], surface --[[LuaSurface]])
local parts = {}
parts[#parts+1] = surface.name
parts[#parts+1] = "---input---"
for fluidName, fluidCount in pairs(fluidStatsTable.input_counts) do
parts[#parts+1] = fluidName .. ":" .. fluidCount
end
parts[#parts+1] = "---output---"
for fluidName, fluidCount in pairs(fluidStatsTable.output_counts) do
parts[#parts+1] = fluidName .. ":" .. fluidCount
end
parts[#parts+1] = surface.name
return table.concat(parts, "\n")
end
---comment
---@param deathStatsTable LuaFlowStatistics
---@param surface LuaSurface
---@return string
function CreateDeathStatisticsString(deathStatsTable --[[LuaFlowStatistics]], surface --[[LuaSurface]])
local parts = {}
parts[#parts+1] = surface.name.."{"
parts[#parts+1] = "---input---"
for name, count in pairs(deathStatsTable.input_counts) do
parts[#parts+1] = name .. ":" .. count
end
parts[#parts+1] = "---output---"
for name, count in pairs(deathStatsTable.output_counts) do
parts[#parts+1] = name .. ":" .. count
end
parts[#parts+1] = surface.name
return table.concat(parts, "\n")
end

20
research-stats.lua Normal file
View File

@@ -0,0 +1,20 @@
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
end
end
---comment
---@param event EventData.on_research_started
function onResearchStarted(event)
end
---comment
---@param event EventData.on_research_queued
function onResearchQueued(event)
end

52
settings.lua Normal file
View File

@@ -0,0 +1,52 @@
data:extend({
{
type = "bool-setting",
name = "factorio-prometheus-exporter-enable",
setting_type = "startup",
default_value = true,
order = "a"
},
{
type = "string-setting",
name = "factorio-prometheus-exporter-udp-address",
setting_type = "startup",
allow_blank = true,
default_value = "",
order = "b"
},
{
type = "bool-setting",
name = "factorio-prometheus-exporter-export_production_stats",
setting_type = "startup",
default_value = true,
order = "c"
},
{
type = "bool-setting",
name = "factorio-prometheus-exporter-export_player_deaths",
setting_type = "startup",
default_value = true,
order = "d"
},
{
type = "bool-setting",
name = "factorio-prometheus-exporter-export_fluid_stats",
setting_type = "startup",
default_value = true,
order = "e"
},
{
type = "bool-setting",
name = "factorio-prometheus-exporter-export_power_stats",
setting_type = "startup",
default_value = false,
order = "f"
},
{
type = "bool-setting",
name = "factorio-prometheus-exporter-export_pollution_stats",
setting_type = "startup",
default_value = true,
order = "g"
}
})