Module:Common: Difference between revisions

1,042 bytes added ,  11 September 2023
getCostString: Initial implementation
(getRequirementString: Support requirement type MonsterKilled)
(getCostString: Initial implementation)
Line 85: Line 85:


return Icons.Icon(iconArgs)
return Icons.Icon(iconArgs)
end
-- getCostString: Given item & currency costs for something, returns human readable wikitext
-- for those costs. If there are no costs, returns the value specified by valueIfNone instead.
-- Costs are in the format:
-- { items = { ... }, gp = 0, sc = 0, rc = 0 }
function p.getCostString(costs, valueIfNone)
local costArray = {}
if type(costs.items) == 'table' and not Shared.tableIsEmpty(costs.items) then
for i, itemCost in ipairs(costs.items) do
local item = GameData.getEntityByID('items', itemCost.id)
if item ~= nil then
table.insert(costArray, Icons.Icon({item.name, type='item', qty=itemCost.quantity}))
end
end
end
if costs.gp ~= nil and costs.gp > 0 then
table.insert(costArray, Icons.GP(costs.gp))
end
if costs.sc ~= nil and costs.sc > 0 then
table.insert(costArray, Icons.SC(costs.sc))
end
if costs.rc ~= nil and costs.rc > 0 then
table.insert(costArray, Icons.RC(costs.rc))
end
if Shared.tableIsEmpty(costArray) then
return valueIfNone
else
return table.concat(costArray, '<br/>')
end
end
end