Anonymous

Module:Items: Difference between revisions

From Melvor Idle
5,378 bytes added ,  1 October 2020
Adding in p.getItemUseTable
(Added level requirement to Alt Magic table)
(Adding in p.getItemUseTable)
Line 908: Line 908:


   return p._getItemUses(item)
   return p._getItemUses(item)
end
function p._getItemUseTable(item)
  local useArray = {}
  --First, loop through all items to find anything that can be made or upgraded into using our source
  for i, item2 in pairs(ItemData.Items) do
    if item2.itemsRequired ~= nil then
      for j, req in pairs(item2.itemsRequired) do
        if req[1] == item.id then
          local mat = item2.itemsRequired
          local xp = 'N/A'
          local rowReq = 'None'
          --Potions do have upgrade requirements though
          if item2.potionTier ~= nil then
            local potTierMastery = {[1] = 15, [2] = 40, [3] = 65}
            rowReq = Icons._MasteryReq(item2.name, potTierMastery[item2.potionTier])
          end
          table.insert(useArray, {item = item2, qty = req[2], mats = mat, skill = 'Upgrade', req = rowReq, xp = xp, gp = item2.trimmedGPCost})
          break
        end
      end
    end
    if item2.craftReq ~= nil then
      for j, req in pairs(item2.craftReq) do
        if req.id == item.id then
          local mat = item2.craftReq
          local xp = item2.craftingXP
          local rowReq = item2.craftingLevel
          table.insert(useArray, {item = item2, qty = req[2], mats = mat, skill = 'Crafting', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.fletchReq ~= nil then
      for j, req in pairs(item2.fletchReq) do
        if req.id == item.id then
          local mat = item2.fletchReq
          local xp = item2.fletchingXP
          local rowReq = item2.fletchingLevel
          table.insert(useArray, {item = item2, qty = req[2], mats = mat, skill = 'Fletching', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.smithReq ~= nil then
      for j, req in pairs(item2.smithReq) do
        if req.id == item.id then
          local mat = item2.smithReq
          local xp = item2.smithingXP
          local rowReq = item2.smithingLevel
          table.insert(useArray, {item = item2, qty = req[2], mats = mat, skill = 'Smithing', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.runecraftReq ~= nil then
      for j, req in pairs(item2.runecraftReq) do
        if req.id == item.id then
          local mat = item2.runecraftReq
          local xp = item2.runecraftingXP
          local rowReq = item2.runecraftingLevel
          table.insert(useArray, {item = item2, qty = req[2], mats = mat, skill = 'Runecrafting', req = rowReq, xp = xp})
          break
        end
      end
    end
    if item2.herbloreReq ~= nil then
      for j, req in pairs(item2.herbloreReq) do
        if req.id == item.id then
          local potionData = SkillData.Herblore.ItemData[item2.herbloreMasteryID + 1]
          local mat = item2.herbloreReq
          local xp = potionData.herbloreXP
          local rowReq = potionData.herbloreLevel
          table.insert(useArray, {item = item2, qty = req[2], mats = mat, skill = 'Herblore', req = rowReq, xp = xp})
          break
        end
      end
    end
  end
  --Finally build the table using what we've learned
  table.sort(useArray, function(a, b)
                        if a.skill ~= b.skill then
                          return a.skill < b.skill
                        elseif type(a.req) ~= type(b.req) then
                          return tostring(a.req) < tostring(b.req)
                        elseif a.req ~= b.req then
                          return a.req < b.req
                        else
                          return a.item.name < b.item.name
                        end end)
                       
  local result = ''
  result = result..'{| class="wikitable sortable"'
  result = result..'\r\n!colspan=2|Item Created!!Type!!Requirements!!XP!!Quantity!!Ingredients'
  for i, row in pairs(useArray) do
    result = result..'\r\n|-\r\n|'..Icons.Icon({row.item.name, type='item', notext=true, size=50})..'||'..row.item.name
    local qty = row.qty ~= nil and row.qty or 1
    if row.skill == 'Upgrade' then
      result = result..'||data-sort-value="Upgrade"|[[Upgrading Items|Upgrade]]'
    else
      result = result..'||data-sort-value="'..row.skill..'"|'..Icons.Icon({row.skill, type='skill'})
    end
    if type(row.req) == 'number' then
      result = result..'|| data-sort-value="'..row.req..'"|'..Icons._SkillReq(row.skill, row.req)
    else
      result = result..'||'..row.req
    end
    if type(row.xp) == 'string' then
      result = result..'||data-sort-value="0"|'..row.xp
    else
      result = result..'||data-sort-value="'..row.xp..'"|'..row.xp..' '..Icons.Icon({row.skill, type='skill', notext=true})..' XP'
    end
    result = result..'||'..qty..'||'
    for i, mat in pairs(row.mats) do
      local matID = mat.id ~= nil and mat.id or mat[1]
      local matQty = mat.qty ~= nil and mat.qty or mat[2]
      matItem = p.getItemByID(matID)
      if i > 1 then result = result..'<br/>' end
      result = result..Icons.Icon({matItem.name, type='item', qty=matQty})
    end
    if row.gp ~= nil then result = result..'<br/>'..Icons.GP(row.gp) end
  end
  result = result..'\r\n|}'
  return result
end
function p.getItemUseTable(frame)
  local itemName = frame.args ~= nil and frame.args[1] or frame
  local item = p.getItem(itemName)
  if item == nil then
    return "ERROR: No item named "..itemName.." exists in the data module"
  end
  return p._getItemUseTable(item)
end
end