diff --git a/config/deconlog.lua b/config/deconlog.lua new file mode 100644 index 00000000..a19d15d8 --- /dev/null +++ b/config/deconlog.lua @@ -0,0 +1,8 @@ +-- @config Deconlog + +return { + decon_area = true, ---@setting decon_area whether to log when an area is being deconstructed + built_entity = true, ---@setting built_entity whether to log when an entity is built + mined_entity = true, ---@setting mined_entity whether to log when an entity is mined + fired_nuke = true, ---@setting fired_nuke whether to log when a nuke is fired +} \ No newline at end of file diff --git a/modules/addons/deconlog.lua b/modules/addons/deconlog.lua index b256ed15..41f72a71 100644 --- a/modules/addons/deconlog.lua +++ b/modules/addons/deconlog.lua @@ -1,5 +1,10 @@ +--- Log certain actions into a file when events are triggered +-- @addon Deconlog + local Event = require 'utils.event' --- @dep utils.event local Roles = require 'expcore.roles' --- @dep expcore.roles +local format_time = _C.format_time +local config = require 'config.deconlog' --- @dep config.deconlog local filepath = "log/decon.log" @@ -7,7 +12,7 @@ local function add_log(data) game.write_file(filepath, data .. "\n", true, 0) -- write data end local function get_secs () - return tostring(math.floor(game.tick / 60)) .. "," + return format_time(game.tick, { hours = true, minutes = true, seconds = true, string = true }) end local function pos_tostring (pos) return tostring(pos.x) .. "," .. tostring(pos.y) @@ -17,35 +22,43 @@ Event.on_init(function() game.write_file(filepath, "\n", false, 0) -- write data end) -Event.add(defines.events.on_player_deconstructed_area, function (e) - local player = game.get_player(e.player_index) - if Roles.player_has_flag(player, "deconlog-bypass") then return end - add_log(get_secs() .. player.name .. ",decon_area," .. pos_tostring(e.area.left_top) .. "," .. pos_tostring(e.area.right_bottom)) -end) +if cofnig.decon_area then + Event.add(defines.events.on_player_deconstructed_area, function (e) + local player = game.get_player(e.player_index) + if Roles.player_has_flag(player, "deconlog-bypass") then return end + add_log(get_secs() .. player.name .. ",decon_area," .. pos_tostring(e.area.left_top) .. "," .. pos_tostring(e.area.right_bottom)) + end) +end -Event.add(defines.events.on_built_entity, function (e) - if not e.player_index then return end - local player = game.get_player(e.player_index) - if Roles.player_has_flag(player, "deconlog-bypass") then return end - local ent = e.created_entity - add_log(get_secs() .. player.name .. ",built_entity," .. ent.name .. "," .. pos_tostring(ent.position) .. "," .. tostring(ent.direction) .. "," .. tostring(ent.orientation)) -end) +if config.built_entity then + Event.add(defines.events.on_built_entity, function (e) + if not e.player_index then return end + local player = game.get_player(e.player_index) + if Roles.player_has_flag(player, "deconlog-bypass") then return end + local ent = e.created_entity + add_log(get_secs() .. player.name .. ",built_entity," .. ent.name .. "," .. pos_tostring(ent.position) .. "," .. tostring(ent.direction) .. "," .. tostring(ent.orientation)) + end) +end +if config.mined_entity then + Event.add(defines.events.on_player_mined_entity, function (e) + local player = game.get_player(e.player_index) + if Roles.player_has_flag(player, "deconlog-bypass") then return end + local ent = e.entity + add_log(get_secs() .. player.name .. ",mined_entity," .. ent.name .. "," .. pos_tostring(ent.position) .. "," .. tostring(ent.direction) .. "," .. tostring(ent.orientation)) + end) +end -Event.add(defines.events.on_player_mined_entity, function (e) - local player = game.get_player(e.player_index) - if Roles.player_has_flag(player, "deconlog-bypass") then return end - local ent = e.entity - add_log(get_secs() .. player.name .. ",mined_entity," .. ent.name .. "," .. pos_tostring(ent.position) .. "," .. tostring(ent.direction) .. "," .. tostring(ent.orientation)) -end) - -Event.add(defines.events.on_player_ammo_inventory_changed, function (e) - local player = game.get_player(e.player_index) - if Roles.player_has_flag(player, "deconlog-bypass") then return end - local ammo_inv = player.get_inventory(defines.inventory.character_ammo) - local item = ammo_inv[player.character.selected_gun_index] - if not item or not item.valid or not item.valid_for_read then return end - if item.name == "atomic-bomb" then - add_log(get_secs() .. player.name .. ",shot-bomb," .. pos_tostring(player.position) .. "," .. pos_tostring(player.shooting_state.position)) - end -end) \ No newline at end of file +if config.fired_nuke then + Event.add(defines.events.on_player_ammo_inventory_changed, function (e) + -- this works only if the player took more than one nuke, which they usually do + local player = game.get_player(e.player_index) + if Roles.player_has_flag(player, "deconlog-bypass") then return end + local ammo_inv = player.get_inventory(defines.inventory.character_ammo) + local item = ammo_inv[player.character.selected_gun_index] + if not item or not item.valid or not item.valid_for_read then return end + if item.name == "atomic-bomb" then + add_log(get_secs() .. player.name .. ",shot-bomb," .. pos_tostring(player.position) .. "," .. pos_tostring(player.shooting_state.position)) + end + end) +end \ No newline at end of file