Module:StatOrderTable

local cargo = mw.ext.cargo local List = require 'Module:ListUtil' local HeroUtil = require 'Module:HeroUtil' local Hash = require 'Module:HashUtil' local HeroTableBuilder = require 'Module:HeroTableBuilder'

local STATS = { ' HP ', ' Atk ', ' Spd ', ' Def ', ' Res ', }

local IVS = { ['Neutral']  = {sort = 1, offs = { 0,  0,  0,  0,  0}}, ['+HP/-Atk'] = {sort = 2, offs = { 1, -1,  0,  0,  0}}, ['+HP/-Spd'] = {sort = 3, offs = { 1,  0, -1,  0,  0}}, ['+HP/-Def'] = {sort = 4, offs = { 1,  0,  0, -1,  0}}, ['+HP/-Res'] = {sort = 5, offs = { 1,  0,  0,  0, -1}}, ['+Atk/-HP'] = {sort = 6, offs = {-1,  1,  0,  0,  0}}, ['+Atk/-Spd'] = {sort = 7, offs = { 0, 1, -1,  0,  0}}, ['+Atk/-Def'] = {sort = 8, offs = { 0, 1,  0, -1,  0}}, ['+Atk/-Res'] = {sort = 9, offs = { 0, 1,  0,  0, -1}}, ['+Spd/-HP'] = {sort = 10, offs = {-1,  0,  1,  0,  0}}, ['+Spd/-Atk'] = {sort = 11, offs = { 0, -1, 1,  0,  0}}, ['+Spd/-Def'] = {sort = 12, offs = { 0, 0,  1, -1,  0}}, ['+Spd/-Res'] = {sort = 13, offs = { 0, 0,  1,  0, -1}}, ['+Def/-HP'] = {sort = 14, offs = {-1,  0,  0,  1,  0}}, ['+Def/-Atk'] = {sort = 15, offs = { 0, -1, 0,  1,  0}}, ['+Def/-Spd'] = {sort = 16, offs = { 0, 0, -1,  1,  0}}, ['+Def/-Res'] = {sort = 17, offs = { 0, 0,  0,  1, -1}}, ['+Res/-HP'] = {sort = 18, offs = {-1,  0,  0,  0,  1}}, ['+Res/-Atk'] = {sort = 19, offs = { 0, -1, 0,  0,  1}}, ['+Res/-Spd'] = {sort = 20, offs = { 0, 0, -1,  0,  1}}, ['+Res/-Def'] = {sort = 21, offs = { 0, 0,  0, -1,  1}}, }

local arrayOrder = function (arr) return List.map(arr, function (lhs, i)		return List.count_if(arr, function (rhs, j)			return (i = rhs) or (i > j and lhs > rhs) end)	end) end

local orderToString = function (order) local order2 = List.zip(order, STATS, function (x, y) return {x, y} end) table.sort(order2, function (x, y) return x[1] > y[1] end) return table.concat(List.map(order2, function (v) return v[2] end), ' > ') end

local main = function local avail = HeroUtil.getLowestRarities {mask = {focus = true, nonfocus = true}}

local heroes = cargo.query(		'Units=U,UnitStats=S',		"U._pageName=page,IFNULL(CONCAT(U.Name,': ',U.Title),U.Name)=name,Entries,MoveType,WeaponType,Lv1HP5,Lv1Atk5,Lv1Spd5,Lv1Def5,Lv1Res5", {			join = 'U.WikiName=S.WikiName',			where = "S.WikiName IS NOT NULL AND IFNULL(U.Properties__full,'') NOT LIKE '%enemy%'",			limit = 1000,		}) for _, v in ipairs(heroes) do		v.stats = {tonumber(v.Lv1HP5), tonumber(v.Lv1Atk5), tonumber(v.Lv1Spd5), tonumber(v.Lv1Def5), tonumber(v.Lv1Res5)}

local allOrders = Hash.map(IVS, function (iv)			return orderToString(arrayOrder(List.zip(v.stats, iv.offs, function (x, y) return x + y end)))		end) v.ivSets = {} for k, order in pairs(allOrders) do			if k == 'Neutral' or order ~= allOrders.Neutral then v.ivSets[order] = v.ivSets[order] or {} table.insert(v.ivSets[order], k)			end end for _, vs in pairs(v.ivSets) do			table.sort(vs, function (x, y) return IVS[x].sort < IVS[y].sort end) vs.sortmin = select(2, List.min(List.map(vs, function (x) return IVS[x].sort end))) end end

local theUnit = nil local builder = HeroTableBuilder.new

builder:heroIcon('60px'):css('width', '4%') builder:heroLink:css('width', '18%') builder:heroEntry('30px'):css('width', '6%') builder:moveType('30px'):css('width', '4%') builder:weaponType('30px'):css('width', '4%') for i, v in ipairs(STATS) do		builder:column(mw.ustring.match(v, '>(.*)<'), function (elem) elem:wikitext(theUnit.stats[i]) end):css('width', '7%') end builder:column('Order(s)', function (elem)		local list = elem:css('padding', '.75em 2em'):tag('table'):addClass('inner-hstrip'):css('width', '100%')		local lastTh = nil		local lastTd = nil		for order, ivs in Hash.sorted_pairs(theUnit.ivSets, function (vs1, vs2) return vs1.sortmin < vs2.sortmin end) do			local row = list:tag('tr')			lastTh = row:tag('th'):wikitext(table.concat(ivs, ', '))			lastTd = row:tag('td'):wikitext(order)		end		lastTh:css('width', '50%')		lastTd:css('width', '50%')	end):addClass('unsortable'):css('width', '30%')

for _, u in ipairs(heroes) do		theUnit = u		builder:addHero(u.page, u.name, u.MoveType, u.WeaponType, u.Entries) end

return builder:build:addClass('wikitable'):attr('id', 'max-stats-table') :css('text-align', 'center'):css('width', '100%') end

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