Module:Sandbox/Monster Domosed

From The Deadlock Wiki
Revision as of 02:27, 11 February 2026 by Monster Domosed (talk | contribs) (test)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local function get_cost(item)
	return tonumber(item["Cost"]) or math.huge
end

local function get_type_rank(item)
	return TYPE_ORDER[item["Slot"]] or math.huge
end

--------------------------------------------------
-- Core logic
--------------------------------------------------

-- Returns a sorted Lua array of item tables
local function get_sorted_items()
	local items = {}

	for _, item in pairs(data) do
		if is_enabled(item) then
			table.insert(items, item)
		end
	end

	table.sort(items, function(a, b)
		local type_a = get_type_rank(a)
		local type_b = get_type_rank(b)

		-- 1. Sort by type
		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
--------------------------------------------------

-- For other Lua modules
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}}
-- Newline-separated, sorted
function p.get_list(frame)
	local names = p._get_enabled_item_names_sorted()
	return table.concat(names, "\n")
end

-- {{#invoke:Monster Domosed|get_csv}}
-- Comma-separated, sorted (Python-friendly)
function p.get_csv(frame)
	local names = p._get_enabled_item_names_sorted()
	return table.concat(names, ",")
end

-- {{#invoke:Monster Domosed|get_count}}
function p.get_count(frame)
	return tostring(#p._get_enabled_item_names_sorted())
end

return p