Fix compacting
This commit is contained in:
+73
-53
@@ -45,6 +45,7 @@ local function ensureStorageBridge()
|
||||
end
|
||||
|
||||
local chainItemsByBaseId
|
||||
local chainAllItemsByBaseId
|
||||
local baseItemIds
|
||||
local allChainItemIds
|
||||
local itemById
|
||||
@@ -117,6 +118,33 @@ local function getCompressionLevel(itemId, stage)
|
||||
return 0
|
||||
end
|
||||
|
||||
local function createChainItem(itemId, iconNfp, stage)
|
||||
return {
|
||||
id = itemId,
|
||||
icon_nfp = iconNfp,
|
||||
stage = stage,
|
||||
compression_level = getCompressionLevel(itemId, stage),
|
||||
equivalent_factor = 1,
|
||||
}
|
||||
end
|
||||
|
||||
local function applyChainEquivalentFactors(baseId, orderedItems)
|
||||
local baseIndex = nil
|
||||
|
||||
for i = 1, #orderedItems do
|
||||
if orderedItems[i].id == baseId then
|
||||
baseIndex = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
assert(baseIndex, "Missing base item in chain: " .. tostring(baseId))
|
||||
|
||||
for i = 1, #orderedItems do
|
||||
orderedItems[i].equivalent_factor = 9 ^ (i - baseIndex)
|
||||
end
|
||||
end
|
||||
|
||||
local function ensureChainsLoaded()
|
||||
if not chainItemsByBaseId then
|
||||
ensureWhitelistLoaded()
|
||||
@@ -134,6 +162,7 @@ local function ensureChainsLoaded()
|
||||
assert(type(chains) == "table", "Invalid JSON data")
|
||||
|
||||
chainItemsByBaseId = {}
|
||||
chainAllItemsByBaseId = {}
|
||||
baseItemIds = {}
|
||||
allChainItemIds = {}
|
||||
itemById = {}
|
||||
@@ -148,60 +177,50 @@ local function ensureChainsLoaded()
|
||||
|
||||
baseItemIds[#baseItemIds + 1] = baseId
|
||||
|
||||
local items = {}
|
||||
local itemCount = 0
|
||||
local orderedItems = {}
|
||||
local pageItems = {}
|
||||
|
||||
if chain.base_id then
|
||||
for _, item in ipairs(chain.items or {}) do
|
||||
local compressionLevel = getCompressionLevel(item.item_id, item.stage)
|
||||
|
||||
if item.stage == "item" then
|
||||
itemById[item.item_id] = {
|
||||
id = item.item_id,
|
||||
icon_nfp = item.icon_nfp_16x16,
|
||||
compression_level = compressionLevel,
|
||||
compression_factor = 9 ^ compressionLevel,
|
||||
}
|
||||
allChainItemIds[#allChainItemIds + 1] = item.item_id
|
||||
else
|
||||
itemCount = itemCount + 1
|
||||
items[itemCount] = {
|
||||
id = item.item_id,
|
||||
icon_nfp = item.icon_nfp_16x16,
|
||||
compression_level = compressionLevel,
|
||||
compression_factor = 9 ^ compressionLevel,
|
||||
}
|
||||
itemById[item.item_id] = items[itemCount]
|
||||
allChainItemIds[#allChainItemIds + 1] = item.item_id
|
||||
end
|
||||
local chainItem = createChainItem(item.item_id, item.icon_nfp_16x16, item.stage)
|
||||
orderedItems[#orderedItems + 1] = chainItem
|
||||
itemById[item.item_id] = chainItem
|
||||
allChainItemIds[#allChainItemIds + 1] = item.item_id
|
||||
end
|
||||
|
||||
else
|
||||
local baseCompressionLevel = getCompressionLevel(baseId, "item")
|
||||
local compactItems = nil
|
||||
|
||||
itemById[baseId] = {
|
||||
id = baseId,
|
||||
icon_nfp = chain[2],
|
||||
compression_level = baseCompressionLevel,
|
||||
compression_factor = 9 ^ baseCompressionLevel,
|
||||
}
|
||||
allChainItemIds[#allChainItemIds + 1] = baseId
|
||||
if type(chain[2]) == "number" then
|
||||
compactItems = chain[3] or {}
|
||||
elseif type(chain[2]) == "table" then
|
||||
compactItems = chain[2]
|
||||
elseif type(chain[2]) == "string" and type(chain[3]) == "table" then
|
||||
orderedItems[1] = createChainItem(baseId, chain[2], "item")
|
||||
itemById[baseId] = orderedItems[1]
|
||||
allChainItemIds[#allChainItemIds + 1] = baseId
|
||||
compactItems = chain[3]
|
||||
else
|
||||
error("Invalid compact chain entry for " .. tostring(baseId))
|
||||
end
|
||||
|
||||
for i = 1, #(chain[3] or {}) do
|
||||
local compactItem = chain[3][i]
|
||||
local compressionLevel = getCompressionLevel(compactItem[1], nil)
|
||||
itemCount = itemCount + 1
|
||||
items[itemCount] = {
|
||||
id = compactItem[1],
|
||||
icon_nfp = compactItem[2],
|
||||
compression_level = compressionLevel,
|
||||
compression_factor = 9 ^ compressionLevel,
|
||||
}
|
||||
itemById[compactItem[1]] = items[itemCount]
|
||||
for i = 1, #compactItems do
|
||||
local compactItem = compactItems[i]
|
||||
local chainItem = createChainItem(compactItem[1], compactItem[2], compactItem[3])
|
||||
orderedItems[#orderedItems + 1] = chainItem
|
||||
itemById[compactItem[1]] = chainItem
|
||||
allChainItemIds[#allChainItemIds + 1] = compactItem[1]
|
||||
end
|
||||
end
|
||||
|
||||
chainItemsByBaseId[baseId] = items
|
||||
applyChainEquivalentFactors(baseId, orderedItems)
|
||||
|
||||
for i = 1, #orderedItems do
|
||||
pageItems[#pageItems + 1] = orderedItems[i]
|
||||
end
|
||||
|
||||
chainAllItemsByBaseId[baseId] = orderedItems
|
||||
chainItemsByBaseId[baseId] = pageItems
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -213,6 +232,12 @@ local function getPageItems(base_id)
|
||||
return chainItemsByBaseId[base_id or defaultBaseId] or {}
|
||||
end
|
||||
|
||||
local function getAllChainItems(base_id)
|
||||
ensureChainsLoaded()
|
||||
|
||||
return chainAllItemsByBaseId[base_id or defaultBaseId] or {}
|
||||
end
|
||||
|
||||
local function getBaseItemIds()
|
||||
ensureChainsLoaded()
|
||||
|
||||
@@ -447,16 +472,11 @@ end
|
||||
|
||||
local function getBaseEquivalentCount(baseId, itemCounts)
|
||||
local total = 0
|
||||
local baseItem = getItemById(baseId)
|
||||
local pageItems = getPageItems(baseId)
|
||||
local chainItems = getAllChainItems(baseId)
|
||||
|
||||
if baseItem then
|
||||
total = total + (tonumber(itemCounts[baseId]) or 0) * (baseItem.compression_factor or 1)
|
||||
end
|
||||
|
||||
for i = 1, #pageItems do
|
||||
local item = pageItems[i]
|
||||
total = total + (tonumber(itemCounts[item.id]) or 0) * (item.compression_factor or 1)
|
||||
for i = 1, #chainItems do
|
||||
local item = chainItems[i]
|
||||
total = total + (tonumber(itemCounts[item.id]) or 0) * (item.equivalent_factor or 1)
|
||||
end
|
||||
|
||||
return total
|
||||
@@ -468,7 +488,7 @@ local function getEquivalentLevelCounts(baseId, pageItems, itemCounts)
|
||||
|
||||
for i = 1, #pageItems do
|
||||
local item = pageItems[i]
|
||||
local factor = item.compression_factor or 1
|
||||
local factor = item.equivalent_factor or 1
|
||||
equivalentCounts[item.id] = math.floor(baseEquivalentCount / factor)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user