Module:HeroTableBuilder

local makeClass = function (libraryName) local obj = {} local checkSelf = require 'libraryUtil'.makeCheckSelfFunction(libraryName, 'obj', obj, 'self')

return obj, setmetatable({}, {		__newindex = function (t, k, v)			obj[k] = function (self, ...)				checkSelf(self, k)				return v(self, ...)			end		end,	}) end

local ctor = function local obj, HeroTableBuilder = makeClass('HeroTableBuilder')

local columns_ = {} local builtin_cols_ = {} local tbl_ = mw.html.create('table'):addClass('sortable') local header_ = tbl_:tag('tr')

local Util = require 'Module:Util' local List = require 'Module:ListUtil' local Hash = require 'Module:HashUtil'

local WEAPON_TYPES = Hash.from_ipairs(mw.ext.cargo.query( 'WeaponTypes=WT,WeaponTypes__Classes=WT_C', "WT.WikiName=WikiName,CONCAT(WT.Type,';',WT.Color,';',GROUP_CONCAT(DISTINCT WT_C._value SEPARATOR ';'))=Props", { join = 'WT._ID=WT_C._rowID', groupBy = 'WT._pageName', orderBy = 'WT.ColorSort,WT.Sort', limit = 200, }), function (v, i)			v._index = i			return v.WikiName, v		end) local MOVE_SORT = Util.getMoveSortOrder local ENTRY_SORT = Hash.from_ipairs(mw.ext.cargo.query('Games', '_pageName,value1'), function (v)		return v._pageName, tonumber(v.value1)	end) -- keep this in sync with Template:EntryText local ENTRY_NAME = {[0] = 'Heroes', 'Shadow Dragon / (New) Mystery', 'Echoes', 'Genealogy of the Holy War', 'Thracia 776', 'The Binding Blade', 'The Blazing Blade', 'The Sacred Stones', 'Path of Radiance', 'Radiant Dawn', 'Awakening', 'Fates', 'Three Houses', 'Tokyo Mirage Sessions ♯FE Encore', }

function HeroTableBuilder:column (title, fn) columns_[#columns_ + 1] = fn		return header_:tag('th'):attr('scope', 'col'):wikitext(title) end

function HeroTableBuilder:heroIcon (size, resplendent) columns_[#columns_ + 1] = {'icon', size or '30px', resplendent or false} builtin_cols_[columns_[#columns_]] = true return header_:tag('th'):attr('scope', 'col'):addClass('unsortable'):css('width', '30px') end

function HeroTableBuilder:heroLink columns_[#columns_ + 1] = {'link'} builtin_cols_[columns_[#columns_]] = true return header_:tag('th'):attr('scope', 'col'):wikitext('Hero') end

function HeroTableBuilder:heroEntry (size) columns_[#columns_ + 1] = {'entry', size or '25px'} builtin_cols_[columns_[#columns_]] = true return header_:tag('th'):attr('scope', 'col'):wikitext('Entry') end

function HeroTableBuilder:moveType (size) columns_[#columns_ + 1] = {'mov', size or '25px'} builtin_cols_[columns_[#columns_]] = true return header_:tag('th'):attr('scope', 'col'):wikitext('Move') end

function HeroTableBuilder:weaponType (size) columns_[#columns_ + 1] = {'wep', size or '25px'} builtin_cols_[columns_[#columns_]] = true return header_:tag('th'):attr('scope', 'col'):wikitext('Weapon') end

function HeroTableBuilder:addHero (page, name, mov, wep, entry) local row = tbl_:tag('tr'):addClass('hero-filter-element') if mov then row:attr('data-move-type', mov) end if wep then row:attr('data-weapon-type', wep):attr('data-weapon-props', WEAPON_TYPES[wep].Props) end if entry then entry = mw.text.split(entry, '%s*,%s*') List.map_self(entry, function (v) return ENTRY_SORT[v] end) table.sort(entry) row:attr('data-titles', table.concat(entry, ';')) end

for _, coldef in ipairs(columns_) do			if builtin_cols_[coldef] then if coldef[1] == 'icon' then row:tag('td'):wikitext(Util.getHeroIcon(page, coldef[2], coldef[3])) elseif coldef[1] == 'link' then row:tag('td'):wikitext(page == name and ('%s'):format(page) or ('%s'):format(page, name)) elseif coldef[1] == 'mov' then row:tag('td'):attr('data-sort-value', MOVE_SORT[mov]) :wikitext((''):format(mov, coldef[2], mov)) elseif coldef[1] == 'wep' then row:tag('td'):attr('data-sort-value', WEAPON_TYPES[wep]._index) :wikitext((''):format(wep, coldef[2], wep)) elseif coldef[1] == 'entry' then local sortvalue = select(2, List.min(entry)) row:tag('td'):attr('data-sort-value', sortvalue) :wikitext(table.concat(List.map(entry, function (v)							return (''):format(v, coldef[2], ENTRY_NAME[v])						end), ' ')) end else coldef(row:tag('td'), page) end end end

function HeroTableBuilder:build return tbl_ end

return obj end

return {new = ctor}