Module:Items: Difference between revisions

From Melvor Idle
(Added support for stab/slash/block bonuses)
(Added getWeaponAttackType)
Line 24: Line 24:
   local ZeroIfNil = args.ForceZero ~= nil and args.ForceZero ~= '' and args.ForceZero ~= 'false'
   local ZeroIfNil = args.ForceZero ~= nil and args.ForceZero ~= '' and args.ForceZero ~= 'false'
   local item = p.getItem(ItemName)
   local item = p.getItem(ItemName)
   if item ~= nil then
   if item == nil then
     local result = item[StatName]
     return "ERROR: No item named "..ItemName.." exists in the data module"
    --Special Overrides:
  end
    if StatName == 'stabBonus' then
  local result = item[StatName]
      if item.attackBonus == nil then  
  --Special Overrides:
        result = nil
  if StatName == 'stabAttackBonus' then
      else
    if item.attacBonus == nil then  
        result = item.attackBonus[1]
      result = nil
      end
    else
    elseif StatName == 'slashBonus' then
      result = item.attackBonus[1]
      if item.attackBonus == nil then  
    end
        result = nil
  elseif StatName == 'slashAttackBonus' then
      else
    if item.attackBonus == nil then  
        result = item.attackBonus[2]
      result = nil
      end
    else
    elseif StatName == 'blockBonus' then
      result = item.attackBonus[2]
      if item.attackBonus == nil then  
    end
        result = nil
  elseif StatName == 'blockAttackBonus' then
      else
    if item.attackBonus == nil then  
        result = item.attackBonus[3]
      result = nil
      end
    else
      result = item.attackBonus[3]
     end
     end
  end
  if result == nil and ZeroIfNil then result = 0 end
  return result
end


 
function p.getWeaponAttackType(frame)
     if result == nil and ZeroIfNil then result = 0 end
  local itemName = frame.args ~= nil and frame.args[1] or frame
     return result
  local item = p.getItem(itemName)
  if item == nil then
     return "ERROR: No item named "..ItemName.." exists in the data module"
  end
  if item.type == 'Weapon' then
    return Icons.Icon({'Melee'})
  elseif item.type == 'Ranged Weapon' then
    return Icons.Icon({'Ranged', type='skill'})
  elseif item.type == 'Magic Staff' or item.type == 'Magic Wand' then
     return Icons.Icon({'Magic', type='skill'})
   else
   else
     return "ERROR: No item named "..ItemName.." exists in the data module"
     return "Invalid"
   end
   end
end
end


return p
return p

Revision as of 02:53, 18 September 2020

Lua module for generating various item tables. Pulls data from Module:GameData/data


local p = {}

local ItemData = mw.loadData('Module:Items/data')

local Shared = require('Module:Shared')
local Icons = require('Module:Icons')

function p.getItem(name)
  local result = nil
  for i, item in pairs(ItemData) do
    if(item.name == name) then
      result = Shared.clone(item)
      --Make sure every item has an id, and account for Lua being 1-index
      result.id = i -1
    end
  end
  return result
end

function p.getItemStat(frame)
  local args = frame.args ~= nil and frame.args or frame
  local ItemName = args[1]
  local StatName = args[2]
  local ZeroIfNil = args.ForceZero ~= nil and args.ForceZero ~= '' and args.ForceZero ~= 'false'
  local item = p.getItem(ItemName)
  if item == nil then
    return "ERROR: No item named "..ItemName.." exists in the data module"
  end
  local result = item[StatName]
  --Special Overrides:
  if StatName == 'stabAttackBonus' then
    if item.attacBonus == nil then 
      result = nil
    else
      result = item.attackBonus[1]
    end
  elseif StatName == 'slashAttackBonus' then
    if item.attackBonus == nil then 
      result = nil
    else
      result = item.attackBonus[2]
    end
  elseif StatName == 'blockAttackBonus' then
    if item.attackBonus == nil then 
      result = nil
    else
      result = item.attackBonus[3]
    end
  end
  if result == nil and ZeroIfNil then result = 0 end
  return result
end

function p.getWeaponAttackType(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
  if item.type == 'Weapon' then
    return Icons.Icon({'Melee'})
  elseif item.type == 'Ranged Weapon' then
    return Icons.Icon({'Ranged', type='skill'})
  elseif item.type == 'Magic Staff' or item.type == 'Magic Wand' then
    return Icons.Icon({'Magic', type='skill'})
  else
    return "Invalid"
  end
end

return p