Module:Reward/VotingGauntlet

local Reward = require 'Module:Reward' local RewardText = require 'Module:RewardText'._main local Datetime = require 'Module:DatetimeUtil' local List = require 'Module:ListUtil' local Hash = require 'Module:HashUtil'

local DATA = require 'Module:Reward/VotingGauntlet/data' local ROUND_DURATION = 3600 * 45 local BREAK_DURATION = 3600 * 3

local getTournamentRewards = function (rewardSets, tournamentNumber) for k, v in pairs(rewardSets) do		local nblow, nbhigh = mw.ustring.match(k, '^%s*(%d*)%s*-%s*(%d*)%s*$') if (tonumber(nblow) or 0) <= tournamentNumber and tournamentNumber <= (tonumber(nbhigh) or math.huge) then return v		end end end

local armyVictory = function (args, frame) local t0 = Datetime.from_iso8601(args.start)

local rewards = getTournamentRewards(DATA.victory, tonumber(args.tournamentNumber)) for i = 1, #rewards do		rewards[i] = Reward.normalize(rewards[i]) if not args['no cargo'] then Reward.define(rewards[i], 'VotingGauntlet', {				source = 'victory',				from = Datetime.to_iso8601(t0 + (i-1) * (ROUND_DURATION + BREAK_DURATION) + ROUND_DURATION),			}, frame) end end local tbl = mw.html.create('table'):addClass('wikitable'):addClass('default'):addClass('reward-table'):css('text-align', 'center') local headerRow = tbl:tag('tr') local bodyRow = tbl:tag('tr') headerRow:tag('th') bodyRow:tag('th'):wikitext('Army victory') for i = 1, #rewards do headerRow:tag('th'):wikitext('Round ' .. tostring(i)) bodyRow:tag('td'):wikitext(RewardText({rewards[i], iconfirst = 1}, frame)) end

return tostring(tbl) end

local rank = function (args, frame) local insert = table.insert local t0 = Datetime.from_iso8601(args.start) local rankRewards = getTournamentRewards(DATA.rank, tonumber(args.tournamentNumber)) local finalRewards = getTournamentRewards(DATA.totalRank, tonumber(args.tournamentNumber))

local rewardTable = {} for i, list in ipairs(rankRewards) do		for k, v in pairs(list) do			k = mw.ustring.gsub(k, '%s', '') if not rewardTable[k] then rewardTable[k] = {} end for _ = #rewardTable[k], i-2 do				insert(rewardTable[k], {}) end rewardTable[k][#rewardTable[k]+1] = Reward.normalize(v) end end for k, v in pairs(finalRewards) do		k = mw.ustring.gsub(k, '%s', '') if not rewardTable[k] then rewardTable[k] = {} end for _ = #rewardTable[k], #rankRewards-1 do			insert(rewardTable[k], {}) end rewardTable[k][#rewardTable[k]+1] = Reward.normalize(v) end if not args['no cargo'] then for i, rewards in ipairs(List.transpose(Hash.values(rewardTable))) do			local best = Reward.best(rewards) if best then Reward.define(best, 'VotingGauntlet', {					source = i <= #rankRewards and 'rank' or 'total',					from = Datetime.to_iso8601(t0 + ((i <= #rankRewards and i or #rankRewards) - 1) * (ROUND_DURATION + BREAK_DURATION) + ROUND_DURATION),				}, frame) else return require 'Module:Error'.error('Best reward is not unique.') end end end

local keys; keys, rewardTable = Hash.to_lists(rewardTable) for i = 1, #keys do		local lo, hi = mw.ustring.match(keys[i], '^(%d*)~(%d*)$') table.insert(rewardTable[i], 1, tonumber(lo) or -1) table.insert(rewardTable[i], 2, tonumber(hi) or -1) end table.sort(rewardTable, function (x, y)		if x[1] ~= y[1] then			return x[1] < y[1]		else			return x[2] < y[2]		end	end)

local lang = mw.language.getContentLanguage local tbl = mw.html.create('table'):addClass('wikitable'):addClass('default'):addClass('reward-table'):css('text-align', 'center') local row = tbl:tag('tr') row:tag('th'):wikitext(' Rank') for i = 1, #rankRewards do row:tag('th'):wikitext('Round ' .. tostring(i)) end row:tag('th'):wikitext('Total') for _, v in ipairs(rewardTable) do		row = tbl:tag('tr') row:tag('th'):wikitext(("%s~%s"):format(v[1] ~= -1 and (lang:formatNum(v[1]) .. ' ') or '', v[2] ~= -1 and (' ' .. lang:formatNum(v[2])) or '')) for i = 3, #v do			row:tag('td'):wikitext(RewardText({v[i] or {}, iconfirst = 1}, frame)) end end return tostring(tbl) end

return require 'Module:MakeMWModule'.makeMWModule { armyVictory = armyVictory, rank = rank, }