Загрузка данных


-- Ranks_kdm1eHvH.lua
-- Система рангов для сервера kdm1eHvH

RANKS = RANKS or {}

RANKS.list = {
    ["user"]         = { level = 0, color = Color(180, 180, 180), name = "User" },
    ["beta_tester"]  = { level = 1, color = Color(80, 220, 220),  name = "Beta Tester" },
    ["vip"]          = { level = 2, color = Color(255, 200, 50),  name = "VIP" },
    ["moderator"]    = { level = 3, color = Color(80, 150, 255),  name = "Moderator" },
    ["admin"]        = { level = 4, color = Color(255, 70, 70),   name = "Admin" },
    ["senior_admin"] = { level = 5, color = Color(200, 30, 30),   name = "Senior Admin" },
    ["manager"]      = { level = 6, color = Color(180, 80, 255),  name = "Manager" },
    ["superadmin"]   = { level = 7, color = Color(255, 130, 0),   name = "Superadmin" },
}

RANKS.order = { "user", "beta_tester", "vip", "moderator", "admin", "senior_admin", "manager", "superadmin" }

RANKS.perms = {
    ["user"]         = { canTakeWeapons = true },
    ["beta_tester"]  = { canTakeWeapons = true, canSeeLogs = true },
    ["vip"]          = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true },
    ["moderator"]    = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true },
    ["admin"]        = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true, canBan = true, canSlay = true, canGod = true, canNoclip = true },
    ["senior_admin"] = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true, canBan = true, canSlay = true, canGod = true, canNoclip = true, canUnban = true, canManageMods = true },
    ["manager"]      = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true, canBan = true, canSlay = true, canGod = true, canNoclip = true, canUnban = true, canManageMods = true },
    ["superadmin"]   = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true, canBan = true, canSlay = true, canGod = true, canNoclip = true, canUnban = true, canManageMods = true, canSetRank = true, canEverything = true },
}

-- ============================================================
-- База данных
-- ============================================================
local function InitDB()
    if sql.TableExists("player_ranks") then return end
    sql.Query([[
        CREATE TABLE player_ranks (
            steamid TEXT PRIMARY KEY,
            rank TEXT DEFAULT 'user',
            assigned_by TEXT DEFAULT '',
            assigned_at INTEGER DEFAULT 0
        )
    ]])
    print("[Ranks] Таблица player_ranks создана")
end

InitDB()

local function GetRankFromDB(steamid)
    if not steamid then return "user" end
    local query = "SELECT rank FROM player_ranks WHERE steamid = " .. sql.SQLStr(steamid)
    local result = sql.QueryRow(query, 1)
    if result and result.rank and RANKS.list[result.rank] then
        return result.rank
    end
    return "user"
end

local function SaveRankToDB(steamid, rank, assignedBy)
    if not steamid or not rank then return end
    local query = string.format(
        "INSERT INTO player_ranks (steamid, rank, assigned_by, assigned_at) VALUES (%s, %s, %s, %d) " ..
        "ON CONFLICT(steamid) DO UPDATE SET rank = %s, assigned_by = %s, assigned_at = %d",
        sql.SQLStr(steamid), sql.SQLStr(rank), sql.SQLStr(assignedBy or ""), os.time(),
        sql.SQLStr(rank), sql.SQLStr(assignedBy or ""), os.time()
    )
    sql.Query(query)
end

-- ============================================================
-- Публичные функции
-- ============================================================
function RANKS.GetRank(ply)
    if not IsValid(ply) then return "user" end
    return ply:GetNWString("kdm1e_Rank", "user")
end

function RANKS.GetLevel(rank)
    local r = RANKS.list[rank]
    if not r then return 0 end
    return r.level
end

function RANKS.HasPerm(ply, perm)
    if not IsValid(ply) then return false end
    if ply:IsSuperAdmin() then return true end

    local rank = RANKS.GetRank(ply)
    local perms = RANKS.perms[rank]
    if not perms then return false end
    return perms[perm] == true
