Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Commit

Permalink
feat(server): Set VARCHAR(60) automatically; improvements to SQL.read…
Browse files Browse the repository at this point in the history
…y functions
  • Loading branch information
thelindat committed Jun 27, 2021
1 parent 698397f commit 7acd7bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 48 deletions.
14 changes: 8 additions & 6 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fx_version 'adamant'
game 'gta5'
description 'https://github.com/thelindat/esx_multicharacter'
version '1.1.1'
version '1.1.2'

dependencies {
'es_extended',
Expand All @@ -10,18 +10,20 @@ dependencies {
'esx_skin'
}

shared_script 'config.lua'
shared_scripts {
'@es_extended/locale.lua',
'locales/*.lua',
'config.lua'
}

server_scripts {
'@es_extended/imports.lua',
'@mysql-async/lib/MySQL.lua',
'server/*.lua',
'server/*.lua'
}

client_scripts {
'@es_extended/locale.lua',
'client/main.lua',
'locales/*.lua',
'client/*.lua'
}

ui_page {
Expand Down
72 changes: 30 additions & 42 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
if not ESX then
SetTimeout(3000, print('[^3WARNING^7] Unable to start Multicharacter - your version of ESX is not compatible '))
elseif ESX.GetConfig().Multichar == true then
local fetchCharacters = -1
local IdentifierTables = {}

local IdentifierTables, Fetch = {}, -1

-- enter the name of your database here
Config.Database = 'es_extended'

-- enter a prefix to prepend to all user identifiers (keep it short)
Config.Prefix = 'char'

RegisterCommand('varchar', function(source)
if source == 0 then
if next(IdentifierTables) == nil then print(' Unable to update your tables - have you defined the correct database?') return end
for _, itable in pairs(IdentifierTables) do
print('Setting `'..itable.table..'` column `'..itable.column..'` to VARCHAR(60)')
MySQL.Sync.execute("ALTER TABLE "..itable.table.." MODIFY COLUMN "..itable.column.." VARCHAR(60)", {})
end
Citizen.Wait(100)
print(' Updated your tables to use the correct field length - you should remove this command')
end
end, true)

SetupCharacters = function(playerId)
while fetchCharacters == -1 do Citizen.Wait(100) end
local SetupCharacters = function(playerId)
while Fetch == -1 do Citizen.Wait(500) end
local identifier = Config.Prefix..'%:'..ESX.GetIdentifier(playerId)
MySQL.Async.fetchAll(fetchCharacters, {'identifier', {identifier}
MySQL.Async.fetchAll(Fetch, {
identifier,
Config.Slots
}, function(result)
local characters = {}
for i=1, #result, 1 do
Expand All @@ -46,41 +36,43 @@ elseif ESX.GetConfig().Multichar == true then
dateofbirth = result[i].dateofbirth,
skin = json.decode(result[i].skin)
}
if result[i].sex == 'm' then characters[id].sex = 'Male' else characters[id].sex = 'Female' end
if result[i].sex == 'm' then characters[id].sex = _('male') else characters[id].sex = _('female') end
end
TriggerClientEvent('esx_multicharacter:SetupUI', playerId, characters)
end)
end

DeleteCharacter = function(playerId, charid)
local DeleteCharacter = function(playerId, charid)
local identifier = Config.Prefix..charid..':'..ESX.GetIdentifier(playerId)
for _, itable in pairs(IdentifierTables) do
MySQL.Async.execute("DELETE FROM "..itable.table.." WHERE "..itable.column.." = @identifier", {
['@identifier'] = identifier
for k, v in pairs(IdentifierTables) do
MySQL.Async.execute("DELETE FROM "..v.table.." WHERE "..v.column.." = ?", {
identifier
})
end
print(('[^2INFO^7] Player [%s] %s has deleted a character (%s)'):format(GetPlayerName(playerId), playerId, identifier))
end

Citizen.CreateThread(function()
MySQL.ready(function ()
fetchCharacters = MySQL.Sync.store("SELECT `identifier`, `accounts`, `job`, `job_grade`, `firstname`, `lastname`, `dateofbirth`, `sex`, `skin` FROM `users` WHERE ?? LIKE ?")

MySQL.Async.fetchAll('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = @id AND TABLE_SCHEMA = @db', { ['@id'] = "owner", ["@db"] = Config.Database}, function(result)
if result then
for k, v in pairs(result) do
IdentifierTables[#IdentifierTables+1] = {table = v.TABLE_NAME, column = 'owner'}
end
local result = MySQL.Sync.fetchAll('SELECT `TABLE_NAME`, `COLUMN_NAME`, `CHARACTER_MAXIMUM_LENGTH` FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND (COLUMN_NAME = ? OR COLUMN_NAME = ?) ', {
Config.Database, 'identifier', 'owner'
})
if result then
local varchar, varsize = {}, 0
for k, v in pairs(result) do
if v.CHARACTER_MAXIMUM_LENGTH < 60 then varchar[v.TABLE_NAME] = v.COLUMN_NAME varsize = varsize+1 end
table.insert(IdentifierTables, {table = v.TABLE_NAME, column = v.COLUMN_NAME})
end
end)
MySQL.Async.fetchAll('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = @id AND TABLE_SCHEMA = @db', { ['@id'] = "identifier", ["@db"] = Config.Database}, function(result)
if result then
for k, v in pairs(result) do
IdentifierTables[#IdentifierTables+1] = {table = v.TABLE_NAME, column = 'identifier'}
if next(varchar) then
for k, v in pairs(varchar) do
MySQL.Sync.execute("ALTER TABLE "..k.." MODIFY COLUMN "..v.." VARCHAR(60)")
end
print(('[^2INFO^7] Attempted to update ^5%s^7 columns to use VARCHAR(60)'):format(varsize))
end
end)
end
if not next(ESX.Jobs) then ESX.Jobs = GetJobs() end
Fetch = MySQL.Sync.store("SELECT `identifier`, `accounts`, `job`, `job_grade`, `firstname`, `lastname`, `dateofbirth`, `sex`, `skin` FROM `users` WHERE identifier LIKE ? LIMIT ?")
end)
if not next(ESX.Jobs) then ESX.Jobs = GetJobs() end
end)

RegisterServerEvent("esx_multicharacter:SetupCharacters")
Expand All @@ -98,8 +90,6 @@ elseif ESX.GetConfig().Multichar == true then
else
TriggerEvent('esx:onPlayerJoined', src, Config.Prefix..charid)
end
else
-- Trigger Ban Event here to ban individuals trying to use SQL Injections
end
end)

Expand All @@ -118,8 +108,6 @@ elseif ESX.GetConfig().Multichar == true then
if type(charid) == "number" and string.len(charid) <= 2 then
DeleteCharacter(src, charid)
SetupCharacters(src)
else
-- Trigger Ban Event here to ban individuals trying to use SQL Injections
end
end)

Expand All @@ -133,6 +121,6 @@ elseif ESX.GetConfig().Multichar == true then
TriggerEvent('esx:playerLogout', source)
end, true)

elseif ESX.GetConfig().Multichar == false then
else
SetTimeout(3000, print('[^3WARNING^7] Multicharacter is disabled - please check your ESX configuration'))
end

0 comments on commit 7acd7bc

Please sign in to comment.