· 8 years ago · Aug 18, 2018, 05:26 PM
1local SERVER_PORT = 22003 -- Only Save Data on Main Server
2
3local ipairs = ipairs
4local type = type
5local tonumber = tonumber
6
7-- local db = dbConnect( "mysql", "dbname=ingame;host=127.0.0.1;port=3306", "root", "24835100a")
8local db = dbConnect("sqlite", "accounts.db")
9dbExec(db, "CREATE TABLE IF NOT EXISTS `accountdata`(`id` INT NOT NULL AUTO_INCREMENT, `name` TEXT, PRIMARY KEY(id))")
10dbExec(db, "CREATE TABLE IF NOT EXISTS `inventory`(`id` INT NOT NULL AUTO_INCREMENT, `name` TEXT, PRIMARY KEY(id))")
11dbExec(db, "CREATE TABLE IF NOT EXISTS `zones`(`name` TEXT, `id` INT, `owner` TEXT, `objects` NUMBER, `ctrl` NUMBER, `radio` TEXT, `price` NUMBER )")
12
13local database_online -- Is Database Online?
14local accountData = {} -- Account Data Cache
15local inventory_online = {} -- Is Database Online?
16local inventory = {} -- Account Data Cache
17
18addEvent("onDatabaseLoad") -- Triggers when the database is ready
19addEvent("onAccountDelete") -- Triggers when an account is deleted
20
21-- Cache Account Data Database
22------------------------------->>
23
24addEventHandler("onResourceStart", resourceRoot, function()
25 dbQuery(cacheDatabase, {}, db, "SELECT * FROM `accountdata`")
26end)
27
28function cacheDatabase(qh)
29 local result = dbPoll(qh, 0)
30 accountData["Console"] = {}
31 for i,row in ipairs(result) do
32 accountData[row.name] = {}
33 for column,value in pairs(row) do
34 if (column ~= "name" or column ~= "id") then
35 if (not accountData["Console"][column]) then
36 accountData["Console"][column] = true
37 end
38 if (value == "true") then value = true end
39 if (value == "false") then value = false end
40 accountData[row.name][column] = value
41 end
42 end
43 end
44 database_online = true
45 triggerEvent("onDatabaseLoad", resourceRoot, "accountdata")
46end
47
48-- Cache Inventory Database
49---------------------------->>
50
51addEventHandler("onResourceStart", resourceRoot, function()
52 for i,player in ipairs(getElementsByType("player")) do
53 local account = getPlayerAccount(player)
54 if (not isGuestAccount(account)) then
55 local account = getAccountName(account)
56 dbQuery(cacheDatabaseByPlayer, {player}, db, "SELECT * FROM `inventory` WHERE `name`=?", account)
57 end
58 end
59end)
60
61addEventHandler("onPlayerLogin", root, function(_, account)
62 if (isGuestAccount(account)) then return false end
63 local account = getAccountName(account)
64 dbQuery(cacheDatabaseByPlayer, {source}, db, "SELECT * FROM `inventory` WHERE `name`=?", account)
65end)
66
67function cacheDatabaseByPlayer(qh, player)
68 if (not inventory["Console"]) then
69 inventory["Console"] = {}
70 end
71
72 local result = dbPoll(qh, 0)
73 local row = result[1]
74 if (not row) then
75 inventory_online[getAccountName(getPlayerAccount(player))] = true
76 return end
77
78 inventory[row.name] = {}
79 for column,value in pairs(row) do
80 if (column ~= "name" or column ~= "id") then
81 if (not inventory["Console"][column]) then
82 inventory["Console"][column] = true
83 end
84 if (value == "true") then value = true end
85 if (value == "false") then value = false end
86 inventory[row.name][column] = value
87 end
88 end
89 inventory_online[row.name] = true
90 triggerEvent("onDatabaseLoad", player, "inventory")
91end
92
93addEventHandler("onPlayerQuit", root, function()
94 local account = getPlayerAccount(source)
95 if (isGuestAccount(account)) then return end
96 local account = getAccountName(account)
97 if (inventory[account]) then
98 inventory[account] = nil
99 inventory_online[account] = nil
100 end
101end)
102
103-- Account Exports
104------------------->>
105
106function SAD(account, key, value)
107 if (not database_online) then return false end
108 if (not account or not key) then return false end
109 if (isGuestAccount(account) or type(key) ~= "string") then return false end
110
111 local account = getAccountName(account)
112
113 if (not accountData[account]) then
114 accountData[account] = {}
115 if (getServerPort() == SERVER_PORT) then
116 dbExec(db, "INSERT INTO `accountdata`(name) VALUES(?)", account)
117 end
118 end
119
120 if (accountData["Console"] and accountData["Console"][key] == nil) then
121 accountData["Console"][key] = true
122 if (getServerPort() == SERVER_PORT) then
123 dbExec(db, "ALTER TABLE `accountdata` ADD `??` text", key)
124 end
125 end
126
127 accountData[account][key] = value
128 if (getServerPort() == SERVER_PORT) then
129 if (value ~= nil) then
130 dbExec(db, "UPDATE `accountdata` SET `??`=? WHERE name=?", key, tostring(value), account)
131 else
132 dbExec(db, "UPDATE `accountdata` SET `??`=NULL WHERE name=?", key, account)
133 end
134 end
135 return true
136end
137
138function GAD(account, key)
139 if (not database_online) then return nil end
140 if (not account or not key) then return nil end
141 if (isGuestAccount(account) or type(key) ~= "string") then return nil end
142
143 local account = getAccountName(account)
144 if (accountData[account] == nil) then return nil end
145 if (accountData[account][key] == nil) then return nil end
146
147 return tonumber(accountData[account][key]) or accountData[account][key]
148end
149
150addEvent("onAccountDelete")
151addEventHandler("onAccountDelete", root, function(account)
152 dbExec(db, "DELETE FROM `accountdata` WHERE name=?", account)
153 accountData[account] = nil
154end)
155
156-- Inventory Exports
157--------------------->>
158
159function invSet(account, key, value)
160 if (not account or not key) then return false end
161 if (isGuestAccount(account) or type(key) ~= "string") then return false end
162 local account = getAccountName(account)
163 if (not inventory_online[account]) then return false end
164
165 if (not inventory[account]) then
166 inventory[account] = {}
167 if (getServerPort() == SERVER_PORT) then
168 dbExec(db, "INSERT INTO `inventory`(name) VALUES(?)", account)
169 end
170 end
171
172 if (inventory["Console"] and inventory["Console"][key] == nil) then
173 inventory["Console"][key] = true
174 if (getServerPort() == SERVER_PORT) then
175 dbExec(db, "ALTER TABLE `inventory` ADD `??` text", key)
176 end
177 end
178
179 inventory[account][key] = value
180 if (getServerPort() == SERVER_PORT) then
181 if (value ~= nil) then
182 dbExec(db, "UPDATE `inventory` SET `??`=? WHERE name=?", key, tostring(value), account)
183 else
184 dbExec(db, "UPDATE `inventory` SET `??`=NULL WHERE name=?", key, account)
185 end
186 end
187 return true
188end
189
190function invGet(account, key)
191 if (not account or not key) then return nil end
192 if (isGuestAccount(account) or type(key) ~= "string") then return nil end
193 local account = getAccountName(account)
194 if (not inventory_online[account]) then return nil end
195
196 if (inventory[account] == nil) then return nil end
197 if (inventory[account][key] == nil) then return nil end
198
199 return tonumber(inventory[account][key]) or inventory[account][key]
200end
201
202addEvent("onAccountDelete")
203addEventHandler("onAccountDelete", root, function(account)
204 dbExec(db, "DELETE FROM `accountdata` WHERE name=?", account)
205 inventory[account] = nil
206end)