Module:RewardUnitTable

local cargo = mw.ext.cargo local List = require 'Module:ListUtil' local Hash = require 'Module:HashUtil' local Datetime = require 'Module:DatetimeUtil'

local main = function (_, frame) local units = List.group_by(cargo.query( 'Distributions,Units', "Unit,IFNULL(CONCAT(Name,': ',Title),Name)=Name,Rarity,Source,Amount,StartTime=t0,EndTime=t1", { join = 'Distributions.Unit=Units._pageName', where = "Type IN ('Automatic','Map','Story','Quest','Tempest Trials','Log-In')", -- exclude heroic grails and purchases limit = 1000, }), function (v) return v.Unit end) for hero, dists in pairs(units) do		dists.name = dists[1].Name dists.minrarity = select(2, List.min(List.select(List.map(dists, function (v) return tonumber(v.Rarity) end), function (x) return x ~= '' end))) dists.t0 = select(2, List.min(List.map(dists, function (v) return v.t0 end))) dists.t1 = select(2, List.max(List.map(dists, function (v) return v.t1 end))) end

local tbl = mw.html.create('table'):addClass('wikitable'):addClass('default'):addClass('sortable'):css('text-align', 'center') local row = tbl:tag('tr') row:tag('th'):css('width', '20%'):wikitext('Hero') row:tag('th'):css('width', '50%'):wikitext('Source') row:tag('th'):css('width', '15%'):wikitext('First available') row:tag('th'):css('width', '15%'):wikitext('Last available')

for hero, dists in Hash.sorted_pairs(units, function (vs1, vs2, k1, k2) return vs1.t0 < vs2.t0 or (vs1.t0 == vs2.t0 and k1 < k2) end) do		local amounts = Hash.map_self(List.group_by(dists, function (v) return v.Source end), function (vs)			return List.sum(List.map(vs, function (v) return tonumber(v.Amount) or 0 end))		end)

row = tbl:tag('tr') row:tag('td'):wikitext(			frame:expandTemplate {title = 'IconFrame', args = {hero = hero, size = 'medium', rarity = dists.minrarity}},			' ', dists.name, '') local ul = row:tag('td'):tag('ul') for source, amount in Hash.sorted_pairs(amounts) do			ul:tag('li'):wikitext(('%s (×%s)'):format(source, amount)) end row:tag('td'):wikitext(Datetime.from_cargo(dists.t0) == 0 and '—' or Datetime.ht(dists.t0)) row:tag('td'):wikitext(Datetime.from_cargo(dists.t1) == 0x7FFFFFFF and '—' or Datetime.ht(dists.t1)) end

return tostring(tbl) end

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