// AntiSpam v1 // By RabidToaster local function GetWeight(spawnType) if ConVarExists("antispam_weight_" .. spawnType) then return GetConVarNumber("antispam_weight_" .. spawnType) end return 0 end if SERVER then AddCSLuaFile("AntiSpam.lua") CreateConVar("antispam_enabled", SinglePlayer() and 0 or 1) CreateConVar("antispam_cooldelay", 1.5, {FCVAR_REPLICATED, FCVAR_ARCHIVE}) CreateConVar("antispam_maxlevel", 10, {FCVAR_ARCHIVE}) CreateConVar("antispam_weight_prop", 1, {FCVAR_REPLICATED, FCVAR_ARCHIVE}) CreateConVar("antispam_weight_tool", 0, {FCVAR_REPLICATED, FCVAR_ARCHIVE}) CreateConVar("antispam_weight_sent", 1, {FCVAR_REPLICATED, FCVAR_ARCHIVE}) CreateConVar("antispam_weight_vehicle", 1, {FCVAR_REPLICATED, FCVAR_ARCHIVE}) local function CanSpawn(ply, spawnType) if GetConVarNumber("antispam_enabled") <= 0 then return true end local last = ply.LastSpawn or 0 // Work out how much the level would've dropped since last spawn. local level = ply.SpamLevel or 0 local coolSpeed = GetConVarNumber("antispam_cooldelay") if coolSpeed <= 0 then coolSpeed = 1 end // Prevents division by 0 errors and weird behaviour. level = math.max(0, level - ((CurTime() - last) / coolSpeed)) // If upping the level would put us over the limit, say no. local weight = GetWeight(spawnType) if (level + weight) > GetConVarNumber("antispam_maxlevel") then return false end // We're fine, say yes. ply.SpamLevel = level return true end local function HasSpawned(ply, spawnType) // They've spawned successfully, we can add to their level. local weight = GetWeight(spawnType) ply.SpamLevel = (ply.SpamLevel or 0) + weight ply.LastSpawn = CurTime() end local function Alert(ply, message) // Only send a message every quarter of a second max. if CurTime() < (ply.AntiSpamMessage or 0) then return end ply.AntiSpamMessage = CurTime() + 0.25 umsg.Start("AntiSpam", ply) umsg.String(message or "") umsg.End() end // Gamemode hooks. hook.Add("PlayerSpawnProp", "AntiSpam", function(ply) if !CanSpawn(ply, "prop") then Alert(ply) return false end end) hook.Add("PlayerSpawnedProp", "AntiSpam", function(ply) HasSpawned(ply, "prop") end) hook.Add("PlayerSpawnSENT", "AntiSpam", function(ply) if !CanSpawn(ply, "sent") then Alert(ply, "sent") return false end end) hook.Add("PlayerSpawnedSENT", "AntiSpam", function(ply) HasSpawned(ply, "sent") end) hook.Add("PlayerSpawnVehicle", "AntiSpam", function(ply) if !CanSpawn(ply, "vehicle") then Alert(ply, "vehicle") return false end end) hook.Add("PlayerSpawnedVehicle", "AntiSpam", function(ply) HasSpawned(ply, "vehicle") end) // Experimental use of CanTool, disable by default in the convars. hook.Add("CanTool", "AntiSpam", function(ply) if !CanSpawn(ply, "tool") then Alert(ply, "tool") return false else HasSpawned(ply, "tool") end end) end if CLIENT then // A table of appropriate verbs. Defaults to "spawning". local verb = { ["tool"] = "trying"; } Sound("buttons/button10.wav") local function Notify(msg) if GAMEMODE.AddNotify then local spawnType = msg:ReadString() if spawnType == "" then spawnType = "prop" end // The time before next spawn depends on the cool speed and the weight of the spawn type. local time = GetConVarNumber("antispam_cooldelay") local weight = GetWeight(spawnType) time = time * weight // If the time has been calculated correctly, we can tell them how long they've got to wait. if time != 0 then GAMEMODE:AddNotify("Please wait " .. time .. " seconds before " .. (verb[spawnType] or "spawning") .. " again.", NOTIFY_ERROR, 5) else GAMEMODE:AddNotify("Please wait before " .. (verb[spawnType] or "spawning") .. " again.", NOTIFY_ERROR, 5) end surface.PlaySound("buttons/button10.wav") end end usermessage.Hook("AntiSpam", Notify) end