Module:Reward/FrontlinePhalanx

local Reward = require 'Module:Reward' local RewardTables = require 'Module:Reward/Tables' local RewardText = require 'Module:RewardText'._main local parseArgs = require 'Module:ObjectArg'.parse local Datetime = require 'Module:DatetimeUtil' local List = require 'Module:ListUtil'

local daily = function(args, frame) local query = mw.ext.cargo.query('FrontlinePhalanx', 'StartTime,EndTime', {		where = ("_pageName='%s'"):format(mw.title.getCurrentTitle.text),		limit = 1,	})[1] t0 = args.start and Datetime.from_iso8601(args.start) or Datetime.from_cargo(query and query['StartTime'] or '') t1 = args['end'] and Datetime.from_iso8601(args['end']) or Datetime.from_cargo(query and query['EndTime'] or '')

local nbDays = math.min(		require 'Module:HashUtil'.count_if(args, function(v, k) return tonumber(k) end),		math.ceil((t1 - t0) / 86400)	) local rewardList = {} for k, v in pairs(args) do		if tonumber(k) then local reward, err = Reward.parse(v) if err then return require 'Module:Error'.error(err) end if tonumber(k) <= nbDays then rewardList[#rewardList + 1] = { k .. '/' .. nbDays, RewardText({reward, size = args.iconsize, iconfirst = '1'}, frame) } Reward.define(reward, 'FrontlinePhalanx', {					source = 'daily',					from = Datetime.to_iso8601(t0 + (tonumber(k) - 1) * 86400),				}, frame) elseif k == nbDays + 1 then rewardList[#rewardList + 1] = { 'Remaining', reward } else List.concat_self(rewardList[#rewardList][2], reward) end end end if #rewardList > 0 and type(rewardList[#rewardList][2]) == 'table' then local reward = List.reduce(rewardList[#rewardList][2], function (t, r)			local _, a = List.find_if(t, function (v) return v.kind == r.kind end)			if a then				a['count'] = a['count'] + r['count']			else				t[#t + 1] = { kind = r.kind, count = r.count }			end			return t		end, {}) Reward.define(reward, 'FrontlinePhalanx', {			source = 'daily',			from = Datetime.to_iso8601(t1 + 1),		}, frame) rewardList[#rewardList][2] = RewardText({rewardList[#rewardList][2], size = args.iconsize, iconfirst = '1'}, frame) end table.sort(rewardList, function (x, y) return x[1] < y[1] end) return tostring(RewardTables.createTable(rewardList, 'Day', args.columns or 1)) .. (rewardList[#rewardList][1] == 'Remaining' and ('Note: Remaining rewards are given to players who claimed all daily rewards.') or '') end

local bosses = function (args, frame) local data = mw.ext.cargo.query('FrontlinePhalanx', 'Bosses__full=Bosses,DefeatTimes__full=DefeatTimes', {		where = ("_pageName='%s'"):format(mw.title.getCurrentTitle.text),		limit = 1,	})[1] bosses = {} defeatTimes = {} mw.ustring.gsub(data['Bosses'], "%s*([^,]+)", function(boss) table.insert(bosses, boss) end) mw.ustring.gsub(data['DefeatTimes'], "%s*([^,]+)", function(t) table.insert(defeatTimes, Datetime.from_cargo(t)) end)

rewardList = {} for i = 1, #bosses do		local def, err = parseArgs(args[i] or '{}') if err then return require 'Module:Error'.error(err) end local reward, err = Reward.normalize(def.rewards or def) if err then return require 'Module:Error'.error(err) end

rewardList[#rewardList + 1] = { frame:expandTemplate({ title = "IconFrame", args = {				hero = def.boss or bosses[i],				rarity = def.rarity or (5 - #bosses + i)			} }), RewardText({reward, size = args.iconsize, iconfirst = '1'}, frame) }		if #defeatTimes >= i then Reward.define(reward, 'FrontlinePhalanx', {				source = 'boss',				from = Datetime.to_iso8601(defeatTimes[i] + 1),				to = Datetime.MAX_TIME,			}, frame) end end return RewardTables.createTable(require 'Module:ListUtil'.reverse(rewardList), 'Boss', args.columns or 1) end

local rank = function (args, frame) local query = args['end'] and {['EndTime'] = args['end']} or mw.ext.cargo.query('FrontlinePhalanx', 'EndTime', {		where = ("_pageName='%s'"):format(mw.title.getCurrentTitle.text),		limit = 1,	})[1] endTime = Datetime.from_cargo(query and query['EndTime'] or '')

return RewardTables.rank(args, function (reward)		Reward.define(reward, 'FrontlinePhalanx', { source = 'rank', from = Datetime.to_iso8601(endTime + 1), to = Datetime.MAX_TIME, }, frame)	end, nil, frame) end

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