Module:Items/ComparisonTables: Difference between revisions

From Melvor Idle
(Added getItemUpgradeTable)
(Added level requirements to getItemUpgradeTable's comparisons)
Line 370: Line 370:
   local SlayBon2 = Items._getItemModifier(item2, 'increasedSkillXP', 'Slayer')
   local SlayBon2 = Items._getItemModifier(item2, 'increasedSkillXP', 'Slayer')
   getSpecificStatString(SlayBon1, SlayBon2, '{V}% '..Icons.Icon({'Slayer', type='skill', notext=true})..' Bonus XP')
   getSpecificStatString(SlayBon1, SlayBon2, '{V}% '..Icons.Icon({'Slayer', type='skill', notext=true})..' Bonus XP')
  getSpecificStatString(item1.attackLevelRequired, item1.attackLevelRequired, '{V} '..Icons.Icon({'Attack', type='skill', notext=true})..' Level Required')
  getSpecificStatString(item1.defenceLevelRequired, item1.defenceLevelRequired, '{V} '..Icons.Icon({'Defence', type='skill', notext=true})..' Level Required')
  getSpecificStatString(item1.rangedLevelRequired, item1.rangedLevelRequired, '{V} '..Icons.Icon({'Ranged', type='skill', notext=true})..' Level Required')
  getSpecificStatString(item1.magicLevelRequired, item1.magicLevelRequired, '{V} '..Icons.Icon({'Magic', type='skill', notext=true})..' Level Required')


   return table.concat(changeArray, '<br/>')
   return table.concat(changeArray, '<br/>')

Revision as of 22:20, 22 March 2021

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

local p = {}

local MonsterData = mw.loadData('Module:Monsters/data')
local ItemData = mw.loadData('Module:Items/data')
local SkillData = mw.loadData('Module:Skills/data')

local Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local Magic = require('Module:Magic')
local Areas = require('Module:CombatAreas')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local ItemSourceTables = require('Module:Items/SourceTables')
local ItemUseTables = require('Module:Items/UseTables')

local weaponTypes = {'Magic Staff', 'Magic Wand', 'Ranged Weapon', 'Weapon'}

function p._getEquipmentTable(itemList)
  --Getting some lists set up here that will be used later
  --First, the list of columns used by both weapons & armour
  local statColumns = {'stabAttackBonus', 'slashAttackBonus','blockAttackBonus','rangedAttackBonus', 'magicAttackBonus', 'strengthBonus', 'rangedStrengthBonus', 'magicDamageBonus', 'defenceBonus', 'rangedDefenceBonus', 'magicDefenceBonus', 'damageReduction', 'attackLevelRequired', 'defenceLevelRequired', 'rangedLevelRequired', 'magicLevelRequired'}

  if(Shared.tableCount(itemList) == 0) then
    return 'ERROR: you must select at least one item to get stats for'
  end

  local isWeaponType = Shared.contains(weaponTypes, itemList[1].type)

  --Now that we have a preliminary list, let's figure out which columns are irrelevant (IE are zero for all items in the selection)
  local ignoreColumns = Shared.clone(statColumns)
  for i, item in pairs(itemList) do
    local ndx = 1
    while Shared.tableCount(ignoreColumns) >= ndx do
      if Items._getItemStat(item, ignoreColumns[ndx], true) ~= 0 then
        table.remove(ignoreColumns, ndx)
      else
        ndx = ndx + 1
      end
    end
  end

  --Now to remove the ignored columns (and also we need to track groups like defence bonuses to see how many remain)
  local attBonusCols = 5
  local strBonusCols = 2
  local defBonusCols = 3
  local lvlReqCols = 4
  local ndx = 1
  while Shared.tableCount(statColumns) >= ndx do
    local colName = statColumns[ndx]
    if Shared.contains(ignoreColumns, colName) then
      if Shared.contains(colName, 'AttackBonus') then attBonusCols = attBonusCols - 1 end
      if Shared.contains(colName, 'trengthBonus') then strBonusCols = strBonusCols - 1 end
      if Shared.contains(colName, 'efenceBonus') then defBonusCols = defBonusCols - 1 end
      if Shared.contains(colName, 'LevelRequired') then lvlReqCols = lvlReqCols - 1 end
      table.remove(statColumns, ndx)
    else
      ndx = ndx + 1
    end
  end
  
  --Alright, let's start the table by building the shared header
  local result = '{| class="wikitable sortable stickyHeader"\r\n|-class="headerRow-0"'
  if isWeaponType then
    --Weapons have extra columns here for Attack Speed and "Two Handed?"
    result = result..'\r\n!colspan="4"|'
  else
    result = result..'\r\n!colspan="2"|'
  end
  if attBonusCols > 0 then
    result = result..'\r\n!colspan="'..attBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Attack Bonus'
  end
  if strBonusCols > 0 then
    result = result..'\r\n!colspan="'..strBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Str. Bonus'
  end
  if Shared.contains(statColumns, 'magicDamageBonus') then
    result = result..'\r\n!colspan="1"style="padding:0 0.5em 0 0.5em;"|% Damage Bonus'
  end
  if defBonusCols > 0 then
    result = result..'\r\n!colspan="'..defBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Defence Bonus'
  end
  if Shared.contains(statColumns, 'damageReduction') then
    result = result..'\r\n!colspan="1"style="padding:0 0.5em 0 0.5em;"|DR'
  end
  if lvlReqCols > 0 then
    result = result..'\r\n!colspan="'..lvlReqCols..'"style="padding:0 0.5em 0 0.5em;"|Lvl Req'
  end
  result = result..'\r\n!colspan="1"|'
  --One header row down, one to go
  result = result..'\r\n|-class="headerRow-1"'
  result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Item'
  result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Name'
  --Weapons have Attack Speed here
  if isWeaponType then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Attack Speed'
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Two Handed?'
  end
  --Attack bonuses
  if Shared.contains(statColumns, 'slashAttackBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Attack', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'stabAttackBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Strength', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'blockAttackBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'rangedAttackBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'magicAttackBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'})
  end
  --Strength bonuses
  if Shared.contains(statColumns, 'strengthBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Strength', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'rangedStrengthBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'magicDamageBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'})
  end
  --Defence bonuses
  if Shared.contains(statColumns, 'defenceBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'rangedDefenceBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'magicDefenceBonus') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'damageReduction') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'})
  end
  --Level requirements
  if Shared.contains(statColumns, 'attackLevelRequired') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Attack', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'defenceLevelRequired') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'rangedLevelRequired') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'})
  end
  if Shared.contains(statColumns, 'magicLevelRequired') then
    result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'})
  end
  --And finally Sources
  result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Sources'

  table.sort(itemList, function(a, b) return a.id < b.id end)
  for i, item in pairs(itemList) do
    if isWeaponType then
      --Building rows for weapons
      result = result..'\r\n|-'
      result = result..'\r\n|style ="text-align: left;padding: 0 0 0 0;"|'..Icons.Icon({item.name, type='item', size=50, notext=true})
      result = result..'\r\n|style ="text-align: left;padding: 0 0.5em 0 0.5em;"|[['..item.name..']]'
      result = result..'\r\n| style ="text-align: right;padding: 0 0.5em 0 0;" |'..Shared.formatnum(item.attackSpeed)
      --That's the first list out of the way, now for 2-Handed
      result = result..'\r\n| style ="text-align: right;"|'
      if item.isTwoHanded then result = result..'Yes' else result = result..'No' end
      for j, statName in pairs(statColumns) do
        local statValue = Items._getItemStat(item, statName, true)
        result = result..'\r\n| style ="text-align: right;padding: 0 0.5em 0 0;'
        if not Shared.contains(statName, 'LevelRequired') then
          if statValue > 0 then
            result = result..'background-color:lightgreen;'
          elseif statValue < 0 then
            result = result..'background-color:lightpink;'
          end
        end
        result = result..'"|'..Shared.formatnum(statValue)
        if statName == 'magicDamageBonus' or statName == 'damageReduction' then result = result..'%' end
      end
      --Finally, the Sources
      result = result..'\r\n| style ="text-align: right;white-space: nowrap;padding: 0 0.5em 0 0.5em;" |'
      result = result..ItemSourceTables._getItemSources(item)
    else
      --Building rows for armour
      result = result..'\r\n|-'
      result = result..'\r\n|style ="text-align: left;padding: 0 0 0 0;"|'..Icons.Icon({item.name, type='item', size=50, notext=true})
      result = result..'\r\n|style ="text-align: left;padding: 0 0.5em 0 0.5em;"|[['..item.name..']]'
      for j, statName in pairs(statColumns) do
        local statValue = Items._getItemStat(item, statName, true)
        result = result..'\r\n| style ="text-align: right;padding: 0 0.5em 0 0;'
        if statValue > 0 then
          result = result..'background-color:lightgreen;'
        elseif statValue < 0 then
          result = result..'background-color:lightpink;'
        end
        result = result..'"|'..Shared.formatnum(statValue)
        if statName == 'magicDamageBonus' or statName == 'damageReduction' then result = result..'%' end
      end
      --Finally, the Sources
      result = result..'\r\n| style ="text-align: right;white-space: nowrap;padding: 0 0.5em 0 0.5em;" |'
      result = result..ItemSourceTables._getItemSources(item)
    end
  end

  result = result..'\r\n|}'
  return result
