Module:HeroicOrdealsRewardTable

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

local BOOKS = { ['Book I'] = {dragonflowers = 2, min_id = 1, max_id = 190, dates = '2017-02-02 – 2017-11-21', bgcol = '#a3e3e3'}, ['Book II'] = {dragonflowers = 8, min_id = 191, max_id = 316, dates = '2017-11-21 – 2018-11-28', bgcol = '#e5a2ba'}, ['Book III'] = {dragonflowers = 40, min_id = 317, max_id = 453, dates = '2018-12-11 – 2019-11-28', bgcol = '#9999cc'}, ['Book IV'] = {dragonflowers = 40, min_id = 454, max_id = 593, dates = '2019-12-06 – 2020-11-27', bgcol = '#9999cc'}, ['Book V'] = {dragonflowers = 40, min_id = 594, max_id = 9999, dates = '2020-12-08 – Present', bgcol = '#9999cc'}, }

local DRAGONFLOWER_ITEMS = { Infantry = 'Dragonflower (I)', Armored = 'Dragonflower (A)', Cavalry = 'Dragonflower (C)', Flying = 'Dragonflower (F)', }

local main = function (args, frame) local MOVE_TYPES = List.map_self(cargo.query('MoveTypes', 'WikiName', {orderBy = 'Sort'}), function (v) return v.WikiName end) local all_heroes = List.map_self(cargo.query('Units', "_pageName=page,IFNULL(CONCAT(Name,': ',Title),Name)=name,MoveType,IntID", { where = "IFNULL(Properties__full,'') NOT LIKE '%enemy%'", orderBy = 'name', limit = 1000, }), function (v)		local num_id = tonumber(v.IntID)		v.book = num_id and Hash.find_if(BOOKS, function (b) return b.min_id and b.max_id and num_id >= b.min_id and num_id <= b.max_id end)		return v	end) local heroes = List.group_by(all_heroes, function (v) return v.book end) local counts_by_book = Hash.map(heroes, function (vs)		return List.sum(List.map(vs, function (v) return v.book and BOOKS[v.book].dragonflowers or 0 end))	end) local counts_by_move = Hash.map(List.group_by(all_heroes, function (v) return v.MoveType end), function (vs)		return List.sum(List.map(vs, function (v) return v.book and BOOKS[v.book].dragonflowers or 0 end))	end) Hash.map_self(heroes, function (units)		return List.group_by(units, function (v) return v.MoveType end)	end)

local tbl = mw.html.create('table'):addClass('wikitable'):addClass('unsortable'):css('text-align', 'center'):css('width', '100%') local row = tbl:tag('tr') row:tag('th'):attr('rowspan', '2'):wikitext('Release date') row:tag('th'):attr('rowspan', '2'):wikitext('Reward amount') row:tag('th'):attr('scope', 'colgroup'):attr('colspan', '4'):wikitext('Heroes') row:tag('th'):attr('rowspan', '2'):wikitext('Total')

row = tbl:tag('tr') for _, mov in ipairs(MOVE_TYPES) do		row:tag('th'):attr('scope', 'col'):wikitext(DRAGONFLOWER_ITEMS[mov]) end

for bname, book in Hash.sorted_pairs(BOOKS) do		row = tbl:tag('tr'):css('background', book.bgcol) row:tag('td'):wikitext(('%s %s'):format(bname, book.dates)) row:tag('td'):wikitext(tostring(book.dragonflowers)) for _, mov in ipairs(MOVE_TYPES) do			local herocell = row:tag('td') for _, v in ipairs(heroes[bname][mov]) do				herocell:wikitext(frame:expandTemplate {title = 'TooltipUnitBox', args = {v.page, v.name}}) end end row:tag('td'):wikitext(counts_by_book[bname]) end

row = tbl:tag('tr') row:tag('th'):attr('colspan', '2'):wikitext('Total') for _, mov in ipairs(MOVE_TYPES) do		row:tag('td'):wikitext(('%d %s'):format(counts_by_move[mov], frame:expandTemplate {title = 'ItemText', args = {DRAGONFLOWER_ITEMS[mov]}})) end row:tag('td'):wikitext(List.sum(Hash.values(counts_by_move)))

-- MediaWiki automatically calls tostring on the returned objects -- we should extend this to other modules whenever possible return tbl end

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