From 498edfd58d3ad27e3ffd265fc9a6de2b8058108f Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sun, 24 Mar 2019 18:47:43 +0000 Subject: [PATCH] Added Death logger --- .../{death_markers.lua => death_logger.lua} | 2 +- config/file_loader.lua | 1 + expcore/common.lua | 47 +++++++---- modules/addons/death-logger.lua | 84 +++++++++++++++++++ modules/addons/death-markers.lua | 40 --------- 5 files changed, 115 insertions(+), 59 deletions(-) rename config/{death_markers.lua => death_logger.lua} (90%) create mode 100644 modules/addons/death-logger.lua delete mode 100644 modules/addons/death-markers.lua diff --git a/config/death_markers.lua b/config/death_logger.lua similarity index 90% rename from config/death_markers.lua rename to config/death_logger.lua index cb195cde..f93518e0 100644 --- a/config/death_markers.lua +++ b/config/death_logger.lua @@ -8,5 +8,5 @@ return { auto_collect_bodies=false, -- enables items being returned to the spawn point in chests upon death show_map_markers=true, -- shows markers on the map where bodies are include_time_of_death=true, -- weather to include the time of death on the map marker - map_icon='' -- the icon that the map marker shows '' means no icon + map_icon=nil -- the icon that the map marker shows; nil means no icon; format as a SingleID } \ No newline at end of file diff --git a/config/file_loader.lua b/config/file_loader.lua index c87e9d85..db6b68bc 100644 --- a/config/file_loader.lua +++ b/config/file_loader.lua @@ -17,6 +17,7 @@ return { -- QoL Addons 'modules.addons.chat-popups', 'modules.addons.damage-popups', + 'modules.addons.death-markers', -- Config Files 'config.command_auth_admin', -- commands tagged with admin_only are blocked for non admins 'config.command_auth_runtime_disable', -- allows commands to be enabled and disabled during runtime diff --git a/expcore/common.lua b/expcore/common.lua index fbc9c158..74f2eb3a 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -138,6 +138,7 @@ end --- Formats tick into a clean format, denominations from highest to lowest -- long will use words rather than letters -- time will use : separates +-- string will return a string not a locale string -- when a denomination is false it will overflow into the next one -- @tparam ticks number the number of ticks that represents a time -- @tparam options table a table of options to use for the format @@ -150,7 +151,8 @@ function Public.format_time(ticks,options) minutes=true, seconds=false, long=false, - time=false + time=false, + string=false } -- Basic numbers that are used in calculations local max_days, max_hours, max_minutes, max_seconds = ticks/5184000, ticks/216000, ticks/3600, ticks/60 @@ -174,18 +176,28 @@ function Public.format_time(ticks,options) suffix = '' suffix_2 = '' end - local div = 'time-format.simple-format-tagged' + local div = options.string and ' ' or 'time-format.simple-format-tagged' if options.time then - div = 'time-format.simple-format-div' + div = options.string and ':' or 'time-format.simple-format-div' suffix = false end -- Adds formatting if suffix ~= false then - rtn_days = {suffix..'days'..suffix_2,rtn_days} - rtn_hours = {suffix..'hours'..suffix_2,rtn_hours} - rtn_minutes = {suffix..'minutes'..suffix_2,rtn_minutes} - rtn_seconds = {suffix..'seconds'..suffix_2,rtn_seconds} + if options.string then + -- format it as a string + local long = suffix == '' + rtn_days = long and rtn_days..' days' or rtn_days..'d' + rtn_hours = long and rtn_hours..' hours' or rtn_hours..'h' + rtn_minutes = long and rtn_minutes..' minutes' or rtn_minutes..'m' + rtn_seconds = long and rtn_seconds..' seconds' or rtn_seconds..'s' + else + rtn_days = {suffix..'days'..suffix_2,rtn_days} + rtn_hours = {suffix..'hours'..suffix_2,rtn_hours} + rtn_minutes = {suffix..'minutes'..suffix_2,rtn_minutes} + rtn_seconds = {suffix..'seconds'..suffix_2,rtn_seconds} + end else + -- weather string or not it has same format rtn_days = string.format('%02d',rtn_days) rtn_hours = string.format('%02d',rtn_hours) rtn_minutes = string.format('%02d',rtn_minutes) @@ -193,18 +205,17 @@ function Public.format_time(ticks,options) end -- The final return is construed local rtn - if options.days then - rtn = rtn_days - end - if options.hours then - rtn = rtn and {div,rtn,rtn_hours} or rtn_hours - end - if options.minutes then - rtn = rtn and {div,rtn,rtn_minutes} or rtn_minutes - end - if options.seconds then - rtn = rtn and {div,rtn,rtn_seconds} or rtn_seconds + local append = function(dom,value) + if dom and options.string then + rtn = rtn and rtn..div..value or value + elseif dom then + rtn = rtn and {div,rtn,value} or value + end end + append(options.day,rtn_days) + append(options.hours,rtn_hours) + append(options.minutes,rtn_minutes) + append(options.seconds,rtn_seconds) return rtn end diff --git a/modules/addons/death-logger.lua b/modules/addons/death-logger.lua new file mode 100644 index 00000000..4743b99d --- /dev/null +++ b/modules/addons/death-logger.lua @@ -0,0 +1,84 @@ +local Event = require 'utils.event' +local Game = require 'utils.game' +local Global = require 'utils.global' +local config = require 'config.death_logger' +local format_time = ext_require('expcore.common','format_time') + +local deaths = { + archive={} -- deaths moved here after body is gone + --{player_name='Cooldude2606',time_of_death='15H 15M',position={x=0,y=0},corpse=LuaEntity,tag=LuaCustomChartTag} +} +Global.register(deaths,function(tbl) + deaths = tbl +end) + +--- Creates a new death marker and saves it to the given death +local function create_map_tag(death) + local player = Game.get_player_from_any(death.player_name) + local message = player.name..' died' + if config.include_time_of_death then + local time = format_time(death.time_of_death,{hours=true,minutes=true,string=true}) + message = message..' at '..time + end + death.tag = player.force.add_chart_tag(player.surface,{ + position=death.position, + icon=config.map_icon, + text=message + }) +end + +--- Checks that all map tags are present and valid +-- adds missing ones, deletes expired ones +local function check_map_tags() + for index,death in ipairs(deaths) do + local map_tag = death.tag + local corpse = death.corpse + -- Check the corpse is valid + if corpse and corpse.valid then + -- Corpse is valid check the map tag + if not map_tag or not map_tag.valid then + -- Map tag is not valid make a new one + create_map_tag(death) + end + else + -- Corpse is not valid so remove the map tag + if map_tag and map_tag.valid then + map_tag.destroy() + end + -- Move the death to the archive + death.corpse = nil + death.tag = nil + table.insert(deaths.archive,death) + table.remove(deaths,index) + end + end +end + +-- when a player dies a new death is added to the records and a map marker is made +Event.add(defines.events.on_player_died,function(event) + local player = Game.get_player_by_index(event.player_index) + local corpse = player.surface.find_entity('character-corpse',player.position) + local death = { + player_name = player.name, + time_of_death = event.tick, + position = player.position, + corpse = corpse + } + if config.show_map_markers then + create_map_tag(death) + end + table.insert(deaths,death) +end) + +-- every 5 min all bodies are checked for valid map tags +if config.show_map_markers then + local check_period = 60*60*5 -- five minutes + Event.on_nth_tick(check_period,function(event) + check_map_tags() + end) +end + +-- this is so other modules can access the logs +return function() + return deaths +end \ No newline at end of file diff --git a/modules/addons/death-markers.lua b/modules/addons/death-markers.lua deleted file mode 100644 index c161dc5a..00000000 --- a/modules/addons/death-markers.lua +++ /dev/null @@ -1,40 +0,0 @@ -local Event = require 'utils.event' -local Game = require 'utils.game' -local Global = require 'utils.global' -local Commands = require 'expcore.commands' -local config = require 'config.death_markers' -local opt_require, format_time = ext_require('expcore.common','opt_require','format_time_simple') -opt_require 'config.command_auth_runtime_disable' -- if the file is present then we can disable the commands rather than not load them - -local bodies = { - --{player_name='Cooldude2606',time_of_death='15H 15M',body=LuaEntity,tag=LuaCustomChartTag} -} -Global.register(bodies,function(tbl) - bodies = tbl -end) - ---- Checks that all map tags are present and valid --- adds missing ones, deletes expired ones -local function check_map_tags() - -end - ---- Teleports the owner of a body to the body -local function teleport_player(body) - -end - ---- Teleports the items in a body to a certain position putting it in chests --- if there are no chests close by them some are created -local function teleport_items(body,position) - -end - -Event.add(defines.events.on_player_died,function(event) - -end) - -local check_period = 60*60*5 -- five minutes -Event.on_nth_tick(check_period,function(event) - -end) \ No newline at end of file