--[[

░██████╗███████╗███╗░░██╗████████╗██╗███╗░░██╗███████╗██╗░░░░░
██╔════╝██╔════╝████╗░██║╚══██╔══╝██║████╗░██║██╔════╝██║░░░░░
╚█████╗░█████╗░░██╔██╗██║░░░██║░░░██║██╔██╗██║█████╗░░██║░░░░░
░╚═══██╗██╔══╝░░██║╚████║░░░██║░░░██║██║╚████║██╔══╝░░██║░░░░░
██████╔╝███████╗██║░╚███║░░░██║░░░██║██║░╚███║███████╗███████╗
╚═════╝░╚══════╝╚═╝░░╚══╝░░░╚═╝░░░╚═╝╚═╝░░╚══╝╚══════╝╚══════╝

████████╗██╗░░██╗███████╗  ██████╗░███████╗░██████╗████████╗
╚══██╔══╝██║░░██║██╔════╝  ██╔══██╗██╔════╝██╔════╝╚══██╔══╝
░░░██║░░░███████║█████╗░░  ██████╦╝█████╗░░╚█████╗░░░░██║░░░
░░░██║░░░██╔══██║██╔══╝░░  ██╔══██╗██╔══╝░░░╚═══██╗░░░██║░░░
░░░██║░░░██║░░██║███████╗  ██████╦╝███████╗██████╔╝░░░██║░░░
░░░╚═╝░░░╚═╝░░╚═╝╚══════╝  ╚═════╝░╚══════╝╚═════╝░░░░╚═╝░░░

░█████╗░███╗░░██╗████████╗██╗░░░░░░░█████╗░██████╗░░█████╗░░██████╗██╗░░██╗
██╔══██╗████╗░██║╚══██╔══╝██║░░░░░░██╔══██╗██╔══██╗██╔══██╗██╔════╝██║░░██║
███████║██╔██╗██║░░░██║░░░██║█████╗██║░░╚═╝██████╔╝███████║╚█████╗░███████║
██╔══██║██║╚████║░░░██║░░░██║╚════╝██║░░██╗██╔══██╗██╔══██║░╚═══██╗██╔══██║
██║░░██║██║░╚███║░░░██║░░░██║░░░░░░╚█████╔╝██║░░██║██║░░██║██████╔╝██║░░██║
╚═╝░░╚═╝╚═╝░░╚══╝░░░╚═╝░░░╚═╝░░░░░░░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░╚═╝░░╚═╝

█▀█ █▀█ █▀█ █░█ █ █▀▄ █▀▀ █▀▄   █▄▄ █▄█   █░░ █ █▄▄ █▄▄ █▄█ ▀ █▀   █▀ ▄▀█ █▄░█ █▀▄ █▄▄ █▀█ ▀▄▀
█▀▀ █▀▄ █▄█ ▀▄▀ █ █▄▀ ██▄ █▄▀   █▄█ ░█░   █▄▄ █ █▄█ █▄█ ░█░ ░ ▄█   ▄█ █▀█ █░▀█ █▄▀ █▄█ █▄█ █░█

█▀▄▀█ ▄▀█ █▀█ █▀▀ █░█   █▄▄   ▀█ █▀█ ▀█ ▀█   █▀ █▀▀ █▀█ ▄▀█ █▀█ █▀█ █▀▀ █▀▄   █░█ █▀▀ █▀█ █▀ █ █▀█ █▄░█
█░▀░█ █▀█ █▀▄ █▄▄ █▀█   █▄█   █▄ █▄█ █▄ █▄   ▄█ █▄▄ █▀▄ █▀█ █▀▀ █▀▀ ██▄ █▄▀   ▀▄▀ ██▄ █▀▄ ▄█ █ █▄█ █░▀█

]]--

--[[
this is sentinel, an anti-crash lag prevention system for Libby's Sandbox
also used on rose's server

entity ghosting, lag prevention when major lag detected, and crash prevention for tools
[NOT A PROP PROTECTION SYSTEM]
]]--


