Module:Reward/Tables

local Reward = require 'Module:Reward' local RewardText = require 'Module:RewardText' local List = require 'Module:ListUtil' local toboolean = require 'Module:Bool'.toboolean

local TIERS = { 'Tier 1: 7th Platoon Trainee', 'Tier 2: Whitewing', 'Tier 3: Dragoon', 'Tier 4: Sable Knight', 'Tier 5: Lanceritter', 'Tier 6: Grünritter', 'Tier 7: Weissritter', 'Tier 8: Archer General', 'Tier 9: Mage General', 'Tier 10: Knight General', 'Tier 11: Great General', 'Tier 12: Wyvern General', 'Tier 13: Eight Generals', 'Tier 14: Imperial General', 'Tier 15: Four-Riders Rank', 'Tier 16: Holy Guard', 'Tier 17: 12 Deadlords', 'Tier 18: Hoshidan Noble', 'Tier 19: Nohrian Noble', 'Tier 20: First Dragons', 'Tier 21: Great Summoner', }

local createTable = function (rewards, header, columns) columns = columns or 1

local rowCount = math.ceil(#rewards / columns) local rewardColumns = List.generate(columns, function (i)		return List.sub(rewards, (i - 1) * rowCount + 1, i * rowCount)	end)

local tbl = mw.html.create('table'):addClass('wikitable'):addClass('default'):addClass('reward-table'):css('text-align', 'center') local tr = tbl:tag('tr') for x = 1, columns do		if x > 1 then tr:tag('th'):attr('rowspan', 0):css('width', '.5em'):css('padding', '0') end tr:tag('th'):wikitext(header or '') tr:tag('th'):addClass('reward-table-header'):wikitext('Reward') end

for y = 1, rowCount do		tr = tbl:tag('tr')--:css('height', '50px') for x = 1, columns do			local scoreCell = tr:tag('td') local rewardCell = tr:tag('td') local v = rewardColumns[x] and rewardColumns[x][y] if v then scoreCell:wikitext(v[1]) rewardCell:wikitext(v[2]) end end end

return tbl end

local scoreTable = function (args, defineFn, header, frame) header = header or ' Score' frame = frame or mw.getCurrentFrame if toboolean(args['no cargo']) or mw.title.getCurrentTitle.namespace ~= 0 then defineFn = function end end

local rewards = {} for k, obj in pairs(args) do		local score = tonumber(k) if score then local reward, err = Reward.parse(obj) if reward then rewards[#rewards + 1] = {score, RewardText._main({reward, size = args.iconsize, iconfirst = '1'}, frame)} defineFn(reward, score) else rewards[#rewards + 1] = {score, require 'Module:Error'.error(err)} end end end table.sort(rewards, toboolean(args.reverse) and function (x, y) return x[1] > y[1] end or function (x, y) return x[1] < y[1] end)

local lang = mw.language.getContentLanguage for _, reward in ipairs(rewards) do		reward[1] = lang:formatNum(reward[1]) end

return createTable(rewards, header, tonumber(args.columns) or 1) end

local rankTable = function (args, defineFn, header, frame) header = header or ' Rank' frame = frame or mw.getCurrentFrame if toboolean(args['no cargo']) or mw.title.getCurrentTitle.namespace ~= 0 then defineFn = function end end

local rewards = {} local rewardobjs = {}

for k, obj in pairs(args) do		local rankhi, ranklo = mw.ustring.match(k, '^(%d*)%s*~%s*(%d*)$') if not ranklo then rankhi = mw.ustring.match(k, '^%d+$') ranklo = rankhi end if ranklo then rankhi, ranklo = tonumber(rankhi) or -1, tonumber(ranklo) or -1 local reward, err = Reward.parse(obj) if reward then rewards[#rewards + 1] = {rankhi, ranklo, RewardText._main({reward, size = args.iconsize, iconfirst = '1'}, frame)} rewardobjs[#rewardobjs + 1] = reward else rewards[#rewards + 1] = {rankhi, ranklo, require 'Module:Error'.error(err)} end end end table.sort(rewards, toboolean(args.reverse) and function (x, y) return x[1] > y[1] end or function (x, y) return x[1] < y[1] end)

local best = Reward.best(rewardobjs) if best then defineFn(best) else return require 'Module:Error'.error('Best reward is not unique.') end

local columns = tonumber(args.columns) or 1 local rowCount = math.ceil(#rewards / columns) local rewardColumns = List.generate(columns, function (i)		return List.sub(rewards, (i - 1) * rowCount + 1, i * rowCount)	end)

local lang = mw.language.getContentLanguage

local tbl = mw.html.create('table'):addClass('wikitable'):addClass('default'):addClass('reward-table'):css('text-align', 'center') local tr = tbl:tag('tr') for x = 1, columns do		if x > 1 then tr:tag('th'):attr('rowspan', 0):css('width', '.5em'):css('padding', '0') end tr:tag('th'):wikitext(header) tr:tag('th'):addClass('reward-table-header'):wikitext('Reward') end

local useArenaTiers = toboolean(args['arena tiers']) local useIntervals = toboolean(args.intervals) for y = 1, rowCount do		tr = tbl:tag('tr')--:css('height', '50px') for x = 1, columns do			local rankCell = tr:tag('td') local rewardCell = tr:tag('td') local v = rewardColumns[x] and rewardColumns[x][y] if v then if useArenaTiers then rankCell:wikitext(TIERS[v[1]]) elseif useIntervals then rankCell:wikitext('Interval ', v[1]) elseif v[1] == v[2] then rankCell:wikitext(v[1] ~= -1 and lang:formatNum(v[1]) or '') else rankCell:wikitext(v[1] ~= -1 and (lang:formatNum(v[1]) .. ' ') or '', '~', v[2] ~= -1 and (' ' .. lang:formatNum(v[2])) or '') end rewardCell:wikitext(v[3]) end end end

return tbl end

return { score = scoreTable, rank = rankTable, createTable = createTable, }