From 8bc906f518aa4082fb73a6777f7d7feb51195c45 Mon Sep 17 00:00:00 2001 From: Cedric LECOMTE Date: Tue, 20 Nov 2018 15:02:50 +0100 Subject: [PATCH] Optimize shop object loading --- server/main.lua | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/server/main.lua b/server/main.lua index 85146473..bba83bad 100644 --- a/server/main.lua +++ b/server/main.lua @@ -5,35 +5,23 @@ TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) -- Load items MySQL.ready(function() - local itemResult = MySQL.Sync.fetchAll('SELECT * FROM items') - local shopResult = MySQL.Sync.fetchAll('SELECT * FROM shops') - - local itemInformation = {} - for i=1, #itemResult, 1 do - - if itemInformation[itemResult[i].name] == nil then - itemInformation[itemResult[i].name] = {} - end - - itemInformation[itemResult[i].name].label = itemResult[i].label - itemInformation[itemResult[i].name].limit = itemResult[i].limit - end +local shopResult = MySQL.Sync.fetchAll('SELECT * FROM shops LEFT JOIN items ON items.name = shops.item') for i=1, #shopResult, 1 do - if itemInformation[shopResult[i].item] then - if ShopItems[shopResult[i].store] == nil then + if shopResult[i].name ~= NULL then + if ShopItems[shopResult[i].name] == nil then ShopItems[shopResult[i].store] = {} end - if itemInformation[shopResult[i].item].limit == -1 then - itemInformation[shopResult[i].item].limit = 30 + if shopResult[i].limit == -1 then + shopResult[i].limit = 30 end table.insert(ShopItems[shopResult[i].store], { - label = itemInformation[shopResult[i].item].label, + label = shopResult[i].label, item = shopResult[i].item, price = shopResult[i].price, - limit = itemInformation[shopResult[i].item].limit + limit = shopResult[i].limit }) else print(('esx_shops: invalid item "%s" found!'):format(shopResult[i].item)) @@ -87,4 +75,4 @@ AddEventHandler('esx_shops:buyItem', function(itemName, amount, zone) local missingMoney = price - xPlayer.getMoney() TriggerClientEvent('esx:showNotification', _source, _U('not_enough', ESX.Math.GroupDigits(missingMoney))) end -end) \ No newline at end of file +end)