1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/utils/spam_protection.lua

121 lines
2.6 KiB
Lua
Raw Normal View History

2020-12-14 20:35:55 +02:00
local Event = require 'utils.event'
local Global = require 'utils.global'
local Public = {}
local this = {
prevent_spam = {}, -- the default table where all player indexes will be stored
2022-02-14 00:32:57 +02:00
default_tick = 10, -- this defines the default tick to check whether or not a user is spamming a button.
debug_text = false,
debug_spam = true
2020-12-14 20:35:55 +02:00
}
2022-02-14 00:32:57 +02:00
local main_text = '[Spam Info] '
2020-12-14 20:35:55 +02:00
Global.register(
this,
function(t)
this = t
end
)
2022-02-14 00:32:57 +02:00
local function debug_text(str)
if not this.debug_text then
return
end
print(main_text .. str)
end
local function debug_spam(str)
if not this.debug_spam then
2021-01-12 22:52:45 +02:00
return
end
2022-02-14 00:32:57 +02:00
print(main_text .. str)
2021-01-12 22:52:45 +02:00
end
2020-12-14 20:35:55 +02:00
function Public.reset_spam_table()
local players = game.connected_players
this.prevent_spam = {}
2020-12-14 20:35:55 +02:00
for i = 1, #players do
local player = players[i]
this.prevent_spam[player.index] = game.tick
end
end
function Public.set_new_value(player)
if this.prevent_spam[player.index] then
this.prevent_spam[player.index] = game.tick
end
end
2021-01-12 22:52:45 +02:00
function Public.is_spamming(player, value_to_compare, text)
2022-02-14 00:32:57 +02:00
if not player or not player.valid then
player = game.get_player(player)
end
2020-12-14 20:35:55 +02:00
if not this.prevent_spam[player.index] then
2021-01-03 11:51:49 +02:00
return false
2020-12-14 20:35:55 +02:00
end
2021-01-12 22:52:45 +02:00
if text then
2022-02-14 00:32:57 +02:00
debug_text('Frame: ' .. text)
2021-01-12 22:52:45 +02:00
end
2021-01-03 11:46:59 +02:00
if game.tick_paused then
2021-01-03 11:51:49 +02:00
return false -- game is paused - shoo
2021-01-03 11:46:59 +02:00
end
2020-12-14 20:35:55 +02:00
local tick = game.tick
local value = value_to_compare or this.default_tick
if this.prevent_spam[player.index] then
if (tick - this.prevent_spam[player.index]) > value then
Public.set_new_value(player)
return false -- is not spamming
else
2022-02-14 00:32:57 +02:00
if text then
debug_spam(player.name .. ' is spamming: ' .. text)
else
debug_spam(player.name .. ' is spamming.')
end
2020-12-14 20:35:55 +02:00
return true -- is spamming
end
end
return false
end
function Public.get(key)
if key then
return this[key]
else
return this
end
end
function Public.set(key, value)
if key and (value or value == false) then
this[key] = value
return this[key]
elseif key then
return this[key]
else
return this
end
end
Event.add(
defines.events.on_player_joined_game,
function(event)
local player = game.get_player(event.player_index)
if not this.prevent_spam[player.index] then
this.prevent_spam[player.index] = game.tick
end
end
)
Event.on_init(
function()
Public.reset_spam_table()
end
)
return Public