Skip to content

Commit

Permalink
fix(server): resolve a stash saving issue
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Nov 15, 2023
1 parent 92534ba commit 03025f3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
29 changes: 18 additions & 11 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,14 @@ function Inventory.Get(inv, key)
end
end

---@class MinimalInventorySlot
---@field name string
---@field count number
---@field slot number
---@field metadata? table

---@param inv inventory
---@return table items table containing minimal inventory data
---@return MinimalInventorySlot[] items
local function minimal(inv)
inv = Inventory(inv) --[[@as OxInventory]]
local inventory, count = {}, 0
Expand Down Expand Up @@ -2190,11 +2196,13 @@ end

exports('GetItemCount', Inventory.GetItemCount)

---@alias InventorySaveData { [1]: MinimalInventorySlot, [2]: string | number, [3]: string | number | nil }

---@param inv OxInventory
---@param buffer table
---@param time integer
---@return integer | false | nil
---@return table | nil
---@return integer?
---@return InventorySaveData?
local function prepareInventorySave(inv, buffer, time)
local shouldSave = not inv.datastore and inv.changed
local n = 0
Expand Down Expand Up @@ -2243,29 +2251,28 @@ local function saveInventories(clearInventories)
isSaving = true
local time = os.time()
local parameters = { {}, {}, {}, {} }
local size = { 0, 0, 0, 0 }
local total = { 0, 0, 0, 0, 0 }
local buffer = {}
local total = 0

for _, inv in pairs(Inventories) do
local index, data = prepareInventorySave(inv, buffer, time)

if index and data then
total += 1
total[5] += 1

if index == 4 then
for i = 1, 3 do
size[index] += 1
parameters[index][size[index]] = data[i]
total[index] += 1
parameters[index][total[index]] = data[i]
end
else
size[index] += 1
parameters[index][size[index]] = data
total[index] += 1
parameters[index][total[index]] = data
end
end
end

if total > 0 then
if total[5] > 0 then
db.saveInventories(parameters[1], parameters[2], parameters[3], parameters[4], total)
end

Expand Down
29 changes: 17 additions & 12 deletions modules/mysql/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,57 +165,62 @@ local function countRows(rows)
return n
end

---@param players InventorySaveData[]
---@param trunks InventorySaveData[]
---@param gloveboxes InventorySaveData[]
---@param stashes (InventorySaveData | string | number)[]
---@param total number[]
function db.saveInventories(players, trunks, gloveboxes, stashes, total)
local numPlayer, numTrunk, numGlove, numStash = #players, #trunks, #gloveboxes, #stashes / 3
local promises = {}
local start = os.nanotime()

shared.info(('Saving %s inventories to the database'):format(total))
shared.info(('Saving %s inventories to the database'):format(total[5]))

if numPlayer > 0 then
if total[1] > 0 then
local p = promise.new()
promises[#promises + 1] = p

MySQL.prepare(Query.UPDATE_PLAYER, players, function(resp)
shared.info(('Saved %d/%d players (%.4f ms)'):format(countRows(resp), numPlayer, (os.nanotime() - start) / 1e6))
shared.info(('Saved %d/%d players (%.4f ms)'):format(countRows(resp), total[1], (os.nanotime() - start) / 1e6))
p:resolve()
end)
end

if numTrunk > 0 then
if total[2] > 0 then
local p = promise.new()
promises[#promises + 1] = p

MySQL.prepare(Query.UPDATE_TRUNK, trunks, function(resp)
shared.info(('Saved %d/%d trunks (%.4f ms)'):format(countRows(resp), numTrunk, (os.nanotime() - start) / 1e6))
shared.info(('Saved %d/%d trunks (%.4f ms)'):format(countRows(resp), total[2], (os.nanotime() - start) / 1e6))
p:resolve()
end)
end

if numGlove > 0 then
if total[3] > 0 then
local p = promise.new()
promises[#promises + 1] = p

MySQL.prepare(Query.UPDATE_GLOVEBOX, gloveboxes, function(resp)
shared.info(('Saved %d/%d gloveboxes (%.4f ms)'):format(countRows(resp), numGlove, (os.nanotime() - start) / 1e6))
shared.info(('Saved %d/%d gloveboxes (%.4f ms)'):format(countRows(resp), total[3], (os.nanotime() - start) / 1e6))
p:resolve()
end)
end

if numStash > 0 then
if total[4] > 0 then
total[4] /= 3
local p = promise.new()
promises[#promises + 1] = p

MySQL.query(Query.UPSERT_STASH:gsub('%(%?, %?, %?%)', string.rep('(?, ?, ?)', numStash, ', ')), stashes, function(resp)
MySQL.query(Query.UPSERT_STASH:gsub('%(%?, %?, %?%)', string.rep('(?, ?, ?)', total[4], ', ')), stashes, function(resp)
local affectedRows = resp.affectedRows

if numStash == 1 then
if total[4] == 1 then
if affectedRows == 2 then affectedRows = 1 end
else
affectedRows -= tonumber(resp.info:match('Duplicates: (%d+)'), 10) or 0
end

shared.info(('Saved %d/%d stashes (%.4f ms)'):format(affectedRows, numStash, (os.nanotime() - start) / 1e6))
shared.info(('Saved %d/%d stashes (%.4f ms)'):format(affectedRows, total[4], (os.nanotime() - start) / 1e6))
p:resolve()
end)
end
Expand Down

0 comments on commit 03025f3

Please sign in to comment.