Module:PES Rerun Calculator
Appearance
This module calculates the duration since a PES character's last run, based on the start_date parameter of the {{Event }} template. Used on {{PES Rerun Calculator }}.
local p = {}
local util = require('Module:Module Util')
local wrapperTemplate = "Template:PES Rerun Calculator"
--Function to determine if string is empty value
local function isEmpty(s)
return s == nil or s == ''
end
local function starts_with(str, start)
return str:sub(1, #start) == start
end
local function constructTableWithHeader()
local output = mw.html.create() --create html frame
output = output:tag('table')
:attr( 'class', 'data-table sortable' )
:css( 'width', '100%' )
:css( 'align', 'center' )
output:tag('tr')
:tag('th')
:attr( 'width', '30%' )
:wikitext("Event Name")
:tag('th')
:attr( 'width', '25%' )
:wikitext("Event Start")
:tag('th')
:attr( 'width', '20%' )
:attr( 'class', 'unsortable' )
:wikitext("Time Since")
:tag('th')
:attr( 'width', '25%' )
:wikitext("Reward Character")
return output
end
local function constructOutputTable(eventDetailsArray)
local outputTable = constructTableWithHeader()
--construct table content
for _, row in ipairs(eventDetailsArray) do
local tr = outputTable:tag('tr')
tr:tag('td'):wikitext(string.format('[[%s|%s]]', row.link, row.name))
tr:tag('td'):wikitext(row.display)
tr:tag('td'):wikitext(util.timeSince(row.key))
tr:tag('td'):wikitext(row.reward_char)
end
return outputTable
end
local function getEventListString(frame)
return frame:callParserFunction( "#dpl:category = Parallel Event System Events",
{
uses = "Template:Event",
include = "{Event}:name,{Event}:time_start,{Event}:reward_char",
ordermethod="titlewithoutnamespace",
allowcachedresults=true,
cacheperiod=604800, --cache period set to 604,800 seconds/7 days - don't need a high refresh on this
listseparators = ",,,",
secseparators = "%TITLE%||,||,"..
",||,"..--time_start [3]
",>,"--reward_char [4]
} )
end
--uses the template as a wrapper, grabbing any arguments passed to the template
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrapper=wrapperTemplate
})
return p._main(args, frame)
end
function p._main(args, frame)
frame = frame or mw.getCurrentFrame()
--find name, image and dates of all events, and split into array
local dpl = getEventListString(frame)
if isEmpty(dpl) then
local emptyTable = constructTableWithHeader()
emptyTable
:tag('tr')--create row
:tag('td')
:attr( 'colspan', '3' )
:wikitext("<center>'''CRUSHED!'''<br>[[File:Crushed Error.png|center|link=|220px]]<br>''We couldn't find any occurences of this event!''</center>[[Category:Events with no History]]")
return emptyTable
end
local killLastCharacter = string.sub(dpl, 1, -2)
local eventSplit = mw.text.split(killLastCharacter, '>', true)
local detailsSplit = {}
for k,v in ipairs(eventSplit) do --split event details
local vArray = mw.text.split(v, '||', true) --split string on the bars
local arrayV = {
["link"] = vArray[1],
["name"] = vArray[2],
["startDate"] = vArray[3],
["reward_char"] = vArray[4],
}
--if the reward char is missing (or G&W), skip event
if not isEmpty(vArray[4]) and not (vArray[4]=="Ginger & Wasabi") then
table.insert(detailsSplit, arrayV)
end
end
--sort the list
table.sort(detailsSplit, function(a,b) return a.startDate < b.startDate end)
-- parse "YYYY-M-D" optionally followed by "H:MM" / "H:MM:SS"
local function parseDate(str)
local y, m, d, hh, mm = tostring(str or ''):match(
'^%s*(%d+)%s*%-%s*(%d+)%s*%-%s*(%d+)%s*' .. -- date
'[T ]?%s*(%d*)%s*:?%s*(%d*)' .. -- optional time
'%s*:?%s*%d*%s*$' -- optional seconds
)
if not y then return nil end
y, m, d = tonumber(y), tonumber(m), tonumber(d)
hh, mm = tonumber(hh) or 0, tonumber(mm) or 0
if m < 1 or m > 12 or d < 1 or d > 31 then return nil end
if hh > 23 or mm > 59 then return nil end
return {
key = string.format('%04d%02d%02d%02d%02d', y, m, d, hh, mm),
display = string.format('%04d-%02d-%02d', y, m, d),
}
end
-- build a map of character -> latest start date
local latestByChar = {}
for _, event in ipairs(detailsSplit) do
local char = mw.text.trim(event.reward_char or '')
local parsed = parseDate(event.startDate)
--DEBUG
if not parseDate(event.startDate) then
mw.log('unparsed date for ' .. tostring(event.reward_char) .. ': [' .. tostring(event.startDate) .. ']')
end
if char ~= '' and parsed then
local current = latestByChar[char]
if not current or parsed.key > current.key then
latestByChar[char] = {
key = parsed.key,
display = parsed.display,
name = event.name,
link = event.link,
}
end
end
end
-- flatten to an array and sort by date, newest first
local latestList = {}
for char, data in pairs(latestByChar) do
latestList[#latestList + 1] = {
reward_char = char,
key = data.key,
display = data.display,
name = data.name,
link = data.link,
}
end
table.sort(latestList, function(a, b) return a.key > b.key end)
return constructOutputTable(latestList)
end
return p