refactor(server): Moved character setup into a function so it can be called from server

This commit is contained in:
Linden
2021-05-21 07:25:36 +10:00
parent 964dcb3ec9
commit 0703767211
+43 -39
View File
@@ -4,32 +4,7 @@ if ESX.GetConfig().Multichar then
Config.databaseName = 'es_extended'
Config.prefix = 'char'
Citizen.CreateThread(function()
MySQL.ready(function ()
MySQL.Async.fetchAll('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = @id AND TABLE_SCHEMA = @db', { ['@id'] = "owner", ["@db"] = Config.databaseName}, function(result)
if result then
for k, v in pairs(result) do
IdentifierTables[#IdentifierTables+1] = {table = v.TABLE_NAME, column = 'owner'}
end
end
end)
MySQL.Async.fetchAll('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = @id AND TABLE_SCHEMA = @db', { ['@id'] = "identifier", ["@db"] = Config.databaseName}, function(result)
if result then
for k, v in pairs(result) do
IdentifierTables[#IdentifierTables+1] = {table = v.TABLE_NAME, column = 'identifier'}
end
end
end)
end)
while next(ESX.Jobs) == nil do
Citizen.Wait(200)
ESX.Jobs = exports['es_extended']:getSharedObject().Jobs
end
end)
RegisterServerEvent("esx_multicharacter:SetupCharacters")
AddEventHandler('esx_multicharacter:SetupCharacters', function()
local playerId = source
SetupCharacters = function(playerId)
local identifier = Config.prefix..'%:'..ESX.GetIdentifier(playerId)
MySQL.Async.fetchAll("SELECT `identifier`, `accounts`, `job`, `job_grade`, `firstname`, `lastname`, `dateofbirth`, `sex`, `skin` FROM `users` WHERE `identifier` LIKE @identifier", {
['@identifier'] = identifier
@@ -56,6 +31,43 @@ if ESX.GetConfig().Multichar then
end
TriggerClientEvent('esx_multicharacter:SetupUI', playerId, characters)
end)
end
DeleteCharacter = function(playerId, charid)
local identifier = 'char'..charid..':'..ESX.GetIdentifier(playerId)
for _, itable in pairs(IdentifierTables) do
MySQL.Async.execute("DELETE FROM "..itable.table.." WHERE "..itable.column.." = @identifier", {
['@identifier'] = identifier
})
end
end
Citizen.CreateThread(function()
MySQL.ready(function ()
MySQL.Async.fetchAll('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = @id AND TABLE_SCHEMA = @db', { ['@id'] = "owner", ["@db"] = Config.databaseName}, function(result)
if result then
for k, v in pairs(result) do
IdentifierTables[#IdentifierTables+1] = {table = v.TABLE_NAME, column = 'owner'}
end
end
end)
MySQL.Async.fetchAll('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = @id AND TABLE_SCHEMA = @db', { ['@id'] = "identifier", ["@db"] = Config.databaseName}, function(result)
if result then
for k, v in pairs(result) do
IdentifierTables[#IdentifierTables+1] = {table = v.TABLE_NAME, column = 'identifier'}
end
end
end)
end)
while next(ESX.Jobs) == nil do
Citizen.Wait(200)
ESX.Jobs = exports['es_extended']:getSharedObject().Jobs
end
end)
RegisterServerEvent("esx_multicharacter:SetupCharacters")
AddEventHandler('esx_multicharacter:SetupCharacters', function()
SetupCharacters(source)
end)
RegisterServerEvent("esx_multicharacter:CharacterChosen")
@@ -73,22 +85,14 @@ if ESX.GetConfig().Multichar then
local src = source
if type(charid) == "number" and string.len(charid) == 1 then
DeleteCharacter(src, charid)
--TriggerClientEvent("esx_multicharacter:ReloadCharacters", src)
SetupCharacters(src)
else
-- Trigger Ban Event here to ban individuals trying to use SQL Injections
end
end)
function DeleteCharacter(playerId, charid)
local identifier = 'char'..charid..':'..ESX.GetIdentifier(playerId)
for _, itable in pairs(IdentifierTables) do
MySQL.Async.execute("DELETE FROM "..itable.table.." WHERE "..itable.column.." = @identifier", {
['@identifier'] = identifier
})
end
end
RegisterCommand('relog', function(source, args, rawCommand)
TriggerEvent('esx:playerLogout', source)
end, false) -- Still experimental! Requires proper setup and modifications to other resources to properly support relogging
end
RegisterCommand('relog', function(source, args, rawCommand)
TriggerEvent('esx:playerLogout', source)
end, true) -- Still experimental! Requires proper setup and modifications to other resources to properly support relogging