Module:User:RichyRich515/tierlisttesting

local List = require 'Module:ListUtil' local Hash = require 'Module:HashUtil' local cargo = mw.ext.cargo local escq = require 'Module:EscQ'.main1 local toboolean = require 'Module:Bool'.toboolean

local TIERS = { {letter = 'S+', number = '10.0'}, {letter = 'S', number = '9.5'}, {letter = 'S-', number = '9.0'}, {letter = 'A+', number = '8.5'}, {letter = 'A', number = '8.0'}, {letter = 'A-', number = '7.5'}, {letter = 'B+', number = '7.0'}, {letter = 'B', number = '6.5'}, {letter = 'B-', number = '6.0'}, {letter = 'C', number = '5.5'}, }

local WEAPON_BGCOLS = { Red = '#efe6e1', Blue = '#e1e6ef', Green = '#e5efe1', Colorless = '#eeeeee', }

local WEAPON_GROUPS = { {color = 'Red', types = {'Red Sword'}, newgroup = true}, {color = 'Red', types = {'Red Dagger', 'Red Bow', 'Red Tome'}}, {color = 'Red', types = {'Red Breath', 'Red Beast'}}, {color = 'Blue', types = {'Blue Lance'}, newgroup = true}, {color = 'Blue', types = {'Blue Dagger', 'Blue Bow', 'Blue Tome'}}, {color = 'Blue', types = {'Blue Breath', 'Blue Beast'}}, {color = 'Green', types = {'Green Axe'}, newgroup = true}, {color = 'Green', types = {'Green Dagger', 'Green Bow', 'Green Tome'}}, {color = 'Green', types = {'Green Breath', 'Green Beast'}}, {color = 'Colorless', types = {'Colorless Bow'}, newgroup = true}, {color = 'Colorless', types = {'Colorless Dagger'}}, {color = 'Colorless', types = {'Colorless Breath', 'Colorless Beast'}}, {color = 'Colorless', types = {'Colorless Staff'}, newgroup = true}, }

local WEAPON_TYPE_INDEX = {} for i, group in ipairs(WEAPON_GROUPS) do	for _, v in ipairs(group.types) do		WEAPON_TYPE_INDEX[v] = i	end end

local tierList = function (args, frame) local isEmblem = toboolean(args.emblem) local iconTemplate = toboolean(args.icons) and 'Template:TierListUnitBoxR' or 'Template:TierListUnitBox'

local weaponTypeQuery = Hash.from_pairs(cargo.query('Units', '_pageName,WeaponType', { where = "IFNULL(Properties__full,'') NOT LIKE '%enemy%'", groupBy = 'WikiName', limit = 1000, }), function (v) return v._pageName, v.WeaponType end)

local tbl = mw.html.create('table'):addClass('wikitable'):addClass('tierlist') :css('text-align', 'center'):css('max-width', '1920px') local tr = tbl:tag('tr') tr:tag('th'):css('height', '35px'):css('border-bottom', 'solid 2px black'):wikitext('Rating')

for _, group in ipairs(WEAPON_GROUPS) do		local th = tr:tag('th'):css('width', '84px'):css('border-bottom', 'solid 2px black') for _, v in ipairs(group.types) do			th:wikitext((''):format(v)) end if group.newgroup then th:css('border-left', 'solid 2px black') end end

local heroCount = 0 local tierTable = List.map(TIERS, function (tier, i)		local tierHeroes = {}		for hero in mw.text.gsplit(mw.text.trim(args[i] or ''), '%s*,%s*') do			if weaponTypeQuery[hero] then				tierHeroes[#tierHeroes + 1] = hero				heroCount = heroCount + 1				if not isEmblem then					frame:callParserFunction('#vardefine', {hero, tier.letter})				end			else				mw.log("Unknown Hero: " .. hero)			end		end		table.sort(tierHeroes)		return tierHeroes	end)

local hiTier = List.find_if(tierTable, function (v) return #v > 0 end) or #TIERS + 1 local loTier = List.rfind_if(tierTable, function (v) return #v > 0 end) or 0

for i, tier in ipairs(TIERS) do		if i >= hiTier and i <= loTier then tr = tbl:tag('tr') local header = tr:tag('td'):tag('b'):wikitext(tier.letter) if not isEmblem then header:tag('div'):css('font-size', '85%'):css('color', '#636161'):wikitext('(', tier.number, ')') end local cells = List.map(WEAPON_GROUPS, function (group)				local td = tr:tag('td'):css('background-color', WEAPON_BGCOLS[group.color])				return group.newgroup and td:css('border-left', 'solid 2px black') or td			end) for _, hero in ipairs(tierTable[i]) do				cells[WEAPON_TYPE_INDEX[weaponTypeQuery[hero]]]:wikitext(frame:expandTemplate {title = iconTemplate, args = {hero}}) end end end

mw.log("Hero count: " .. heroCount) return tostring(tbl) end

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