Begin rebranding (refactor+rewrite)

- Kashacters events renamed
- Update MySQL.Async queries to modern standards and remove string concatenation
- Rewrote `GetPlayerCharacters` to only use relevant information
This commit is contained in:
Linden
2021-05-17 01:21:11 +10:00
parent 99f51e1df8
commit 5d3f2884dc
5 changed files with 62 additions and 78 deletions
+41 -53
View File
@@ -7,7 +7,6 @@ if ESX.GetConfig().Multichar then
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
--print(json.encode(result))
for k, v in pairs(result) do
table.insert( IdentifierTables, {table = v.TABLE_NAME, column = "owner"})
end
@@ -15,29 +14,50 @@ if ESX.GetConfig().Multichar then
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
--print(json.encode(result))
for k, v in pairs(result) do
table.insert( IdentifierTables, {table = v.TABLE_NAME, column = "identifier"})
end
end
end)
end)
Citizen.Wait(1000)
Citizen.Wait(500)
ESX.Jobs = exports['es_extended']:getSharedObject().Jobs
end)
end
GetTables()
RegisterServerEvent("kashactersS:SetupCharacters")
AddEventHandler('kashactersS:SetupCharacters', function()
local src = source
local Characters = GetPlayerCharacters(src)
TriggerClientEvent('kashactersC:SetupUI', src, Characters)
RegisterServerEvent("esx_multicharacter:SetupCharacters")
AddEventHandler('esx_multicharacter:SetupCharacters', function()
local playerId = source
local identifier = 'char%:'..ESX.GetIdentifier(playerId)
MySQL.Async.fetchAll("SELECT `identifier`, `accounts`, `job`, `job_grade`, `firstname`, `lastname`, `dateofbirth`, `sex` FROM `users` WHERE `identifier` LIKE @identifier", {
['@identifier'] = identifier
}, function(result)
local characters = {}
for i=1, #result, 1 do
local job = ESX.Jobs[result[i].job]
local grade = ESX.Jobs[job.name].grades[tostring(result[i].job_grade)].label
local accounts = json.decode(result[i].accounts)
if grade == 'Unemployed' then grade = '' end
characters[i] = {
bank = accounts.bank,
money = accounts.money,
job = job.label,
job_grade = grade,
firstname = result[i].firstname,
lastname = result[i].lastname,
dateofbirth = result[i].dateofbirth,
identifier = result[i].identifier
}
if result[i].sex == 'm' then characters[i].sex = 'Male' else characters[i].sex = 'Female' end
end
TriggerClientEvent('esx_multicharacter:SetupUI', playerId, characters)
end)
end)
RegisterServerEvent("kashactersS:CharacterChosen")
AddEventHandler('kashactersS:CharacterChosen', function(charid, ischar)
RegisterServerEvent("esx_multicharacter:CharacterChosen")
AddEventHandler('esx_multicharacter:CharacterChosen', function(charid, ischar)
local src = source
local isNew = true
if ischar then isNew = false end
@@ -49,57 +69,25 @@ if ESX.GetConfig().Multichar then
end
end)
RegisterServerEvent("kashactersS:DeleteCharacter")
AddEventHandler('kashactersS:DeleteCharacter', function(charid)
RegisterServerEvent("esx_multicharacter:DeleteCharacter")
AddEventHandler('esx_multicharacter:DeleteCharacter', function(charid)
local src = source
if type(charid) == "number" and string.len(charid) == 1 then
DeleteCharacter(ESX.GetIdentifier(src), charid)
TriggerClientEvent("kashactersC:ReloadCharacters", src)
DeleteCharacter(src, charid)
TriggerClientEvent("esx_multicharacter:ReloadCharacters", src)
else
-- Trigger Ban Event here to ban individuals trying to use SQL Injections
end
end)
function GetPlayerCharacters(source)
local Chars = MySQLAsyncExecute("SELECT * FROM `users` WHERE `identifier` LIKE 'char%:"..ESX.GetIdentifier(source).."'")
for i = 1, #Chars, 1 do
local charJob = ESX.Jobs[Chars[i].job]
local charGrade = ESX.Jobs[Chars[i].job].grades[tostring(Chars[i].job_grade)]
local accounts = json.decode(Chars[i].accounts)
Chars[i].bank = accounts["bank"]
Chars[i].money = accounts["money"]
Chars[i].job = charJob.label
if charJob.label == "Unemployed" then
Chars[i].job_grade = ""
else
Chars[i].job_grade = charGrade.label
end
if Chars[i].sex == "m" then
Chars[i].sex = "Male"
else
Chars[i].sex = "Female"
end
end
return Chars
end
function DeleteCharacter(identifier, charid)
local identifier = 'char'..charid..':'..identifier
function DeleteCharacter(playerId, charid)
local identifier = 'char'..charid..':'..ESX.GetIdentifier(playerId)
for _, itable in pairs(IdentifierTables) do
MySQLAsyncExecute("DELETE FROM `"..itable.table.."` WHERE `"..itable.column.."` = '"..identifier.."'")
MySQL.Async.fetchAll("DELETE FROM @table WHERE @column = @identifier", {
['@table'] = itable.table,
['@column'] = itable.column,
['@identifier'] = identifier
})
end
end
function MySQLAsyncExecute(query)
local IsBusy = true
local result = nil
MySQL.Async.fetchAll(query, {}, function(data)
result = data
IsBusy = false
end)
while IsBusy do
Citizen.Wait(0)
end
return result
end
end