Module:WeaponSpecQuery

From Granblue Fantasy Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:WeaponSpecQuery/doc

local p = {}

local WEAPON_TYPES = {"Sabre", "Dagger", "Spear", "Axe", "Staff", "Gun", "Melee", "Bow", "Harp", "Katana"}
local ELEMENT_ORDER = {fire=1, water=2, earth=3, wind=4, light=5, dark=6, any=7}
local ELEMENT_COLORS = {'#ffe5e5', '#e5e5ff', '#edc3a5', '#e5f2e5', '#ffffe5', '#f2e5f2', '#e7f5fb'}

local function cmpElementName(a, b)
	local ac, bc = a.element, b.element
	if ac ~= bc then
		return (ELEMENT_ORDER[ac] or 99) < (ELEMENT_ORDER[bc] or 99)
	end
	return a.name < b.name
end

local function fetchCharacters(rarity)
	local queryFields = 'characters.id=id,characters._pageName=link,rarity,element,name,weapon'
	local queryArgs = {limit = 1000, where=('rarity = %q'):format(type(rarity) == "string" and rarity:match("[%a%d]+") or "SSR")}

	local result = {}
	for _, row in ipairs(mw.ext.cargo.query('characters', queryFields, queryArgs)) do
		result[#result+1] = row
		row.link = row.link or row.name
		row.element = (row.element or ""):lower()
		row.weaponTypes = {}
		for w in (row.weapon or ''):gmatch("[^, ]+") do
			row.weaponTypes[w:lower()] = 1
		end
	end

	return result
end

function p.renderTableForRarity(frame)
	local chars = fetchCharacters(frame.args.rarity or "SSR")
	local isIconOutput = frame.args.format == "icon"
	table.sort(chars, cmpElementName)
	local tbody = {}
	for w=1, #WEAPON_TYPES do
		w = WEAPON_TYPES[w]
		local row, wf, lastElement = {'<tr style="vertical-align: top"><td style="vertical-align: middle">[[File:Label_Weapon_' .. w .. '.png|link=Category:' .. w .. ' Characters|61px]]'}, w:lower(), 0
		local hasChars, hasElementChars
		for i=1, #chars do
			local char = chars[i]
			for i=lastElement+1, ELEMENT_ORDER[char.element] or 0 do
				row[#row+1] = '</td><td style="background-color: ' .. ELEMENT_COLORS[i] .. '; width:165px;">'
				lastElement, hasElementChars = i
			end
			if char.weaponTypes[wf] then
				row[#row+1] = isIconOutput and ('[[File:' .. char.link .. ' iconA.jpg|80px|link=' .. char.link .. ']]') or ((hasElementChars and '<br/>[[' or '[[') .. char.link .. ']]')
				hasElementChars, hasChars = true, true
			end
		end
		if hasChars then
			for i=lastElement+1, #ELEMENT_COLORS do
				row[#row+1] = '</td><td style="background-color: ' .. ELEMENT_COLORS[i] .. '; width:165px;">'
			end
			row[#row+1] = '</td></tr>'
			tbody[#tbody+1] = table.concat(row, "")
		end
	end
	return table.concat(tbody, "\n")
end

return p