Skip to content

Commit

Permalink
Add logging to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerkiz committed Feb 25, 2024
1 parent 1b88c87 commit 6f4c0db
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 25 deletions.
13 changes: 13 additions & 0 deletions utils/antigrief.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
81 changes: 57 additions & 24 deletions utils/command_handler.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)
3 changes: 3 additions & 0 deletions utils/gui/admin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion utils/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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, '['}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6f4c0db

Please sign in to comment.