From fc2bb50939379289762b561d0f2756f9e915f920 Mon Sep 17 00:00:00 2001 From: gononono64 Date: Thu, 9 Jan 2025 09:40:40 -0600 Subject: [PATCH] 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 --- modules/utilities/shared/callbacks.lua | 145 +++++++++++++++++++++++++ {use => modules/utilities}/tables.lua | 0 unit_tests/callbacks_test.lua | 44 ++++++++ 3 files changed, 189 insertions(+) create mode 100644 modules/utilities/shared/callbacks.lua rename {use => modules/utilities}/tables.lua (100%) create mode 100644 unit_tests/callbacks_test.lua diff --git a/modules/utilities/shared/callbacks.lua b/modules/utilities/shared/callbacks.lua new file mode 100644 index 0000000..72977c9 --- /dev/null +++ b/modules/utilities/shared/callbacks.lua @@ -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) + diff --git a/use/tables.lua b/modules/utilities/tables.lua similarity index 100% rename from use/tables.lua rename to modules/utilities/tables.lua diff --git a/unit_tests/callbacks_test.lua b/unit_tests/callbacks_test.lua new file mode 100644 index 0000000..de88fa2 --- /dev/null +++ b/unit_tests/callbacks_test.lua @@ -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)