Module:Sandbox/Monster Domosed: Difference between revisionsGive feedback
Jump to navigation
Jump to search
test |
No edit summary |
||
| Line 24: | Line 24: | ||
end | end | ||
-- Returns numeric cost or nil if invalid | |||
local function get_cost(item) | local function get_cost(item) | ||
local cost = tonumber(item["Cost"]) | |||
if cost == nil then | |||
return nil | |||
end | |||
return cost | |||
end | end | ||
| Line 36: | Line 41: | ||
-------------------------------------------------- | -------------------------------------------------- | ||
local function get_sorted_items() | local function get_sorted_items() | ||
local items = {} | local items = {} | ||
for _, item in pairs(data) do | for _, item in pairs(data) do | ||
if is_enabled(item) then | -- Exclude items with null / missing / non-numeric cost | ||
local cost = get_cost(item) | |||
if is_enabled(item) and cost ~= nil then | |||
table.insert(items, item) | table.insert(items, item) | ||
end | end | ||
| Line 47: | Line 54: | ||
table.sort(items, function(a, b) | table.sort(items, function(a, b) | ||
-- 1. Sort by type | |||
local type_a = get_type_rank(a) | local type_a = get_type_rank(a) | ||
local type_b = get_type_rank(b) | local type_b = get_type_rank(b) | ||
if type_a ~= type_b then | if type_a ~= type_b then | ||
return type_a < type_b | return type_a < type_b | ||
| Line 58: | Line 64: | ||
local cost_a = get_cost(a) | local cost_a = get_cost(a) | ||
local cost_b = get_cost(b) | local cost_b = get_cost(b) | ||
if cost_a ~= cost_b then | if cost_a ~= cost_b then | ||
return cost_a < cost_b | return cost_a < cost_b | ||
| Line 74: | Line 79: | ||
-------------------------------------------------- | -------------------------------------------------- | ||
function p._get_enabled_items_sorted() | function p._get_enabled_items_sorted() | ||
return get_sorted_items() | return get_sorted_items() | ||
| Line 95: | Line 99: | ||
-- {{#invoke:Monster Domosed|get_list}} | -- {{#invoke:Monster Domosed|get_list}} | ||
function p.get_list(frame) | function p.get_list(frame) | ||
return table.concat(p._get_enabled_item_names_sorted(), "\n") | |||
end | end | ||
-- {{#invoke:Monster Domosed|get_csv}} | -- {{#invoke:Monster Domosed|get_csv}} | ||
function p.get_csv(frame) | function p.get_csv(frame) | ||
return table.concat(p._get_enabled_item_names_sorted(), ",") | |||
end | end | ||
Revision as of 02:29, 11 February 2026
Documentation for this module may be created at Module:Sandbox/Monster Domosed/doc
local p = {}
local data = mw.loadJsonData("Data:ItemData.json")
--------------------------------------------------
-- Constants
--------------------------------------------------
-- Desired type order
local TYPE_ORDER = {
Weapon = 1,
Armor = 2, -- Vitality
Tech = 3 -- Spirit
}
--------------------------------------------------
-- Helpers
--------------------------------------------------
local function is_enabled(item)
return item
and item["Name"] ~= nil
and (item["IsDisabled"] == false or item["IsDisabled"] == nil)
end
-- Returns numeric cost or nil if invalid
local function get_cost(item)
local cost = tonumber(item["Cost"])
if cost == nil then
return nil
end
return cost
end
local function get_type_rank(item)
return TYPE_ORDER[item["Slot"]] or math.huge
end
--------------------------------------------------
-- Core logic
--------------------------------------------------
local function get_sorted_items()
local items = {}
for _, item in pairs(data) do
-- Exclude items with null / missing / non-numeric cost
local cost = get_cost(item)
if is_enabled(item) and cost ~= nil then
table.insert(items, item)
end
end
table.sort(items, function(a, b)
-- 1. Sort by type
local type_a = get_type_rank(a)
local type_b = get_type_rank(b)
if type_a ~= type_b then
return type_a < type_b
end
-- 2. Sort by cost (low → high)
local cost_a = get_cost(a)
local cost_b = get_cost(b)
if cost_a ~= cost_b then
return cost_a < cost_b
end
-- 3. Stable fallback: name
return a["Name"] < b["Name"]
end)
return items
end
--------------------------------------------------
-- Public (module-level) access
--------------------------------------------------
function p._get_enabled_items_sorted()
return get_sorted_items()
end
function p._get_enabled_item_names_sorted()
local items = get_sorted_items()
local names = {}
for _, item in ipairs(items) do
table.insert(names, item["Name"])
end
return names
end
--------------------------------------------------
-- Invoke access points
--------------------------------------------------
-- {{#invoke:Monster Domosed|get_list}}
function p.get_list(frame)
return table.concat(p._get_enabled_item_names_sorted(), "\n")
end
-- {{#invoke:Monster Domosed|get_csv}}
function p.get_csv(frame)
return table.concat(p._get_enabled_item_names_sorted(), ",")
end
-- {{#invoke:Monster Domosed|get_count}}
function p.get_count(frame)
return tostring(#p._get_enabled_item_names_sorted())
end
return p