Skip to content

Commit

Permalink
feat(server/functions): added SearchPlayers export
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoenixVeil authored Sep 21, 2024
1 parent 53b3a50 commit c7a9b1c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
22 changes: 22 additions & 0 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,28 @@ end

exports('GetPlayersData', getPlayersData)

---@param filters table <string, any>
---@return Player[]
local function searchPlayerEntities(filters)
local result = {}
local response = storage.searchPlayerEntities(filters)
for i = 1, #response do
local citizenid = response[i].citizenid
local player = GetPlayerByCitizenId(citizenid)
if player then
result[#result+1] = player
else
local offlinePlayer = GetOfflinePlayer(citizenid)
if offlinePlayer then
result[#result+1] = offlinePlayer
end
end
end
return result
end

exports("SearchPlayers", searchPlayerEntities)

local function isGradeBoss(group, grade)
local groupData = GetJob(group) or GetGang(group)
if not groupData then return end
Expand Down
53 changes: 53 additions & 0 deletions server/storage/players.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,58 @@ local function fetchPlayerEntity(citizenId)
} or nil
end

---@param filters table<string, any>
local function handleSearchFilters(filters)
if not (filters) then return '', {} end
local holders = {}
local clauses = {}
if filters.license then
clauses[#clauses + 1] = 'license = ?'
holders[#holders + 1] = filters.license
end
if filters.job then
clauses[#clauses + 1] = 'JSON_EXTRACT(job, "$.name") = ?'
holders[#holders + 1] = filters.job
end
if filters.gang then
clauses[#clauses + 1] = 'JSON_EXTRACT(gang, "$.name") = ?'
holders[#holders + 1] = filters.gang
end
if filters.metadata then
local strict = filters.metadata.strict
for key, value in pairs(filters.metadata) do
if key ~= "strict" then
if type(value) == "number" then
if strict then
clauses[#clauses + 1] = 'JSON_EXTRACT(metadata, "$.' .. key .. '") = ?'
else
clauses[#clauses + 1] = 'JSON_EXTRACT(metadata, "$.' .. key .. '") >= ?'
end
holders[#holders + 1] = value
elseif type(value) == "boolean" then
clauses[#clauses + 1] = 'JSON_EXTRACT(metadata, "$.' .. key .. '") = ?'
holders[#holders + 1] = tostring(value)
elseif type(value) == "string" then
clauses[#clauses + 1] = 'JSON_UNQUOTE(JSON_EXTRACT(metadata, "$.' .. key .. '")) = ?'
holders[#holders + 1] = value
end
end
end
end
return string.format(' WHERE %s', table.concat(clauses, ' AND ')), holders
end

---@param filters table <string, any>
---@return PlayerEntityDatabase[]
local function searchPlayerEntities(filters)
local query = "SELECT citizenid FROM players"
local where, holders = handleSearchFilters(filters)
lib.print.debug(query .. where)
---@type PlayerEntityDatabase[]
local response = MySQL.query.await(query .. where, holders)
return response
end

---Checks if a table exists in the database
---@param tableName string
---@return boolean
Expand Down Expand Up @@ -326,4 +378,5 @@ return {
fetchGroupMembers = fetchGroupMembers,
removePlayerFromJob = removePlayerFromJob,
removePlayerFromGang = removePlayerFromGang,
searchPlayerEntities = searchPlayerEntities,
}
1 change: 1 addition & 0 deletions types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
---@field upsertPlayerEntity fun(request: UpsertPlayerRequest)
---@field fetchPlayerSkin fun(citizenId: string): PlayerSkin?
---@field fetchPlayerEntity fun(citizenId: string): PlayerEntity?
---@field searchPlayerEntities fun(filters: table<string, any>): Player[]
---@field fetchAllPlayerEntities fun(license2: string, license?: string): PlayerEntity[]
---@field deletePlayer fun(citizenId: string): boolean success
---@field fetchIsUnique fun(type: UniqueIdType, value: string|number): boolean
Expand Down

0 comments on commit c7a9b1c

Please sign in to comment.