From 6f4c0db8b51f286f5d3f73c51a91a950192d5b9d Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Sun, 25 Feb 2024 13:58:12 +0100 Subject: [PATCH] Add logging to backend --- utils/antigrief.lua | 13 +++++++ utils/command_handler.lua | 81 +++++++++++++++++++++++++++------------ utils/gui/admin.lua | 3 ++ utils/server.lua | 14 ++++++- 4 files changed, 86 insertions(+), 25 deletions(-) diff --git a/utils/antigrief.lua b/utils/antigrief.lua index 1490377d9..74c405f5c 100644 --- a/utils/antigrief.lua +++ b/utils/antigrief.lua @@ -319,6 +319,7 @@ local function on_player_built_tile(event) str = str .. ' ' str = str .. 'surface:' .. surface increment(this.landfill_history, str) + Server.log_antigrief_data('landfill', str) end local function on_built_entity(event) @@ -425,6 +426,7 @@ local function on_player_used_capsule(event) str = str .. ' ' str = str .. 'surface:' .. player.surface.index increment(this.capsule_history, str) + Server.log_antigrief_data('capsule', str) end end @@ -483,6 +485,7 @@ local function on_entity_died(event) str = str .. ' ' str = str .. 'surface:' .. event.entity.surface.index increment(this.friendly_fire_history, str) + Server.log_antigrief_data('friendly_fire', str) elseif not blacklisted_types[event.entity.type] and this.whitelist_types[event.entity.type] then if cause then if cause.force.name ~= 'player' then @@ -514,8 +517,10 @@ local function on_entity_died(event) if cause and cause.name == 'character' and cause.player then increment(this.friendly_fire_history, str) + Server.log_antigrief_data('friendly_fire', str) else increment(this.friendly_fire_history, str) + Server.log_antigrief_data('friendly_fire', str) end end end @@ -554,6 +559,7 @@ local function on_player_mined_entity(event) str = str .. ' ' str = str .. 'surface:' .. entity.surface.index increment(this.whitelist_mining_history, str) + Server.log_antigrief_data('whitelist_mining', str) return end @@ -589,6 +595,7 @@ local function on_player_mined_entity(event) str = str .. ' ' str = str .. 'surface:' .. event.entity.surface.index increment(this.mining_history, str) + Server.log_antigrief_data('mining', str) end local function on_gui_opened(event) @@ -641,6 +648,7 @@ local function on_gui_opened(event) str = str .. ' ' str = str .. 'surface:' .. event.entity.surface.index increment(this.corpse_history, str) + Server.log_antigrief_data('corpse', str) end end @@ -696,6 +704,7 @@ local function on_pre_player_mined_item(event) str = str .. ' ' str = str .. 'surface:' .. entity.surface.index increment(this.corpse_history, str) + Server.log_antigrief_data('corpse', str) end end @@ -831,6 +840,7 @@ local function on_player_cancelled_crafting(event) str = str .. ' ' str = str .. 'surface:' .. player.surface.index increment(this.cancel_crafting_history, str) + Server.log_antigrief_data('cancel_crafting', str) end end @@ -955,6 +965,7 @@ local function on_player_deconstructed_area(event) str = str .. ' ' str = str .. 'surface:' .. player.surface.index increment(this.deconstruct_history, str) + Server.log_antigrief_data('deconstruct', str) if this.enable_jail_when_decon and not player.admin then if not this.players_warn_when_decon[player.index] then @@ -1124,6 +1135,7 @@ function Public.insert_into_capsule_history(player, position, msg) str = str .. ' ' str = str .. 'surface:' .. player.surface.index increment(this.capsule_history, str) + Server.log_antigrief_data('capsule', str) end --- This will reset the table of antigrief @@ -1263,6 +1275,7 @@ function Public.append_scenario_history(player, entity, message) str = str .. ' ' str = str .. 'surface:' .. player.surface.index increment(this.scenario_history, str) + Server.log_antigrief_data('scenario', str) end --- Returns the table. diff --git a/utils/command_handler.lua b/utils/command_handler.lua index d1275112b..ccefa98bf 100644 --- a/utils/command_handler.lua +++ b/utils/command_handler.lua @@ -1,19 +1,25 @@ local Event = require 'utils.event' local Server = require 'utils.server' local Timestamp = require 'utils.timestamp' +local Discord = require 'utils.discord_handler' + local format = string.format local function on_console_command(event) local cmd = event.command - if not event.player_index then - return - end - local player = game.players[event.player_index] - local param = event.parameters - if not player.admin then + local commands = { + ['editor'] = true, + ['command'] = true, + ['silent-command'] = true, + ['sc'] = true, + ['debug'] = true + } + + if not commands[cmd] then return end + local param = event.parameters local server_time = Server.get_current_time() if server_time then @@ -26,35 +32,62 @@ local function on_console_command(event) param = nil end - local commands = { - ['editor'] = true, - ['command'] = true, - ['silent-command'] = true, - ['sc'] = true, - ['debug'] = true - } + local server_name = Server.get_server_name() or 'CommandHandler' - if not commands[cmd] then - return - end + if event.player_index then + local player = game.get_player(event.player_index) + + if not player.admin then + return + end - if player then if param then + Discord.send_notification_raw(server_name, player.name .. ' ran: ' .. cmd .. ' "' .. param .. '" ' .. server_time) print('[COMMAND HANDLER] ' .. player.name .. ' ran: ' .. cmd .. ' "' .. param .. '" ' .. server_time) return else + Discord.send_notification_raw(server_name, player.name .. ' ran: ' .. cmd .. server_time) print('[COMMAND HANDLER] ' .. player.name .. ' ran: ' .. cmd .. server_time) return end + end + + if param then + Discord.send_notification_raw(server_name, cmd .. ' "' .. param .. '" ' .. server_time) + print('[COMMAND HANDLER] ran: ' .. cmd .. ' "' .. param .. '" ' .. server_time) + return else - if param then - print('[COMMAND HANDLER] ran: ' .. cmd .. ' "' .. param .. '" ' .. server_time) - return - else - print('[COMMAND HANDLER] ran: ' .. cmd .. server_time) - return - end + Discord.send_notification_raw(server_name, cmd .. server_time) + print('[COMMAND HANDLER] ran: ' .. cmd .. server_time) + return end end Event.add(defines.events.on_console_command, on_console_command) + +Event.add( + defines.events.on_player_promoted, + function(event) + local player = game.get_player(event.player_index) + local server_name = Server.get_server_name() or 'CommandHandler' + Discord.send_notification_raw(server_name, player.name .. ' was promoted.') + end +) + +Event.add( + defines.events.on_player_demoted, + function(event) + local player = game.get_player(event.player_index) + local server_name = Server.get_server_name() or 'CommandHandler' + Discord.send_notification_raw(server_name, player.name .. ' was demoted.') + end +) + +Event.add( + defines.events.on_player_kicked, + function(event) + local player = game.get_player(event.player_index) + local server_name = Server.get_server_name() or 'CommandHandler' + Discord.send_notification_raw(server_name, player.name .. ' was kicked.') + end +) diff --git a/utils/gui/admin.lua b/utils/gui/admin.lua index 7b74ae437..708500c90 100644 --- a/utils/gui/admin.lua +++ b/utils/gui/admin.lua @@ -74,6 +74,9 @@ local delayed_last_page_token = end local player_data = get_player_data(player) + if not player_data or not player_data.table_count then + return + end local last_page = ceil(player_data.table_count / rows_per_page) player_data.current_page = last_page diff --git a/utils/server.lua b/utils/server.lua index a37ef428d..111913c60 100644 --- a/utils/server.lua +++ b/utils/server.lua @@ -96,6 +96,7 @@ local unbanned_sync_tag = '[UNBANNED-SYNC]' local query_players_tag = '[QUERY-PLAYERS]' local player_join_tag = '[PLAYER-JOIN]' local player_leave_tag = '[PLAYER-LEAVE]' +local antigrief_tag = '[ANTIGRIEF-LOG]' Public.raw_print = raw_print @@ -689,6 +690,14 @@ local function send_try_get_data_and_print(data_set, key, to_print, callback_tok output_data(message) end +local function log_antigrief_data(category, action) + category = double_escape(category) + action = double_escape(action) + + local message = concat {antigrief_tag, '{', 'category:"', category, '",action:"', action, '"}'} + output_data(message) +end + local cancelable_callback_token = Token.register( function(data) @@ -1031,6 +1040,9 @@ Public.raise_data_set = data_set_changed --- Called by the web server to notify the client that the subscribed scenario has changed. Public.raise_scenario_changed = scenario_changed +-- Tracks antigrief and sends them to a specific log channel. +Public.log_antigrief_data = log_antigrief_data + --- Called by the web server to determine which data_sets are being tracked. function Public.get_tracked_data_sets() local message = {data_tracked_tag, '['} @@ -1136,7 +1148,7 @@ end -- This is the current server's name, in the case the save has been loaded on multiple servers. -- @return string function Public.get_server_name() - return start_data.server_name or '' + return start_data.server_name or nil end --- Gets the server's name and matches it against a string.