Module:Items

-- local p = {}

local logbook = require("Module:Items/LogbookData") local data = require("Module:Items/Data") local data_e = require("Module:Equipment/Data") local all_items = data.items local all_equipment = data_e.equipment

Count function p.ItemCount local c, e = 0, 0 for i, item in pairs(all_items) do   if item.Rarity ~= "NoTier" then c = c + 1 end end for i in pairs(all_equipment) do   e = e + 1 end return c .. " items and " .. e .. " equipment" end

Item Infobox local infobox_template_item = [=[]=]

function p.InfoboxItem(frame) local item_name = frame.args[1] ~= "" and frame.args[1] or mw.title.getCurrentTitle.text local item = all_items[item_name] return frame:preprocess(infobox_template_item:format( item_name, item_name, item.Quote, item.Desc:gsub("\r\n", " "), item.Rarity, item.Boss or "", item.Category and Category(item.Category) or "", item.Unlock and "" .. item.Unlock .. "" or "", item.ID, item.Stats and Stats(item.Stats) or "" )) end

Equipment Infobox local infobox_template_equipment = [=[]=]

function p.InfoboxEquipment(frame) local equipment_name = frame.args[1] ~= "" and frame.args[1] or mw.title.getCurrentTitle.text local equipment = all_equipment[equipment_name] return frame:preprocess(infobox_template_equipment:format( equipment_name, equipment_name, equipment.Quote, equipment.Desc:gsub("\r\n", " "), equipment.Rarity, equipment.Cooldown and equipment.Cooldown .. "s" or "", equipment.Unlock and "" .. equipment.Unlock .. "" or "", equipment.ID )) end

Category function Category(list) return list:gsub("([^,]+)", function(m)   m = m:gsub("^%l", string.upper)    return "" .. m .. ""  end):gsub(",", " ") end

Stats local stats_row = [=[| stat%s = %s ]=]
 * value%s = %s
 * stack%s = %s
 * add%s = %s

function Stats(stats) local rows = "" for i, v in ipairs(stats) do   rows = rows .. stats_row:format(     i, v.Stat,      i, v.Value,      i, Stack(v.Stack),      i, v.Add    ) end return rows end

Stack local colors = {} colors["Linear"] = "#CCCCCC" colors["Hyperbolic"] = "#33CC33" colors["Exponential"] = "#CC6666" colors["Special"] = "#8888FF" colors["None"] = "#888888"

function Stack(stack) if stack == "None" then return "None " end for k, v in pairs(colors) do   if stack == k then stack = stack:gsub(k, "" .. k .. " ") break end end return stack end

