Module:ResourceBreakdown

From ARK: Survival Evolved Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:ResourceBreakdown/doc

local p = {}
function p.breakdownList( f )
  local dlclink = require('Module:DLCLink').link
  local args = f:getParent().args
  local resourceList = {}
  local baseResources = {}
  local showBaseIngredients, collapsibleIngredients, collapsibleBaseIngredients = true, false, false
  local printCategories, categories = false, {}
  local noDlcIcon = args.noDlcIcon or false
  if noDlcIcon and ( noDlcIcon == '' or noDlcIcon == 0) then -- lua evaluates anything to true except nil and false
    noDlcIcon = false
  end
  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,noDlcIcon)
        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,'[[Category:Crafted with '..res..']]')
        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 = '<span class="hover-text" title="'..bcnt..' to be precise">'..roundedCnt..'</span>'
      end
      table.insert(baseResourcesList,roundedCnt..' × || [[File:'..string.gsub(string.gsub(bres, '[:/]', ' '), '+', '-')..'.png|30px|link='..bres..']] '..dlclink(bres,noDlcIcon))
    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..'>\'\'\'Total Base Ingredients\'\'\'<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-res'..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 collapsibleIngredients and collapsibleButton == '' then
    return '<div style="display:inline-block;margin:0.2em 1em 0.2em 0;vertical-align:top;text-align:left">'..table.concat(resourceList,'\n')..'</div>'..baseResourcesDiv..table.concat(categories)
  else
    return '<div style="display:inline-block;margin:0.2em 1em 0.2em 0;vertical-align:top;text-align:left">\'\'\'Resources breakdown\'\'\''..collapsibleButton..'\n\n'..table.concat(resourceList,'\n')..'</div>'..baseResourcesDiv..table.concat(categories)
  end
end

function p.breakdown(resource,count,indent,collapsible,collapsibleId,noDlcIcon)
  local dlclink = require('Module:DLCLink').link
  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 = '<span class="hover-text" title="'..count..' to be precise">'..roundedCnt..'</span>'
  end

  local resourceBreakdown = {'<div style="'..style..'padding-left:5px"'..collapsibleClass..'>'..fontweight..roundedCnt..' × [[File:'..string.gsub(string.gsub(string.gsub(string.gsub(resource, '[:/]', ' '), '+', '-'), 'Kibble %(Dodo Egg%)', 'Kibble %(Mobile%)'), 'Kibble %(Dilo Egg%)', 'Kibble %(Mobile%)')..'.png|'..iconsize..'px|link='..resource..']] '..dlclink(resource,noDlcIcon)..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,noDlcIcon)
      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')..'</div>',baseResources,collapsibleId}
end

return p