local Save = {};
function isAccountExists( Account )
local DB = dbPoll(dbQuery(TestDB, "SELECT * FROM test WHERE PlayerAccount = ?", Account), -1);
if type(DB) == "table" and #DB ~= 0 and tostring(DB[1]["PlayerAccount"]) == tostring(Account) then
return true
end
end
addEventHandler( "onResourceStart", resourceRoot,
function ()
TestDB = dbConnect("sqlite","test.db");
dbExec(TestDB, "CREATE TABLE IF NOT EXISTS test ( PlayerAccount, parked )" );
for k,v in ipairs(getElementsByType("player")) do
if not isGuestAccount(getPlayerAccount(v)) then
local row = dbPoll(dbQuery(TestDB, "SELECT * FROM test WHERE PlayerAccount = ?", getAccountName(getPlayerAccount(v))), -1);
if not isAccountExists(getAccountName(getPlayerAccount(v))) then
dbExec(TestDB, "INSERT INTO test ( PlayerAccount, parked ) VALUES( ?, ? )", getAccountName(getPlayerAccount(v)), "0");
else
for index, value in ipairs(row) do
if not (Save[v]) then
Save[v] = {["data"] = false};
end
Save[v]["data"] = value["parked"]
end
end
end
end
end );
addEventHandler("onPlayerQuit", root,
function ()
local playeraccount = getPlayerAccount(source);
if playeraccount and not isGuestAccount(playeraccount) then
local data = Save[source]["data"] or false
local row = dbPoll (dbQuery(TestDB, "SELECT * FROM test WHERE PlayerAccount = ?", getAccountName(playeraccount) ), -1)
if isAccountExists(getAccountName(playeraccount)) then
dbExec(TestDB, "UPDATE test SET parked = ? WHERE PlayerAccount = ?", tostring(Save[source]["data"]), getAccountName(playeraccount));
end
end
Save[source] = nil
end );
addEventHandler("onPlayerLogin", root,
function ( _, playeraccount)
if playeraccount then
local time = dbPoll ( dbQuery(TestDB, "SELECT * FROM test WHERE PlayerAccount = ?", getAccountName(playeraccount) ), -1);
if isAccountExists(getAccountName(playeraccount)) then
for index, value in ipairs(time) do
setElementData (source,"parked", value["parked"] );
Save[source]["data"] = value["parked"];
end
else
setElementData (source, "parked",false )
dbExec(TestDB, "INSERT INTO test ( PlayerAccount, parked ) VALUES( ?, ? )", getAccountName(playeraccount), "0");
Save[source]["data"] = false
end
end
end );
addEventHandler("onResourceStop", resourceRoot,
function ()
for k,v in ipairs(getElementsByType("player")) do
if not isGuestAccount(getPlayerAccount(v)) then
local data = Save[v]["data"] or false
if isAccountExists(getAccountName(getPlayerAccount(v))) then
dbExec(TestDB, "UPDATE test SET parked = ? WHERE PlayerAccount = ?", data, getAccountName(getPlayerAccount(v)) )
end
end
end
end );
Author : MoDeR2014
#BrosS : تخريب