Module:ItemConfig
Documentation for this module may be created at Module:ItemConfig/doc
local p = {}
-- Load JSON data
local data = mw.loadJsonData('Module:ItemConfig/data.json')
-- Helper to copy JSON array to proper Lua table (mw.loadJsonData arrays don't work with # or table.concat)
local function copyArray(jsonArray)
local result = {}
for i, v in ipairs(jsonArray) do
result[i] = v
end
return result
end
-- Expose raw data (copy arrays to fix table.concat issues)
p.types = data.types
p.rarities = data.rarities
p.slots = copyArray(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]
if s and s.icon and s.icon ~= "" then
return s.icon
end
return 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
-- Debug function
function p.debugSlots(frame)
local output = {}
table.insert(output, "p.slots length: " .. tostring(#p.slots))
table.insert(output, "p.slots content: " .. table.concat(p.slots, ", "))
return table.concat(output, "\n")
end
return p