Module:Variants: Difference between revisions

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
(Fix error when no variant smw data exists, and add tracking category)
 
Line 25: Line 25:
}
}
local smw_data = mw.smw.ask(query)
local smw_data = mw.smw.ask(query)
if not smw_data then
return {'[[Category:Empty variant list]]'}
end
table.sort(smw_data, sort_entries)
table.sort(smw_data, sort_entries)
local table_contents = {}
local table_contents = {}

Latest revision as of 17:19, 11 November 2024

Creates a section containing all items with the same Variant property, ordered by the sum of the profession level requirements in ascending order.

If a variant does not have a profession level, 0 is used as a fallback, meaning items with unknown level requirements will always appear first in the list.

Usage:

{{Variants|Eel}}

Result:


local p = {}

function sort_entries(entry1, entry2)
	-- Sort the variants based on the sum of the profession levels
	local entry1_value = (entry1['Profession Level A'] or 0) + (entry1['Profession Level B'] or 0)
	local entry2_value = (entry2['Profession Level A'] or 0) + (entry2['Profession Level B'] or 0)
	return entry1_value < entry2_value
end

function format_entry(entry)
	-- Format the search result as [[Page]]. At the moment, don't include profession data in the table
	local page = entry[1]
	return page
end

function get_table_info(args)
	local variant_name = args.variant or mw.title.getCurrentTitle().fullText
	local query = {
        '[[Variant of::'..variant_name..']]',
        -- '?Profession A',
        '?Profession Level A',
        -- '?Profession B',
        '?Profession Level B',
        limit = args.limit or 500,
    }
	local smw_data = mw.smw.ask(query)
	if not smw_data then
		return {'[[Category:Empty variant list]]'}
	end
	table.sort(smw_data, sort_entries)
    local table_contents = {}
    for _, entry in ipairs(smw_data) do
    	table.insert(table_contents, format_entry(entry))
	end
	return table_contents
end

function p.variant_table(args)
	local elements = get_table_info(args)
	local table_contents = table.concat(elements, '&nbsp;•&nbsp;')
	local html = mw.html.create('div'):addClass('variants-header'):wikitext(table_contents)
	return html
end

function p.main(frame)
	local args = frame:getParent().args
	args.variant = args[1] or args.variant
	return p.variant_table(args)
end

return p