-- Libby's new way of detecting prop spam, for now
hexanenetlibrarydebug = hexanenetlibrarydebug or {}
hexanenetlibrarydebug.antiNetSpam = {}
hexanenetlibrarydebug.flaggedNetPlayers = {}
hexanenetlibrarydebug.antiConSpam = {}
hexanenetlibrarydebug.flaggedConPlayers = {}
hexanenetlibrarydebug.threshold = 20
hexanenetlibrarydebug.crun = hexanenetlibrarydebug.crun or concommand.Run
ranexplodecommand = 0

function concommand.Run(ply, cmd, args, argStr)
    if not IsValid(ply) then return hexanenetlibrarydebug.crun(ply, cmd, args, argStr) end
    if not cmd then return hexanenetlibrarydebug.crun(ply, cmd, args, argStr) end
    local plySteamid = IsValid(ply) and ply:SteamID() or "UNKNOWN STEAMID"
    local plyNick = IsValid(ply) and ply:Nick() or "UNKNOWN PLAYER NAME"
    local plyIP = IsValid(ply) and ply:IPAddress() or "UNKNOWN IP"
    local antiConSpam = hexanenetlibrarydebug.antiConSpam
    local flaggedConPlayers = hexanenetlibrarydebug.flaggedConPlayers
    local commandstart, commandend = string.find(cmd, "gm_spawn")

    if commandstart then
        timer.Create("reset_concommandspam", 0.275, 1, function()
            antiConSpam[plySteamid][cmd] = 0
        end)
    end

    antiConSpam[plySteamid] = antiConSpam[plySteamid] or {}
    antiConSpam[plySteamid][cmd] = (antiConSpam[plySteamid][cmd] or 0) + 1
    local temp = (args and args ~= "" and #args ~= 0) and " " .. table.concat(args, " ") or "None"

    -- if antiConSpam[plySteamid][cmd] > 24 then
    --     local commandstart, commandend = string.find(cmd, "gm_spawn")

    --     if commandstart then
    --         if not flaggedConPlayers[plySteamid] then
    --             //PrintMessage(HUD_PRINTTALK, "[Sentinel] " .. ply:Nick() .. " [" .. ply:SteamID() .. "] could be prop spamming! | they have been kicked")
    --             sentinel.Broadcast( ply:Nick() .." Expected entity spamming | kicked")
    --             ply:Kick("[Sentinel] Kicked for potentially spamming. rejoin server if false alarm")
    --         end
    --     end
    -- end

    --print("" .. ply:Nick() .. " (" .. cmd .. ") - (" .. temp .. ")")
    local commandstart, commandend = string.find(temp, "ulx explode")

    if commandstart then
        print("Player " .. ply:Nick() .. " ran the explode command")
        ranexplodecommand = ranexplodecommand + 1

        timer.Create("explodetimerreset", 0.25, 1, function()
            ranexplodecommand = 0
        end)

        if ranexplodecommand > 5 then
            ply:SendLua("chat.AddText('[Sentinel]: Refrain from spamming the explode command right now!')")

            if ranexplodecommand > 20 then
                ply:Kick("[Sentinel]: Spamming the ulx explode command, Maybe you shouldn't have access to this anymore.")
            end
        end
    end
  
    --print("" .. ply:Nick() .. " (" .. cmd .. ") - (" .. temp .. ")")
    local commandstart, commandend = string.find(temp, "ulx psay")

    if commandstart then
        print("Player " .. ply:Nick() .. " ran the psay command")
        ranexplodecommand = ranexplodecommand + 1

        timer.Create("explodetimerreset", 0.25, 1, function()
            ranexplodecommand = 0
        end)

        if ranexplodecommand > 5 then
            ply:SendLua("chat.AddText('[Sentinel]: Refrain from spamming the psay command right now!')")

            if ranexplodecommand > 20 then
                ply:Kick("[Sentinel]: Spamming the ulx psay command, Maybe you shouldn't have access to this anymore.")
            end
        end
    end

    return hexanenetlibrarydebug.crun(ply, cmd, args, argStr)
end

sentinel.Broadcast("kickspam_module loaded") 