Difference between revisions of "Module:Card/models/Limitation history"
(doc comments, rename some functions, make `currentStatus` an attribute) |
(function to render a status as a badge) |
||
Line 107: | Line 107: | ||
return nil | return nil | ||
+ | end | ||
+ | |||
+ | -- Render a given status as a badge | ||
+ | -- @param status string | ||
+ | -- @param context string Text to display in parentheses after the status | ||
+ | function LimitationHistory.renderStatusBadge(status, context) | ||
+ | local badge = mw.html.create('div') | ||
+ | |||
+ | local statusText = '[[' .. status .. ']]' | ||
+ | |||
+ | if (context and context ~= '') then | ||
+ | statusText = statusText .. ' (' .. context .. ')' | ||
+ | end | ||
+ | |||
+ | badge | ||
+ | :addClass('status-' .. (status:gsub(' ', '-')):lower()) | ||
+ | :css('border', '2px solid #666') | ||
+ | :css('border-radius', '3px') | ||
+ | :css('display', 'inline-block') | ||
+ | :css('padding', '.1em .5em') | ||
+ | :wikitext(statusText) | ||
+ | |||
+ | return tostring(badge) | ||
end | end | ||
return LimitationHistory | return LimitationHistory |
Revision as of 14:08, 10 December 2023
-- @field card string Page name of the card whose history this is for
-- @field format string The format the history is for (e.g. "Advanced Format", "OCG", "Korean")
-- @field history table List with details on each limitation list that the card was on and the status it had on each list
-- @field groupedHistory table Shorter version of `history` where consecutive lists in which the card had the same status are grouped together
-- @field currentStatus string The card's status on the list that's in effect
local LimitationHistory = {
card = nil,
format = nil,
history = {},
groupedHistory = {},
currentStatus = nil,
}
-- Create a new instance of a LimitationHistory object
-- @param card string Page name of the card
-- @param format string Page name of the format
-- @return LimitationHistory
function LimitationHistory:new(card, format)
local lh = mw.clone(LimitationHistory)
lh.card = card
lh.format = format
lh.history = lh:lookupHistory()
lh.groupedHistory = lh:getGroupedHistory()
lh.currentStatus = lh:getCurrentStatus()
return lh
end
-- Look up all limitation status lists that include the card for the format
-- @return table
function LimitationHistory:lookupHistory()
local results = mw.smw.ask{
'[[List contains::' .. self.card .. ']]' ..
'[[-Has subobject.Page type::Status list]]' ..
'[[-Has subobject.Format::' .. self.format .. ']]',
'?Status# = status',
'?-Has subobject# = list',
'?-Has subobject.Start date# = startDate',
'?-Has subobject.Start date = startDateFormatted',
'?-Has subobject.End date# = endDate',
'?-Has subobject.End date = endDateFormatted',
'mainlabel = -',
'limit = 500'
} or {}
-- SMW sorting doesn't work with inverse properties. Doing it in Lua instead.
table.sort(results and results, orderHistory)
return results
end
-- Merge consecutive history items with the same status together
-- @return table
function LimitationHistory:getGroupedHistory()
local i = 0
local groupedHistory = {}
-- Can't use `for item in pairs(self.history) do`
-- because the order will get messed up.
for h = 1, #self.history do
local item = self.history[h]
-- Check if this item has a different status than the incumbent item
-- Always true for the first item.
local hasNewStatus = (i == 0 or item.status ~= groupedHistory[i].status)
if (hasNewStatus) then
-- Create a new object at the next space in the table
i = i + 1
groupedHistory[i] = mw.clone(item)
groupedHistory[i].firstList = item.list
groupedHistory[i].list = nil
else
-- Update the end date on the existing object
groupedHistory[i].endDate = item.endDate
groupedHistory[i].endDateFormatted = item.endDateFormatted
groupedHistory[i].lastList = item.list
end
end
return groupedHistory
end
function orderHistory(item1, item2)
return (item1.startDate or 'a') < (item2.startDate or 'a')
end
-- Get the current limitation status
-- @return string
-- @todo: Avoid potential issue with non-active lists that don't specify an end date
function LimitationHistory:getCurrentStatus()
local today = os.date('%Y-%m-%d')
-- Loop through the history items
-- If today falls within the start and end dates, that's the current status
for _, item in pairs(self.groupedHistory) do
local todayAfterStartDate = not item.startDate or item.startDate <= today
local todayBeforeEndDate = not item.endDate or item.endDate >= today
if (todayAfterStartDate and todayBeforeEndDate) then
-- Return the status or `nil` if it's "Unlimited"
return item.status == 'Unlimited'
and nil
or item.status
end
end
return nil
end
-- Render a given status as a badge
-- @param status string
-- @param context string Text to display in parentheses after the status
function LimitationHistory.renderStatusBadge(status, context)
local badge = mw.html.create('div')
local statusText = '[[' .. status .. ']]'
if (context and context ~= '') then
statusText = statusText .. ' (' .. context .. ')'
end
badge
:addClass('status-' .. (status:gsub(' ', '-')):lower())
:css('border', '2px solid #666')
:css('border-radius', '3px')
:css('display', 'inline-block')
:css('padding', '.1em .5em')
:wikitext(statusText)
return tostring(badge)
end
return LimitationHistory