Module:DefResChart

local cargo = mw.ext.cargo local Util = require 'Module:Util' local List = require 'Module:ListUtil' local Hash = require 'Module:HashUtil' local FEHStatUtil = require 'Module:FEHStatUtil'

local mitModes = { sum = function (def, res) return def + res, nil end, min = function (def, res) return math.min(def, res), def < res and 'Def' or res < def and 'Res' or 'Both' end, max = function (def, res) return math.max(def, res), def > res and 'Def' or res > def and 'Res' or 'Both' end, }

local main = function (args, frame) local move_types = Hash.invert(Util.getMoveSortOrder) local mode = args[1] local rarity = tonumber(args.rarity) or 5

local heroes_by_mit = List.group_by(List.map_self(cargo.query('Units=u,UnitStats=s', "u._pageName=page,IFNULL(CONCAT(Name,': ',Title),Name)=name,MoveType=mov,Lv1Def5,DefGR3,Lv1Res5,ResGR3", {		join = 'u.WikiName=s.WikiName',		where = "IFNULL(Properties__full,'') NOT LIKE '%enemy%'",		groupBy = 'u.WikiName',		limit = 1000,	}), function (hero) hero.def = tonumber(hero.Lv1Def5) + FEHStatUtil.getGrowthValue(rarity, tonumber(hero.DefGR3)) hero.res = tonumber(hero.Lv1Res5) + FEHStatUtil.getGrowthValue(rarity, tonumber(hero.ResGR3)) hero.name = ('%s Def: %d, Res: %d'):format(hero.name, hero.def, hero.res) hero.mit, hero.mittext = mitModes[mode](hero.def, hero.res) return hero end), function (v) return v.mit end)

local tbl = mw.html.create('table'):addClass('wikitable'):addClass('default'):addClass('sortable') :css('text-align', 'center'):css('width', '100%'):css('margin', 'auto'):css('font-weight', '700') local row = tbl:tag('tr') row:tag('th'):css('width', '8%'):wikitext('Mitigation') for _, mov in ipairs(move_types) do		row:tag('th'):css('width', '23%'):wikitext(mov) end

local reverseMit = function (_, _, k1, k2) return k1 > k2 end for mit, heroes in Hash.sorted_pairs(heroes_by_mit, reverseMit) do		local heroes_by_mov = Hash.map_self(List.group_by(heroes, function (v) return v.mov end), function (vs, mov)			return List.map_self(vs, function (hero) return mw.html.create('div'):css('display', 'inline-block') :wikitext(frame:expandTemplate {title = 'Template:TooltipUnitBox', args = {hero.page, hero.name}}) :wikitext(hero.mittext and (' ' .. hero.mittext)) end)		end)

row = tbl:tag('tr') row:tag('th'):wikitext(tonumber(mit)) for _, mov in ipairs(move_types) do			List.reduce(heroes_by_mov[mov] or {}, function (cell, div) return cell:node(div) end, row:tag('td')) end end

return tbl end

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