Module:ItemDistribution/test

local CargoUtil = require 'Module:CargoUtil' local escq = require 'Module:EscQ'.main1 local List = require 'Module:ListUtil' local Hash = require 'Module:HashUtil'

local REWARD_SOURCES = { ['Story Maps'] = { eventTable = 'Maps', timeTable = 'MapDates', rewardTable = 'MapRewards', cond = "EV.Map RLIKE '^Sdigit:.*'", },	['Paralogue Maps'] = { eventTable = 'Maps', timeTable = 'MapDates', rewardTable = 'MapRewards', cond = "EV.Map LIKE 'X%'", },	['Tactics Drills'] = { eventTable = 'Maps', timeTable = 'MapDates', rewardTable = 'MapRewards', cond = "EV.Map LIKE 'P%'", },	['Heroic Ordeals'] = { eventTable = 'Maps', timeTable = 'MapDates', rewardTable = 'MapRewards', cond = "EV.Map LIKE 'H%'", },	['Chain Challenge'] = { eventTable = 'Maps', timeTable = 'MapDates', rewardTable = 'MapRewards', cond = "EV.Map LIKE 'ST\\_%'", },	['Squad Assault'] = { eventTable = 'Maps', timeTable = 'MapDates', rewardTable = 'MapRewards', cond = "EV.Map LIKE 'SB\\_%'", },	['Special Maps'] = { eventTable = 'Maps', timeTable = 'MapDates', rewardTable = 'MapRewards', cond = "EV.Map LIKE 'L%' OR EV.Map LIKE 'Q%' OR EV.Map LIKE 'R%' OR EV.Map LIKE 'T%' OR EV.Map LIKE 'U%' OR EV.Map LIKE 'V%'", },	['Blessed Gardens'] = { eventTable = 'Maps', timeTable = 'MapDates', rewardTable = 'MapRewards', cond = "EV.Map LIKE 'BG\\_%'", },	['Arena'] = { eventTable = 'ColiseumSeasons', rewardTable = 'ColiseumRewards', cond = "R.Mode='arena'", },	['Arena Assault'] = { eventTable = 'ColiseumSeasons', rewardTable = 'ColiseumRewards', cond = "R.Mode='arena_assault'", },	['Allegiance Battles'] = { eventTable = 'ColiseumSeasons', rewardTable = 'ColiseumRewards', cond = "R.Mode='allegiance_battles'", },	['Voting Gauntlet'] = nil, ['Tempest Trials'] = { eventTable = 'TempestTrials', rewardTable = 'TempestTrialsRewards', },	['Tap Battle'] = nil, ['Grand Conquests'] = { eventTable = 'GrandConquests', rewardTable = 'GrandConquestsRewards', },	['Forging Bonds'] = { eventTable = 'ForgingBonds', rewardTable = 'ForgingBondsRewards', },	['Røkkr Sieges'] = { eventTable = 'RokkrSieges', rewardTable = 'RokkrSiegesRewards', },	['Lost Lore'] = { eventTable = 'LostLore', rewardTable = 'LostLoreRewards', },	['Hall of Forms'] = { eventTable = 'HallOfForms', rewardTable = 'HallOfFormsRewards', },	["Mjölnir's Strike"] = { eventTable = 'MjolnirsStrike', rewardTable = 'MjolnirsStrikeRewards', },	["Heroes' Path"] = nil, ['Quests'] = { eventTable = 'Quests', rewardTable = 'QuestRewards', },	['Events'] = nil, ['Web'] = nil, ['Log-In'] = nil, ['Other'] = nil, }

local ITEM_QUERIES = { ['Orb'] = "R.Kind='item' AND R.Item='Orb'", ['Hero Feather'] = "R.Kind='item' AND R.Item='Hero Feather'", ['Sacred Coin'] = "R.Kind='item' AND R.Item='Sacred Coin'", ['Refining Stone'] = "R.Kind='item' AND R.Item='Refining Stone'", ['Heroic Grail'] = "R.Kind='item' AND R.Item='Heroic Grail'", ['Aether Stone'] = "R.Kind='item' AND R.Item='Aether Stone'", ['Midgard Gem'] = "R.Kind='item' AND R.Item='Midgard Gem'", ['Fire Blessing'] = "R.Kind='item' AND R.Item='Fire Blessing'", }

