Module:Sandbox/grouped list

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
Documentation for Module:Sandbox/grouped list does not exist yet [create] (How does this work?)
local getArgs = require("Module:Arguments").getArgs
local yesno = require("Module:Yesno")

function notempty(str)
	if str == "" then
		return nil
	end
	return str
end

local p = {}

local printouts = {
	"?=Pagename#",
	"?Has canonical name",
	"?Has game icon#",
	"?Has level requirement",
	"?Has item level",
	"?Is for class#",
	"?Is for job#",
	"?Has weapon physical damage",
	"?Has weapon magic damage",
	"?Has weapon delay",
	"?Has weapon auto-attack damage",
	"?Has materia slots",
	"?Has advanced melding",
	"?Has strength bonus",
	"?Has dexterity bonus",
	"?Has intelligence bonus",
	"?Has mind bonus",
	"?Has vitality bonus",
	"?Has critical hit bonus",
	"?Has determination bonus",
	"?Has direct hit rate bonus",
	"?Has skill speed bonus",
	"?Has spell speed bonus",
	"?Has tenacity bonus",
	"?Has piety bonus",
	"?Has hq weapon physical damage",
	"?Has hq weapon magic damage",
	"?Has hq weapon auto-attack damage",
	"?Has hq strength bonus",
	"?Has hq dexterity bonus",
	"?Has hq intelligence bonus",
	"?Has hq mind bonus",
	"?Has hq vitality bonus",
	"?Has hq critical hit bonus",
	"?Has hq determination bonus",
	"?Has hq direct hit rate bonus",
	"?Has hq skill speed bonus",
	"?Has hq spell speed bonus",
	"?Has hq tenacity bonus",
	"?Has hq piety bonus",
	"?Has customizable substats",
	"?Has dye channels",
	"?Has random substats",
}

function p.main(frame)
	local args = getArgs(frame)
	local renderEmptyCategories = yesno(args["empty groups"])

	-- Query conditions
	local query = {args["query"]}

	-- Query options
	query["sort"] = notempty(args["sort"]) or "Has level requirement, Has item level, Has canonical name"
	query["limit"] = 999

	-- Query printouts
	for _, v in ipairs(printouts) do
		table.insert(query, v)
	end

	local results = mw.smw.ask(query)

	if not results then
		results = {}
	end

	local output = ""

	-- group by every 10 levels
	-- TODO: rendering empty groups when results do not span entire requested range
	function heading(levelCap)
		return "===Level " .. levelCap - 9 .. "–" .. levelCap .. "===\n"
	end
	local tableStart = mw.getCurrentFrame():expandTemplate({title = "Weapon table header full"}) .. "\n"
	local defaultText = "No items.\n"

	local currentGroupLevelCap = 0
	for _, item in ipairs(results) do
		local thisLevel = tonumber(item["Has level requirement"], 10)

		-- add heading if we've gone past the previous group's range
		if thisLevel > currentGroupLevelCap then
			-- add table terminator for previous section, if there was one
			if output ~= "" then
				output = output .. "|}\n"
			end

			currentGroupLevelCap = currentGroupLevelCap + 10
			if renderEmptyCategories then
				output = output .. heading(currentGroupLevelCap)

				-- add empty headings until we catch up
				while thisLevel > currentGroupLevelCap do
					currentGroupLevelCap = currentGroupLevelCap + 10
					output = output .. defaultText .. heading(currentGroupLevelCap)
				end
			else
				while thisLevel > currentGroupLevelCap do
					currentGroupLevelCap = currentGroupLevelCap + 10
				end
				output = output .. heading(currentGroupLevelCap)
			end

			-- start table for this item
			output = output .. tableStart
		end

		-- render this item
		local itemArgs = {}
		for _, printout in ipairs(printouts) do
			printout = printout:gsub("%?=?(.-)#?$", "%1") -- strip formatting directives and "?=" syntax for pagename
			itemArgs[printout] = item[printout]
		end
		output = output .. mw.getCurrentFrame():expandTemplate({
			title = "Weapon table row format",
			args = itemArgs
		})
	end

	if output == "" then
		output = defaultText
	else
		output = output .. "|}\n"
	end

	return output
end

return p