end

function RANKS.HasRankOrHigher(ply, rank)
    if not IsValid(ply) then return false end
    local playerLevel = RANKS.GetLevel(RANKS.GetRank(ply))
    local requiredLevel = RANKS.GetLevel(rank)
    return playerLevel >= requiredLevel
end

function RANKS.SetRank(ply, rank, assignedBy)
    if not IsValid(ply) then return false end
    if not RANKS.list[rank] then return false end

    ply:SetNWString("kdm1e_Rank", rank)
    SaveRankToDB(ply:SteamID64(), rank, assignedBy or "console")

    print(string.format("[Ranks] %s -> %s (выдал: %s)", ply:Nick(), rank, assignedBy or "console"))
    return true
end

-- ============================================================
-- Загрузка ранга при заходе
-- ============================================================
hook.Add("PlayerInitialSpawn", "Ranks_kdm1eHvH_Load", function(ply)
    timer.Simple(1, function()
        if not IsValid(ply) then return end
        local steamid = ply:SteamID64()
        if not steamid then return end

        local rank = GetRankFromDB(steamid)
        ply:SetNWString("kdm1e_Rank", rank)
        print(string.format("[Ranks] %s загружен с рангом: %s", ply:Nick(), rank))
    end)
end)

-- Первый заход — user по умолчанию
hook.Add("PlayerInitialSpawn", "Ranks_kdm1eHvH_CreateDefault", function(ply)
    timer.Simple(1.5, function()
        if not IsValid(ply) then return end
        local steamid = ply:SteamID64()
        if not steamid then return end

        local query = "SELECT rank FROM player_ranks WHERE steamid = " .. sql.SQLStr(steamid)
        local result = sql.QueryRow(query, 1)
        if not result then
            SaveRankToDB(steamid, "user", "system")
            ply:SetNWString("kdm1e_Rank", "user")
        end
    end)
end)

print("[Ranks] Система рангов загружена")-- Ranks_kdm1eHvH.lua
-- Система рангов для сервера kdm1eHvH

RANKS = RANKS or {}

RANKS.list = {
    ["user"]         = { level = 0, color = Color(180, 180, 180), name = "User" },
    ["beta_tester"]  = { level = 1, color = Color(80, 220, 220),  name = "Beta Tester" },
    ["vip"]          = { level = 2, color = Color(255, 200, 50),  name = "VIP" },
    ["moderator"]    = { level = 3, color = Color(80, 150, 255),  name = "Moderator" },
    ["admin"]        = { level = 4, color = Color(255, 70, 70),   name = "Admin" },
    ["senior_admin"] = { level = 5, color = Color(200, 30, 30),   name = "Senior Admin" },
    ["manager"]      = { level = 6, color = Color(180, 80, 255),  name = "Manager" },
    ["superadmin"]   = { level = 7, color = Color(255, 130, 0),   name = "Superadmin" },
}

RANKS.order = { "user", "beta_tester", "vip", "moderator", "admin", "senior_admin", "manager", "superadmin" }

RANKS.perms = {
    ["user"]         = { canTakeWeapons = true },
    ["beta_tester"]  = { canTakeWeapons = true, canSeeLogs = true },
    ["vip"]          = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true },
    ["moderator"]    = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true },
    ["admin"]        = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true, canBan = true, canSlay = true, canGod = true, canNoclip = true },
    ["senior_admin"] = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true, canBan = true, canSlay = true, canGod = true, canNoclip = true, canUnban = true, canManageMods = true },
    ["manager"]      = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true, canBan = true, canSlay = true, canGod = true, canNoclip = true, canUnban = true, canManageMods = true },
    ["superadmin"]   = { canTakeWeapons = true, canSeeLogs = true, canUseVIP = true, canKick = true, canMute = true, canWarn = true, canJail = true, canBan = true, canSlay = true, canGod = true, canNoclip = true, canUnban = true, canManageMods = true, canSetRank = true, canEverything = true },
}

