From 23594e98ab49ecd711c163f37ede6ebce353d1c3 Mon Sep 17 00:00:00 2001 From: solareon <769465+solareon@users.noreply.github.com> Date: Mon, 24 Jun 2024 14:01:00 +0200 Subject: [PATCH] feat: HasGroup export This adds a utility export that allows for a string/table of job/gang/citizenId to be passed against a function and quickly return if the condition matches. Utilizes ox_target code for group/citizenId matching. --- client/functions.lua | 69 ++++++++++++++++++++++++++++++++++++++++++ server/functions.lua | 71 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/client/functions.lua b/client/functions.lua index 7f940dde6..39c0b60c2 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -43,3 +43,72 @@ function GetPlayerData() end exports('GetPlayerData', GetPlayerData) + +--- HasPlayerGotGroup function borrowed from ox_target: https://github.com/overextended/ox_target/blob/aefc464d01da9b7aa3565e79161dd0a489945b90/client/framework/qb.lua#L41 + +-- MIT License + +-- Copyright (c) 2022 Overextended + +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: + +-- The above copyright notice and this permission notice shall be included in all +-- copies or substantial portions of the Software. + +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +-- SOFTWARE. + +---@param filter string | string[] | table +---@return boolean +function HasGroup(filter) + if not filter then return false end + local _type = type(filter) + + if _type == 'string' then + local job = QBX.PlayerData.job.name == filter + local gang = QBX.PlayerData.gang.name == filter + local citizenId = QBX.PlayerData.citizenid == filter + + if job or gang or citizenId then + return true + end + elseif _type == 'table' then + local tabletype = table.type(filter) + + if tabletype == 'hash' then + for name, grade in pairs(filter) do + local job = QBX.PlayerData.job.name == name + local gang = QBX.PlayerData.gang.name == name + local citizenId = QBX.PlayerData.citizenid == name + + if job and grade <= QBX.PlayerData.job.grade.level or gang and grade <= QBX.PlayerData.gang.grade.level or citizenId then + return true + end + end + elseif tabletype == 'array' then + for i = 1, #filter do + local name = filter[i] + local job = QBX.PlayerData.job.name == name + local gang = QBX.PlayerData.gang.name == name + local citizenId = QBX.PlayerData.citizenid == name + + if job or gang or citizenId then + return true + end + end + end + end + return false +end + +exports('HasGroup', HasGroup) \ No newline at end of file diff --git a/server/functions.lua b/server/functions.lua index 3dc7fd034..d180ff39a 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -422,3 +422,74 @@ local function ExploitBan(playerId, origin) end exports('ExploitBan', ExploitBan) + +--- HasPlayerGotGroup function borrowed from ox_target: https://github.com/overextended/ox_target/blob/aefc464d01da9b7aa3565e79161dd0a489945b90/client/framework/qb.lua#L41 + +-- MIT License + +-- Copyright (c) 2022 Overextended + +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: + +-- The above copyright notice and this permission notice shall be included in all +-- copies or substantial portions of the Software. + +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +-- SOFTWARE. + +---@param source Source +---@param filter string | string[] | table +---@return boolean +function HasGroup(source, filter) + local playerData = QBX.Players[source].PlayerData + if not filter then return false end + local _type = type(filter) + + if _type == 'string' then + local job = playerData.job.name == filter + local gang = playerData.gang.name == filter + local citizenId = playerData.citizenid == filter + + if job or gang or citizenId then + return true + end + elseif _type == 'table' then + local tabletype = table.type(filter) + + if tabletype == 'hash' then + for name, grade in pairs(filter) do + local job = playerData.job.name == name + local gang = playerData.gang.name == name + local citizenId = playerData.citizenid == name + + if job and grade <= playerData.job.grade.level or gang and grade <= playerData.gang.grade.level or citizenId then + return true + end + end + elseif tabletype == 'array' then + for i = 1, #filter do + local name = filter[i] + local job = playerData.job.name == name + local gang = playerData.gang.name == name + local citizenId = playerData.citizenid == name + + if job or gang or citizenId then + return true + end + end + end + end + return false +end + +exports('HasGroup', HasGroup)