Module:MapList

From Fire Emblem Heroes Wiki
Jump to: navigation, search
Documentation for this module may be created at Module:MapList/doc
local cargo = mw.ext.cargo
local escq = require 'Module:EscQ'.main1
local toboolean = require 'Module:Bool'.toboolean
local mf = require 'Module:MF'.main1
local List = require 'Module:ListUtil'
local Hash = require 'Module:HashUtil'

local byGroup = function (args)
	local group = args[1]
	local isDerived = toboolean(args.derived)

	local unitList = cargo.query(
		isDerived and 'Maps=M,DerivedMaps=DM,MapUnits=MU,Units=U' or 'Maps=M,MapUnits=MU,Units=U',
		"M.Map=map,M._pageName=mpage,M.MapName=mname,M.HasReward=rewards,U._pageName=upage,U.Name=uname,IF(U.Properties__full LIKE '%generic%','1','')=isGeneric", {
			join = isDerived and 'M._pageName=DM._pageName,DM.BaseMap=MU._pageName,MU.Unit=U.WikiName' or 'M._pageName=MU._pageName,MU.Unit=U.WikiName',
			where = ("M.MapGroup='%s' AND U.WikiName IS NOT NULL"):format(escq(group)),
			groupBy = 'MU._pageName,MU.Unit',
			orderBy = 'MU.Slot,uname',
			limit = 2000,
		})
	local unitsByMap = List.group_by(unitList, function (v) return v.mpage end)

	local tbl = mw.html.create('table'):addClass('wikitable'):addClass('default'):addClass('unsortable')
		:css('text-align', 'center'):css('width', '90%')

	local row = tbl:tag('tr')
	row:tag('th'):css('width', '20%'):wikitext('Map name')
	row:tag('th'):css('width', '10%'):wikitext('Rewards')
	row:tag('th'):css('width', '60%'):wikitext('Units')

	for mpage, vs in Hash.sorted_pairs(unitsByMap, function (vs1, vs2) return vs1[1].map < vs2[1].map end) do
		row = tbl:tag('tr')
		row:tag('td'):wikitext(('[[%s|%s]]'):format(mpage, vs[1].mname))
		row:tag('td'):css('text-align', 'left'):wikitext(vs[1].rewards)
		local cell = row:tag('td')
		for _, v in ipairs(vs) do
			cell:tag('div'):css('display', 'inline-block'):css('width', '70px'):css('vertical-align', 'top'):css('word-wrap', 'break-word')
				:wikitext(('[[File:%s_%s.png|x63px|link=%s]]<br/>[[%s%s]]'):format(
					mf(v.upage), v.isGeneric ~= '' and 'Mini_Unit_Idle' or 'Face_FC', v.upage, v.upage, v.uname ~= v.upage and ('|' .. v.uname) or ''))
		end
	end

	return tostring(tbl)
end

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