-- ============================================================
-- База данных
-- ============================================================
local function InitDB()
    if sql.TableExists("player_ranks") then return end
    sql.Query([[
        CREATE TABLE player_ranks (
            steamid TEXT PRIMARY KEY,
            rank TEXT DEFAULT 'user',
            assigned_by TEXT DEFAULT '',
            assigned_at INTEGER DEFAULT 0
        )
    ]])
    print("[Ranks] Таблица player_ranks создана")
end

InitDB()

local function GetRankFromDB(steamid)
    if not steamid then return "user" end
    local query = "SELECT rank FROM player_ranks WHERE steamid = " .. sql.SQLStr(steamid)
    local result = sql.QueryRow(query, 1)
    if result and result.rank and RANKS.list[result.rank] then
        return result.rank
    end
    return "user"
end

local function SaveRankToDB(steamid, rank, assignedBy)
    if not steamid or not rank then return end
    local query = string.format(
        "INSERT INTO player_ranks (steamid, rank, assigned_by, assigned_at) VALUES (%s, %s, %s, %d) " ..
        "ON CONFLICT(steamid) DO UPDATE SET rank = %s, assigned_by = %s, assigned_at = %d",
        sql.SQLStr(steamid), sql.SQLStr(rank), sql.SQLStr(assignedBy or ""), os.time(),
        sql.SQLStr(rank), sql.SQLStr(assignedBy or ""), os.time()
    )
    sql.Query(query)
end

-- ============================================================
-- Публичные функции
-- ============================================================
function RANKS.GetRank(ply)
    if not IsValid(ply) then return "user" end
    return ply:GetNWString("kdm1e_Rank", "user")
end

function RANKS.GetLevel(rank)
    local r = RANKS.list[rank]
    if not r then return 0 end
    return r.level
end

function RANKS.HasPerm(ply, perm)
    if not IsValid(ply) then return false end
    if ply:IsSuperAdmin() then return true end

    local rank = RANKS.GetRank(ply)
    local perms = RANKS.perms[rank]
    if not perms then return false end
    return perms[perm] == true
end

function RANKS.HasRankOrHigher(ply, rank)
    if not IsValid(ply) then return false end
    local playerLevel = RANKS.GetLevel(RANKS.GetRank(ply))
    local requiredLevel = RANKS.GetLevel(rank)
    return playerLevel >= requiredLevel
end

function RANKS.SetRank(ply, rank, assignedBy)
    if not IsValid(ply) then return false end
    if not RANKS.list[rank] then return false end

    ply:SetNWString("kdm1e_Rank", rank)
    SaveRankToDB(ply:SteamID64(), rank, assignedBy or "console")

    print(string.format("[Ranks] %s -> %s (выдал: %s)", ply:Nick(), rank, assignedBy or "console"))
    return true
end

-- ============================================================
-- Загрузка ранга при заходе
-- ============================================================
hook.Add("PlayerInitialSpawn", "Ranks_kdm1eHvH_Load", function(ply)
    timer.Simple(1, function()
        if not IsValid(ply) then return end
        local steamid = ply:SteamID64()
        if not steamid then return end

        local rank = GetRankFromDB(steamid)
        ply:SetNWString("kdm1e_Rank", rank)
        print(string.format("[Ranks] %s загружен с рангом: %s", ply:Nick(), rank))
    end)
end)

-- Первый заход — user по умолчанию
hook.Add("PlayerInitialSpawn", "Ranks_kdm1eHvH_CreateDefault", function(ply)
    timer.Simple(1.5, function()
        if not IsValid(ply) then return end
        local steamid = ply:SteamID64()
        if not steamid then return end

        local query = "SELECT rank FROM player_ranks WHERE steamid = " .. sql.SQLStr(steamid)
        local result = sql.QueryRow(query, 1)
        if not result then
            SaveRankToDB(steamid, "user", "system")
            ply:SetNWString("kdm1e_Rank", "user")
        end
    end)
end)

print("[Ranks] Система рангов загружена")