1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-03-19 21:10:19 +02:00

Add logging to backend

This commit is contained in:
Gerkiz 2024-02-25 13:58:12 +01:00
parent 1b88c87afd
commit 6f4c0db8b5
4 changed files with 90 additions and 29 deletions

View File

@ -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.

View File

@ -1,30 +1,12 @@
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
return
end
local server_time = Server.get_current_time()
if server_time then
server_time = format(' (Server time: %s)', Timestamp.to_string(server_time))
else
server_time = ' at tick: ' .. game.tick
end
if string.len(param) <= 0 then
param = nil
end
local commands = {
['editor'] = true,
@ -37,24 +19,75 @@ local function on_console_command(event)
if not commands[cmd] then
return
end
local param = event.parameters
local server_time = Server.get_current_time()
if server_time then
server_time = format(' (Server time: %s)', Timestamp.to_string(server_time))
else
server_time = ' at tick: ' .. game.tick
end
if string.len(param) <= 0 then
param = nil
end
local server_name = Server.get_server_name() or 'CommandHandler'
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
)

View File

@ -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

View File

@ -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.