update: ActivePlayers to NDCore.players

This commit is contained in:
Andyyy7666
2023-08-23 00:30:38 +02:00
parent 27d6ce4f9c
commit ab15847a32
3 changed files with 22 additions and 15 deletions

View File

@@ -1,20 +1,20 @@
---@param src number ---@param src number
---@return table ---@return table
function NDCore.getPlayer(src) function NDCore.getPlayer(src)
return ActivePlayers[src] return NDCore.players[src]
end end
---@param metadata string ---@param metadata string
---@param data any ---@param data any
---@return table ---@return table
function NDCore.getPlayers(key, value) function NDCore.getPlayers(key, value)
if not key or not value then return ActivePlayers end if not key or not value then return NDCore.players end
local players = {} local players = {}
local keyTypes = {firstname = "firstname", lastname = "lastname", gender = "gender", groups = "groups"} local keyTypes = {firstname = "firstname", lastname = "lastname", gender = "gender", groups = "groups"}
local findBy = keyTypes[key] or "metadata" local findBy = keyTypes[key] or "metadata"
for src, info in pairs(ActivePlayers) do for src, info in pairs(NDCore.players) do
if info[findBy][key] == value then if info[findBy][key] == value then
players[src] = info players[src] = info
end end

View File

@@ -1,5 +1,5 @@
NDCore = {} NDCore = {}
ActivePlayers = {} NDCore.players = {}
PlayersInfo = {} PlayersInfo = {}
local resourceName = GetCurrentResourceName() local resourceName = GetCurrentResourceName()
local tempPlayersInfo = {} local tempPlayersInfo = {}
@@ -80,7 +80,7 @@ local function checkDiscordIdentifier(identifiers)
local discordIdentifier = identifiers["discord"] local discordIdentifier = identifiers["discord"]
if not discordIdentifier then return end if not discordIdentifier then return end
return getDiscordInfo(discordIdentifier:gsub("discord:", "")) return NDCore.getDiscordInfo(discordIdentifier:gsub("discord:", ""))
end end
AddEventHandler("onResourceStart", function(name) AddEventHandler("onResourceStart", function(name)
@@ -92,6 +92,7 @@ AddEventHandler("onResourceStart", function(name)
identifiers = identifiers, identifiers = identifiers,
discord = checkDiscordIdentifier(identifiers) discord = checkDiscordIdentifier(identifiers)
} }
Wait(65)
end end
end) end)
@@ -129,7 +130,7 @@ end)
AddEventHandler("playerDropped", function() AddEventHandler("playerDropped", function()
local src = source local src = source
local char = ActivePlayers[src] local char = NDCore.players[src]
if char then if char then
char.unload() char.unload()
end end

View File

@@ -27,7 +27,7 @@ local function createCharacterTable(info)
local amount = tonumber(amount) local amount = tonumber(amount)
if not amount or account ~= "bank" and account ~= "cash" then return end if not amount or account ~= "bank" and account ~= "cash" then return end
self[account] -= amount self[account] -= amount
if ActivePlayers[self.source] then if NDCore.players[self.source] then
self.triggerEvent("ND:updateMoney", self.cash, self.bank) self.triggerEvent("ND:updateMoney", self.cash, self.bank)
TriggerEvent("ND:moneyChange", self.source, account, amount, "remove", reason) TriggerEvent("ND:moneyChange", self.source, account, amount, "remove", reason)
end end
@@ -42,7 +42,7 @@ local function createCharacterTable(info)
local amount = tonumber(amount) local amount = tonumber(amount)
if not amount or account ~= "bank" and account ~= "cash" then return end if not amount or account ~= "bank" and account ~= "cash" then return end
self[account] += amount self[account] += amount
if ActivePlayers[self.source] then if NDCore.players[self.source] then
self.triggerEvent("ND:updateMoney", self.cash, self.bank) self.triggerEvent("ND:updateMoney", self.cash, self.bank)
TriggerEvent("ND:moneyChange", self.source, account, amount, "add", reason) TriggerEvent("ND:moneyChange", self.source, account, amount, "add", reason)
end end
@@ -71,9 +71,15 @@ local function createCharacterTable(info)
if type(key) == "table" then if type(key) == "table" then
for k, v in pairs(key) do for k, v in pairs(key) do
self[k] = v self[k] = v
if k == "cash" or k == "bank" then
TriggerEvent("ND:moneyChange", self.source, k, v, "set")
end
end end
else else
self[key] = value self[key] = value
if key == "cash" or key == "bank" then
TriggerEvent("ND:moneyChange", self.source, key, value, "set")
end
end end
self.triggerEvent("ND:updateCharacter", self) self.triggerEvent("ND:updateCharacter", self)
end end
@@ -96,15 +102,15 @@ local function createCharacterTable(info)
-- Completely delete character -- Completely delete character
function self.delete() function self.delete()
local result = MySQL.query.await("DELETE FROM nd_characters WHERE charid = ?", {self.id}) local result = MySQL.query.await("DELETE FROM nd_characters WHERE charid = ?", {self.id})
if result and ActivePlayers[self.source] then if result and NDCore.players[self.source] then
ActivePlayers[self.source] = nil NDCore.players[self.source] = nil
end end
return result return result
end end
-- Unload and save character -- Unload and save character
function self.unload() function self.unload()
if not ActivePlayers[self.source] then return end if not NDCore.players[self.source] then return end
local ped = GetPlayerPed(self.source) local ped = GetPlayerPed(self.source)
if ped then if ped then
local coords = GetEntityCoords(ped) local coords = GetEntityCoords(ped)
@@ -118,7 +124,7 @@ local function createCharacterTable(info)
end end
TriggerEvent("ND:characterUnloaded", self.source, self) TriggerEvent("ND:characterUnloaded", self.source, self)
self.save() self.save()
ActivePlayers[self.source] = nil NDCore.players[self.source] = nil
end end
-- Save character information to database -- Save character information to database
@@ -203,10 +209,10 @@ local function createCharacterTable(info)
-- Set the character as the players active character/currently playing character -- Set the character as the players active character/currently playing character
function self.active() function self.active()
local char = ActivePlayers[self.source] local char = NDCore.players[self.source]
if char and char.id == self.id then return true end if char and char.id == self.id then return true end
if char then char.unload() end if char then char.unload() end
ActivePlayers[self.source] = self NDCore.players[self.source] = self
TriggerEvent("ND:characterLoaded", self) TriggerEvent("ND:characterLoaded", self)
self.triggerEvent("ND:characterLoaded", self) self.triggerEvent("ND:characterLoaded", self)
return true return true
@@ -373,5 +379,5 @@ function NDCore.setActiveCharacter(src, id)
local character = NDCore.fetchCharacter(id, src) local character = NDCore.fetchCharacter(id, src)
character.name = GetPlayerName(src) character.name = GetPlayerName(src)
character.active() character.active()
return ActivePlayers[src] return character
end end