-
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.
Merge pull request #719 from bitpredator/dev
convert: esx_datastore > bpt_datastore
- Loading branch information
Showing
14 changed files
with
217 additions
and
237 deletions.
There are no files selected for viewing
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
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
19 changes: 19 additions & 0 deletions
19
server-data/resources/[bpt_addons]/bpt_datastore/README.md
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<h1 align='center'>bpt_datastore</a></h1> | ||
<p align='center'><a href='https://discord.gg/ksGfNvDEfq'>Discord</a> | ||
|
||
Copyright (C) 2024 bitpredator | ||
|
||
This program Is free software: you can redistribute it And/Or modify it under the terms Of the GNU General Public License As published by the Free Software Foundation, either version 3 Of the License, Or (at your option) any later version. | ||
|
||
This program Is distributed In the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty Of MERCHANTABILITY Or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License For more details. | ||
|
||
ATTENTION: | ||
You are not authorized to change the name of the resource and the resources within it. | ||
|
||
If you want to contribute you can open a pull request. | ||
|
||
You are not authorized to sell this software (this is free project). | ||
|
||
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. |
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
16 changes: 16 additions & 0 deletions
16
server-data/resources/[bpt_addons]/bpt_datastore/fxmanifest.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
fx_version("adamant") | ||
|
||
game("gta5") | ||
|
||
description("Used for storing Data, such as society inventories") | ||
|
||
version("1.0.0") | ||
|
||
lua54("yes") | ||
|
||
server_scripts({ | ||
"@es_extended/imports.lua", | ||
"@oxmysql/lib/MySQL.lua", | ||
"server/classes/datastore.lua", | ||
"server/main.lua", | ||
}) |
166 changes: 85 additions & 81 deletions
166
...sx_datastore/server/classes/datastore.lua → ...pt_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,81 +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 | ||
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 | ||
end | ||
function stringsplit(inputstr, sep) | ||
if sep == nil then | ||
sep = "%s" | ||
end | ||
|
||
local t = {} | ||
local 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 _ = 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 _ = 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 |
80 changes: 80 additions & 0 deletions
80
server-data/resources/[bpt_addons]/bpt_datastore/server/main.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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
local DataStores, DataStoresIndex, SharedDataStores = {}, {}, {} | ||
|
||
AddEventHandler("onResourceStart", function(resourceName) | ||
if resourceName == GetCurrentResourceName() then | ||
local dataStore = MySQL.query.await("SELECT * FROM datastore_data LEFT JOIN datastore ON datastore_data.name = datastore.name UNION SELECT * FROM datastore_data RIGHT JOIN datastore ON datastore_data.name = datastore.name") | ||
|
||
local newData = {} | ||
for i = 1, #dataStore do | ||
local data = dataStore[i] | ||
if data.shared == 0 then | ||
if not DataStores[data.name] then | ||
DataStoresIndex[#DataStoresIndex + 1] = data.name | ||
DataStores[data.name] = {} | ||
end | ||
DataStores[data.name][#DataStores[data.name] + 1] = CreateDataStore(data.name, data.owner, json.decode(data.data)) | ||
else | ||
if data.data then | ||
SharedDataStores[data.name] = CreateDataStore(data.name, nil, json.decode(data.data)) | ||
else | ||
newData[#newData + 1] = { data.name, "'{}'" } | ||
end | ||
end | ||
end | ||
|
||
if next(newData) then | ||
MySQL.prepare("INSERT INTO datastore_data (name, data) VALUES (?, ?)", newData) | ||
for i = 1, #newData do | ||
local new = newData[i] | ||
SharedDataStores[new[1]] = CreateDataStore(new[1], nil, {}) | ||
end | ||
end | ||
end | ||
end) | ||
|
||
function GetDataStore(name, owner) | ||
for i = 1, #DataStores[name], 1 do | ||
if DataStores[name][i].owner == owner then | ||
return DataStores[name][i] | ||
end | ||
end | ||
end | ||
|
||
function GetDataStoreOwners(name) | ||
local identifiers = {} | ||
|
||
for i = 1, #DataStores[name], 1 do | ||
table.insert(identifiers, DataStores[name][i].owner) | ||
end | ||
|
||
return identifiers | ||
end | ||
|
||
function GetSharedDataStore(name) | ||
return SharedDataStores[name] | ||
end | ||
|
||
AddEventHandler("bpt_datastore:getDataStore", function(name, owner, cb) | ||
cb(GetDataStore(name, owner)) | ||
end) | ||
|
||
AddEventHandler("bpt_datastore:getDataStoreOwners", function(name, cb) | ||
cb(GetDataStoreOwners(name)) | ||
end) | ||
|
||
AddEventHandler("bpt_datastore:getSharedDataStore", function(name, cb) | ||
cb(GetSharedDataStore(name)) | ||
end) | ||
|
||
AddEventHandler("esx:playerLoaded", function(playerId, xPlayer) | ||
for i = 1, #DataStoresIndex, 1 do | ||
local name = DataStoresIndex[i] | ||
local dataStore = GetDataStore(name, xPlayer.identifier) | ||
|
||
if not dataStore then | ||
MySQL.insert("INSERT INTO datastore_data (name, owner, data) VALUES (?, ?, ?)", { name, xPlayer.identifier, "{}" }) | ||
|
||
DataStores[name][#DataStores[name] + 1] = CreateDataStore(name, xPlayer.identifier, {}) | ||
end | ||
end | ||
end) |
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
Oops, something went wrong.