Module:Reward/LostLore

local Reward = require 'Module:Reward' local RewardTables = require 'Module:Reward/Tables' local Hash = require 'Module:HashUtil' local List = require 'Module:ListUtil' local Datetime = require 'Module:DatetimeUtil' local parseArgs = require 'Module:ObjectArg'.parse local MF = require 'Module:MF'.main1

local SECONDS_PER_LINE = 1 * 60 * 60 / 1.2 local STRIKE_NECESSARY_SECONDS = 20 * 60 * 60

local lines = function (args, frame) args.columns = args.columns or 2

local start = args.start and {['StartTime'] = args.start} or mw.ext.cargo.query('LostLore', 'StartTime', {		where = ("_pageName='%s'"):format(mw.title.getCurrentTitle.text),		limit = 1,	})[1] start = frame:callParserFunction('#time', 'U', start and start['StartTime'] or '')

local teamCount = 1 local extraTeams = {} mw.ustring.gsub(args.extraTeams or '', '(%d+)', function(v) table.insert(extraTeams, v) end) local keys = Hash.keys(args) table.sort(keys, function(v1, v2) return (tonumber(v1) or -1) < (tonumber(v2) or -1) end) local linesToTime = {} local oldKey = 0 for _, key in ipairs(keys) do		if tonumber(key) then start = start + (tonumber(key) - oldKey) * SECONDS_PER_LINE / teamCount oldKey = tonumber(key) table.insert(linesToTime, key, Datetime.to_iso8601(start)) if Hash.any(extraTeams, function(value) return tonumber(value) == tonumber(key) end) then teamCount = teamCount + 1 end end end

return RewardTables.score(args, function (reward, line)		Reward.define(reward, 'LostLore', { from = linesToTime[tonumber(line)], wikiname = args.wikiname or MF(mw.title.getCurrentTitle.text .. ' ' .. os.date('%Y%m%d', start)), }, frame)	end, 'Lore Lines', frame) end

local location = function (args, frame) local extraTeams = {} mw.ustring.gsub(args.extraTeams or '', '(%d+)', function(v) table.insert(extraTeams, tonumber(v)) end) table.sort(extraTeams) local start = args.start and {['StartTime'] = args.start} or mw.ext.cargo.query('LostLore', 'StartTime', {		where = ("_pageName='%s'"):format(mw.title.getCurrentTitle.text),		limit = 1,	})[1] start = frame:callParserFunction('#time', 'U', start and start['StartTime'] or '') local locations = parseArgs(args.locations or '{}')

local tbl = mw.html.create('table'):addClass('wikitable'):addClass('sortable'):css('text-align', 'center') local row = tbl:tag('tr') row:tag('th'):wikitext('Location') row:tag('th'):wikitext('Image') row:tag('th'):wikitext('Lines required') row:tag('th'):wikitext('Clear rewards')

local teamCount = 1 local timer = start local linesCounter = 0 local reverseRow = mw.html.create for _, locGroup in ipairs(locations) do		for _, location in ipairs(locGroup) do			row = mw.html.create('tr') row:tag('td'):wikitext(location.name) row:tag('td'):wikitext('') row:tag('td'):wikitext(location.isCombat and '—' or tostring(location.linesReq)) row:tag('td'):wikitext(require 'Module:RewardText'._main({ Reward.normalize(location.rewards), iconfirst=1 }, frame)) row:node(tostring(reverseRow)) reverseRow = row

Reward.define(Reward.normalize(location.rewards), 'LostLore', {				location = location.name,				from = Datetime.to_iso8601(timer + (location.isCombat and STRIKE_NECESSARY_SECONDS or location.linesReq * SECONDS_PER_LINE)),				wikiname = args.wikiname or MF(mw.title.getCurrentTitle.text .. ' ' .. os.date('%Y%m%d', start)),			}, frame) end local neededLines = List.reduce(locGroup, function(x, y) return x + (y.isCombat and STRIKE_NECESSARY_SECONDS / SECONDS_PER_LINE or y.linesReq) end, 0) while #extraTeams >= 1 and extraTeams[1] <= linesCounter + neededLines do			local diff = extraTeams[1] - linesCounter timer = timer + diff * SECONDS_PER_LINE / math.min(teamCount, #locGroup) neededLines = neededLines - diff linesCounter = linesCounter + diff teamCount = teamCount + 1 table.remove(extraTeams, 1) end timer = timer + neededLines * SECONDS_PER_LINE / math.min(teamCount, #locGroup) linesCounter = linesCounter + neededLines end

tbl:node(tostring(reverseRow)) return tostring(tbl) end

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