Module:ItemInfo

-- local p = {}

local getArgs = require('Module:Arguments').getArgs local loader = require('Module:Loader')

local string, table, yesno, color, stat, rarity, ability, slot = loader.require('String', 'Table', 'Yesno', 'Color', 'Statname', 'RarityTier', 'Ability', 'Inventory slot')

local statData = mw.loadData('Module:Statname/Data')

local PAGENAME = mw.title.getCurrentTitle.text local DATA_MODULES = { -- ['Item'] = 'Item', ['Weapon'] = 'Weapon/Data', ['Armor Piece'] = 'Armor/Data', } local ARMORPIECEPREFIXES = { 'helmet', 'chestplate', 'leggings', 'boots', } local MAXABILITIES = 3 local ABILITYTYPES = { {'ability', 'ia', 'ability'}, -- ability type in Module:Ability, table in Data modules, template parameter prefix {'piece', 'pcb', 'piece_bonus'}, {'full_set', 'fsb', 'full_set_bonus'}, {'tiered_armor', 'tab', 'tiered_armor_bonus'}, } local ABILITYPROPERTIES = { {'Soulflow Cost', 'soulflow_cost', color = 'dark_aqua'}, {'Mana Cost', 'cost', color = 'dark_aqua'}, {'Health Cost', 'health_cost', color = 'red'}, {'Coin Cost', 'coin_cost', color = 'gold'}, {'Cooldown', 'cooldown', color = 'green'}, {'Cooldown', 'cooldownAlt', color = 'dark_gray'}, {'Max Souls', 'max_souls', color = 'light_purple'}, -- not in game {'Armor Pieces Required', 'required_pieces', color = 'gray'}, {'Upgraded By', 'upgraded_by', color = 'green'}, }

-- converts template arguments into a table following those in Module:Weapon/Data local function convertItemArguments(args) local data = { exists = yesno(args.exist or args.exists, true), name = args.name, slots = args.slot or args.slots, rarity = args.rarity, stats = {}, specialEffect = args.specialEffect or args.special_effects or args.special_effect or args.effects, }

-- stats for k, v in pairs(statData) do -- item stats data.stats[v.shortcode] = args[v.shortcode] -- or nil end

-- abilities for i = 1, MAXABILITIES, 1 do       local suffix = i ~= 1 and tostring(i) or ''

for _, abilityType in ipairs(ABILITYTYPES) do           if args[abilityType[3] .. '_name' .. suffix] then data[abilityType[2] .. suffix] = { name = args[abilityType[3] .. '_name' .. suffix], secret = yesno(args[abilityType[3] .. '_secret' .. suffix]), activation = args[abilityType[3] .. '_activation' .. suffix], req = args[abilityType[3] .. '_requirement' .. suffix] or args[abilityType[3] .. '_req' .. suffix], desc = args[abilityType[3] .. '_desc' .. suffix] or args[abilityType[3] .. '_description' .. suffix], }

for _, property in ipairs(ABILITYPROPERTIES) do                   data[abilityType[2] .. suffix][property[2]] = args[abilityType[2] .. '_' .. property[2] .. suffix] end end end

end

return data end

local function convertArmorArguments(args) local armorData = { helmet = {}, chestplate = {}, leggings = {}, boots = {}, total = convertItemArguments(args), name = args.name, rarity = args.rarity, total_label = args.totalLabel or args.total_label, }

for _, piece in ipairs(ARMORPIECEPREFIXES) do       for k, v in pairs(args) do            if k:find('^' .. piece) then armorData[piece][k:gsub(piece .. '_', '')] = v           end end end

for _, piece in ipairs(ARMORPIECEPREFIXES) do       armorData[piece] = convertItemArguments(armorData[piece]) end

return armorData end

local function getSlots(name, default) local slotsList = {}

if name and name:find('|') then for _, slotName in string.split(name, '|') do           table.push(slotsList, slot.slot{slotName}) end else table.push(slotsList, slot.slot{name or default}) end return table.concat(slotsList, ' ') end

local function getStats(statsTable) if not statsTable then return end

local statsList = {}

for k, v in pairs(statsTable) do       table.push(statsList, ('*%s: +%s'):format(stat.stat(k), tostring(v))) end return table.concat(statsList, '\n') end

local function getTotalStats(data) if table.length(data.total.stats) > 0 then return getStats(data.total.stats) end

local statsTable = {} for _, piece in ipairs(ARMORPIECEPREFIXES) do       for k, v in pairs(data[piece].stats) do            statsTable[k] = (statsTable[k] and statsTable[k] + tonumber(v)) or tonumber(v) end end

return getStats(statsTable) end

local function getEffects(data) local effectsList = {data.specialEffect or data.special_effects or data.effects or data.effect} for _, abilityType in ipairs(ABILITYTYPES) do       for i = 1, MAXABILITIES, 1 do            local suffix = i > 1 and tostring(i) or ''

if data[abilityType[2] .. suffix] then local itemAbility = data[abilityType[2] .. suffix]

local abilityString = { (ability._ability('%s', itemAbility.secret, abilityType[1]) .. ' %s%s %s'):format(                       itemAbility.name,                        itemAbility.activation and (mw.getCurrentFrame:expandTemplate{ title = 'Mctxt', args = {'e', itemAbility.activation} } .. ' ') or ,                       itemAbility.req and ('(Requires ' .. itemAbility.req .. ')') or ,                        itemAbility.desc or ''                    ) }

for _, property in ipairs(ABILITYPROPERTIES) do                   if itemAbility[property[2]] then table.push(abilityString, (' ' .. color.makeColor('dark_gray', '%s:') .. ' %s'):format( property[1], color.makeColor(property.color, itemAbility[property[2]]) ))	               end end

