Module:Items/ComparisonTables: Difference between revisions

Added getItemUpgradeTable
(Final tweaks)
(Added getItemUpgradeTable)
Line 4: Line 4:
local ItemData = mw.loadData('Module:Items/data')
local ItemData = mw.loadData('Module:Items/data')
local SkillData = mw.loadData('Module:Skills/data')
local SkillData = mw.loadData('Module:Skills/data')
local Constants = mw.loadData('Module:Constants/data')


local Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local Magic = require('Module:Magic')
local Magic = require('Module:Magic')
Line 228: Line 228:
   local slot = nil
   local slot = nil
   if slotStr ~= nil then
   if slotStr ~= nil then
     slot = Constants.equipmentSlot[slotStr]
     slot = Constants.getEquipmentSlotID(slotStr)
   end
   end
    
    
Line 327: Line 327:


   return table.concat(rowResult, '\r\n')
   return table.concat(rowResult, '\r\n')
end
function p.numStrWithSign(number)
  if number >= 0 then
    return '+'..Shared.formatnum(number)
  else
    return Shared.formatnum(number)
  end
end
function p.getStatChangeString(item1, item2)
  --Used by getItemUpgradeTable to get the stat change between two items
  local changeArray = {}
  local getSpecificStatString = function(val1, val2, subStr)
      if val1 == nil then val1 = 0 end
      if val2 == nil then val2 = 0 end
      if val1 ~= val2 then
        local txt = string.gsub(subStr, '{V}', p.numStrWithSign(val1 - val2))
        table.insert(changeArray, txt)
      end
    end
  --Unfortunately just gonna have to manually check all the changes I think...
  local attBon1 = item1.attackBonus ~= nil and item1.attackBonus or {0, 0, 0}
  local attBon2 = item2.attackBonus ~= nil and item2.attackBonus or {0, 0, 0}
  getSpecificStatString(attBon1[1], attBon2[1], '{V} '..Icons.Icon({'Melee', notext=true})..' Stab Bonus')
  getSpecificStatString(attBon1[2], attBon2[2], '{V} '..Icons.Icon({'Melee', notext=true})..' Slash Bonus')
  getSpecificStatString(attBon1[3], attBon2[3], '{V} '..Icons.Icon({'Melee', notext=true})..' Block Bonus')
  getSpecificStatString(item1.strengthBonus, item2.strengthBonus, '{V} '..Icons.Icon({'Strength', type='skill', notext=true})..' Strength Bonus')
  getSpecificStatString(item1.rangedStrengthBonus, item2.rangedStrengthBonus, '{V} '..Icons.Icon({'Ranged', type='skill', notext=true})..' Strength Bonus')
  getSpecificStatString(item1.magicDamageBonus, item2.magicDamageBonus, '{V}% '..Icons.Icon({'Magic', type='skill', notext=true})..' Damage Bonus')
  getSpecificStatString(item1.defenceBonus, item2.defenceBonus, '{V} '..Icons.Icon({'Defence', type='skill', notext=true})..' Defence Bonus')
  getSpecificStatString(item1.rangedDefenceBonus, item2.rangedDefenceBonus, '{V} '..Icons.Icon({'Ranged', type='skill', notext=true})..' Defence Bonus')
  getSpecificStatString(item1.magicDefenceBonus, item2.magicDefenceBonus, '{V} '..Icons.Icon({'Magic', type='skill', notext=true})..' Defence Bonus')
  getSpecificStatString(item1.damageReduction, item2.damageReduction, '{V}% Damage Reduction')
  local SlayBon1 = Items._getItemModifier(item1, 'increasedSkillXP', 'Slayer')
  local SlayBon2 = Items._getItemModifier(item2, 'increasedSkillXP', 'Slayer')
  getSpecificStatString(SlayBon1, SlayBon2, '{V}% '..Icons.Icon({'Slayer', type='skill', notext=true})..' Bonus XP')
  return table.concat(changeArray, '<br/>')
end
function p.getItemUpgradeTable(frame)
  local category = frame.args ~= nil and frame.args[1] or frame
  local itemArray = {}
  local isEquipment = false
  if string.upper(category) == 'POTION' then
    itemArray = Items.getItems(function(item) return Shared.contains(item.name, 'Potion') and item.itemsRequired ~= nil end)
  elseif string.upper(category) == 'OTHER' then
    itemArray = Items.getItems(function(item)
          return not Shared.contains(item.name, 'Potion') and item.itemsRequired ~= nil and item.equipmentSlot == nil
        end)
  else
    local equipSlot = Constants.getEquipmentSlotID(category)
    if equipSlot == nil then
      return 'ERROR: Invalid option. Choose either an equipment slot, Potion, or Other[[Category:Pages with script errors]]'
    end
    isEquipment = true
    itemArray = Items.getItems(function(item)
          return item.itemsRequired ~= nil and item.equipmentSlot == equipSlot
        end)
  end
  table.sort(itemArray, function(a, b) return a.id < b.id end)
  local result = '{| class="wikitable sortable stickyHeader"'
  result = result..'\r\n|- class="headerRow-0"'
  result = result..'\r\n!colspan="2"|Upgraded Item!!Ingredients'
  if isEquipment then result = result..'!!Stat Change' end
  for i, item in Shared.skpairs(itemArray) do
    result = result..'\r\n|-'
    result = result..'\r\n|'..Icons.Icon({item.name, type='item', size='50', notext=true})..'||'..item.name
    local matArray = {}
    local statChangeString = ''
    for i, row in Shared.skpairs(item.itemsRequired) do
      local mat = Items.getItemByID(row[1])
      table.insert(matArray, Icons.Icon({mat.name, type='item', qty=row[2]}))
      if item.equipmentSlot ~= nil and mat.equipmentSlot ~= nil and statChangeString == '' then
        statChangeString = p.getStatChangeString(item, mat)
      end
    end
    if item.trimmedGPCost ~= nil and item.trimmedGPCost > 0 then
      table.insert(matArray, Icons.GP(item.trimmedGPCost))
    end
    result = result..'||'..table.concat(matArray, '<br/>')
    if isEquipment then
      result = result..'||'..statChangeString
    end
  end
  result = result..'\r\n|}'
  return result
end
end


return p
return p