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:
@@ -0,0 +1,67 @@
|
||||
-- AD Storage Stats
|
||||
-- Rene beregninger (ingen spil-API) til fanen Lagre: pr. vare i en bygning fyldning, værdi nu og
|
||||
-- ved årets toppris, gårdens samlede beholdning og hvor mange døgn den rækker ved gårdens forbrug.
|
||||
|
||||
ADStorageStats = {}
|
||||
|
||||
-- item = {fillType, level, capacity}; farmTotal = liter af varen på hele gården;
|
||||
-- rate = {consumption, production} liter pr. døgn (eller nil); quote = {price, top} pr. liter (eller nil).
|
||||
function ADStorageStats.itemStats(item, farmTotal, rate, quote)
|
||||
local level = item.level or 0
|
||||
local capacity = item.capacity or 0
|
||||
local consumption = rate ~= nil and rate.consumption or 0
|
||||
local production = rate ~= nil and rate.production or 0
|
||||
return {
|
||||
fillType = item.fillType,
|
||||
level = level,
|
||||
capacity = capacity,
|
||||
percent = capacity > 0 and math.floor(100 * level / capacity + 0.5) or nil,
|
||||
valueNow = quote ~= nil and quote.price ~= nil and math.floor(level * quote.price + 0.5) or nil,
|
||||
valueTop = quote ~= nil and quote.top ~= nil and math.floor(level * quote.top + 0.5) or nil,
|
||||
farmTotal = farmTotal or level,
|
||||
consumption = consumption,
|
||||
production = production,
|
||||
net = production - consumption,
|
||||
days = consumption > 0 and (farmTotal or level) / consumption or nil,
|
||||
}
|
||||
end
|
||||
|
||||
function ADStorageStats.buildingTotals(stats)
|
||||
local now, top = 0, 0
|
||||
for _, stat in ipairs(stats) do
|
||||
now = now + (stat.valueNow or 0)
|
||||
top = top + (stat.valueTop or 0)
|
||||
end
|
||||
return {valueNow = now, valueTop = top}
|
||||
end
|
||||
|
||||
-- inventory = { {entries = {{fillType, level}}} } -> {[fillType] = liter}
|
||||
function ADStorageStats.farmTotals(inventory)
|
||||
local totals = {}
|
||||
for _, building in ipairs(inventory or {}) do
|
||||
for _, entry in ipairs(building.entries or {}) do
|
||||
totals[entry.fillType] = (totals[entry.fillType] or 0) + (entry.level or 0)
|
||||
end
|
||||
end
|
||||
return totals
|
||||
end
|
||||
|
||||
-- Et stald-trug æder `perDay` liter i alt; fordel på fodertyperne efter hvad truget indeholder.
|
||||
-- Tomt trug: alt på foderet med højest vægt (det der hentes først).
|
||||
function ADStorageStats.splitTroughConsumption(perDay, levels, weights)
|
||||
local total = 0
|
||||
for _, level in pairs(levels or {}) do total = total + (level or 0) end
|
||||
local result = {}
|
||||
if total > 0 then
|
||||
for fillType, level in pairs(levels) do
|
||||
if (level or 0) > 0 then result[fillType] = perDay * level / total end
|
||||
end
|
||||
return result
|
||||
end
|
||||
local best, bestWeight = nil, -1
|
||||
for fillType, weight in pairs(weights or {}) do
|
||||
if weight > bestWeight or (weight == bestWeight and fillType < best) then best, bestWeight = fillType, weight end
|
||||
end
|
||||
if best ~= nil then result[best] = perDay end
|
||||
return result
|
||||
end
|
||||
Reference in New Issue
Block a user