casing, RegisterModule adjustments, remove duplicate utilities table

This commit is contained in:
MrNewb committed 2025-01-10 16:36:20 -05:00
1 parent 515d4412b1
commit 733271562d
75 files changed
+1739 -14

No files matched your search

+145
View File
@@ -0,0 +1,145 @@
Callbacks = {}
cbData = {}
Callback = Callback or {}
local resource = GetCurrentResourceName() or 'unknown'
local clientToServerName = resource .. ':server:CS:TriggerCallback'
local clientToServerBackToClientName = resource .. ':client:CSC:TriggerCallback'
local serverToClientName = resource .. ':client:SC:TriggerCallback'
local serverToClientBackToServerName = resource .. ':server:SCS:TriggerCallback'
if not IsDuplicityVersion() then goto client end
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ ██▀ █▀▄ █ █ ██▀ █▀▄
-- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄
-- -- -- -- -- -- -- -- -- --
-- client to server rebound
function Callback.Register(name, callback, all)
Callback[name] = {
callback = callback,
toAll = all
}
end
RegisterNetEvent(clientToServerName, function(name, ...)
local src = source
local data = Callback[name]
if not data then return end
local func = data.callback
if not func then return end
local toAll = data.toAll and -1 or src
local cb = function(...)
TriggerClientEvent(clientToServerBackToClientName, toAll, name, ...)
end
func(src, cb, ...)
end)
-----------------------------
-- server to client rebound
function Callback.Trigger(name, src, func, ...)
if src == nil then src = -1 end
local p = promise.new()
cbData[name] = {cb = func, p = p}
if type(src) == 'table' then
for i, d in pairs(src) do
TriggerClientEvent(serverToClientName, tonumber(d), name, ...)
end
return Citizen.Await(p)
end
TriggerClientEvent(serverToClientName, tonumber(src), name, ...)
return Citizen.Await(p)
end
RegisterNetEvent(serverToClientBackToServerName, function(name, ...)
print('Rebound')
local data = cbData[name]
if not data then return end
local func = data.cb
if not func then return end
data.p:resolve(func(...) or true)
cbData[name] = nil
end)
-----------------------------
exports('RegisterCallback', Callback.Register)
exports('TriggerCallback', Callback.Trigger)
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀
-- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █
-- -- -- -- -- -- -- -- -- --
if IsDuplicityVersion() then return end
::client::
cbRebounds = {}
function Callback.Register(name, callback, all)
Callback[name] = {
callback = callback,
}
end
function Callback.RegisterRebound(name, callback)
cbRebounds[name] = callback
end
-- client to server rebound
function Callback.Trigger(name, func, ...)
local p = promise.new()
TriggerServerEvent(clientToServerName, name, ...)
cbData[name] = {cb = func, p = p}
CreateThread(function()
Wait(5000)
if cbData[name] then
cbData[name].p:resolve()
cbData[name] = nil
end
end)
print('Triggered')
return Citizen.Await(p)
end
RegisterNetEvent(clientToServerBackToClientName, function(name, ...)
local clientRebound = cbRebounds[name]
if clientRebound then
clientRebound(...)
end
local data = cbData[name]
if not data then return end
local func = data.cb
if not func then return end
data.p:resolve(func(...) or true)
cbData[name] = nil
end)
-----------------------------
-- server to client rebound
RegisterNetEvent(serverToClientName, function(name, ...)
local data = Callback[name]
if not data then return end
local func = data.callback
if not func then return end
local cb = function(...)
TriggerServerEvent(serverToClientBackToServerName, name, ...)
end
func(cb, ...)
end)
-----------------------------
exports('RegisterCallback', Callback.Register)
exports('RegisterRebound', Callback.RegisterRebound)
exports('TriggerCallback', Callback.Trigger)
+24
View File
@@ -0,0 +1,24 @@
Prints = Prints or {}
local function printMessage(level, color, message)
if type(message) == 'table' then
message = json.encode(message)
end
print(color .. '[' .. level .. ']:', message)
end
Prints.Info = function(message)
printMessage('INFO', '^5', message)
end
Prints.Warn = function(message)
printMessage('WARN', '^3', message)
end
Prints.Error = function(message)
printMessage('ERROR', '^1', message)
end
Prints.Debug = function(message)
printMessage('DEBUG', '^2', message)
end
+108
View File
@@ -0,0 +1,108 @@
Table = {}
Table.CheckPopulated = function(tbl)
if #tbl == 0 then
for _, _ in pairs(tbl) do
return true
end
return false
end
return true
end
Table.DeepClone = function(tbl, out, omit)
if type(tbl) ~= "table" then return tbl end
local new = out or {}
omit = omit or {}
for key, data in pairs(tbl) do
if not omit[key] then
if type(data) == "table" then
new[key] = Table.DeepClone(data)
else
new[key] = data
end
end
end
return new
end
Table.TableContains = function(tbl, search, nested)
for _, v in pairs(tbl) do
if nested and type(v) == "table" then
return Table.TableContains(v, search)
elseif v == search then
return true, v
end
end
return false
end
Table.TableContainsKey = function(tbl, search)
for k, _ in pairs(tbl) do
if k == search then
return true, k
end
end
return false
end
Table.GetClosest = function(coords, tbl)
local closestPoint = nil
local dist = math.huge
for k, v in pairs(tbl) do
local c = v.coords
local d = c and #(coords - c)
if d < dist then
dist = d
closestPoint = v
end
end
return closestPoint
end
Table.FindFirstUnoccupiedSlot = function(tbl)
local occupiedSlots = {}
for _, v in pairs(tbl) do
occupiedSlots[v.slot] = true
end
for i = 1, BridgeServerConfig.MaxInventorySlots do
if not occupiedSlots[i] then
return i
end
end
return nil
end
Table.Append = function(tbl1, tbl2)
for _, v in pairs(tbl2) do
table.insert(tbl1, v)
end
return tbl1
end
Table.Split = function(tbl, size)
local new1 = {}
local new2 = {}
size = size or math.floor(#tbl / 2)
if size > #tbl then
assert(false, "Size is greater than the length of the table.")
end
for i = 1, size do
table.insert(new1, tbl[i])
end
for i = size + 1, #tbl do
table.insert(new2, tbl[i])
end
return new1, new2
end
Table.Shuffle = function(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
return Table
+25
View File
@@ -0,0 +1,25 @@
Utility = Utility or {}
-- for unique ids
Utility.CreateUniqueId = function(tbl, len, pattern) -- both optional
tbl = tbl or {} -- table to check uniqueness. Ids to check against must be the key to the tables value
len = len or 8
local id = ""
for i = 1, len do
local char = ""
if pattern then
char = pattern:sub(math.random(1, #pattern), math.random(1, #pattern))
else
char = math.random(1, 2) == 1 and string.char(math.random(65, 90)) or math.random(0, 9) -- CAP letter and number
end
id = id .. char
end
if tbl[id] then
return Utility.CreateUniqueId(tbl, len, pattern)
end
return id
end
Utility.RandomString = function()
return Utility.CreateUniqueId(nil, 14, nil)
end