Module:Latest tournaments

local cargo = mw.ext.cargo local tooltip = require( 'Module:Tooltip' )._main local p = {}

local TOTAL = 10 local STATES = { 'Upcoming', 'Ongoing', 'Finished', } local TIERS = { ['The International'] = { ['color'] = '#FFF3B2', ['tooltip'] = tooltip({ 'M', 'Major' }) }, ['Major'] = { ['color'] = '#FFF3B2', ['tooltip'] = tooltip({ 'M', 'Major' }) }, ['Minor'] = { ['color'] = '#EFEFEFB3', ['tooltip'] = tooltip({ 'm', 'Minor' }) }, ['Premium'] = { ['color'] = '#F0F5FF', ['tooltip'] = tooltip({ 'P', 'Premium' }) }, ['Qualifier'] = { ['color'] = '#FCFCFC', ['tooltip'] = tooltip({ 'Q', 'Qualifier' }) }, }

local function query(where, limit) --- Run a query against the tournaments table. -- @param where string: A Cargo where clause. -- @param limit number: The total number of tournaments to return. -- @return table: A table holding the Cargo output. if not limit then limit = TOTAL end return cargo.query('tournaments', '_pageName, icon, start_date, end_date, tier', { where=where,                    groupBy="_pageName", orderBy="end_date DESC", limit=limit }) end

local function get_tournaments --- Get the latest tournaments. Gets a maximum of TOTAL tournaments. -- @return table: A table holding the TOTAL most recent tournaments, sorted by whether they are -- upcoming, ongoing or finished. local today = mw.getContentLanguage:formatDate('Y-m-d')

local tournaments = {} tournaments[1] = query('start_date > "' .. today .. '"') tournaments[2] = query('start_date <= "' .. today .. '" AND end_date >= "' .. today .. '"', TOTAL - #tournaments[1]) tournaments[3] = query('end_date < "' .. today .. '"', TOTAL - #tournaments[1] - #tournaments[2])

return tournaments end

local function make_header(state_number) --- Create a table heading. -- @param state_number number: The state's number. -- @return string: The table header. local state_name = STATES[state_number] local header = '\n| colspan="3" style="text-align:center; border-bottom:1px solid #CCCCCC; color:#660000;" | ' .. state_name .. ' ' return header end

local function format_entry_date(date, sign) --- Format a date for display in a tournament entry. -- @param date string: The date to format. -- @param sign string: A sign to add before the date (usually '&gt;' and '&lt;'). -- @return string: The formatted date. date = mw.getContentLanguage:formatDate('M d', date) date = string.format("%s %s ", sign, date) return date end

local function make_entry(tournament) --- Create a table entry for a tournament. -- @param tournament table: The tournament. -- @return string: The table row. local color = TIERS[tournament.tier].color local tier = tostring(TIERS[tournament.tier].tooltip) local start_date = format_entry_date(tournament.start_date, '&gt;') local end_date = format_entry_date(tournament.end_date, '&lt;')

local entry = string.format('style="font-size:9pt; background-color:%s"\n|align="center" width="25px" height="21px"| %s \n|18px|link= %s \n|align=center| %s %s ',                              color, tier, tournament.icon, tournament._pageName, tournament._pageName, start_date, end_date)

return entry end

function p.main local rows = {} local entries = get_tournaments

for i,tournaments in ipairs(entries) do   if tournaments[1] then table.insert(rows, make_header(i)) for _, tournament in ipairs(tournaments) do       -- Add default values where necessary if tournament.tier == '' then tournament.tier = 'Premium' end if tournament.icon == '' then tournament.icon = 'File:Tournament icon Default.png' end table.insert(rows, make_entry(tournament)) end end end rows = table.concat(rows, '\n|-') return string.format(' \n{| style="width: 100%%;"%s\n|}\n ', rows) end

return p