Jump to content

Module:ItemConfig: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Create centralized item configuration module (via create-page on MediaWiki MCP Server)
 
Dane (talk | contribs)
Add getRarityList() function (via update-page on MediaWiki MCP Server)
Line 16: Line 16:
p.singulars = {}
p.singulars = {}
p.parentCategories = {}
p.parentCategories = {}
p.rarityList = {}


-- Initialize derived structures from JSON
-- Initialize derived structures from JSON
Line 30: Line 31:
         table.insert(p.parentCategories[parent], typeName)
         table.insert(p.parentCategories[parent], typeName)
     end
     end
end
-- Build rarity list
for rarityName, _ in pairs(data.rarities) do
    table.insert(p.rarityList, rarityName)
end
end


-- Sort type list alphabetically
-- Sort type list alphabetically
table.sort(p.typeList)
table.sort(p.typeList)
-- Sort rarity list by intended order (common to legendary)
local rarityOrder = {common = 1, uncommon = 2, rare = 3, epic = 4, legendary = 5}
table.sort(p.rarityList, function(a, b)
    return (rarityOrder[a] or 99) < (rarityOrder[b] or 99)
end)


-- Helper functions
-- Helper functions
Line 79: Line 91:
function p.getSlotList(frame)
function p.getSlotList(frame)
     return "," .. table.concat(p.slots, ",")
     return "," .. table.concat(p.slots, ",")
end
function p.getRarityList(frame)
    return table.concat(p.rarityList, ",")
end
end



Revision as of 20:57, 31 January 2026

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

local p = {}

-- Load JSON data
local data = mw.loadJsonData('Module:ItemConfig/data.json')

-- Expose raw data
p.types = data.types
p.rarities = data.rarities
p.slots = data.slots
p.cargoFields = data.cargoFields
p.stats = data.stats

-- Build derived data structures
p.typeList = {}
p.plurals = {}
p.singulars = {}
p.parentCategories = {}
p.rarityList = {}

-- Initialize derived structures from JSON
for typeName, typeData in pairs(data.types) do
    table.insert(p.typeList, typeName)
    p.plurals[typeName] = typeData.plural
    p.singulars[typeData.plural] = typeName
    
    -- Build parent category mappings
    for _, parent in ipairs(typeData.parents or {}) do
        if not p.parentCategories[parent] then
            p.parentCategories[parent] = {}
        end
        table.insert(p.parentCategories[parent], typeName)
    end
end

-- Build rarity list
for rarityName, _ in pairs(data.rarities) do
    table.insert(p.rarityList, rarityName)
end

-- Sort type list alphabetically
table.sort(p.typeList)

-- Sort rarity list by intended order (common to legendary)
local rarityOrder = {common = 1, uncommon = 2, rare = 3, epic = 4, legendary = 5}
table.sort(p.rarityList, function(a, b)
    return (rarityOrder[a] or 99) < (rarityOrder[b] or 99)
end)

-- Helper functions
function p.pluralize(singular)
    return p.plurals[singular] or (singular .. "s")
end

function p.singularize(plural)
    return p.singulars[plural] or plural:gsub("s$", "")
end

function p.isParentCategory(category)
    return p.parentCategories[category] ~= nil
end

function p.getChildTypes(category)
    return p.parentCategories[category]
end

function p.getRarityColor(rarity)
    local r = p.rarities[string.lower(rarity or "common")]
    return r and r.color or "silver"
end

function p.getRarityLabel(rarity)
    local r = p.rarities[string.lower(rarity or "common")]
    return r and r.label or ""
end

function p.getStatIcon(stat)
    local s = p.stats[stat]
    return s and s.icon or nil
end

function p.getStatLabel(stat)
    local s = p.stats[stat]
    return s and s.label or stat
end

-- Template-callable functions
function p.getTypeList(frame)
    return table.concat(p.typeList, ",")
end

function p.getSlotList(frame)
    return "," .. table.concat(p.slots, ",")
end

function p.getRarityList(frame)
    return table.concat(p.rarityList, ",")
end

function p.getCargoFields(frame)
    local fieldSet = frame and frame.args[1] or "items"
    return p.cargoFields[fieldSet] or p.cargoFields.items
end

return p