Skip to content

Cache common Identifiers for faster access #1183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 69 additions & 19 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,67 @@ QBCore.Player_Buckets = {}
QBCore.Entity_Buckets = {}
QBCore.UsableItems = {}

-- Create a table with the most commons Identifier
local playerIndices = {
citizenid = {},
license = {},
phone = {},
account = {}
}

local function updatePlayerIdentifiers(player, oldData)
if not player and oldData then
if oldData.citizenid then playerIndices.citizenid[oldData.citizenid] = nil end
if oldData.license then playerIndices.license[oldData.license] = nil end
if oldData.charinfo and oldData.charinfo.phone then playerIndices.phone[oldData.charinfo.phone] = nil end
if oldData.charinfo and oldData.charinfo.account then playerIndices.account[oldData.charinfo.account] = nil end
end

if player and player.PlayerData then
if player.PlayerData.citizenid then
playerIndices.citizenid[player.PlayerData.citizenid] = player.PlayerData.source
end
if player.PlayerData.license then
playerIndices.license[player.PlayerData.license] = player.PlayerData.source
end
if player.PlayerData.charinfo then
if player.PlayerData.charinfo.phone then
playerIndices.phone[player.PlayerData.charinfo.phone] = player.PlayerData.source
end
if player.PlayerData.charinfo.account then
playerIndices.account[player.PlayerData.charinfo.account] = player.PlayerData.source
end
end
end
end

-------------------------------------------------
-- Hooks to get the player updated
-----------------------------------------------------

AddEventHandler('QBCore:Server:PlayerLoaded', function(player)
updatePlayerIdentifiers(player)
end)

-- Clean the Identifier from the table
AddEventHandler('QBCore:Server:OnPlayerUnload', function(source)
local Player = QBCore.Functions.GetPlayer(source)
if Player then
local oldData = Player.PlayerData
if playerIndices.citizenid[oldData.citizenid] then
updatePlayerIdentifiers(nil,oldData)
end
end
end)

-- Not sure about this one because we already have the Identifier on player load
-- AddEventHandler('QBCore:Player:SetPlayerData', function(player)
-- if player then
-- updatePlayerIdentifiers(player, player.PlayerData)
-- end
-- end)


-- Getters
-- Get your player first and then trigger a function on them
-- ex: local player = QBCore.Functions.GetPlayer(source)
Expand Down Expand Up @@ -56,12 +117,8 @@ end
---@param citizenid string
---@return table?
function QBCore.Functions.GetPlayerByCitizenId(citizenid)
for src in pairs(QBCore.Players) do
if QBCore.Players[src].PlayerData.citizenid == citizenid then
return QBCore.Players[src]
end
end
return nil
local source = playerIndices.citizenid[citizenid]
return source and QBCore.Players[source] or nil
end

---Get offline player by citizen id
Expand All @@ -75,31 +132,24 @@ end
---@param license string
---@return table?
function QBCore.Functions.GetPlayerByLicense(license)
return QBCore.Player.GetPlayerByLicense(license)
local source = playerIndices.license[license]
return source and QBCore.Players[source] or nil
end

---Get player by phone number
---@param number number
---@return table?
function QBCore.Functions.GetPlayerByPhone(number)
for src in pairs(QBCore.Players) do
if QBCore.Players[src].PlayerData.charinfo.phone == number then
return QBCore.Players[src]
end
end
return nil
local source = playerIndices.phone[number]
return source and QBCore.Players[source] or nil
end

---Get player by account id
---@param account string
---@return table?
function QBCore.Functions.GetPlayerByAccount(account)
for src in pairs(QBCore.Players) do
if QBCore.Players[src].PlayerData.charinfo.account == account then
return QBCore.Players[src]
end
end
return nil
local source = playerIndices.account[account]
return source and QBCore.Players[source] or nil
end

---Get player passing property and value to check exists
Expand Down