end

function p.getEquipmentTable(frame)
  local args = frame.args ~= nil and frame.args or frame
  local type = args.type
  local tier = args.tier
  local slotStr = args.slot
  local ammoTypeStr = args.ammoType
  local category = args.category ~= nil and args.category or 'Combat'

   --Find out what Ammo Type we're working with
  local ammoType = nil
  if ammoTypeStr ~= nil then
    if ammoTypeStr == "Arrows" then
      ammoType = 0
    elseif ammoTypeStr == 'Bolts' then
      ammoType = 1
    elseif ammoTypeStr == 'Javelins' then
      ammoType = 2
    elseif ammoTypeStr == 'Throwing Knives' then
      ammoType = 3
    end
  end

  --Find out what slot we're working with
  local slot = nil
  if slotStr ~= nil then
    slot = Constants.getEquipmentSlotID(slotStr)
  end
  
  local isWeaponType = Shared.contains(weaponTypes, type)

  --Now we need to figure out which items are in this list
  local itemList = {}
  for i, itemBase in pairs(ItemData.Items) do
    local item = Shared.clone(itemBase)
    item.id = i - 1
    local listItem = false
    if isWeaponType then
    listItem = item.type == type and item.category == category
      if ammoType ~= nil then listItem = listItem and item.ammoTypeRequired == ammoType end
    else
      --Now for handling armour
      if type == "Armour" or type == "Melee" then
        listItem = item.defenceLevelRequired ~= nil or (item.category == 'Combat' and item.type == 'Armour')
      elseif type == "Ranged Armour" or type == "Ranged" then
        listItem = item.rangedLevelRequired ~= nil or (item.category == 'Combat' and item.type == 'Ranged Armour')
      elseif type == "Magic Armour" or type == "Magic" then
        listItem = item.magicLevelRequired ~= nil or (item.category == 'Combat' and item.type == 'Magic Armour')
      else
        listItem = item.type == type and item.category ~= 'Combat'
      end
      if ammoType ~= nil then listItem = listItem and item.ammoType == ammoType end
      if slot ~= nil then listItem = listItem and item.equipmentSlot == slot end
    end
    if listItem then
      table.insert(itemList, item)
    end
  end

  return p._getEquipmentTable(itemList)