table.push(effectsList, table.concat(abilityString)) end end end

return table.concat(effectsList, ' ') end

-- For single items function p.itemInfo(frame) local args = getArgs(frame) local type = args.itemtype return p._itemInfo(type, args) end

function p._itemInfo(itemType, args) local data if table.length(args) <= 2 and args[1] then local dataModule = mw.loadData('Module:' .. DATA_MODULES[itemType]) data = dataModule[(args[1] or PAGENAME):lower] else data = convertItemArguments(args) end

local name = data.name or args[1] or PAGENAME local slots = getSlots(data.slot or data.slots, name) local stats = getStats(data.stats) local effects = getEffects(data)

local wikitable = mw.html.create('table'):addClass('wikitable'):css('overflow', 'auto'):css('border-width', '2px 2px 2px 2px !important'):css('vertical-align', 'text-top') wikitable:tag('tr') :tag('th'):attr('colspan', '2'):wikitext(itemType or 'Item'):done :done :tag('tr') :tag('td'):addClass('ct'):wikitext(slots):done -- slot :tag('td'):wikitext(('%s \n%s'):format(rarity._link(data.rarity or 'c', name), stats or '')):done -- stats :done

if effects ~= '' then wikitable:tag('tr') :tag('th'):attr('colspan', '2'):wikitext('Effects'):done :done :tag('tr') :tag('td'):attr('colspan', '2'):wikitext(effects):done -- effects :done end

wikitable:done return wikitable end

-- For Armor Sets function p.armorInfo(frame) local args = getArgs(frame)

return p._armorInfo(args) end

function p._armorInfo(args) local data if table.length(args) <= 2 then local dataModule = mw.loadData('Module:' .. DATA_MODULES[itemType]) data = dataModule[args[1]:lower or PAGENAME:lower] else data = convertArmorArguments(args) end

local armorName = data.name or args[1] or PAGENAME local pieces = { helmet = not data.helmet and false or data.helmet.exists, chestplate = not data.chestplate and false or data.chestplate.exists, leggings = not data.leggings and false or data.leggings.exists, boots = not data.boots and false or data.boots.exists, }   local names = { helmet = data.helmet.name or armorName:gsub('Armor', 'Helmet'), chestplate = data.chestplate.name or armorName:gsub('Armor', 'Chestplate'), leggings = data.leggings.name or armorName:gsub('Armor', 'Leggings'), boots = data.boots.name or armorName:gsub('Armor', 'Boots'), }   local slots = { helmet = getSlots(data.helmet.slot or data.helmet.slots, names.helmet), chestplate = getSlots(data.chestplate.slot or data.chestplate.slots, names.chestplate), leggings = getSlots(data.leggings.slot or data.leggings.slots, names.leggings), boots = getSlots(data.boots.slot or data.boots.slots, names.boots), }   local stats = { helmet = getStats(data.helmet.stats), chestplate = getStats(data.chestplate.stats), leggings = getStats(data.leggings.stats), boots = getStats(data.boots.stats), total = getTotalStats(data) }   local effects = { helmet = getEffects(data.helmet), chestplate = getEffects(data.chestplate), leggings = getEffects(data.leggings), boots = getEffects(data.boots), total = getEffects(data.total) }

local wikitable = mw.html.create('table'):addClass('wikitable'):css('overflow', 'auto'):css('border-width', '2px 2px 2px 2px !important'):css('vertical-align', 'text-top') wikitable:tag('tr') :tag('th'):attr('colspan', '2'):wikitext(data.helmet.label or 'Helmet'):done :tag('th'):attr('colspan', '2'):wikitext(data.chestplate.label or 'Chestplate'):done :done :tag('tr') :tag('td'):wikitext(pieces.helmet and slots.helmet or string.blankCell):done :tag('td'):wikitext(pieces.helmet and ('%s \n%s\n%s'):format(rarity._link(data.helmet.rarity or data.rarity or 'c', names.helmet), stats.helmet or , effects.helmet or ) or string.blankCell):done :tag('td'):wikitext(pieces.chestplate and slots.chestplate or string.blankCell):done :tag('td'):wikitext(pieces.chestplate and ('%s \n%s\n%s'):format(rarity._link(data.chestplate.rarity or data.rarity or 'c', names.chestplate), stats.chestplate or , effects.chestplate or ) or string.blankCell):done :done :tag('tr') :tag('th'):attr('colspan', '2'):wikitext(data.leggings.label or 'Leggings'):done :tag('th'):attr('colspan', '2'):wikitext(data.boots.label or 'Boots'):done :done :tag('tr') :tag('td'):wikitext(pieces.leggings and slots.leggings or string.blankCell):done :tag('td'):wikitext(pieces.leggings and ('%s \n%s\n%s'):format(rarity._link(data.leggings.rarity or data.rarity or 'c', names.leggings), stats.leggings or , effects.leggings or ) or string.blankCell):done :tag('td'):wikitext(pieces.boots and slots.boots or string.blankCell):done :tag('td'):wikitext(pieces.boots and ('%s \n%s\n%s'):format(rarity._link(data.boots.rarity or data.rarity or 'c', names.boots), stats.boots or , effects.boots or ) or string.blankCell):done :done :tag('tr') :tag('th'):attr('colspan', '4'):wikitext('Total'):done :done :tag('tr') :tag('td'):attr('colspan', '4'):wikitext(('%s \n%s\n%s'):format(rarity._link(data.rarity or 'c', armorName), stats.total or , effects.total or )):done :done

wikitable:done

return wikitable end

return p