From e3c7e5ce5c2dc1de56d5102dc01f33698a001293 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 26 Oct 2021 00:53:06 +1100 Subject: [PATCH] feat(server): Utilise transactions to reduce server load --- fxmanifest.lua | 2 +- server/main.lua | 50 ++++++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index 7f98c72d..992b8ce1 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -1,7 +1,7 @@ fx_version 'cerulean' game 'gta5' description 'https://github.com/thelindat/esx_multicharacter' -version '1.3.1' +version '1.4' lua54 'yes' dependencies { diff --git a/server/main.lua b/server/main.lua index e6179a71..f4137076 100644 --- a/server/main.lua +++ b/server/main.lua @@ -53,7 +53,7 @@ elseif ESX.GetConfig().Multichar == true then local slots = MySQL.Sync.fetchScalar("SELECT slots FROM multicharacter_slots WHERE identifier = ?", { identifier }) or Config.Slots - identifier = ('%s:%s'):format(PREFIX, identifier) + identifier = PREFIX..'%:'..identifier MySQL.Async.fetchAll(FETCH, {identifier, slots}, function(result) local characters @@ -100,23 +100,25 @@ elseif ESX.GetConfig().Multichar == true then end end) - local function DeleteCharacter(playerId, charid) - local identifier = GetIdentifier(playerId) - local character = ('%s%s:%s'):format(PREFIX, charid, identifier) - local counter = 0 - for _, v in pairs(DB_TABLES) do - MySQL.Async.execute("DELETE FROM "..v.table.." WHERE "..v.column.." = ?", { - character - }, function() - counter = counter + 1 - if counter == #DB_TABLES then - print(('[^2INFO^7] Player [%s] %s has deleted a character (%s)'):format(GetPlayerName(playerId), playerId, character)) - Citizen.Wait(100) - SetupCharacters(playerId) - end - end) - Citizen.Wait(5) + local function DeleteCharacter(source, charid) + local identifier = ('%s%s:%s'):format(PREFIX, charid, GetIdentifier(source)) + local query = "DELETE FROM ?? WHERE ?? = ?" + local tableCount = #DB_TABLES + local queries = table.create(tableCount, 0) + + for i=1, tableCount do + local v = DB_TABLES[i] + queries[i] = {query = query, values = {v.table, v.column, identifier}} end + + MySQL.Async.transaction(queries, function(result) + if result then + print(('[^2INFO^7] Player [%s] %s has deleted a character (%s)'):format(GetPlayerName(source), source, identifier)) + SetupCharacters(source) + else + print(result) + end + end) end MySQL.ready(function() @@ -130,10 +132,20 @@ elseif ESX.GetConfig().Multichar == true then table.insert(DB_TABLES, {table = v.TABLE_NAME, column = v.COLUMN_NAME}) end if next(varchar) then + local query = "ALTER TABLE ?? MODIFY COLUMN ?? VARCHAR(60)" + local queries = table.create(varsize, 0) + for k, v in pairs(varchar) do - MySQL.Sync.execute("ALTER TABLE "..k.." MODIFY COLUMN "..v.." VARCHAR(60)") + queries[#queries+1] = {query = query, values = {k, v}} end - print(('[^2INFO^7] Attempted to update ^5%s^7 columns to use VARCHAR(60)'):format(varsize)) + + MySQL.Async.transaction(queries, function(result) + if result then + print(('[^2INFO^7] Updated ^5%s^7 columns to use VARCHAR(60)'):format(varsize)) + else + print(('[^2INFO^7] Unable to update ^5%s^7 columns to use VARCHAR(60)'):format(varsize)) + end + end) end ESX.Jobs = {} local function GetJobs()