Bump version to 0.1.6, add player kill tracking, and enhance power/lab stats handling
This commit is contained in:
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"Lua.workspace.userThirdParty": [
|
||||
"c:\\Users\\jangr\\AppData\\Roaming\\Code\\User\\workspaceStorage\\8bef1fd407f1cda3acb8320784a68ee6\\justarandomgeek.factoriomod-debug\\sumneko-3rd"
|
||||
"c:\\Users\\jangr\\AppData\\Roaming\\Code\\User\\workspaceStorage\\071322d83ef859d27b899371aa0efcc4\\justarandomgeek.factoriomod-debug\\sumneko-3rd"
|
||||
],
|
||||
"Lua.workspace.checkThirdParty": "ApplyInMemory",
|
||||
"factorio.versions": [
|
||||
|
||||
77
control.lua
77
control.lua
@@ -10,7 +10,6 @@ udpAddress = 52555
|
||||
isInitialized = false
|
||||
sendIndex = 0
|
||||
serverIndex = 0
|
||||
LabsScanned = false
|
||||
|
||||
options = {
|
||||
enableMod = false,
|
||||
@@ -34,6 +33,11 @@ script.on_init(function ()
|
||||
storage.totalResearchSpeed = 0
|
||||
storage.totalReseachProductivity = 0
|
||||
storage.labs = {}
|
||||
storage.playerKillCount = {}
|
||||
storage.representativePoles = {}
|
||||
|
||||
storage.scannedGrids = false
|
||||
storage.scannedLabs = false
|
||||
sendIndex = 0
|
||||
|
||||
options.enableMod = settings.global["factorio-metrics-exporter-enable"].value
|
||||
@@ -73,6 +77,11 @@ script.on_configuration_changed(function()
|
||||
storage.totalLabCount = storage.totalLabCount or 0
|
||||
storage.totalReseachProductivity = storage.totalReseachProductivity or 0
|
||||
storage.totalResearchSpeed = storage.totalResearchSpeed or 0
|
||||
storage.playerKillCount = storage.playerKillCount or {}
|
||||
storage.representativePoles = storage.representativePoles or {}
|
||||
storage.scannedGrids = storage.scannedGrids or false
|
||||
storage.scannedLabs = storage.scannedLabs or false
|
||||
ScanNetworks()
|
||||
end
|
||||
)
|
||||
|
||||
@@ -122,10 +131,22 @@ end)
|
||||
|
||||
|
||||
script.on_event(defines.events.on_player_died, function(event)
|
||||
if event.cause.is_player() then
|
||||
local killer_index = event.cause.player.index
|
||||
local victim_index = event.player_index
|
||||
|
||||
storage.playerKillCount[killer_index] =
|
||||
storage.playerKillCount[killer_index] or {}
|
||||
|
||||
storage.playerKillCount[killer_index][victim_index] =
|
||||
(storage.playerKillCount[killer_index][victim_index] or 0) + 1
|
||||
end
|
||||
--local player = game.get_player(event.player_index)
|
||||
storage.playerDeathCount[event.player_index] = (storage.playerDeathCount[event.player_index] or 0) + 1
|
||||
end)
|
||||
|
||||
|
||||
|
||||
function SendGameStats(event)
|
||||
local returnParts = {}
|
||||
returnParts[#returnParts+1] = GetMods()
|
||||
@@ -149,22 +170,23 @@ function SendAll(event)
|
||||
end
|
||||
isInitialized = true
|
||||
|
||||
log("ServerIndex is "..serverIndex.." now")
|
||||
--log("ServerIndex is "..serverIndex.." now")
|
||||
|
||||
if PowerGridScanned == false then
|
||||
GenerateNetworks()
|
||||
if storage.scannedGrids == false then
|
||||
ScanNetworks()
|
||||
storage.scannedGrids = true
|
||||
end
|
||||
|
||||
if LabsScanned == false then
|
||||
GenerateLabInfo()
|
||||
if storage.scannedLabs == false then
|
||||
ScanLabs()
|
||||
end
|
||||
|
||||
|
||||
if options.enableMod==true then
|
||||
|
||||
if (event.tick % math.floor(tickInterval/7) ~= 0) then return end
|
||||
if (event.tick % math.floor(tickInterval/8) ~= 0) then return end
|
||||
|
||||
sendIndex = (sendIndex % 7) + 1
|
||||
sendIndex = (sendIndex % 8) + 1
|
||||
if sendIndex == 1 then SendProductionStats() end
|
||||
if sendIndex == 2 then SendPollutionStats() end
|
||||
if sendIndex == 3 then SendKillStats() end
|
||||
@@ -172,6 +194,7 @@ function SendAll(event)
|
||||
if sendIndex == 5 then SendBuildStats() end
|
||||
if sendIndex == 6 then SendResearchStats() end
|
||||
if sendIndex == 7 then SendLogisticStats() end
|
||||
if sendIndex == 8 then SendPowerStats() end
|
||||
|
||||
if(event.tick % tickInterval*2 == 0) then
|
||||
if options.enablePlayers then
|
||||
@@ -180,13 +203,41 @@ function SendAll(event)
|
||||
end
|
||||
end
|
||||
|
||||
function UpdateStorage(event)
|
||||
if not event then return end
|
||||
if event.entity.type == "lab" then
|
||||
UpdateLabs(event)
|
||||
end
|
||||
if event.entity.type == "electric-pole" then
|
||||
AddPowerPole(event)
|
||||
end
|
||||
end
|
||||
|
||||
function RemoveStorage(event)
|
||||
if not event then return end
|
||||
if event.entity.type =="lab" then
|
||||
RemoveLab(event)
|
||||
end
|
||||
if event.entity.type == "electric-pole" then
|
||||
RemovePowerPole(event)
|
||||
end
|
||||
if event.entity.name == "crash-site-spaceship" then
|
||||
if event.name == defines.events.on_player_mined_entity then
|
||||
if settings.global["factorio-metrics-exporter-enable_denkmalschutz"].value == true then
|
||||
game.ban_player(event.player_index,"You violated the rules of DENKMALSCHUTZ!!!")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
script.on_event(defines.events.on_tick, SendAll)
|
||||
|
||||
--Script hooks for power stats
|
||||
script.on_event(defines.events.on_built_entity,UpdateGrids,{{filter = "type", type = "electric-pole"}})
|
||||
script.on_event(defines.events.on_player_mined_entity, PurgeNetworks,{{filter = "type", type = "electric-pole"}})
|
||||
script.on_event(defines.events.on_robot_built_entity,UpdateGrids,{{filter = "type", type = "electric-pole"}})
|
||||
script.on_event(defines.events.on_robot_mined_entity,PurgeNetworks,{{filter = "type", type = "electric-pole"}})
|
||||
--Script hooks for power and lab stats
|
||||
script.on_event(defines.events.on_built_entity,UpdateStorage,{{filter = "type", type = "electric-pole" or "lab"}})
|
||||
script.on_event(defines.events.on_player_mined_entity, RemoveStorage,{{filter = "type", type = "electric-pole"or "lab" or "container"}})
|
||||
script.on_event(defines.events.on_robot_built_entity,UpdateStorage,{{filter = "type", type = "electric-pole" or "lab"}})
|
||||
script.on_event(defines.events.on_robot_mined_entity,RemoveStorage,{{filter = "type", type = "electric-pole" or "lab" or "container"}})
|
||||
script.on_event(defines.events.on_entity_died,RemoveStorage,{{filter = "type", type = "electric-pole" or "lab" or "container"}})
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "factorio-metrics-exporter",
|
||||
"version": "0.1.5",
|
||||
"version": "0.1.6",
|
||||
"title": "Prometheus Metrics Exporter",
|
||||
"author": "Jan Grießhaber",
|
||||
"contact": "jan@griesshaber.systems",
|
||||
|
||||
@@ -1,42 +1,63 @@
|
||||
PowerGridScanned = false
|
||||
function GetGlobalGridSurfaces()
|
||||
local globalGrids = {}
|
||||
function AddPowerPole(event)
|
||||
local e = event.entity
|
||||
if e and e.valid and e.type == "electric-pole" then
|
||||
storage.representativePoles[e.unit_number] = e
|
||||
end
|
||||
end
|
||||
|
||||
function RemovePowerPole(event)
|
||||
local e = event.entity
|
||||
if e then
|
||||
storage.representativePoles[e.unit_number] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function GetNetworks()
|
||||
local networks = {}
|
||||
|
||||
for _, pole in pairs(storage.representativePoles) do
|
||||
if pole.valid then
|
||||
local net = pole.electric_network_id
|
||||
if net then
|
||||
networks[net] = pole
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return networks
|
||||
end
|
||||
|
||||
function ScanNetworks()
|
||||
storage.representative_poles = {}
|
||||
|
||||
for _, surface in pairs(game.surfaces) do
|
||||
if(surface.has_global_electric_network) then
|
||||
globalGrids[#globalGrids+1] = surface
|
||||
for _, pole in pairs(surface.find_entities_filtered{type = "electric-pole"}) do
|
||||
if pole.valid and pole.electric_network_id then
|
||||
storage.representativePoles[pole.unit_number] = pole
|
||||
end
|
||||
--log("Surface: "..surface.name.." has global electric network: "..tostring(grids))
|
||||
end
|
||||
end
|
||||
|
||||
function UpdateGrids(event)
|
||||
local entity = event.entity
|
||||
local gridID = entity.electric_network_id
|
||||
if gridID == nil then return end
|
||||
storage.electricGrids[gridID] = (storage.electricGrids[gridID] or 0) + 1
|
||||
log(("Network %d has counter of %d"):format(gridID,storage.electricGrids[gridID]))
|
||||
end
|
||||
|
||||
function PurgeNetworks()
|
||||
for ID, Counter in pairs(storage.electricGrids) do
|
||||
if Counter <= 0 then
|
||||
table.remove(storage.electricGrids, ID)
|
||||
log(("Removed electric network with id %d from list"):format(ID))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function GenerateNetworks()
|
||||
if PowerGridScanned==false then
|
||||
for _,surface in pairs(game.surfaces) do
|
||||
for _, powerPole in pairs(surface.find_entities_filtered({filter = "type",type = "electric-pole"})) do
|
||||
storage.electricGrids[powerPole.electric_network_id] = (storage.electricGrids[powerPole.electric_network_id] or 0) + 1
|
||||
function SendPowerStats()
|
||||
if options.enablePower then
|
||||
local powerPart = {}
|
||||
powerPart[#powerPart+1] = "---power-stats---\n"
|
||||
for _, pole in pairs(GetNetworks()) do
|
||||
if pole.valid and pole.type == "electric-pole" then
|
||||
local input = pole.electric_network_statistics.input_counts
|
||||
local output = pole.electric_network_statistics.output_counts
|
||||
local surfaceName = pole.surface.name
|
||||
for item,value in pairs(input) do
|
||||
powerPart[#powerPart+1] = ("%s:%d:in:%s:%d"):format(surfaceName, pole.electric_network_id, item, value)
|
||||
end
|
||||
|
||||
for item, value in pairs(output) do
|
||||
powerPart[#powerPart+1] = ("%s:%d:out:%s:%d"):format(surfaceName,pole.electric_network_id, item, value)
|
||||
end
|
||||
end
|
||||
PowerGridScanned = true
|
||||
end
|
||||
helpers.send_udp(udpAddress,table.concat(powerPart,"\n"),serverIndex)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -1,21 +1,33 @@
|
||||
storage.totalLabCount = 0
|
||||
local labBaseSpeed = 1
|
||||
local biolabBaseSpeed = 2
|
||||
|
||||
LabsScanned = false
|
||||
|
||||
|
||||
function GenerateLabInfo()
|
||||
function ScanLabs()
|
||||
storage.labs = {}
|
||||
for _, surface in pairs(game.surfaces) do
|
||||
local labs = surface.find_entities_filtered({type="lab"})
|
||||
for index, lab in pairs(labs) do
|
||||
if lab.valid then
|
||||
storage.labs[index] = lab
|
||||
for _, lab in pairs(labs) do
|
||||
storage.labs[lab.unit_number] = lab
|
||||
end
|
||||
end
|
||||
storage.scannedLabs = true
|
||||
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
|
||||
LabsScanned = true
|
||||
end
|
||||
|
||||
|
||||
@@ -58,7 +70,6 @@ function UpdateLabInfos()
|
||||
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)))
|
||||
--local labSpeed = labBaseSpeed * lab.effects.speed
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -84,6 +84,13 @@ data:extend({
|
||||
setting_type = "runtime-global",
|
||||
default_value = true,
|
||||
order = "i"
|
||||
},
|
||||
{
|
||||
type = "bool-setting",
|
||||
name = "factorio-metrics-exporter-enable_denkmalschutz",
|
||||
setting_type = "runtime-global",
|
||||
default_value = false,
|
||||
order = "z"
|
||||
}
|
||||
|
||||
})
|
||||
Reference in New Issue
Block a user