Skip to content
Closed
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
308 changes: 156 additions & 152 deletions [core]/esx_identity/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,199 +125,203 @@ local function formatName(name)
end

local function setIdentity(xPlayer)
if not alreadyRegistered[xPlayer.identifier] then
return
end
local currentIdentity = playerIdentity[xPlayer.identifier]
SetPlayerData(xPlayer, currentIdentity)
local identifier = xPlayer.getIdentifier()
local source = xPlayer.getSource()
if not alreadyRegistered[identifier] then
return
end
local currentIdentity = playerIdentity[identifier]
SetPlayerData(xPlayer, currentIdentity)

TriggerClientEvent("esx_identity:setPlayerData", xPlayer.source, currentIdentity)
if currentIdentity.saveToDatabase then
saveIdentityToDatabase(xPlayer.identifier, currentIdentity)
TriggerClientEvent("esx_identity:setPlayerData", source, currentIdentity)
if currentIdentity.saveToDatabase then
saveIdentityToDatabase(identifier, currentIdentity)
end

playerIdentity[identifier] = nil
end

local function checkIdentity(xPlayer)
local source = xPlayer.src
local identifier = xPlayer.getIdentifier()
MySQL.single("SELECT firstname, lastname, dateofbirth, sex, height FROM users WHERE identifier = ?", { identifier }, function(result)
if not result then
return TriggerClientEvent("esx_identity:showRegisterIdentity", source)
end
if not result.firstname then
playerIdentity[identifier] = nil
alreadyRegistered[identifier] = false
return TriggerClientEvent("esx_identity:showRegisterIdentity", source)
end

playerIdentity[xPlayer.identifier] = nil
playerIdentity[identifier] = {
firstName = result.firstname,
lastName = result.lastname,
dateOfBirth = result.dateofbirth,
sex = result.sex,
height = result.height,
}

alreadyRegistered[identifier] = true
setIdentity(source)
end)
end

