-- Kills_kdm1eHvH.lua
-- Положи в lua/autorun/server/Kills_kdm1eHvH.lua
local function InitDB()
if sql.TableExists("player_kills") then return end
sql.Query([[
CREATE TABLE player_kills (
steamid TEXT PRIMARY KEY,
kills INTEGER DEFAULT 0,
deaths INTEGER DEFAULT 0
)
]])
end
InitDB()
local function LoadKills(ply)
local sid64 = ply:SteamID64()
if not sid64 then return 0 end
local query = "SELECT kills, deaths FROM player_kills WHERE steamid = " .. sql.SQLStr(sid64)
local result = sql.QueryRow(query, 1)
local kills, deaths = 0, 0
if result then
kills = tonumber(result.kills) or 0
deaths = tonumber(result.deaths) or 0
else
sql.Query("INSERT INTO player_kills (steamid, kills, deaths) VALUES (" ..
sql.SQLStr(sid64) .. ", 0, 0)")
end
ply:SetFrags(kills)
ply:SetDeaths(deaths)
ply:SetNWInt("PersistentKills", kills)
ply:SetNWInt("PersistentDeaths", deaths)
end
local function SaveKills(ply)
if not IsValid(ply) then return end
local sid64 = ply:SteamID64()
if not sid64 then return end
local kills = ply:Frags()
local deaths = ply:Deaths()
ply:SetNWInt("PersistentKills", kills)
ply:SetNWInt("PersistentDeaths", deaths)
local query = string.format(
"INSERT INTO player_kills (steamid, kills, deaths) VALUES (%s, %d, %d) " ..
"ON CONFLICT(steamid) DO UPDATE SET kills = %d, deaths = %d",
sql.SQLStr(sid64), kills, deaths, kills, deaths
)
sql.Query(query)
end
hook.Add("PlayerInitialSpawn", "PersistentKills_Load", function(ply)
timer.Simple(1, function()
if IsValid(ply) then LoadKills(ply) end
end)
end)
hook.Add("PlayerDisconnected", "PersistentKills_Save", function(ply)
SaveKills(ply)
end)
timer.Create("PersistentKills_AutoSave", 60, 0, function()
for _, ply in ipairs(player.GetAll()) do
if IsValid(ply) then SaveKills(ply) end
end
end)
hook.Add("PlayerDeath", "PersistentKills_OnKill", function(victim, inflictor, attacker)
if IsValid(attacker) and attacker:IsPlayer() and attacker ~= victim then
timer.Simple(0, function()
if IsValid(attacker) then SaveKills(attacker) end
end)
end
if IsValid(victim) and victim:IsPlayer() then
timer.Simple(0, function()
if IsValid(victim) then SaveKills(victim) end
end)
end
end)
concommand.Add("resetkills", function(ply, cmd, args)
if IsValid(ply) and not ply:IsAdmin() then return end
local target = ply
if IsValid(ply) and args[1] then
target = player.GetBySteamID64(args[1])
end
if not IsValid(target) then
if IsValid(ply) then ply:PrintMessage(HUD_PRINTTALK, "Игрок не найден") end
return
end
sql.Query("UPDATE player_kills SET kills = 0, deaths = 0 WHERE steamid = " ..
sql.SQLStr(target:SteamID64()))
target:SetFrags(0)
target:SetDeaths(0)
target:SetNWInt("PersistentKills", 0)
target:SetNWInt("PersistentDeaths", 0)
if IsValid(ply) then
ply:PrintMessage(HUD_PRINTTALK, "Статистика сброшена: " .. target:Nick())
end
end, nil, "Сбросить киллы игрока (аргумент: SteamID64)")