Module:Heroes Navbox

From Fire Emblem Heroes Wiki
Jump to: navigation, search
Documentation for this module may be created at Module:Heroes Navbox/doc
local cargo = mw.ext.cargo
local Util = require 'Module:Util'
local List = require 'Module:ListUtil'
local Hash = require 'Module:HashUtil'
local Navbox = require 'Module:Navbox'

local main = function (_, frame)
	local navboxArgs = {
		state = 'autocollapse',
		name = 'Heroes Navbox',
		title = '[[List of Heroes|Heroes]]',
	}

	local heroes = cargo.query(
		'Units,WeaponTypes',
		'Units._pageName=page,WeaponType=wep,WeaponTypes._pageName=weppage,WeaponTypes.Name=wepname,Color=color,Sort=sort,ColorSort=colorsort', {
			join = 'Units.WeaponType=WeaponTypes.WikiName',
			where = "IFNULL(Properties__full,'') NOT LIKE '%enemy%'",
			groupBy = 'Units.WikiName',
			orderBy = 'IFNULL(IntID,2147483647)',
			limit = 1000,
		})
	local heroesByWeapon = Hash.map_self(List.group_by(heroes, function (v) return v.color end), function (vs)
		return List.group_by(vs, function (v) return v.wep end)
	end)

	local i = 1
	for color, vss in Hash.sorted_pairs(heroesByWeapon, function (x, y) return tonumber(x[next(x)][1].colorsort) < tonumber(y[next(y)][1].colorsort) end) do
		navboxArgs['group' .. i] = color
		local subgroupArgs = {groupwidth = '8em'}
		local j = 1
		for wep, vs in Hash.sorted_pairs(vss, function (x, y) return tonumber(x[1].sort) < tonumber(y[1].sort) end) do
			subgroupArgs['group' .. j] = ('%s [[%s|%s]]'):format(frame:expandTemplate {title = 'WeaponTypeIcon', args = {wep}},
				vs[1].weppage, mw.getContentLanguage():ucfirst(mw.ustring.gsub(vs[1].wepname, color .. '%s*', '')))
			subgroupArgs['list' .. j] = table.concat(List.map(vs, function (v) return Util.getHeroIcon(v.page, '45x45px') end), ' ')
			j = j + 1
		end
		navboxArgs['list' .. i] = frame:expandTemplate {title = 'Navbox subgroup', args = subgroupArgs}
		i = i + 1
	end

	return Navbox._navbox(navboxArgs)
end

return require 'Module:MakeMWModule'.makeMWModule {main = main}