local function checkIdentity(xPlayer)
MySQL.single("SELECT firstname, lastname, dateofbirth, sex, height FROM users WHERE identifier = ?", { xPlayer.identifier }, function(result)
if not multichar then
AddEventHandler("playerConnecting", function(_, _, deferrals)
deferrals.defer()
local _, identifier = source, nil

local correctLicense, _ = pcall(function()
identifier = ESX.GetIdentifier(source)
end)

Wait(40)

if not identifier or not correctLicense then
return deferrals.done(TranslateCap("no_identifier"))
end
MySQL.single("SELECT firstname, lastname, dateofbirth, sex, height FROM users WHERE identifier = ?", { identifier }, function(result)
if not result then
return TriggerClientEvent("esx_identity:showRegisterIdentity", xPlayer.source)
playerIdentity[identifier] = nil
alreadyRegistered[identifier] = false
return deferrals.done()
end
if not result.firstname then
playerIdentity[xPlayer.identifier] = nil
alreadyRegistered[xPlayer.identifier] = false
return TriggerClientEvent("esx_identity:showRegisterIdentity", xPlayer.source)
playerIdentity[identifier] = nil
alreadyRegistered[identifier] = false
return deferrals.done()
end

playerIdentity[xPlayer.identifier] = {
playerIdentity[identifier] = {
firstName = result.firstname,
lastName = result.lastname,
dateOfBirth = result.dateofbirth,
sex = result.sex,
height = result.height,
}

alreadyRegistered[xPlayer.identifier] = true
setIdentity(xPlayer)
end)
end
alreadyRegistered[identifier] = true

if not multichar then
AddEventHandler("playerConnecting", function(_, _, deferrals)
deferrals.defer()
local _, identifier = source, nil

local correctLicense, _ = pcall(function()
identifier = ESX.GetIdentifier(source)
end)

Wait(40)

if not identifier or not correctLicense then
return deferrals.done(TranslateCap("no_identifier"))
end
MySQL.single("SELECT firstname, lastname, dateofbirth, sex, height FROM users WHERE identifier = ?", { identifier }, function(result)
if not result then
playerIdentity[identifier] = nil
alreadyRegistered[identifier] = false
return deferrals.done()
end
if not result.firstname then
playerIdentity[identifier] = nil
alreadyRegistered[identifier] = false
return deferrals.done()
end

playerIdentity[identifier] = {
firstName = result.firstname,
lastName = result.lastname,
dateOfBirth = result.dateofbirth,
sex = result.sex,
height = result.height,
}

alreadyRegistered[identifier] = true

deferrals.done()
end)
deferrals.done()
end)
end)

AddEventHandler("onResourceStart", function(resource)
if resource ~= GetCurrentResourceName() then
return
end
Wait(300)
AddEventHandler("onResourceStart", function(resource)
if resource ~= GetCurrentResourceName() then
return
end
Wait(300)

while not ESX do
Wait(0)
end
while not ESX do
Wait(0)
end

local xPlayers = ESX.GetExtendedPlayers()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each static xPlayer is passed as an arg to the checkIdentity function. In that function it attempts to access player properties, which is not possible with static xPlayers. The function has to be refactored, it won't work like this.

local xPlayers = ESX.ExtendedPlayers()

for i = 1, #xPlayers do
if xPlayers[i] then
checkIdentity(xPlayers[i])
end
for i = 1, #xPlayers do
if xPlayers[i] then
checkIdentity(xPlayers[i])
end
end)

RegisterNetEvent("esx:playerLoaded", function(_, xPlayer)
local currentIdentity = playerIdentity[xPlayer.identifier]

if currentIdentity and alreadyRegistered[xPlayer.identifier] then
SetPlayerData(xPlayer, currentIdentity)

TriggerClientEvent("esx_identity:setPlayerData", xPlayer.source, currentIdentity)
if currentIdentity.saveToDatabase then
saveIdentityToDatabase(xPlayer.identifier, currentIdentity)
end
end
end)

Wait(0)
RegisterNetEvent("esx:playerLoaded", function(_, xPlayer)
local currentIdentity = playerIdentity[xPlayer.identifier]

TriggerClientEvent("esx_identity:alreadyRegistered", xPlayer.source)
if currentIdentity and alreadyRegistered[xPlayer.identifier] then
SetPlayerData(xPlayer, currentIdentity)

playerIdentity[xPlayer.identifier] = nil
else
TriggerClientEvent("esx_identity:showRegisterIdentity", xPlayer.source)
TriggerClientEvent("esx_identity:setPlayerData", xPlayer.source, currentIdentity)
if currentIdentity.saveToDatabase then
saveIdentityToDatabase(xPlayer.identifier, currentIdentity)
end
end)
end

ESX.RegisterServerCallback("esx_identity:registerIdentity", function(source, cb, data)
local xPlayer = ESX.Player(source)
Wait(0)

if not checkNameFormat(data.firstname) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_firstname_format"), "error")
return cb(false)
end
if not checkNameFormat(data.lastname) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_lastname_format"), "error")
return cb(false)
end
if not checkSexFormat(data.sex) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_sex_format"), "error")
return cb(false)
end
if not checkDOBFormat(data.dateofbirth) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_dob_format"), "error")
return cb(false)
end
if not checkHeightFormat(data.height) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_height_format"), "error")
return cb(false)
end

if xPlayer then
local identifier = xPlayer.getIdentifier()
if alreadyRegistered[identifier] then
xPlayer.showNotification(TranslateCap("already_registered"), "error")
return cb(false)
end
TriggerClientEvent("esx_identity:alreadyRegistered", xPlayer.source)

playerIdentity[identifier] = {
firstName = formatName(data.firstname),
lastName = formatName(data.lastname),
dateOfBirth = formatDate(data.dateofbirth),
sex = data.sex,
height = data.height,
}

local currentIdentity = playerIdentity[identifier]
playerIdentity[xPlayer.identifier] = nil
else
TriggerClientEvent("esx_identity:showRegisterIdentity", xPlayer.source)
end
end)
end

SetPlayerData(xPlayer, currentIdentity)
ESX.RegisterServerCallback("esx_identity:registerIdentity", function(source, cb, data)
local xPlayer = ESX.Player(source)

TriggerClientEvent("esx_identity:setPlayerData", xPlayer.src, currentIdentity)
saveIdentityToDatabase(identifier, currentIdentity)
alreadyRegistered[identifier] = true
playerIdentity[identifier] = nil
return cb(true)
end
if not checkNameFormat(data.firstname) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_firstname_format"), "error")
return cb(false)
end
if not checkNameFormat(data.lastname) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_lastname_format"), "error")
return cb(false)
end
if not checkSexFormat(data.sex) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_sex_format"), "error")
return cb(false)
end
if not checkDOBFormat(data.dateofbirth) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_dob_format"), "error")
return cb(false)
end
if not checkHeightFormat(data.height) then
TriggerClientEvent("esx:showNotification", source, TranslateCap("invalid_height_format"), "error")
return cb(false)
end

if not multichar then
TriggerClientEvent("esx:showNotification", source, TranslateCap("data_incorrect"), "error")
if xPlayer then
local identifier = xPlayer.getIdentifier()
if alreadyRegistered[identifier] then
xPlayer.showNotification(TranslateCap("already_registered"), "error")
return cb(false)
end

local formattedFirstName = formatName(data.firstname)
local formattedLastName = formatName(data.lastname)
local formattedDate = formatDate(data.dateofbirth)

data.firstname = formattedFirstName
data.lastname = formattedLastName
data.dateofbirth = formattedDate
local Identity = {
firstName = formattedFirstName,
lastName = formattedLastName,
dateOfBirth = formattedDate,
playerIdentity[identifier] = {
firstName = formatName(data.firstname),
lastName = formatName(data.lastname),
dateOfBirth = formatDate(data.dateofbirth),
sex = data.sex,
height = data.height,
}

TriggerEvent("esx_identity:completedRegistration", source, data)
TriggerClientEvent("esx_identity:setPlayerData", source, Identity)
cb(true)
end)
local currentIdentity = playerIdentity[identifier]

SetPlayerData(xPlayer, currentIdentity)

TriggerClientEvent("esx_identity:setPlayerData", xPlayer.src, currentIdentity)
saveIdentityToDatabase(identifier, currentIdentity)
alreadyRegistered[identifier] = true
playerIdentity[identifier] = nil
return cb(true)
end

if not multichar then
TriggerClientEvent("esx:showNotification", source, TranslateCap("data_incorrect"), "error")
return cb(false)
end

local formattedFirstName = formatName(data.firstname)
local formattedLastName = formatName(data.lastname)
local formattedDate = formatDate(data.dateofbirth)

data.firstname = formattedFirstName
data.lastname = formattedLastName
data.dateofbirth = formattedDate
local Identity = {
firstName = formattedFirstName,
lastName = formattedLastName,
dateOfBirth = formattedDate,
sex = data.sex,
height = data.height,
}

TriggerEvent("esx_identity:completedRegistration", source, data)
TriggerClientEvent("esx_identity:setPlayerData", source, Identity)
cb(true)
end)

if Config.EnableCommands then
ESX.RegisterCommand("char", "user", function(xPlayer)
Expand Down
Loading