Die Dokumentation für dieses Modul kann unter Modul:ResourceBreakdown/Doku erstellt werden
local util_args = require('Module:ArgsUtil')
local BREAKDOWNS = require( 'Module:ResourceBreakdown/breakdowns' )
local h = {}
function h.getSetting(arg, default)
-- if arg is specified then we'll cast it as a bool - false, no, 0 all evaluate to false, any other value evaluates to true
-- otherwise return the default
if not arg then
return default
end
return util_args.castAsBool(arg)
end
function h.addCountsToArgs(args)
local limit = 2 * #args
for i = 1, limit, 2 do
if args[i] and not tonumber(args[i]) then
table.insert(args, 1, i)
end
end
end
function h.getSecondLevelResources(ingredients)
if not ingredients[1] then return {} end
local secondlevel = {}
for i = 1, #ingredients, 2 do
local count = tonumber(ingredients[i]) or 1
local resource = ingredients[i+1]
for bres, bcount in pairs(BREAKDOWNS[resource] or {}) do
secondlevel[bres] = (secondlevel[bres] or 0) + bcount * count
end
end
local output = {}
for resource, count in pairs(secondlevel) do
output[#output+1] = { count = count, resource = resource }
end
return output
end
function h.getResourcesFromArgs(args)
local output = {}
for i = 1, #args, 2 do
output[#output+1] = { count = tonumber(args[i]), resource = args[i+1] }
end
return output
end
function h.getBreakdown(resource,count,indent,collapsible,collapsibleId)
local baseResources = {}
local style, fontweight = '', ''
local collapsibleClass = ''
local iconsize = '20'
if indent > 0 then
style = 'border-left:1px solid grey;margin-left:1.6em;'
if collapsible and indent == 1 then
collapsibleClass = ' class="mw-collapsible mw-collapsed" id="mw-customcollapsible-resource'..collapsibleId..'"'
collapsibleId = collapsibleId+1
end
else
fontweight = '\'\'\''
iconsize = '20'
end
local roundedCnt = math.ceil(count)
if roundedCnt ~= count then
roundedCnt = '<span style="cursor: help; border-bottom: 2px dotted #397d75;" title="'..count..' to be precise">'..roundedCnt..'</span>'
end
local resourceBreakdown = {'<div style="'..style..'padding-left:5px"'..collapsibleClass..'>'..fontweight..roundedCnt..' × '..mw.getCurrentFrame():expandTemplate{title='ItemLink', args={(string.gsub(resource,':',' '))}}..fontweight}
-- if resource is combined, list components
if BREAKDOWNS[resource] ~= nil then
for resource,cnt in pairs(BREAKDOWNS[resource]) do
local thisbreakdown = h.getBreakdown(resource,cnt*count,indent+1,collapsible,collapsibleId)
collapsibleId = thisbreakdown[3]
table.insert(resourceBreakdown,thisbreakdown[1])
-- insert base resources
for bres,bcnt in pairs(thisbreakdown[2]) do
baseResources[bres] = (baseResources[bres] or 0) + bcnt
end
end
else
-- no breakdown-data found, assuming it's a base resource, add it to basecount
baseResources[resource] = (baseResources[resource] or 0) + count
end
return {table.concat(resourceBreakdown,'\n')..'</div>',baseResources,collapsibleId}
end
local p = {}
function p.breakdownList( f )
local args = util_args.merge(true) -- convert empty string to nil
h.addCountsToArgs(args) -- our life will be MUCH easier if we can assume it's count, res, count, res, etc
local resourceList = {}
local baseResources = {}
local categories = {}
local printCategories = h.getSetting(args.printCategories, false)
local showBaseIngredients = h.getSetting(args.showBaseIngredients, true)
local collapsibleIngredients = h.getSetting(args.collapsibleIngredients, false)
local collapsibleBaseIngredients = h.getSetting(args.collapsibleBaseIngredients, false)
local skipTopLevelIngredient = h.getSetting(args.skipTopLevelIngredient, false)
local collapsibleId = 1
local resourceArgs = skipTopLevelIngredient and h.getSecondLevelResources(args) or h.getResourcesFromArgs(args)
for _,res in ipairs(resourceArgs) do
local breakdown = h.getBreakdown(res.resource,res.count or 1,0,collapsibleIngredients,collapsibleId)
collapsibleId = breakdown[3]
table.insert(resourceList,breakdown[1])
-- insert base resources
for bres,bcnt in pairs(breakdown[2]) do
-- add count to baseresource
baseResources[bres] = (baseResources[bres] or 0) + bcnt
end
if printCategories then
table.insert(categories,'[[Category:Hergestellt mit '..res.resource..']]')
end
end
local baseResourcesList, baseResourcesDiv, roundedCnt = {}, '', ''
if showBaseIngredients then
for bres,bcnt in pairs(baseResources) do
roundedCnt = math.ceil(bcnt)
if roundedCnt ~= bcnt then
roundedCnt = '<span style="cursor: help; border-bottom: 2px dotted #397d75;" title="'..bcnt..' to be precise">'..roundedCnt..'</span>'
end
table.insert(baseResourcesList,roundedCnt..' × || '..mw.getCurrentFrame():expandTemplate{title='ItemLink', args={(string.gsub(bres,':',' '))}})
end
local collapsibleBaseClassH, collapsibleBaseClassC = '',''
if collapsibleBaseIngredients then
collapsibleBaseClassH,collapsibleBaseClassC = ' class="mw-collapsible mw-collapsed"', ' class="mw-collapsible-content"'
end
baseResourcesDiv = '<div style="display:inline-block;margin:0.2em 0.2em 0;vertical-align:top;text-align:left"'..collapsibleBaseClassH..'>\'\'\'Grundkomponenten\'\'\'<div'..collapsibleBaseClassC..'>\n{|\n|-\n| align="right" | '..table.concat(baseResourcesList,'\n|-\n| align="right" | ')..'\n|}\n</div></div>'
end
local collapsibleButton = ''
if collapsibleIngredients then
if collapsibleId > 1 then
local collapsibles = {}
for i=1,(collapsibleId-1) do
table.insert(collapsibles,'mw-customtoggle-resource'..i)
end
collapsibleButton = ' <span class="'..table.concat(collapsibles,' ')..'">[<span style="cursor:pointer;" class="pseudo-link">Expand</span>]</span>'
else
-- base resources are the same as displayed resources, so don't show them additionally
baseResourcesDiv = ''
end
end
if next(resourceList) == nil then
return nil
end
return '<div style="display:inline-block;margin:0.2em 1em 0.2em 0;vertical-align:top;text-align:left">\'\'\'Aufgliederung Komponenten\'\'\''..collapsibleButton..'\n\n'..table.concat(resourceList,'\n')..'</div>'..baseResourcesDiv..table.concat(categories)
end
function p.docs()
local output = {}
for key, row in pairs(BREAKDOWNS) do
local recipe = {}
for k, v in pairs(row) do
recipe[#recipe+1] = ("'''Item:''' %s '''Quantity:''' %s"):format(k, v)
end
output[#output+1] = ("'''Item:''' %s '''Breakdown:''' %s"):format(key, table.concat(recipe,' '))
end
return table.concat(output,'<br>')
end
return p