end

function p.getTableForList(frame)
  local stuffString = frame.args ~= nil and frame.args[1] or frame
  local itemNames = Shared.splitString(stuffString, ',')

  local itemList = {}
  local errMsg = 'ERROR: Some items not found in database: '
  local hasErr = false
  for i, name in Shared.skpairs(itemNames) do
    local nextItem = Items.getItem(Shared.trim(name))
    if nextItem == nil then
      errMsg = errMsg.." '"..name.."'"
      hasErr = true
    else
      table.insert(itemList, nextItem)
    end
  end

  if hasErr then
    return errMsg
  else
    return p._getEquipmentTable(itemList)
  end
end

function p.getDoubleLootTable(frame)
  local itemList = Items.getItems(function(item) return item.chanceToDoubleLoot ~= nil and item.chanceToDoubleLoot > 0 end)
  table.sort(itemList, function(a, b) return a.id < b.id end)
  local result = '{| class="wikitable sortable stickyHeader"\r\n|-class="headerRow-0"'
  result = result..'\r\n!colspan="2"|Name!!Bonus!!Description!!Sources'
  for i, item in Shared.skpairs(itemList) do
    result = result..'\r\n|-'
    result = result..'\r\n|data-sort-value="'..item.name..'"|'..Icons.Icon({item.name, type='item', size=50, notext=true})
    result = result..'||[['..item.name..']]'
    result = result..'||style ="text-align: right;" data-sort-value="'..item.chanceToDoubleLoot..'"|'..item.chanceToDoubleLoot..'%'
    result = result..'||'..item.description
    result = result..'\r\n| style ="text-align: right;white-space: nowrap;padding: 0 0.5em 0 0.5em;" |'
    result = result..ItemSourceTables._getItemSources(item)
  end

  result = result..'\r\n|}'
  return result
