Module:Sequences

local p = {} local h = {}

local Franchise = require("Module:Franchise") local Term = require("Module:Term") local utilsArg = require("Module:UtilsArg") local utilsCargo = require("Module:UtilsCargo") local utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable")

local CARGO_TABLE = "Sequences"

function p.SortValue(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Sort Value"]) local game = args.game local sequence = args.sequence local subject = args.subject if err then return subject, err.categoryText end local term = Term.link(subject, game) local sortValue = p.sortValue(game, sequence, subject) if not sortValue then local warn = ("Sort value not found for  in game  . Defaulting to alphabetical. See Data:Sequences/%s for list of valid sort values.") :format(subject, game, game) mw.addWarning(warn) return ""..term else local dataSortValue = string.format('data-sort-value="%s"|', sortValue) return dataSortValue..term end end

function p.sortValue(game, sequence, subject) local queryResults = utilsCargo.query(CARGO_TABLE, "ordinality", {		where = utilsCargo.allOf({ game = game, subject = subject, sequence = sequence, }),		orderBy = "sequence",	}) local sortValue = queryResults[1] and tonumber(queryResults[1].ordinality) return sortValue end

p.Templates = { ["Sort Value"] = { purpose = "Displays terms in sortable tables such that the table data can be sorted according to an in-game order that was pre-defined using Template:Term List.", usesData = true, paramOrder = {1, 2, "sequence"}, params = { [1] = {				name = "game", type = "string", required = true, desc = "A game code.", enum = Franchise.enum, },			[2] = {				name = "subject", type = "string", required = true, desc = "A wiki page name to be sorted according to an in-game sequence.", },			sequence = { type = "string", canOmit = true, desc = "If the given subject belongs to more than one sequence in the given game, use this parameter to specify which sequence to use for sorting. See Module:Sequences." },		},	}, }

function p.Schemas return { getSortValue = { game = { type = "string", required = true, desc = "A game code from Data:Franchise.", },			sequence = { name = "sequence", type = "string", required = true, desc = "A sequence name. See Template:Sort Value.", },			subject = { type = "string", required = true, desc = "A wiki page name to get sort value for.", }		}	} end

function p.Documentation return { sortValue = { params = {"game", "sequence", "subject"}, returns = "Integer sort value of  for the given   in , or   if none found.", cases = { outputOnly = true, {					args = {"ST", "Realms", "Snow Realm"}, expect = 2 },				{					args = {"ST", "Realms", "💩 Realm"}, expect = nil },			}		},	} end

return p