mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-02-01 13:08:05 +02:00
new module - anti spam
This commit is contained in:
parent
47322b6d9b
commit
835c5ee22d
86
utils/spam_protection.lua
Normal file
86
utils/spam_protection.lua
Normal file
@ -0,0 +1,86 @@
|
||||
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
|
||||
default_tick = 15 -- this defines the default tick to check weather or not a user is spamming a button.
|
||||
}
|
||||
|
||||
Global.register(
|
||||
this,
|
||||
function(t)
|
||||
this = t
|
||||
end
|
||||
)
|
||||
|
||||
function Public.reset_spam_table()
|
||||
local players = game.connected_players
|
||||
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
|
||||
return this.prevent_spam[player.index]
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function Public.is_spamming(player, value_to_compare)
|
||||
if not this.prevent_spam[player.index] then
|
||||
return
|
||||
end
|
||||
|
||||
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
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user