Module:Items/ComparisonTables: Difference between revisions

_getEquipmentTable: Avoid repeated string concatenation in an attempt to reduce memory usage
m (Fix case issue)
(_getEquipmentTable: Avoid repeated string concatenation in an attempt to reduce memory usage)
Line 69: Line 69:


   --Alright, let's start the table by building the shared header
   --Alright, let's start the table by building the shared header
   local result = '{| class="wikitable sortable stickyHeader"\r\n|-class="headerRow-0"'
   local resultPart = {}
  table.insert(resultPart, '{| class="wikitable sortable stickyHeader"\r\n|-class="headerRow-0"')
   if isWeaponType then
   if isWeaponType then
     --Weapons have extra columns here for Attack Speed and "Two Handed?"
     --Weapons have extra columns here for Attack Speed and "Two Handed?"
     result = result..'\r\n!colspan="4"|'
     table.insert(resultPart, '\r\n!colspan="4"|')
   else
   else
     result = result..'\r\n!colspan="2"|'
     table.insert(resultPart, '\r\n!colspan="2"|')
   end
   end
   if attBonusCols > 0 then
   if attBonusCols > 0 then
     result = result..'\r\n!colspan="'..attBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Attack Bonus'
     table.insert(resultPart, '\r\n!colspan="'..attBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Attack Bonus')
   end
   end
   if strBonusCols > 0 then
   if strBonusCols > 0 then
     result = result..'\r\n!colspan="'..strBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Str. Bonus'
     table.insert(resultPart, '\r\n!colspan="'..strBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Str. Bonus')
   end
   end
   if Shared.contains(statColumns, 'magicDamageBonus') then
   if Shared.contains(statColumns, 'magicDamageBonus') then
     result = result..'\r\n!colspan="1"style="padding:0 0.5em 0 0.5em;"|% Dmg Bonus'
     table.insert(resultPart, '\r\n!colspan="1"style="padding:0 0.5em 0 0.5em;"|% Dmg Bonus')
   end
   end
   if defBonusCols > 0 then
   if defBonusCols > 0 then
     result = result..'\r\n!colspan="'..defBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Defence Bonus'
     table.insert(resultPart, '\r\n!colspan="'..defBonusCols..'"style="padding:0 0.5em 0 0.5em;"|Defence Bonus')
   end
   end
   if Shared.contains(statColumns, 'damageReduction') then
   if Shared.contains(statColumns, 'damageReduction') then
     result = result..'\r\n!colspan="1"style="padding:0 0.5em 0 0.5em;"|DR'
     table.insert(resultPart, '\r\n!colspan="1"style="padding:0 0.5em 0 0.5em;"|DR')
   end
   end
   if lvlReqCols > 0 then
   if lvlReqCols > 0 then
     result = result..'\r\n!colspan="'..lvlReqCols..'"style="padding:0 0.5em 0 0.5em;"|Lvl Req'
     table.insert(resultPart, '\r\n!colspan="'..lvlReqCols..'"style="padding:0 0.5em 0 0.5em;"|Lvl Req')
   end
   end
   if includeModifiers and includeDescription then
   if includeModifiers and includeDescription then
     result = result..'\r\n!colspan="3"|'
     table.insert(resultPart, '\r\n!colspan="3"|')
   elseif includeModifiers or includeDescription then
   elseif includeModifiers or includeDescription then
     result = result..'\r\n!colspan="2"|'
     table.insert(resultPart, '\r\n!colspan="2"|')
   else
   else
     result = result..'\r\n!colspan="1"|'
     table.insert(resultPart, '\r\n!colspan="1"|')
   end
   end
   --One header row down, one to go
   --One header row down, one to go
   result = result..'\r\n|-class="headerRow-1"'
   table.insert(resultPart, '\r\n|-class="headerRow-1"')
   result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Item'
   table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|Item')
   result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Name'
   table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|Name')
   --Weapons have Attack Speed here
   --Weapons have Attack Speed here
   if isWeaponType then
   if isWeaponType then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Attack Speed'
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|Attack Speed')
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Two Handed?'
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|Two Handed?')
   end
   end
   --Attack bonuses
   --Attack bonuses
   if Shared.contains(statColumns, 'slashAttackBonus') then
   if Shared.contains(statColumns, 'slashAttackBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Attack', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Attack', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'stabAttackBonus') then
   if Shared.contains(statColumns, 'stabAttackBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Strength', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Strength', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'blockAttackBonus') then
   if Shared.contains(statColumns, 'blockAttackBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'rangedAttackBonus') then
   if Shared.contains(statColumns, 'rangedAttackBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'magicAttackBonus') then
   if Shared.contains(statColumns, 'magicAttackBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'}))
   end
   end
   --Strength bonuses
   --Strength bonuses
   if Shared.contains(statColumns, 'strengthBonus') then
   if Shared.contains(statColumns, 'strengthBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Strength', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Strength', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'rangedStrengthBonus') then
   if Shared.contains(statColumns, 'rangedStrengthBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'magicDamageBonus') then
   if Shared.contains(statColumns, 'magicDamageBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'}))
   end
   end
   --Defence bonuses
   --Defence bonuses
   if Shared.contains(statColumns, 'defenceBonus') then
   if Shared.contains(statColumns, 'defenceBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'rangedDefenceBonus') then
   if Shared.contains(statColumns, 'rangedDefenceBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'magicDefenceBonus') then
   if Shared.contains(statColumns, 'magicDefenceBonus') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'damageReduction') then
   if Shared.contains(statColumns, 'damageReduction') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'}))
   end
   end
   --Level requirements
   --Level requirements
   if Shared.contains(statColumns, 'attackLevelRequired') then
   if Shared.contains(statColumns, 'attackLevelRequired') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Attack', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Attack', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'defenceLevelRequired') then
   if Shared.contains(statColumns, 'defenceLevelRequired') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Defence', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'rangedLevelRequired') then
   if Shared.contains(statColumns, 'rangedLevelRequired') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Ranged', type='skill', notext='true'}))
   end
   end
   if Shared.contains(statColumns, 'magicLevelRequired') then
   if Shared.contains(statColumns, 'magicLevelRequired') then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'})
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|'..Icons.Icon({'Magic', type='skill', notext='true'}))
   end
   end
   --If includeModifiers is set to 'true', add the Modifiers column
   --If includeModifiers is set to 'true', add the Modifiers column
   if includeModifiers then
   if includeModifiers then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Modifiers'
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|Modifiers')
   end
   end
   --If includeDescription is set to 'true', add the Description column
   --If includeDescription is set to 'true', add the Description column
   if includeDescription then
   if includeDescription then
     result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Description'
     table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|Description')
   end
   end


   --And finally Sources
   --And finally Sources
   result = result..'\r\n!style="padding:0 1em 0 0.5em;"|Sources'
   table.insert(resultPart, '\r\n!style="padding:0 1em 0 0.5em;"|Sources')


   local getStatValue = function(item, statName, equipStats, levelReqs)
   local getStatValue = function(item, statName, equipStats, levelReqs)
Line 193: Line 194:
     if isWeaponType then
     if isWeaponType then
       --Building rows for weapons
       --Building rows for weapons
       result = result..'\r\n|-'
       table.insert(resultPart, '\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})
       table.insert(resultPart, '\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..']]'
       table.insert(resultPart, '\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(equipStats.attackSpeed)
       table.insert(resultPart, '\r\n| style ="text-align: right;padding: 0 0.5em 0 0;" |'..Shared.formatnum(equipStats.attackSpeed))
       --That's the first list out of the way, now for 2-Handed
       --That's the first list out of the way, now for 2-Handed
       result = result..'\r\n| style ="text-align: right;"|'
       table.insert(resultPart, '\r\n| style ="text-align: right;"|')
       if Items._getItemStat(item, 'isTwoHanded') then result = result..'Yes' else result = result..'No' end
       table.insert(resultPart, Items._getItemStat(item, 'isTwoHanded') and 'Yes' or 'No')
       for j, statName in pairs(statColumns) do
       for j, statName in pairs(statColumns) do
         local statValue = getStatValue(item, statName, equipStats, levelReqs)
         local statValue = getStatValue(item, statName, equipStats, levelReqs)
         result = result..'\r\n| style ="text-align: right;padding: 0 0.5em 0 0;'
         table.insert(resultPart, '\r\n| style ="text-align: right;padding: 0 0.5em 0 0;')
         if string.find(statName, '^(.+)LevelRequired$') == nil then
         if string.find(statName, '^(.+)LevelRequired$') == nil then
           if statValue > 0 then
           if statValue > 0 then
             result = result..'background-color:lightgreen;'
             table.insert(resultPart, 'background-color:lightgreen;')
           elseif statValue < 0 then
           elseif statValue < 0 then
             result = result..'background-color:lightpink;'
             table.insert(resultPart, 'background-color:lightpink;')
           end
           end
         end
         end
         result = result..'"|'..Shared.formatnum(statValue)
         table.insert(resultPart, '"|'..Shared.formatnum(statValue))
         if statName == 'magicDamageBonus' or statName == 'damageReduction' then result = result..'%' end
         if statName == 'magicDamageBonus' or statName == 'damageReduction' then table.insert(resultPart, '%') end
       end
       end
       --If requested, add the item Modifiers
       --If requested, add the item Modifiers
       if includeModifiers then
       if includeModifiers then
         result = result..'\r\n|style="text-align:left;white-space:nowrap;padding:0 0.5em 0 0.5em;"|'
         table.insert(resultPart, '\r\n|style="text-align:left;white-space:nowrap;padding:0 0.5em 0 0.5em;"|')
         result = result..Constants.getModifiersText(item.modifiers, true)
         table.insert(resultPart, Constants.getModifiersText(item.modifiers, true))
       end
       end
       --If requested, add description
       --If requested, add description
       if includeDescription then
       if includeDescription then
         result = result..'\r\n|style="text-align:left;padding:0 0.5em 0 0.5em;"|'
         table.insert(resultPart, '\r\n|style="text-align:left;padding:0 0.5em 0 0.5em;"|')
         result = result..(item.description ~= nil and item.description or '')
         table.insert(resultPart, item.description ~= nil and item.description or '')
       end
       end
       --Finally, the Sources
       --Finally, the Sources
       result = result..'\r\n| style ="text-align: right;padding: 0 0.5em 0 0.5em;" |'
       table.insert(resultPart, '\r\n| style ="text-align: right;padding: 0 0.5em 0 0.5em;" |')
       result = result..ItemSourceTables._getItemSources(item)
       table.insert(resultPart, ItemSourceTables._getItemSources(item))
     else
     else
       --Building rows for armour
       --Building rows for armour
       result = result..'\r\n|-'
       table.insert(resultPart, '\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})
       table.insert(resultPart, '\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..']]'
       table.insert(resultPart, '\r\n|style ="text-align: left;padding: 0 0.5em 0 0.5em;"|[['..item.name..']]')
       for j, statName in pairs(statColumns) do
       for j, statName in pairs(statColumns) do
         local statValue = getStatValue(item, statName, equipStats, levelReqs)
         local statValue = getStatValue(item, statName, equipStats, levelReqs)
         result = result..'\r\n| style ="text-align: right;padding: 0 0.5em 0 0;'
         table.insert(resultPart, '\r\n| style ="text-align: right;padding: 0 0.5em 0 0;')
         if statValue > 0 then
         if statValue > 0 then
           result = result..'background-color:lightgreen;'
           table.insert(resultPart, 'background-color:lightgreen;')
         elseif statValue < 0 then
         elseif statValue < 0 then
           result = result..'background-color:lightpink;'
           table.insert(resultPart, 'background-color:lightpink;')
         end
         end
         result = result..'"|'..Shared.formatnum(statValue)
         table.insert(resultPart, '"|'..Shared.formatnum(statValue))
         if statName == 'magicDamageBonus' or statName == 'damageReduction' then result = result..'%' end
         if statName == 'magicDamageBonus' or statName == 'damageReduction' then table.insert(resultPart, '%') end
       end
       end
       --If requested, add the item Modifiers
       --If requested, add the item Modifiers
       if includeModifiers then
       if includeModifiers then
         result = result..'\r\n|style="text-align:left;white-space:nowrap;padding:0 0.5em 0 0.5em;"|'
         table.insert(resultPart, '\r\n|style="text-align:left;white-space:nowrap;padding:0 0.5em 0 0.5em;"|')
         result = result..Constants.getModifiersText(item.modifiers, true)
         table.insert(resultPart, Constants.getModifiersText(item.modifiers, true))
       end
       end
       --If requested, add description
       --If requested, add description
       if includeDescription then
       if includeDescription then
         result = result..'\r\n|style="text-align:left;padding:0 0.5em 0 0.5em;"|'
         table.insert(resultPart, '\r\n|style="text-align:left;padding:0 0.5em 0 0.5em;"|')
         result = result..(item.description ~= nil and item.description or '')
         table.insert(resultPart, item.description ~= nil and item.description or '')
       end
       end
       --Finally, the Sources
       --Finally, the Sources
       result = result..'\r\n| style ="text-align: right;padding: 0 0.5em 0 0.5em;" |'
       table.insert(resultPart, '\r\n| style ="text-align: right;padding: 0 0.5em 0 0.5em;" |')
       result = result..ItemSourceTables._getItemSources(item)
       table.insert(resultPart, ItemSourceTables._getItemSources(item))
     end
     end
   end
   end


   result = result..'\r\n|}'
   table.insert(resultPart, '\r\n|}')
   return result
   return table.concat(resultPart)
end
end