Module:ResourceBreakdown

local p = {} function p.breakdownList( f ) local args = f:getParent.args local resourceList = {} local baseResources = {} local showBaseIngredients, collapsibleIngredients, collapsibleBaseIngredients = true, false, false local printCategories, categories = false, {} if args.showBaseIngredients ~= nil and args.showBaseIngredients == 'false' then showBaseIngredients = false end if args.collapsibleIngredients ~= nil and args.collapsibleIngredients == 'true' then collapsibleIngredients = true end if args.collapsibleBaseIngredients ~= nil and args.collapsibleBaseIngredients == 'true' then collapsibleBaseIngredients = true end if args.printCategories ~= nil and args.printCategories == 'true' then printCategories = true end

local count, collapsibleId = 1, 1 for _,res in ipairs(args) do   if string.len(res) > 0 then if tonumber(res) ~= nil then count = tonumber(res) else local returnTable = p.breakdown(res,count,0,collapsibleIngredients,collapsibleId) collapsibleId = returnTable[3] table.insert(resourceList,returnTable[1])

-- insert base resources for bres,bcnt in pairs(returnTable[2]) do         if baseResources[bres] == nil then baseResources[bres] = 0 end -- add count to baseresource baseResources[bres] = baseResources[bres] + bcnt end -- set resource count default to 1 (if no number is given) count = 1 if printCategories then table.insert(categories,'') end end 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 = ' '..roundedCnt..' ' end table.insert(baseResourcesList,roundedCnt..' × || '..mw.getCurrentFrame:expandTemplate{title='ItemLink', args={string.gsub(bres,':',' '), 30}}) end local collapsibleBaseClassH, collapsibleBaseClassC = , if collapsibleBaseIngredients then collapsibleBaseClassH,collapsibleBaseClassC = ' class="mw-collapsible mw-collapsed"', ' class="mw-collapsible-content"' end baseResourcesDiv = '\'\'\'Total Base Ingredients\'\'\'\n{|\n|-\n| align="right" | '..table.concat(baseResourcesList,'\n|-\n| align="right" | ')..'\n|}\n ' end local collapsibleButton = '' if collapsibleIngredients then if collapsibleId > 1 then local collapsibles = {} for i=1,(collapsibleId-1) do       table.insert(collapsibles,'mw-customtoggle-res'..i)      end collapsibleButton = ' [ Expand ] ' else -- base resources are the same as displayed resources, so don't show them additionally baseResourcesDiv = '' end end return ' \'\'\'Resources breakdown\'\'\''..collapsibleButton..'\n\n'..table.concat(resourceList,'\n')..' '..baseResourcesDiv..table.concat(categories) end

function p.breakdown(resource,count,indent,collapsible,collapsibleId) local breakdowns = mw.loadData( 'Module:ResourceBreakdown/breakdowns' ) local baseResources = {} local recursiveReturned = {} 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-res'..collapsibleId..'"' collapsibleId = collapsibleId+1 end else fontweight = '\'\'\'' iconsize = '25' end

local roundedCnt = math.ceil(count) if roundedCnt ~= count then roundedCnt = ' '..roundedCnt..' ' end

local resourceBreakdown = {' '..fontweight..roundedCnt..' × '..mw.getCurrentFrame:expandTemplate{title='ItemLink', args={string.gsub(resource,':',' '), iconsize}}..fontweight} -- if resource is combined, list components if breakdowns[resource] ~= nil then for res,cnt in pairs(breakdowns[resource]) do     recursiveReturned = p.breakdown(res,cnt*count,indent+1,collapsible,collapsibleId) collapsibleId = recursiveReturned[3] table.insert(resourceBreakdown,recursiveReturned[1]) -- insert base resources for bres,bcnt in pairs(recursiveReturned[2]) do       if baseResources[bres] == nil then baseResources[bres] = 0 end -- add count to baseresource baseResources[bres] = baseResources[bres] + bcnt end end else -- no breakdown-data found, assuming it's a base resource, add it to basecount if baseResources[resource] == nil then baseResources[resource] = 0 end -- add count to baseresource baseResources[resource] = baseResources[resource] + count end return {table.concat(resourceBreakdown,'\n')..' ',baseResources,collapsibleId} end

return p