Skip to content

Commit

Permalink
Merge pull request #719 from bitpredator/dev
Browse files Browse the repository at this point in the history
convert: esx_datastore > bpt_datastore
  • Loading branch information
bitpredator authored Jun 12, 2024
2 parents 49a9d1a + 9a4e266 commit ec81f71
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ end)
AddEventHandler("bpt_status:loaded", function(status)
TriggerEvent("bpt_status:registerStatus", "hunger", 1000000, "#CFAD0F", function(status)
return Config.Visible
end, function()
end, function(status)
status.remove(100)
end)

TriggerEvent("bpt_status:registerStatus", "thirst", 1000000, "#0C98F1", function()
return Config.Visible
end, function()
end, function(status)
status.remove(75)
end)
end)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Expand Down Expand Up @@ -631,8 +631,8 @@ to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

esx_datastore
Copyright (C) 2015-2022 Jérémie N'gadi
<one line to give the program's name and a brief idea of what it does.>
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
Expand All @@ -645,14 +645,14 @@ the "copyright" line and a pointer to where the full notice is found.
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/>.
along with this program. If not, see <https://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper mail.

If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:

esx_datastore Copyright (C) 2015-2022 Jérémie N'gadi
<program> Copyright (C) 2024 bitpredator
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
Expand All @@ -664,11 +664,11 @@ might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
<https://www.gnu.org/licenses/>.

The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
<https://www.gnu.org/licenses/why-not-lgpl.html>.
19 changes: 19 additions & 0 deletions server-data/resources/[bpt_addons]/bpt_datastore/README.md
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/.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ CREATE TABLE `datastore_data` (
UNIQUE INDEX `index_datastore_data_name_owner` (`name`, `owner`),
INDEX `index_datastore_data_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

16 changes: 16 additions & 0 deletions server-data/resources/[bpt_addons]/bpt_datastore/fxmanifest.lua
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",
})
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 server-data/resources/[bpt_addons]/bpt_datastore/server/main.lua
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)
6 changes: 3 additions & 3 deletions server-data/resources/[esx]/esx_society/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ AddEventHandler("esx_society:putVehicleInGarage", function(societyName, vehicle)
print(("[^3WARNING^7] Player ^5%s^7 attempted to put vehicle in non-existing society garage - ^5%s^7!"):format(source, societyName))
return
end
TriggerEvent("esx_datastore:getSharedDataStore", society.datastore, function(store)
TriggerEvent("bpt_datastore:getSharedDataStore", society.datastore, function(store)
local garage = store.get("garage") or {}
table.insert(garage, vehicle)
store.set("garage", garage)
Expand All @@ -171,7 +171,7 @@ AddEventHandler("esx_society:removeVehicleFromGarage", function(societyName, veh
print(("[^3WARNING^7] Player ^5%s^7 attempted to remove vehicle from non-existing society garage - ^5%s^7!"):format(source, societyName))
return
end
TriggerEvent("esx_datastore:getSharedDataStore", society.datastore, function(store)
TriggerEvent("bpt_datastore:getSharedDataStore", society.datastore, function(store)
local garage = store.get("garage") or {}

for i = 1, #garage, 1 do
Expand Down Expand Up @@ -398,7 +398,7 @@ ESX.RegisterServerCallback("esx_society:getVehiclesInGarage", function(_, cb, so
print(("[^3WARNING^7] Attempting To get a non-existing society - %s!"):format(societyName))
return
end
TriggerEvent("esx_datastore:getSharedDataStore", society.datastore, function(store)
TriggerEvent("bpt_datastore:getSharedDataStore", society.datastore, function(store)
local garage = store.get("garage") or {}
cb(garage)
end)
Expand Down
Loading

0 comments on commit ec81f71

Please sign in to comment.