From c6d1288d7c3982d93434f734a27d3a5874381c4c Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sun, 27 Feb 2022 07:41:25 +1100 Subject: [PATCH] refactor(inventory/server): Bulk-save inventories --- fxmanifest.lua | 2 +- modules/inventory/server.lua | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index 035be13d71..b60dc8bc4d 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -7,7 +7,7 @@ game 'gta5' --[[ Resource Information ]]-- name 'ox_inventory' author 'Overextended' -version '2.4.3' +version '2.4.4' repository 'https://github.com/overextended/ox_inventory' description 'Slot-based inventory with metadata' diff --git a/modules/inventory/server.lua b/modules/inventory/server.lua index 262859ae66..93869edb02 100644 --- a/modules/inventory/server.lua +++ b/modules/inventory/server.lua @@ -858,12 +858,29 @@ else end) end +local function prepareSave(inv) + inv.changed = false + + if inv.type == 'trunk' then + return 1, { json.encode(minimal(inv)), Inventory.GetPlateFromId(inv.id) } + elseif inv.type == 'glovebox' then + return 2, { json.encode(minimal(inv)), Inventory.GetPlateFromId(inv.id) } + else + return 3, { inv.owner or '', inv.id, json.encode(minimal(inv)) } + end +end + SetInterval(function() local time = os.time() - for id, inv in pairs(Inventories) do + local parameters = { {}, {}, {} } + local size = { 0, 0, 0 } + + for _, inv in pairs(Inventories) do if not inv.player and not inv.open then if not inv.datastore and inv.changed then - Inventory.Save(inv) + local i, data = prepareSave(inv) + size[i] += 1 + parameters[i][size[i]] = data end if (inv.datastore or inv.owner) and time - inv.time >= 3000 then @@ -871,6 +888,19 @@ SetInterval(function() end end end + + if #parameters[1] > 0 then + MySQL.prepare.await('UPDATE owned_vehicles SET trunk = ? WHERE plate = ?', parameters[1]) + end + + if #parameters[2] > 0 then + MySQL.prepare.await('UPDATE owned_vehicles SET glovebox = ? WHERE plate = ?', parameters[2]) + end + + if #parameters[3] > 0 then + MySQL.prepare.await('INSERT INTO ox_inventory (owner, name, data) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)', parameters[3]) + end + end, 600000) local function saveInventories()