From bde14af3529c856dd3836387c0d9b66cc5f0acb3 Mon Sep 17 00:00:00 2001 From: Zoo <34508112+FjamZoo@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:57:42 +0200 Subject: [PATCH] Feat: triggerGroupEvent (#4) --- server/api.lua | 26 +++++++++++++++++++------- server/groups.lua | 12 ++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/server/api.lua b/server/api.lua index d633ba4..b611659 100644 --- a/server/api.lua +++ b/server/api.lua @@ -34,13 +34,25 @@ function api.pNotifyGroup(groupID, header, msg) return lib.print.error('pNotifyGroup was sent an invalid groupID :'..groupID) end - lib.triggerClientEvent('slrn_groups:client:CustomNotification', group:getGroupMembers(), - header or 'NO HEADER', - msg or 'NO MSG' - ) + group:triggerGroupEvent('slrn_groups:client:CustomNotification', header or 'NO HEADER', msg or 'NO MSG') end utils.exportHandler('pNotifyGroup', api.pNotifyGroup) +---Triggers a client event for each member of a group (stolen from ox_lib) +---@param eventName string +---@param groupId number +---@param ... any +function api.triggerGroupEvent(eventName, groupId, ...) + local group = groups[groupId] + + if not group then + return lib.print.error('triggerGroupEvent was sent an invalid groupID :'..groupId) + end + + group:triggerGroupEvent(eventName, ...) +end +utils.exportHandler('triggerGroupEvent', api.triggerGroupEvent) + ---@class BlipData ---@field entity number? ---@field netId number? @@ -65,7 +77,7 @@ function api.CreateBlipForGroup(groupID, name, data) return lib.print.error('CreateBlipForGroup was sent an invalid groupID :'..groupID) end - lib.triggerClientEvent('groups:createBlip', group:getGroupMembers(), name, data) + group:triggerGroupEvent('groups:createBlip', name, data) end utils.exportHandler('CreateBlipForGroup', api.CreateBlipForGroup) @@ -79,7 +91,7 @@ function api.RemoveBlipForGroup(groupID, name) return lib.print.error('RemoveBlipForGroup was sent an invalid groupID :'..groupID) end - lib.triggerClientEvent('slrn_groups:client:RemoveBlipForGroup', group:getGroupMembers(), name) + group:triggerGroupEvent('groups:removeBlip', name) end utils.exportHandler('RemoveBlipForGroup', api.RemoveBlipForGroup) @@ -260,7 +272,7 @@ function api.setJobStatus(groupID, status, stages) group.status = status group.stage = stages - lib.triggerClientEvent('slrn_groups:client:updateGroupStage', group:getGroupMembers(), status, stages) + group:triggerGroupEvent('slrn_groups:client:updateGroupStage', status, stages) end utils.exportHandler('setJobStatus', api.setJobStatus) diff --git a/server/groups.lua b/server/groups.lua index f756be4..2425e98 100644 --- a/server/groups.lua +++ b/server/groups.lua @@ -92,6 +92,18 @@ end +---Triggers an event for all members of the group. (Stolen from ox_lib so thanks to the original author) +---@param eventName string +---@param ... any +function groups:triggerGroupEvent(eventName, ...) + local payload = msgpack.pack_args(...) + local payloadLen = #payload + + for i = 1, #self.members do + TriggerClientEventInternal(eventName, self.members[i].Player --[[@as string]], payload, payloadLen) + end +end +