server
unknown
lua
2 years ago
5.2 kB
1
Indexable
Never
------------------------------- Config = { KillData = "Zombie kills", LevelData = "Level", DeathsData = "deaths", Top = 100 }; ------------------------------- function getTable() local DB = dbPoll(WilliemDB:query("SELECT * FROM dataPlayersss ORDER BY kills, level, deaths DESC LIMIT 100"..Config["Top"]), -1); if #DB > 0 then return DB else return false end end ------------------------------- function getSortedTables() local SortedTable = {} local tablee = getTable() if tablee then for index, value in ipairs(tablee) do local acc = getAccount(value["acc"]) table.insert(SortedTable, { name = value["player"], acc = value["acc"], kills = value["kills"], level = value["level"], deaths = value["deaths"], status = acc and getAccountPlayer(acc) and "Online" or "Offline" }); end return SortedTable; end end ------------------------------- function databaseplayers(player) if not isGuestAccount(getPlayerAccount(player)) then local acc = getAccountName(getPlayerAccount(player)) local q = dbPoll(WilliemDB:query("SELECT * FROM dataPlayersss WHERE acc = ?", acc), -1) if q and #q > 0 then WilliemDB:exec("UPDATE dataPlayersss SET level = ?, kills = ?, player = ? WHERE acc = ?", getElementData(player,Config["LevelData"]) or 0, getElementData(player,Config["KillData"]) or 0, getPlayerName(player), acc); else WilliemDB:exec("INSERT INTO dataPlayersss VALUES(?, ?, ?, 0, 0)", getPlayerName(player), acc, getElementData(player,Config["KillData"]) or 0, getElementData(player,Config["LevelData"]) or 0, getElementData(player,Config["DeathsData"]) or 0); end end end ------------------------------- function loadDataPlayer(player) local a = dbPoll(WilliemDB:query("SELECT * FROM dataPlayersss WHERE acc = ?", acc), -1) if (#a > 0) then setElementData(player, Config["KillData"], a[1].kills) setElementData(player, Config["LevelData"], a[1].level) setElementData(player, Config["DeathsData"], a[1].deaths) return true end return false end ------------------------------- addEvent("top:ranks1", true) addEventHandler("top:ranks1", root, function() if source == client then databaseplayers(source); triggerClientEvent(client, "top:ranks3", client, getSortedTables(),Config["Top"]); end end); ------------------------------- addEvent("top:ranks2", true) addEventHandler("top:ranks2", root, function() if source == client then if not isGuestAccount(getPlayerAccount(client)) then databaseplayers(source); end end end); ------------------------------- addEventHandler("onResourceStart", resourceRoot, function() WilliemDB = Connection.create("sqlite","databases.db") WilliemDB:exec("CREATE TABLE IF NOT EXISTS dataPlayersss (player TEXT, acc TEXT, kills INTEGER DEFAULT 0, level INTEGER DEFAULT 0, deaths INTEGER DEFAULT 0)"); local player = getElementsByType("player") for i=1, #player do loadDataPlayer(player[i]) end end); ------------------------------- addEventHandler ( 'onPlayerLogin', getRootElement ( ), function ( _, theCurrentAccount ) local acc = getAccountName(theCurrentAccount) triggerClientEvent(getLoginPlayers(), "setOnline", resourceRoot, acc) end ) addEventHandler ( 'onPlayerLogout', getRootElement ( ), function ( prevAcc, theCurrentAccount ) local acc = getAccountName(prevAcc) triggerClientEvent(getLoginPlayers(), "setOffline", resourceRoot, acc) end ) function getLoginPlayers() local loginPlayers = {} for _,player in ipairs(getElementsByType("player")) do if not isGuestAccount(getPlayerAccount(player)) then table.insert(loginPlayers,player) end end return loginPlayers end ------------------------------- addEventHandler("onPlayerWasted", root, function(_,killer,wep,part) if killer and killer ~= source then if getElementType(killer) == "player" then local killerAccount = getPlayerAccount(killer) if not isGuestAccount(killerAccount) then local killerName = getAccountName(killerAccount) WilliemDB:exec("UPDATE dataPlayersss SET kills = kills + 1 WHERE acc = ?",killerName) end end local sourceAccount = getPlayerAccount(source) if not isGuestAccount(sourceAccount) then local sourceName = getAccountName(sourceAccount) WilliemDB:exec("UPDATE dataPlayersss SET deaths = deaths + 1 WHERE acc = ?",sourceName) end end end) ------------------------------- addEventHandler("onElementDataChange", root, function(key, old, new) if (getElementType(source) == "player") then if (key == Config["KillData"] or key == Config["LevelData"]) then local new = tonumber(new); if (not new or not old) then return false end databaseplayers(source); end end end); -------------------------------