feat(lagre): vælg bygning -> statistik pr. vare: værdi nu/ved toppris, gårdens beholdning i døgn, forbrug og produktion pr. døgn (v1.15.0.0)

This commit is contained in:
2026-09-24 01:54:47 +02:00
parent eb053083c2
commit a6547462fc
10 changed files with 356 additions and 3 deletions
@@ -288,6 +288,7 @@ function SmartPickupFrame:clearRows()
self.overviewRows = {}
self.productRows, self.outboundStatusRows = {}, {}
self.storageRows = {}
self.storageDetailRows = {}
self.boardRows = {}
self.ingredientModeRowOption, self.collectRoundRowOption, self.directionRowOption = nil, nil, nil
end
@@ -178,6 +178,10 @@ function SmartPickupFrame:onClickSource(state, element)
end
function SmartPickupFrame:onClickProduct(state, element)
local clicked = self.rowData[element]
if clicked ~= nil and clicked.kind == "storageBuilding" then
return self:safe(function(frame) frame:onClickStorageBuilding(state) end)
end
self:safe(function(frame)
local run, data = frame:getRun(), frame.rowData[element]
if run == nil or data == nil or data.kind ~= "product" then return end
@@ -26,10 +26,114 @@ function SmartPickupFrame:getInventory(adEnv)
return ADSources.getFarmInventory(adEnv, g_currentMission:getFarmId())
end
-- ---- detaljer for den valgte bygning ----
function SmartPickupFrame.formatMoney(value)
if value == nil then return "-" end
if g_i18n ~= nil and g_i18n.formatMoney ~= nil then
local ok, text = pcall(g_i18n.formatMoney, g_i18n, value, 0, true, true)
if ok and text ~= nil then return text end
end
return string.format("%d", math.floor(value + 0.5))
end
function SmartPickupFrame:fillTypeTitle(fillType)
local title = g_fillTypeManager.getFillTypeTitleByIndex ~= nil and g_fillTypeManager:getFillTypeTitleByIndex(fillType) or nil
return tostring(title or g_fillTypeManager:getFillTypeNameByIndex(fillType))
end
-- Beregner stats for alle varer i bygningen (gårdens totaler og rater regnes én gang pr. opdatering).
function SmartPickupFrame:computeBuildingStats(adEnv, entry, inventory)
local farmId = g_currentMission:getFarmId()
local totals = ADStorageStats.farmTotals(inventory)
local rates = ADSources.getFarmRates ~= nil and ADSources.getFarmRates(adEnv, farmId) or {}
local stats = {}
for _, item in ipairs(entry.entries) do
local quote = ADSources.getMarketQuote ~= nil and ADSources.getMarketQuote(item.fillType) or nil
table.insert(stats, ADStorageStats.itemStats(item, totals[item.fillType], rates[item.fillType], quote))
end
return stats
end
-- "Sukker — 150.0k/180.0k (83 %) · nu 240.300 kr · top 300.400 kr · gården 412.0k = 3.2 døgn · forbrug 130.0k/døgn · prod. 48.0k/døgn"
function SmartPickupFrame:describeStat(stat)
local i18n = self.i18n
local fc = SmartPickupFrame.formatCompact
local parts = {string.format("%s — %s/%s%s", self:fillTypeTitle(stat.fillType), fc(stat.level), fc(stat.capacity), stat.percent ~= nil and string.format(" (%d %%)", stat.percent) or "")}
if stat.valueNow ~= nil then
table.insert(parts, string.format(i18n:getText("spu_stat_now"), SmartPickupFrame.formatMoney(stat.valueNow)))
table.insert(parts, string.format(i18n:getText("spu_stat_top"), SmartPickupFrame.formatMoney(stat.valueTop)))
else
table.insert(parts, i18n:getText("spu_stat_noSell"))
end
local days = stat.days ~= nil and string.format(i18n:getText("spu_stat_days"), stat.days) or i18n:getText("spu_stat_noUse")
table.insert(parts, string.format(i18n:getText("spu_stat_farm"), fc(stat.farmTotal), days))
if stat.consumption > 0 then table.insert(parts, string.format(i18n:getText("spu_stat_use"), fc(stat.consumption))) end
if stat.production > 0 then table.insert(parts, string.format(i18n:getText("spu_stat_prod"), fc(stat.production))) end
return " " .. table.concat(parts, " · ")
end
function SmartPickupFrame:describeStatTotals(stats)
local totals = ADStorageStats.buildingTotals(stats)
return string.format(" " .. self.i18n:getText("spu_stat_total"), SmartPickupFrame.formatMoney(totals.valueNow), SmartPickupFrame.formatMoney(totals.valueTop))
end
-- Navne til vælgeren; ens navne (40 Farma-siloer) får løbenummer.
function SmartPickupFrame:storageLabels(inventory)
local counts, seen, labels = {}, {}, {}
for _, entry in ipairs(inventory) do counts[entry.name] = (counts[entry.name] or 0) + 1 end
for index, entry in ipairs(inventory) do
seen[entry.name] = (seen[entry.name] or 0) + 1
labels[index] = counts[entry.name] > 1 and string.format("%s #%d", entry.name, seen[entry.name]) or entry.name
end
return labels
end
function SmartPickupFrame:fillStorageDetail(adEnv, inventory)
self.storageDetailRows = {}
if #inventory == 0 then
return
end
self.storageIndex = math.max(1, math.min(self.storageIndex or 1, #inventory))
local row = self:addRow(self.productPrefab, {kind = "storageBuilding"})
local option = row:getDescendantByName("option")
option:setTexts(self:storageLabels(inventory))
option:setState(self.storageIndex, false)
row:getDescendantByName("title"):setText(self.i18n:getText("spu_storage_select"))
local entry = inventory[self.storageIndex]
self.storageDetailPlaceable = entry.placeable
local stats = self:computeBuildingStats(adEnv, entry, inventory)
for _, stat in ipairs(stats) do
table.insert(self.storageDetailRows, self:addTextRow(self:describeStat(stat)))
end
table.insert(self.storageDetailRows, self:addTextRow(self:describeStatTotals(stats)))
end
function SmartPickupFrame:refreshStorageDetail(adEnv, inventory)
local entry = nil
for _, candidate in ipairs(inventory) do
if candidate.placeable == self.storageDetailPlaceable then entry = candidate end
end
if entry == nil or #(self.storageDetailRows or {}) ~= #entry.entries + 1 then
return
end
local stats = self:computeBuildingStats(adEnv, entry, inventory)
for index, stat in ipairs(stats) do
self.storageDetailRows[index]:setText(self:describeStat(stat))
end
self.storageDetailRows[#stats + 1]:setText(self:describeStatTotals(stats))
end
function SmartPickupFrame:onClickStorageBuilding(state)
self.storageIndex = state
self:requestRebuild()
end
function SmartPickupFrame:fillStorageRows(adEnv)
self.storageRows = {}
self:addTextRow(self.i18n:getText("spu_section_storage"))
local inventory = self:getInventory(adEnv)
self:fillStorageDetail(adEnv, inventory)
self:addTextRow(self.i18n:getText("spu_section_storage"))
for _, entry in ipairs(inventory) do
table.insert(self.storageRows, {placeable = entry.placeable, title = self:addTextRow(self:describeInventory(entry))})
end
@@ -42,10 +146,12 @@ function SmartPickupFrame:refreshStorageRows(adEnv)
if self.currentTab ~= SmartPickupFrame.TAB_STORAGE or #(self.storageRows or {}) == 0 then
return
end
local inventory = self:getInventory(adEnv)
local byPlaceable = {}
for _, entry in ipairs(self:getInventory(adEnv)) do
for _, entry in ipairs(inventory) do
byPlaceable[entry.placeable] = entry
end
pcall(self.refreshStorageDetail, self, adEnv, inventory)
for _, row in ipairs(self.storageRows) do
local entry = byPlaceable[row.placeable]
if entry ~= nil then