Jump to content

Module:ItemConfig

From Apogea Wiki
Revision as of 20:48, 31 January 2026 by Dane (talk | contribs) (Create centralized item configuration module (via create-page on MediaWiki MCP Server))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 = {}

-- 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

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

-- 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.getCargoFields(frame)
    local fieldSet = frame and frame.args[1] or "items"
    return p.cargoFields[fieldSet] or p.cargoFields.items
end

return p