From 74bf9bec20904aff525571a2be63a8feb0938314 Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Wed, 5 Jan 2022 22:49:07 +0100 Subject: [PATCH 1/2] Minor changes to logging to discord --- utils/core.lua | 70 ------ utils/datastore/current_time_data.lua | 4 +- utils/datastore/jail_data.lua | 30 ++- utils/datastore/rpg_data.lua | 4 +- utils/datastore/supporters.lua | 6 + utils/server.lua | 336 +++++++++++++++++++++++--- 6 files changed, 330 insertions(+), 120 deletions(-) diff --git a/utils/core.lua b/utils/core.lua index ff9a381e..74791a45 100644 --- a/utils/core.lua +++ b/utils/core.lua @@ -146,76 +146,6 @@ function Public.ternary(c, t, f) end end -local function CurrentDate(z) - z = floor(z / 86400) + 719468 - local era = floor(z / 146097) - local doe = floor(z - era * 146097) - local yoe = floor((doe - doe / 1460 + doe / 36524 - doe / 146096) / 365) - local y = floor(yoe + era * 400) - local doy = doe - floor((365 * yoe + yoe / 4 - yoe / 100)) - local mp = floor((5 * doy + 2) / 153) - local d = ceil(doy - (153 * mp + 2) / 5 + 1) - local m = floor(mp + (mp < 10 and 3 or -9)) - return y + (m <= 2 and 1 or 0), m, d -end - -function Public.CurrentTime(epoch) - if not epoch then - return - end - - local unixTime = floor(epoch) - (60 * 60 * (-2)) - - local hours = floor(unixTime / 3600 % 12) - local minutes = floor(unixTime / 60 % 60) - local seconds = floor(unixTime % 60) - - local year, month, day = CurrentDate(unixTime) - - month = tonumber(month) - month = 0 .. month - - day = tonumber(day) - if day < 10 then - day = 0 .. day - end - - return { - year = year, - month = month, - day = day, - hours = hours, - minutes = minutes < 10 and '0' .. minutes or minutes, - seconds = seconds < 10 and '0' .. seconds or seconds - } -end - -local CurrentTime = Public.CurrentTime - -function Public.get_current_date(pretty) - local server_time = Server.get_current_time() - if not server_time then - return false - end - - local date = CurrentTime(server_time) - if pretty then - return tonumber(date.year .. '-' .. date.month .. '-' .. date.day) - else - return tonumber(date.year .. date.month .. date.day) - end -end - -function Public.get_current_date_with_time() - local server_time = Server.get_current_time() - if not server_time then - return false - end - - local date = CurrentTime(server_time) - return date.year .. '-' .. date.month .. '-' .. date.day .. ' ' .. date.hours .. ':' .. date.minutes -end - --- Takes a time in ticks and returns a string with the time in format "x hour(s) x minute(s)" function Public.format_time(ticks) local result = {} diff --git a/utils/datastore/current_time_data.lua b/utils/datastore/current_time_data.lua index 2c95de3d..64b4b663 100644 --- a/utils/datastore/current_time_data.lua +++ b/utils/datastore/current_time_data.lua @@ -28,7 +28,7 @@ local function set_location(player) end local function create_label(player) - local date = Core.get_current_date_with_time() + local date = Server.get_current_date_with_time() if not date then date = '1970-01-01' end @@ -65,7 +65,7 @@ Event.add( Event.on_nth_tick( 60, function() - local date = Core.get_current_date_with_time() + local date = Server.get_current_date_with_time() if not date then date = '1969-01-01 00:00' end diff --git a/utils/datastore/jail_data.lua b/utils/datastore/jail_data.lua index c81e663f..ade80222 100644 --- a/utils/datastore/jail_data.lua +++ b/utils/datastore/jail_data.lua @@ -387,7 +387,10 @@ local vote_to_jail = function(player, griefer, msg) votejail[griefer][player.name] = true votejail[griefer].index = votejail[griefer].index + 1 Utils.print_to(player, 'You have voted to jail player ' .. griefer .. '.') - if votejail[griefer].index >= settings.votejail_count or (votejail[griefer].index == #game.connected_players - 1 and #game.connected_players > votejail[griefer].index) then + if + votejail[griefer].index >= settings.votejail_count or + (votejail[griefer].index == #game.connected_players - 1 and #game.connected_players > votejail[griefer].index) + then Public.try_ul_data(griefer, true, votejail[griefer].actor, msg) end else @@ -415,7 +418,10 @@ local vote_to_free = function(player, griefer) votefree[griefer].index = votefree[griefer].index + 1 Utils.print_to(player, 'You have voted to free player ' .. griefer .. '.') - if votefree[griefer].index >= settings.votejail_count or (votefree[griefer].index == #game.connected_players - 1 and #game.connected_players > votefree[griefer].index) then + if + votefree[griefer].index >= settings.votejail_count or + (votefree[griefer].index == #game.connected_players - 1 and #game.connected_players > votefree[griefer].index) + then Public.try_ul_data(griefer, false, votefree[griefer].actor) votejail[griefer] = nil votefree[griefer] = nil @@ -460,7 +466,10 @@ local jail = function(player, griefer, msg, raised) end Utils.print_to(nil, message) - Utils.action_warning_embed('[Jailed]', message) + local data = Server.build_embed_data() + data.username = griefer + data.admin = player + Server.to_jailed_embed(data) if votejail[griefer] then votejail[griefer].jailed = true @@ -489,7 +498,10 @@ local free = function(player, griefer) set_data(jailed_data_set, griefer, nil) Utils.print_to(nil, message) - Utils.action_warning_embed('[Jailed]', message) + local data = Server.build_embed_data() + data.username = griefer + data.admin = player + Server.to_unjailed_embed(data) return true end @@ -703,7 +715,10 @@ Event.add( if trusted and playtime >= settings.playtime_for_vote and playtime < settings.playtime_for_instant_jail and not player.admin then if cmd == 'jail' then if not terms_tbl[player.name] then - Utils.warning(player, 'Abusing the jail command will lead to revoked permissions. Jailing someone in case of disagreement is _NEVER_ OK!') + Utils.warning( + player, + 'Abusing the jail command will lead to revoked permissions. Jailing someone in case of disagreement is _NEVER_ OK!' + ) Utils.warning(player, 'Run this command again to if you really want to do this!') for i = 1, 4 do Task.set_timeout_in_ticks(delay, play_alert_sound, {name = player.name}) @@ -725,7 +740,10 @@ Event.add( if player.admin or playtime >= settings.playtime_for_instant_jail then if cmd == 'jail' then if not terms_tbl[player.name] then - Utils.warning(player, 'Abusing the jail command will lead to revoked permissions. Jailing someone in case of disagreement is _NEVER_ OK!') + Utils.warning( + player, + 'Abusing the jail command will lead to revoked permissions. Jailing someone in case of disagreement is _NEVER_ OK!' + ) Utils.warning(player, 'Run this command again to if you really want to do this!') for i = 1, 4 do Task.set_timeout_in_ticks(delay, play_alert_sound, {name = player.name}) diff --git a/utils/datastore/rpg_data.lua b/utils/datastore/rpg_data.lua index 0e3a5f6e..e5599cd7 100644 --- a/utils/datastore/rpg_data.lua +++ b/utils/datastore/rpg_data.lua @@ -88,7 +88,7 @@ local try_download_amount_of_resets_token = local old_value = data.value if old_value then old_value = tonumber(old_value) - local new_value = Core.get_current_date() + local new_value = Server.get_current_date() local time_to_reset = (new_value - old_value) if not time_to_reset then return @@ -102,7 +102,7 @@ local try_download_amount_of_resets_token = try_get_all_data(this.settings.dataset, clear_all_data_token) end else - local new_value = Core.get_current_date() + local new_value = Server.get_current_date() if new_value then set_data(this.settings.dataset, this.settings.reset_key, tonumber(new_value)) diff --git a/utils/datastore/supporters.lua b/utils/datastore/supporters.lua index e309bb11..d5c8d747 100644 --- a/utils/datastore/supporters.lua +++ b/utils/datastore/supporters.lua @@ -30,6 +30,12 @@ end local sync_supporters_callback = Token.register( function(data) + if not data then + return + end + if not data.entries then + return + end table.clear_table(this.supporters) for k, v in pairs(data.entries) do this.supporters[k] = v diff --git a/utils/server.lua b/utils/server.lua index ed030992..91ce85db 100644 --- a/utils/server.lua +++ b/utils/server.lua @@ -5,14 +5,21 @@ local Event = require 'utils.event' local Game = require 'utils.game' local Print = require('utils.print_override') -local serialize = serpent.serialize +-- local constants +local floor = math.floor +local ceil = math.ceil +local insert = table.insert local concat = table.concat +local serialize = serpent.serialize local remove = table.remove local tostring = tostring local len = string.len local gmatch = string.gmatch -local insert = table.insert local raw_print = Print.raw_print +local minutes_to_ticks = 60 * 60 +local hours_to_ticks = 60 * 60 * 60 +local ticks_to_minutes = 1 / minutes_to_ticks +local ticks_to_hours = 1 / hours_to_ticks local serialize_options = {sparse = true, compact = true} @@ -48,6 +55,12 @@ local discord_bold_tag = '[DISCORD-BOLD]' local discord_admin_tag = '[DISCORD-ADMIN]' local discord_banned_tag = '[DISCORD-BANNED]' local discord_banned_embed_tag = '[DISCORD-BANNED-EMBED]' +local discord_unbanned_tag = '[DISCORD-UNBANNED]' +local discord_unbanned_embed_tag = '[DISCORD-UNBANNED-EMBED]' +local discord_jailed_tag = '[DISCORD-JAILED]' +local discord_jailed_embed_tag = '[DISCORD-JAILED-EMBED]' +local discord_unjailed_tag = '[DISCORD-UNJAILED]' +local discord_unjailed_embed_tag = '[DISCORD-UNJAILED-EMBED]' local discord_admin_raw_tag = '[DISCORD-ADMIN-RAW]' local discord_embed_tag = '[DISCORD-EMBED]' local discord_embed_raw_tag = '[DISCORD-EMBED-RAW]' @@ -186,7 +199,7 @@ function Public.to_admin(message, locale) end end ---- Sends a message to the linked banend discord channel. The message is sanitized of markdown server side. +--- Sends a message to the linked banned discord channel. The message is sanitized of markdown server side. -- @param message message to send. -- @param locale if the message should be handled as localized. function Public.to_banned(message, locale) @@ -196,6 +209,37 @@ function Public.to_banned(message, locale) raw_print(discord_banned_tag .. message) end end +--- Sends a message to the linked banned discord channel. The message is sanitized of markdown server side. +-- @param message message to send. +-- @param locale if the message should be handled as localized. +function Public.to_unbanned(message, locale) + if locale then + print(message, discord_unbanned_tag) + else + raw_print(discord_unbanned_tag .. message) + end +end + +--- Sends a message to the linked connected discord channel. The message is sanitized of markdown server side. +-- @param message message to send. +-- @param locale if the message should be handled as localized. +function Public.to_jailed(message, locale) + if locale then + print(message, discord_jailed_tag) + else + raw_print(discord_jailed_tag .. message) + end +end +--- Sends a message to the linked connected discord channel. The message is sanitized of markdown server side. +-- @param message message to send. +-- @param locale if the message should be handled as localized. +function Public.to_unjailed(message, locale) + if locale then + print(message, discord_unjailed_tag) + else + raw_print(discord_unjailed_tag .. message) + end +end --- Sends a message to the linked admin discord channel. The message is not sanitized of markdown. -- @param message message to send. @@ -242,13 +286,92 @@ function Public.to_admin_embed(message, locale) end --- Sends a embed message to the linked banned discord channel. The message is sanitized of markdown server side. --- @param message the content of the embed. +-- @param message the content of the embed. -- @param locale if the message should be handled as localized. function Public.to_banned_embed(message, locale) + local table_to_json = game.table_to_json + if not type(message) == 'table' then + return + end if locale then print(message, discord_banned_embed_tag) else - raw_print(discord_banned_embed_tag .. message) + if not message.username then + return + end + if not message.reason then + return + end + if not message.admin then + return + end + raw_print(discord_banned_embed_tag .. table_to_json(message)) + end +end + +--- Sends a embed message to the linked banned discord channel. The message is sanitized of markdown server side. +-- @param message the content of the embed. +-- @param locale if the message should be handled as localized. +function Public.to_unbanned_embed(message, locale) + local table_to_json = game.table_to_json + if not type(message) == 'table' then + return + end + if locale then + print(message, discord_unbanned_embed_tag) + else + if not message.username then + return + end + if not message.admin then + return + end + raw_print(discord_unbanned_embed_tag .. table_to_json(message)) + end +end + +--- Sends a embed message to the linked connected discord channel. The message is sanitized of markdown server side. +-- @param message the content of the embed. +-- @param locale if the message should be handled as localized. +function Public.to_jailed_embed(message, locale) + local table_to_json = game.table_to_json + if not type(message) == 'table' then + return + end + if locale then + print(message, discord_jailed_embed_tag) + else + if not message.username then + return + end + if not message.reason then + return + end + if not message.admin then + return + end + raw_print(discord_jailed_embed_tag .. table_to_json(message)) + end +end + +--- Sends a embed message to the linked connected discord channel. The message is sanitized of markdown server side. +-- @param message the content of the embed. +-- @param locale if the message should be handled as localized. +function Public.to_unjailed_embed(message, locale) + local table_to_json = game.table_to_json + if not type(message) == 'table' then + return + end + if locale then + print(message, discord_jailed_embed_tag) + else + if not message.username then + return + end + if not message.admin then + return + end + raw_print(discord_jailed_embed_tag .. table_to_json(message)) end end @@ -377,6 +500,27 @@ local function validate_arguments(data_set, key, callback_token) end end +local function getOnlineAdmins() + local online = game.connected_players + local i = 0 + for _, p in pairs(online) do + if p.admin then + i = i + 1 + end + end + return i +end + +local function build_embed_data() + local d = { + time = Public.format_time(game.ticks_played), + onlinePlayers = #game.connected_players, + totalPlayers = #game.players, + onlineAdmins = getOnlineAdmins() + } + return d +end + local function send_try_get_data(data_set, key, callback_token) data_set = double_escape(data_set) key = double_escape(key) @@ -907,7 +1051,109 @@ function Public.get_current_time() return math.floor(secs + diff / game.speed / 60) end ---- Called be the web server to re sync which players are online. +--- Converts from epoch to yymmddhhmm. +-- @param epoch +-- @return date? +function Public.convertFromEpoch(epoch) + if not epoch then + return + end + + local function date(z) + z = floor(z / 86400) + 719468 + local era = floor(z / 146097) + local doe = floor(z - era * 146097) + local yoe = floor((doe - doe / 1460 + doe / 36524 - doe / 146096) / 365) + local y = floor(yoe + era * 400) + local doy = doe - floor((365 * yoe + yoe / 4 - yoe / 100)) + local mp = floor((5 * doy + 2) / 153) + local d = ceil(doy - (153 * mp + 2) / 5 + 1) + local m = floor(mp + (mp < 10 and 3 or -9)) + return y + (m <= 2 and 1 or 0), m, d + end + + local unixTime = floor(epoch) - (60 * 60 * (-2)) + + local hours = floor(unixTime / 3600 % 12) + local minutes = floor(unixTime / 60 % 60) + local seconds = floor(unixTime % 60) + + local year, month, day = date(unixTime) + + month = tonumber(month) + month = 0 .. month + + day = tonumber(day) + if day < 10 then + day = 0 .. day + end + + return { + year = year, + month = month, + day = day, + hours = hours, + minutes = minutes < 10 and '0' .. minutes or minutes, + seconds = seconds < 10 and '0' .. seconds or seconds + } +end + +-- Returns the current date. +-- @param pretty +-- @return date? +function Public.get_current_date(pretty) + local s = Public.get_current_time() + if not s then + return false + end + + local date = Public.convertFromEpoch(s) + if pretty then + return tonumber(date.year .. '-' .. date.month .. '-' .. date.day) + else + return tonumber(date.year .. date.month .. date.day) + end +end + +-- Returns the total played time in yymmddhhmm. +-- @return date? +function Public.get_current_date_with_time() + local s = Public.get_current_time() + if not s then + return false + end + + local date = Public.convertFromEpoch(s) + return date.year .. '-' .. date.month .. '-' .. date.day .. ' ' .. date.hours .. ':' .. date.minutes +end + +--- Takes a time in ticks and returns a string with the time in format "x hour(s) x minute(s)" +function Public.format_time(ticks) + local result = {} + + local hours = floor(ticks * ticks_to_hours) + if hours > 0 then + ticks = ticks - hours * hours_to_ticks + insert(result, hours) + if hours == 1 then + insert(result, 'hour') + else + insert(result, 'hours') + end + end + + local minutes = floor(ticks * ticks_to_minutes) + insert(result, minutes) + if minutes == 1 then + insert(result, 'minute') + else + insert(result, 'minutes') + end + + return concat(result, ' ') +end + +--- Called by the web server to re sync which players are online. function Public.query_online_players() local message = {query_players_tag, '['} @@ -1014,10 +1260,7 @@ Event.add( defines.events.on_console_command, function(event) local cmd = event.command - if not event.player_index then - return - end - local player = game.players[event.player_index] + local user = event.parameters if not user then return @@ -1027,10 +1270,6 @@ Event.add( return end - if not player.admin then - return - end - local userIndex local reason local str = '' @@ -1052,41 +1291,56 @@ Event.add( end local banishedPlayer - if game.players[userIndex] then - banishedPlayer = game.players[userIndex] + if game.get_player(userIndex) then + banishedPlayer = game.get_player(userIndex) else return end - if banishedPlayer.index == player.index then - return - end - - if cmd == 'ban' then - Public.set_data(jailed_data_set, player.name, nil) -- this is added here since we don't want to clutter the jail dataset. - if player then - if not reason then - Public.to_banned_embed(table.concat {player.name .. ' banned ' .. banishedPlayer.name .. '. Reason: Not specified.'}) - return - else - Public.to_banned_embed(table.concat {player.name .. ' banned ' .. banishedPlayer.name .. '. Reason: ' .. reason}) + if event.player_index then + local player = game.get_player(event.player_index) + if player and player.valid and player.admin then + if banishedPlayer.index == player.index then return end - else - if not reason then - Public.to_banned_embed(table.concat {'Server banned ' .. banishedPlayer.name .. '. Reason: Not specified.'}) - return - else - Public.to_banned_embed(table.concat {'Server banned ' .. banishedPlayer.name .. '. Reason: ' .. reason}) + + local data = build_embed_data() + data.username = banishedPlayer.name + data.admin = player.name + + if cmd == 'ban' then + Public.set_data(jailed_data_set, banishedPlayer.name, nil) -- this is added here since we don't want to clutter the jail dataset. + if not reason then + data.reason = 'Not specified.' + Public.to_banned_embed(data) + return + else + data.reason = reason + Public.to_banned_embed(data) + return + end + elseif cmd == 'unban' then + Public.to_unbanned_embed(data) return end end - elseif cmd == 'unban' then - if player then - Public.to_banned_embed(table.concat {player.name .. ' unbanned ' .. banishedPlayer.name}) - return - else - Public.to_banned_embed(table.concat {'Server unbanned ' .. banishedPlayer.name}) + else + local data = build_embed_data() + data.username = banishedPlayer.name + data.admin = '' + + if cmd == 'ban' then + if not reason then + data.reason = 'Not specified.' + Public.to_banned_embed(data) + return + else + data.reason = reason + Public.to_banned_embed(data) + return + end + elseif cmd == 'unban' then + Public.to_unbanned_embed(data) return end end @@ -1124,4 +1378,6 @@ Event.add( end ) +Public.build_embed_data = build_embed_data + return Public From d7dcb7270c9f561e4c08c25a5cd76ad5ae302876 Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Wed, 5 Jan 2022 22:53:25 +0100 Subject: [PATCH 2/2] Fix missing commit --- utils/core.lua | 1 - utils/datastore/current_time_data.lua | 1 - utils/datastore/rpg_data.lua | 1 - utils/server.lua | 4 ++-- 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/utils/core.lua b/utils/core.lua index 74791a45..4269e0db 100644 --- a/utils/core.lua +++ b/utils/core.lua @@ -7,7 +7,6 @@ local Server = require 'utils.server' local random = math.random local sqrt = math.sqrt local floor = math.floor -local ceil = math.ceil local format = string.format local match = string.match local insert = table.insert diff --git a/utils/datastore/current_time_data.lua b/utils/datastore/current_time_data.lua index 64b4b663..1e5d4339 100644 --- a/utils/datastore/current_time_data.lua +++ b/utils/datastore/current_time_data.lua @@ -1,5 +1,4 @@ local Server = require 'utils.server' -local Core = require 'utils.core' local Event = require 'utils.event' local ComfyGui = require 'comfy_panel.main' local Color = require 'utils.color_presets' diff --git a/utils/datastore/rpg_data.lua b/utils/datastore/rpg_data.lua index e5599cd7..085f9bf5 100644 --- a/utils/datastore/rpg_data.lua +++ b/utils/datastore/rpg_data.lua @@ -1,6 +1,5 @@ local Global = require 'utils.global' local Game = require 'utils.game' -local Core = require 'utils.core' local Token = require 'utils.token' local Task = require 'utils.task' local Server = require 'utils.server' diff --git a/utils/server.lua b/utils/server.lua index 91ce85db..dc686a91 100644 --- a/utils/server.lua +++ b/utils/server.lua @@ -363,7 +363,7 @@ function Public.to_unjailed_embed(message, locale) return end if locale then - print(message, discord_jailed_embed_tag) + print(message, discord_unjailed_embed_tag) else if not message.username then return @@ -371,7 +371,7 @@ function Public.to_unjailed_embed(message, locale) if not message.admin then return end - raw_print(discord_jailed_embed_tag .. table_to_json(message)) + raw_print(discord_unjailed_embed_tag .. table_to_json(message)) end end