Callbacks update

Callbacks:
Client to server to client
-> has ability to trigger on all clients

Server to client to server

Unit test

Moved tables from use to utilities
This commit is contained in:
gononono64 committed 2025-01-09 09:40:40 -06:00
1 parent 52af741ed8
commit fc2bb50939
3 files changed
+189

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)
File renamed without changes.
+44
View File
@@ -0,0 +1,44 @@
-- ▄▀▀ ▄▀▄ █ █ ██▄ ▄▀▄ ▄▀▀ █▄▀ ▄▀▀ ▄▀▀ █▄█ ▄▀▄ █▀▄ ██▀ █▀▄ █ █ █▄ █ █ ▀█▀ ▀█▀ ██▀ ▄▀▀ ▀█▀
-- ▀▄▄ █▀█ █▄▄ █▄▄ █▄█ █▀█ ▀▄▄ █ █ ▄█▀ ▄█▀ █ █ █▀█ █▀▄ █▄▄ █▄▀ ▀▄█ █ ▀█ █ █ █ █▄▄ ▄█▀ █
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ ██▀ █▀▄ █ █ ██▀ █▀▄
-- ▄█▀ █▄▄ █▀▄ ▀▄▀ █▄▄ █▀▄
-- -- -- -- -- -- -- -- -- --
if not IsDuplicityVersion() then goto client end
exports.community_bridge:RegisterCallback('ClientToServerToClient', function(src, cb, ftpcsftug)
cb(ftpcsftug, 'some random string')
end)
RegisterCommand('cb_server_test', function(source)
local someData = exports.community_bridge:TriggerCallback('ServerToClientToServer', source, function(ftpcsftug, derpa)
print(ftpcsftug, derpa)
return "This data gets passed up the chain"
end,"herpaderpa")
print("print", someData)
end)
-- -- -- -- -- -- -- -- -- --
-- ▄▀▀ █ █ ██▀ █▄ █ ▀█▀
-- ▀▄▄ █▄▄ █ █▄▄ █ ▀█ █
-- -- -- -- -- -- -- -- -- --
if IsDuplicityVersion() then return end
::client::
RegisterCommand('cb_client_test', function()
local someData = exports.community_bridge:TriggerCallback('ClientToServerToClient', function(ftpcsftug, derpa)
print(ftpcsftug, derpa, "This will fire on the player who triggered the event")
end,"herpaderpa")
print(someData)
end)
exports.community_bridge:RegisterRebound('ClientToServerToClient', function(src, cb, ftpcsftug)
print("this is a rebound from another player or himself")
end)
exports.community_bridge:RegisterCallback('ServerToClientToServer', function(cb, ftpcsftug)
cb(ftpcsftug, 'TestyMcTesterton')
end)