Jump to content

Module:ItemCategoryTable: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Use centralized Module:ItemTypes for type definitions (via update-page on MediaWiki MCP Server)
Dane (talk | contribs)
Use centralized Module:ItemConfig (via update-page on MediaWiki MCP Server)
Line 1: Line 1:
local p = {}
local p = {}
local ItemTypes = require('Module:ItemTypes')
local ItemConfig = require('Module:ItemConfig')


-- Query items by type from Cargo
-- Query items by type from Cargo
local function getItemsByType(itemType)
local function getItemsByType(itemType)
     local tables = 'Items'
     local tables = 'Items'
     local fields = 'name,sprite,type,weight,damage,armor,defense,health,mana,ability,magic,rng,move_speed,attack_speed,health_regen,mana_regen,container,size,rarity'
     local fields = ItemConfig.cargoFields.itemsTable
     local args = {
     local args = {
         where = 'type LIKE "%' .. itemType .. '%"',
         where = 'type LIKE "%' .. itemType .. '%"',
Line 18: Line 18:
local function getItemsByTypes(types)
local function getItemsByTypes(types)
     local tables = 'Items'
     local tables = 'Items'
     local fields = 'name,sprite,type,weight,damage,armor,defense,health,mana,ability,magic,rng,move_speed,attack_speed,health_regen,mana_regen,container,size,rarity'
     local fields = ItemConfig.cargoFields.itemsTable
      
      
     -- Build OR conditions for each type
     -- Build OR conditions for each type
Line 38: Line 38:
local function getAllItems()
local function getAllItems()
     local tables = 'Items'
     local tables = 'Items'
     local fields = 'name,sprite,type,weight,damage,armor,defense,health,mana,ability,magic,rng,move_speed,attack_speed,health_regen,mana_regen,container,size,rarity'
     local fields = ItemConfig.cargoFields.itemsTable
     local args = {
     local args = {
         orderBy = 'name',
         orderBy = 'name',
Line 77: Line 77:
     -- Check if this is a parent category or all items
     -- Check if this is a parent category or all items
     local isAllItems = (categoryName == "Items")
     local isAllItems = (categoryName == "Items")
     local isParent = ItemTypes.isParentCategory(categoryName) or isAllItems
     local isParent = ItemConfig.isParentCategory(categoryName) or isAllItems
     local isContainers = (categoryName == "Containers")
     local isContainers = (categoryName == "Containers")
     local items
     local items
Line 83: Line 83:
     if isAllItems then
     if isAllItems then
         items = getAllItems()
         items = getAllItems()
     elseif ItemTypes.isParentCategory(categoryName) then
     elseif ItemConfig.isParentCategory(categoryName) then
         items = getItemsByTypes(ItemTypes.getChildTypes(categoryName))
         items = getItemsByTypes(ItemConfig.getChildTypes(categoryName))
     else
     else
         local itemType = ItemTypes.singularize(categoryName)
         local itemType = ItemConfig.singularize(categoryName)
         items = getItemsByType(itemType)
         items = getItemsByType(itemType)
     end
     end
Line 157: Line 157:
         -- Type column (for parent categories)
         -- Type column (for parent categories)
         if isParent then
         if isParent then
             local typeCategory = ItemTypes.pluralize(item.type or "")
             local typeCategory = ItemConfig.pluralize(item.type or "")
             row:tag('td'):wikitext('[[:Category:' .. typeCategory .. '|' .. (item.type or "") .. ']]')
             row:tag('td'):wikitext('[[:Category:' .. typeCategory .. '|' .. (item.type or "") .. ']]')
         end
         end

Revision as of 20:50, 31 January 2026

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

local p = {}
local ItemConfig = require('Module:ItemConfig')

-- Query items by type from Cargo
local function getItemsByType(itemType)
    local tables = 'Items'
    local fields = ItemConfig.cargoFields.itemsTable
    local args = {
        where = 'type LIKE "%' .. itemType .. '%"',
        orderBy = 'name',
        limit = 500
    }
    
    return mw.ext.cargo.query(tables, fields, args)
end

-- Query items by multiple types
local function getItemsByTypes(types)
    local tables = 'Items'
    local fields = ItemConfig.cargoFields.itemsTable
    
    -- Build OR conditions for each type
    local conditions = {}
    for _, t in ipairs(types) do
        table.insert(conditions, 'type LIKE "%' .. t .. '%"')
    end
    
    local args = {
        where = table.concat(conditions, ' OR '),
        orderBy = 'name',
        limit = 500
    }
    
    return mw.ext.cargo.query(tables, fields, args)
end

-- Query all items
local function getAllItems()
    local tables = 'Items'
    local fields = ItemConfig.cargoFields.itemsTable
    local args = {
        orderBy = 'name',
        limit = 500
    }
    
    return mw.ext.cargo.query(tables, fields, args)
end

-- Get numeric value from stat (0 if empty)
local function statNum(value)
    if value and value ~= "" then
        return tonumber(value) or 0
    end
    return 0
end

-- Format stat value (return 0 if empty)
local function stat(value)
    if value and value ~= "" then
        return value
    end
    return "0"
end

-- Calculate total stat power for an item
local function calcPower(item)
    return statNum(item.damage) + statNum(item.armor) + statNum(item.defense) +
           statNum(item.health) + statNum(item.mana) + statNum(item.ability) +
           statNum(item.magic) + statNum(item.rng) + statNum(item.move_speed) +
           statNum(item.attack_speed)
end

function p.render(frame)
    local args = frame:getParent().args
    local categoryName = args[1] or args.category or mw.title.getCurrentTitle().text:gsub("^Category:", "")
    
    -- Check if this is a parent category or all items
    local isAllItems = (categoryName == "Items")
    local isParent = ItemConfig.isParentCategory(categoryName) or isAllItems
    local isContainers = (categoryName == "Containers")
    local items
    
    if isAllItems then
        items = getAllItems()
    elseif ItemConfig.isParentCategory(categoryName) then
        items = getItemsByTypes(ItemConfig.getChildTypes(categoryName))
    else
        local itemType = ItemConfig.singularize(categoryName)
        items = getItemsByType(itemType)
    end
    
    if not items or #items == 0 then
        return '<p class="mw-empty">No items found.</p>'
    end
    
    -- Calculate power for each item and sort
    for _, item in ipairs(items) do
        item._power = calcPower(item)
    end
    
    -- Sort by slots for containers, otherwise by power
    if isContainers then
        table.sort(items, function(a, b) return statNum(a.container) > statNum(b.container) end)
    else
        table.sort(items, function(a, b) return a._power > b._power end)
    end
    
    -- Build sortable table
    local tbl = mw.html.create('table')
    tbl:addClass('wikitable')
       :addClass('sortable')
       :css('width', '100%')
    
    -- Header row
    local header = tbl:tag('tr')
    header:tag('th')
        :css('width', '64px')
        :addClass('unsortable')
        :wikitext('')
    header:tag('th'):wikitext('Item')
    if isParent then
        header:tag('th'):wikitext('Type')
    end
    if isContainers then
        header:tag('th'):attr('title', 'Container Slots'):wikitext('Slots')
    end
    header:tag('th'):attr('title', 'Weight'):wikitext('WGT')
    header:tag('th'):attr('title', 'Damage'):wikitext('DMG')
    header:tag('th'):attr('title', 'Armor'):wikitext('ARM')
    header:tag('th'):attr('title', 'Defense'):wikitext('DEF')
    header:tag('th'):attr('title', 'Health'):wikitext('HP')
    header:tag('th'):attr('title', 'Mana'):wikitext('MP')
    header:tag('th'):attr('title', 'Ability'):wikitext('ABL')
    header:tag('th'):attr('title', 'Magic'):wikitext('MAG')
    header:tag('th'):attr('title', 'Range'):wikitext('RNG')
    header:tag('th'):attr('title', 'Move Speed'):wikitext('MS')
    header:tag('th'):attr('title', 'Attack Speed'):wikitext('AS')
    header:tag('th'):attr('title', 'Total Stats'):wikitext('Total')
    
    -- Data rows
    for _, item in ipairs(items) do
        local row = tbl:tag('tr')
        
        -- Icon column
        local iconCell = row:tag('td')
            :css('width', '64px')
            :css('text-align', 'center')
        local sprite = item.sprite or item.name or "Unknown"
        if sprite and sprite ~= "" then
            iconCell:wikitext(frame:preprocess('{{Sprite|' .. sprite .. '|x2}}'))
        end
        
        -- Item name column
        local itemName = item.name or "Unknown"
        row:tag('td'):wikitext('[[' .. itemName .. ']]')
        
        -- Type column (for parent categories)
        if isParent then
            local typeCategory = ItemConfig.pluralize(item.type or "")
            row:tag('td'):wikitext('[[:Category:' .. typeCategory .. '|' .. (item.type or "") .. ']]')
        end
        
        -- Slots column (for containers)
        if isContainers then
            row:tag('td'):css('text-align', 'right'):wikitext(stat(item.container))
        end
        
        -- Stats
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.weight))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.damage))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.armor))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.defense))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.health))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.mana))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.ability))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.magic))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.rng))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.move_speed))
        row:tag('td'):css('text-align', 'right'):wikitext(stat(item.attack_speed))
        row:tag('td'):css('text-align', 'right'):wikitext(item._power)
    end
    
    return tostring(tbl)
end

return p