· 7 years ago · Feb 07, 2019, 11:00 AM
1
2local isInSaveDB = {}
3local db = dbConnect("sqlite", "/playerdata.db")
4dbExec(db, "CREATE TABLE IF NOT EXISTS saving (account TEXT, model TEXT, x TEXT, y TEXT, z TEXT, Wanted TEXT, interior TEXT, dimension TEXT, team TEXT, rotation TEXT, money TEXT, Occupation TEXT)")
5
6function loadData(query)
7 local d = dbPoll(query, 0)
8
9 if (not d) then return end
10 for ind, data in ipairs(d) do
11 isInSaveDB[data.account] = {data.model, data.x, data.y, data.z, data.Wanted, data.interior, data.dimension, data.team, data.rotation, data.money, data.Occupation}
12 end
13end
14dbQuery(loadData, db, "SELECT * FROM saving")
15
16function setData(plr)
17
18 local account = getAccountName(getPlayerAccount(plr))
19 if (account ~= "guest") then
20
21 local team = getPlayerTeam(plr)
22 if (team) then
23 team = getTeamName(team)
24 end
25
26 local model = getElementModel(plr)
27 local Wanted = getElementData(plr,"Wanted") or 0
28 local money = getPlayerMoney(plr)
29 local x, y, z = getElementPosition(plr)
30 local rotation = getPedRotation(plr)
31 local occupation = getElementData(plr, "Occupation")
32 local interior, dimension = getElementInterior(plr), getElementDimension(plr)
33
34 if (isInSaveDB[account]) then
35 dbExec(db, "UPDATE saving SET model=?, x=?, y=?, z=?, Wanted=?, interior=?, dimension=?, team=?, rotation=?, money=?, Occupation=? WHERE account=?", tostring(model), tostring(x), tostring(y), tostring(z), tostring(wanted), tostring(interior), tostring(dimension), tostring(team), tostring(rotation), tostring(money), tostring(occupation), tostring(account))
36 isInSaveDB[account] = {model, x, y, z, Wanted, interior, dimension, team, rotation, money, occupation}
37 else
38 dbExec(db, "INSERT INTO saving (account, model, x, y, z, Wanted, interior, dimension, team, rotation, money, Occupation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", tostring(account), tostring(model), tostring(x), tostring(y), tostring(z), tostring(wanted), tostring(interior), tostring(dimension), tostring(team), tostring(rotation), tostring(money), tostring(occupation))
39 isInSaveDB[account] = {model, x, y, z, Wanted, interior, dimension, team, rotation, money, occupation}
40 end
41 end
42end
43
44function executeData()
45
46 local account = getAccountName(getPlayerAccount(source))
47
48 if (isInSaveDB[account] and account ~= "guest") then
49 local model = isInSaveDB[account][1]
50 local x, y, z = isInSaveDB[account][2], isInSaveDB[account][3], isInSaveDB[account][4]
51 local Wanted = isInSaveDB[account][5]
52 local interior = isInSaveDB[account][6]
53 local dimension = isInSaveDB[account][7]
54 local team = isInSaveDB[account][8]
55 local rotation = isInSaveDB[account][9]
56 local money = isInSaveDB[account][10]
57 local Occupation = isInSaveDB[account][11]
58
59 setPlayerMoney(source, tonumber(money))
60 setElementData(source, "Money", tonumber(money))
61 setPlayerWantedLevel(source, Wanted)
62 exports.CRteam:setPlayerTeam(source, team)
63 spawnPlayer(source, x, y, z, rot, model, int, dim)
64 setElementModel(source, model)
65 setElementData(source, "Occupation", tostring(Occupation))
66 setCameraTarget(source, source)
67 setElementInterior(source, interior)
68 setElementDimension(source, dimension)
69
70 else
71 spawnPlayer (source, 1481.0855712891, -1771.2996826172, 18.795753479004, 0,78, 0, 0)
72 exports.CRaccounts:GPM(source, 250000, "Welcome gift", true)
73 exports.CRteam:setPlayerTeam(source, "Unemployed")
74 setElementData(source, "Occupation", "Jobless")
75
76
77 local city = exports.CRmisc:formatCity(source)
78 if (lvBanned[source]) then
79
80 if (city ~= "LV") then
81 setElementPosition(source, 1606.26, 1845.99, 10.82)
82 end
83 exports.CRmisc:dm("Your ban will expire in "..lvBanned[source][2].." days", source, 255, 255, 0, "default-bold", true, 0.1)
84 end
85 end
86end
87addEventHandler("onPlayerLogin", root, executeData)
88
89function runDBSave()
90 for ind, plr in pairs(getElementsByType("player")) do
91 setData(plr)
92 end
93end
94setTimer(runDBSave, 10000, 0)