Item Table local table_head_item = [=[{| class="wikitable sortable firstcolumn-center-nowrap floatheader" style="width:100%;max-width:1350px" ! style="width:20%" | Item ! class="unsortable" | Description ! style="width:10%" | Stack Type ]=]

local table_row_item = [=[|- ]=]
 * data-sort-value="%s" | %s.png%s
 * %s
 * %s

function p.Table(frame) local rarity = frame.args[1] local table = table_head_item for i, item in ipairs(Sort(all_items)) do   if item.Rarity == rarity then table = table .. frame:preprocess(table_row_item:format( item.Name, item.Name, item.Name, item.Name, Description(item.Desc), StackTypes(item.Stats) ))   end end return table .. "|}" end

Boss Table local table_head_boss = [=[{| class="wikitable sortable firstcolumn-center-nowrap floatheader" style="width:100%;max-width:1350px" ! style="width:20%" | Item ! class="unsortable" | Description ! style="width:20%" | Boss / Acquired From ! style="width:10%" | Stack Type ]=]

local table_row_boss = [=[|- ]=]
 * data-sort-value="%s" | %s.png%s
 * %s
 * style="text-align:center;white-space:nowrap" data-sort-value="%s" | %s.png%s
 * %s

function p.BossTable(frame) local table = table_head_boss for i, item in ipairs(Sort(all_items)) do   if item.Rarity == "Boss" then table = table .. frame:preprocess(table_row_boss:format( item.Name, item.Name, item.Name, item.Name, Description(item.Desc), item.Boss, item.Boss, item.Boss, item.Boss, StackTypes(item.Stats) ))   end end return table .. "|}" end

Equipment Table local table_head_equipment = [=[{| class="wikitable sortable firstcolumn-center-nowrap floatheader" style="width:100%%;max-width:1350px" ! style="width:20%%" | Item ! class="unsortable" | Description ! style="white-space:nowrap;width:10%%" data-sort-type="number" | %s ]=]

local table_row_equipment = [=[|- ]=]
 * data-sort-value="%s" | %s.png%s
 * %s
 * style="text-align:center" | %s

function p.EquipmentTable(frame) local rarity = frame.args[1] local table = table_head_equipment:format(rarity == "Elite Equipment" and "Elite" or "Cooldown") for i, equipment in ipairs(Sort(all_equipment)) do   if equipment.Rarity == rarity then table = table .. frame:preprocess(table_row_equipment:format(equipment.Name, equipment.Name, equipment.Name, equipment.Name, Description(equipment.Desc), rarity == "Elite Equipment" and equipment.Elite or (equipment.Cooldown and equipment.Cooldown .. "s" or "-"))) end end return table .. "|}" end

Description local link_patterns = {} link_patterns["Damage"] = { "[Dd]amage" } link_patterns["Health"] = { "[Hh]ealth" } link_patterns["Shield"] = { "[Ss]hield" } link_patterns["Barrier"] = { "[Bb]arrier" } link_patterns["Armor"] = { "[Aa]rmor" } link_patterns["Movement Speed"] = { "[Mm]ovement [Ss]peed", "[Ss]print [Ss]peed" } link_patterns["Chests#Rusty Lockbox"] = { "[Hh]idden [Cc]ache" } link_patterns["57 Leaf Clover"] = { "[Ll]uck" } link_patterns["Monsters#Elites"] = { "[Ee]lites?" }

function Description(desc) for link, patterns in pairs(link_patterns) do   for i, v in ipairs(patterns) do      local a, b = desc:find(v) if a then desc = desc:sub(1, a - 1) .. "" .. desc:sub(a, b) .. "" .. desc:sub(b + 1, desc:len) break end end end return desc end

StackTypes function StackTypes(stats) if not stats then return "-" end local stack = {} for i, v in ipairs(stats) do   if v.Stack ~= stack[#stack] then table.insert(stack, v.Stack) end end return table.concat(stack, " ") end

Nav Items local nav_head_items = [=[{| class="navbox" style="margin-top:1em;border:1px solid;font-size:90%;text-align:center" ! style="background-color:#282828;font-size:175%;white-space:nowrap" | Items ]=]

local nav_row_items = [=[|- ]=]
 * style="background-color:#3A3A3A;font-size:125%%;font-weight:bold;white-space:nowrap" | %s
 * style="padding:0.2em 0.5em" | %s
 * style="padding:0.2em 0.5em" | %s

function p.NavItems(frame) local nav = nav_head_items local items = { Common = {}, Uncommon = {}, Legendary = {}, Boss = {}, Lunar = {}, NoTier = {} } for name, item in pairs(all_items) do   table.insert(items[item.Rarity], "" .. name .. "") end for rarity in pairs(items) do   table.sort(items[rarity]) end for i, rarity in ipairs({ "Common", "Uncommon", "Legendary", "Boss", "Lunar" }) do   nav = nav .. nav_row_items:format(     rarity,      rarity,      table.concat(items[rarity], " • ")    ) end return nav .. "|}" end

Nav Equipment local nav_head_equipment = [=[{| class="navbox" style="margin-top:1em;border:1px solid;font-size:90%;text-align:center" ! style="background-color:#282828;font-size:175%;white-space:nowrap" | Equipment ]=]

local nav_row_equipment = [=[|- ]=]
 * style="background-color:#3A3A3A;font-size:125%%;font-weight:bold;white-space:nowrap" | %s
 * style="padding:0.2em 0.5em" | %s
 * style="padding:0.2em 0.5em" | %s

function p.NavEquipment(frame) local nav = nav_head_equipment local equipment = { Equipment = {}, ["Lunar Equipment"] = {}, ["Elite Equipment"] = {} } for name, e in pairs(all_equipment) do   table.insert(equipment[e.Rarity], "" .. name .. "") end for rarity in pairs(equipment) do   table.sort(equipment[rarity]) end for i, rarity in ipairs({ "Equipment", "Lunar Equipment", "Elite Equipment" }) do   nav = nav .. nav_row_equipment:format(     rarity,      rarity:gsub(" Equipment", ""):gsub("Equipment", "Normal"),      table.concat(equipment[rarity], " • ")    ) end return nav .. "|}" end

Sort function Sort(list) local items = {} for name, item in pairs(list) do   item.Name = name table.insert(items, item) end table.sort(items, function(a, b) return a.Name < b.Name end) return items end

GetItemOrEquipment -- returns item object (table) or string with error function GetItemOrEquipment(item_name) if item_name == nil then return ' Error: Name missing. 1st parameter is required. ' end local item = all_items[item_name] or all_equipment[item_name] if item == nil then return ' Error: Incorrect name. Check capitalization. ' else return item end end

GetRarity function p.GetRarity(frame) local item_name = frame.args[1] local simple_form = frame.args[2] or false local item = GetItemOrEquipment(item_name) if type(item) == 'string' then return item end local rarity = item.Rarity if simple_form and rarity == 'Elite Equipment' then rarity = 'Equipment' elseif simple_form and rarity == 'Lunar Equipment' then rarity = 'Lunar' end return rarity end

GetDescription function p.GetDescription(frame) local item_name = frame.args[1] local item = GetItemOrEquipment(item_name) if type(item) == 'string' then return item else return frame:preprocess(item.Desc:gsub("\r\n", " ")) end end

GetQuote function p.GetQuote(frame) local item_name = frame.args[1] local item = GetItemOrEquipment(item_name) if type(item) == 'string' then return item else return frame:preprocess(item.Quote) end end

local GetItemBox local itembox_template = ' ' ..  ..                           ' ' ..                               ..                            ' ' ..                          ' ' local function GetItemBox(frame, name, size) frame.args[1] = name frame.args[2] = 'simple' local rarity = p.GetRarity(frame) local itemSize = math.floor(size * 0.9375) local offset = (size - itemSize) / 2 return string.format(itembox_template,     rarity,      size,      name,      offset,      offset,      name,      itemSize,      name  ) end

GetTooltip local tooltip_template = ' ' .. ' %s ' .. ' ' ..                             '%s  ' .. '%s' .. ' ' ..                         ' '

function p.GetTooltip(frame) local item_name = frame.args[1] local item = GetItemOrEquipment(item_name) if type(item) == 'string' then return item end --local item_box = frame:expandTemplate{ title = 'ItemBox', args = { item_name, 48 } } -- can use expandTemplate instead of local function for simplicity but that'd be more CPU expensive local item_box = GetItemBox(frame, item_name, 48) local quote = p.GetQuote(frame) local desc = p.GetDescription(frame) return string.format(tooltip_template,   item_box,    quote,    desc  ) end

Logbook local logbook_template = [=[{| style = "border-spacing: %spx" %s
 * }]=]

function p.Logbook(frame) local size = frame.args.size or frame.args[1] or 64 local spacing = math.max(0, size / 16 - 2) local rows = {} for i, item in ipairs(logbook.order) do   rows[#rows+1] = '| ' .. GetItemBox(frame, item, size) if i % logbook.width == 0 and i ~= count then rows[#rows+1] = '|-' end end return string.format(logbook_template, spacing, table.concat(rows, '\n')) end

return p --