Skip to content

Commit

Permalink
refactor(inventory/server): Bulk-save inventories
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Feb 26, 2022
1 parent 7c3dbcd commit c6d1288
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
34 changes: 32 additions & 2 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -858,19 +858,49 @@ 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
Inventory.Remove(id, inv.type)
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()
Expand Down

0 comments on commit c6d1288

Please sign in to comment.