local itemQuery = function(rewardSource, args, frame) local cargoTables = {('%s=R'):format(rewardSource.rewardTable)} local cargoJoin = {} local cargoWhere = {ITEM_QUERIES[args.kind], rewardSource.cond} local xfield = 'R.StartTime'

if rewardSource.eventTable then cargoTables[#cargoTables + 1] = ('%s=EV'):format(rewardSource.eventTable) cargoJoin[#cargoJoin + 1] = 'R._pageName=EV._pageName' if rewardSource.timeTable then cargoTables[#cargoTables + 1] = ('%s=T'):format(rewardSource.timeTable) cargoJoin[#cargoJoin + 1] = 'EV._pageName=T._pageName' xfield = 'IFNULL(R.StartTime,T.StartTime)' else xfield = 'IFNULL(R.StartTime,EV.StartTime)' end end

local iMax = nil if args.year then iMax = 12 cargoWhere[#cargoWhere + 1] = ("DATE_FORMAT(%s, '%%Y')='%s'"):format(xfield, escq(args.year)) else local ym = frame:callParserFunction('#time', 'Y-m', args.ym) local y, m = mw.ustring.match(ym, '^([^-]+)%-([^-]+)$') iMax = os.difftime(os.time {year = y, month = m + 1, day = 1, hour = 0}, os.time {year = y, month = m, day = 1, hour = 0}) / 86400 cargoWhere[#cargoWhere + 1] = ("DATE_FORMAT(%s, '%%Y-%%m')='%s'"):format(xfield, escq(ym)) end

local rewardsByIndex = List.group_by(CargoUtil.full_query( table.concat(cargoTables, ','), ("R.Amount=count,DATE_FORMAT(MIN(%s), '%s')=x"):format(xfield, args.year and '%m' or '%d'), { join = #cargoJoin > 0 and table.concat(cargoJoin, ',') or nil, where = table.concat(List.map(cargoWhere, function (v) return '(' .. v .. ')' end), ' AND '), groupBy = 'R._ID', }), function (v) return tonumber(v.x) end) Hash.map_self(rewardsByIndex, function (vs) return List.sum(List.map(vs, function (v) return tonumber(v.count) end)) end) return rewardsByIndex, iMax end

local itemTableRow = function (args, frame) if not args.year and not args.ym then return require 'Module:Error'.error('Missing "year" or "ym" argument.') elseif not args.kind then return require 'Module:Error'.error('Missing "kind" argument.') end

local rewardsByIndex = {} local iMax = 0 local tbl = nil

if args.reason then local rewardSource = REWARD_SOURCES[args.reason] if not rewardSource then return require 'Module:Error'.error('TODO: support other reward sources') end rewardsByIndex, iMax = itemQuery(rewardSource, args, frame) else for kind, _ in pairs(REWARD_SOURCES) do			tbl, iMax = itemQuery(REWARD_SOURCES[kind], args, frame) Hash.merge_self(rewardsByIndex, tbl, function (v1, v2) return v1 + v2 end) end end

local total = List.sum(Hash.values(rewardsByIndex)) if total == 0 then return '' end

local tr = mw.html.create('tr') tr:tag('td'):wikitext(args.title and args.title or						     args.reason and ('' .. args.reason .. '†') or 								   frame:expandTemplate{ title = 'ItemIcon', args = {args.kind, size = '40px'} }) local tds = List.generate(iMax, function return tr:tag('td') end) for i = 1, iMax do		if rewardsByIndex[i] then tds[i]:wikitext(rewardsByIndex[i]) elseif args.year or not args.reason then tds[i]:wikitext(0) end end tr:tag('td'):tag('b'):wikitext(total)

return tostring(tr) end

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