-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: bpt_datastore\server\classes\datastore.lua 🎨 Run formatter
- Loading branch information
1 parent
cadedf4
commit 13a5168
Showing
1 changed file
with
80 additions
and
77 deletions.
There are no files selected for viewing
157 changes: 80 additions & 77 deletions
157
server-data/resources/[bpt_addons]/bpt_datastore/server/classes/datastore.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,85 @@ | ||
function stringsplit(inputstr, sep) | ||
if sep == nil then | ||
sep = "%s" | ||
end | ||
|
||
local t = {} | ||
i = 1 | ||
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do | ||
t[i] = str | ||
i = i + 1 | ||
end | ||
|
||
return t | ||
if sep == nil then | ||
sep = "%s" | ||
end | ||
|
||
local t = {} | ||
i = 1 | ||
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do | ||
t[i] = str | ||
i = i + 1 | ||
end | ||
|
||
return t | ||
end | ||
|
||
function CreateDataStore(name, owner, data) | ||
local self = {} | ||
|
||
self.name = name | ||
self.owner = owner | ||
self.data = type(data) == "string" and json.decode(data) or data | ||
|
||
local timeoutCallbacks = {} | ||
|
||
function self.set(key, val) | ||
data[key] = val | ||
self.save() | ||
end | ||
|
||
function self.get(key, i) | ||
local path = stringsplit(key, ".") | ||
local obj = self.data | ||
|
||
for i = 1, #path, 1 do | ||
obj = obj[path[i]] | ||
end | ||
|
||
if i == nil then | ||
return obj | ||
else | ||
return obj[i] | ||
end | ||
end | ||
|
||
function self.count(key, i) | ||
local path = stringsplit(key, ".") | ||
local obj = self.data | ||
|
||
for i = 1, #path, 1 do | ||
obj = obj[path[i]] | ||
end | ||
|
||
if i ~= nil then | ||
obj = obj[i] | ||
end | ||
|
||
if obj == nil then | ||
return 0 | ||
else | ||
return #obj | ||
end | ||
end | ||
|
||
function self.save() | ||
for i = 1, #timeoutCallbacks, 1 do | ||
ESX.ClearTimeout(timeoutCallbacks[i]) | ||
timeoutCallbacks[i] = nil | ||
end | ||
|
||
local timeoutCallback = ESX.SetTimeout(10000, function() | ||
if self.owner == nil then | ||
MySQL.update("UPDATE datastore_data SET data = ? WHERE name = ?", { json.encode(self.data), self.name }) | ||
else | ||
MySQL.update("UPDATE datastore_data SET data = ? WHERE name = ? and owner = ?", { json.encode(self.data), self.name, self.owner }) | ||
end | ||
end) | ||
|
||
table.insert(timeoutCallbacks, timeoutCallback) | ||
end | ||
|
||
return self | ||
local self = {} | ||
|
||
self.name = name | ||
self.owner = owner | ||
self.data = type(data) == "string" and json.decode(data) or data | ||
|
||
local timeoutCallbacks = {} | ||
|
||
function self.set(key, val) | ||
data[key] = val | ||
self.save() | ||
end | ||
|
||
function self.get(key, i) | ||
local path = stringsplit(key, ".") | ||
local obj = self.data | ||
|
||
for i = 1, #path, 1 do | ||
obj = obj[path[i]] | ||
end | ||
|
||
if i == nil then | ||
return obj | ||
else | ||
return obj[i] | ||
end | ||
end | ||
|
||
function self.count(key, i) | ||
local path = stringsplit(key, ".") | ||
local obj = self.data | ||
|
||
for i = 1, #path, 1 do | ||
obj = obj[path[i]] | ||
end | ||
|
||
if i ~= nil then | ||
obj = obj[i] | ||
end | ||
|
||
if obj == nil then | ||
return 0 | ||
else | ||
return #obj | ||
end | ||
end | ||
|
||
function self.save() | ||
for i = 1, #timeoutCallbacks, 1 do | ||
ESX.ClearTimeout(timeoutCallbacks[i]) | ||
timeoutCallbacks[i] = nil | ||
end | ||
|
||
local timeoutCallback = ESX.SetTimeout(10000, function() | ||
if self.owner == nil then | ||
MySQL.update("UPDATE datastore_data SET data = ? WHERE name = ?", { json.encode(self.data), self.name }) | ||
else | ||
MySQL.update( | ||
"UPDATE datastore_data SET data = ? WHERE name = ? and owner = ?", | ||
{ json.encode(self.data), self.name, self.owner } | ||
) | ||
end | ||
end) | ||
|
||
table.insert(timeoutCallbacks, timeoutCallback) | ||
end | ||
|
||
return self | ||
end |