end

function p.getItemTableRows(frame)
  local startID = frame.args ~= nil and frame.args[1] or frame[1]
  local rowCount = frame.args ~= nil and frame.args[2] or frame[2]
  if type(startID ) == 'string' then startID = tonumber(startID) end
  if rowCount == nil then rowCount = 200 end
  if type(rowCount) == 'string' then rowCount = tonumber(rowCount) end
  
  local rowResult = {}
  for i = startID, startID + rowCount - 1, 1 do
    local item = Items.getItemByID(i)
    if item == nil then break end
    local rowTxt = '|-\r\n|'..Icons.Icon({item.name, type='item', size='50', notext=true})..'||[['..item.name..']]'
    rowTxt = rowTxt..'||style="text-align:right;"|'..i..'||'..item.category..'||'..item.type
    rowTxt = rowTxt..'||style="text-align:right;" data-sort-value="'..item.sellsFor..'"|'..Icons.GP(item.sellsFor)
    rowTxt = rowTxt..'||style="text-align:right;"|'..ItemSourceTables._getItemSources(item, false, 'false')
    rowTxt = rowTxt..'||style="text-align:right;"|'..ItemUseTables._getItemUses(item, false, 'false')
    table.insert(rowResult, rowTxt)
  end

  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')

  getSpecificStatString(item1.attackLevelRequired, item1.attackLevelRequired, '{V} '..Icons.Icon({'Attack', type='skill', notext=true})..' Level Required')
  getSpecificStatString(item1.defenceLevelRequired, item1.defenceLevelRequired, '{V} '..Icons.Icon({'Defence', type='skill', notext=true})..' Level Required')
  getSpecificStatString(item1.rangedLevelRequired, item1.rangedLevelRequired, '{V} '..Icons.Icon({'Ranged', type='skill', notext=true})..' Level Required')
  getSpecificStatString(item1.magicLevelRequired, item1.magicLevelRequired, '{V} '..Icons.Icon({'Magic', type='skill', notext=true})..' Level Required')

  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

return p