Module:Latest tournaments

From Dota 2 Wiki
Jump to: navigation, search

Documentation for Module:Latest tournaments Jump to code ↴ [ edit | purge ]

Reality Rift icon.png
▶️ Planeshift.
The documentation for this module can be found at Template:Latest tournaments.
You may be forwarded to another wiki language, in case a translation is not available.


Dependencies

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 tournaments = {}
  tournaments[1] = query('DATEDIFF(NOW(), start_date)<0')
  tournaments[2] = query('DATEDIFF(NOW(), start_date)>0 AND DATEDIFF(NOW(), end_date)<0', TOTAL - #tournaments[1])
  tournaments[3] = query('DATEDIFF(NOW(), end_date)>0', 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;" | <span style="font-weight:bold">' .. state_name .. '</span>'
  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("'''<span style=\"float: left;\">%s</span>&nbsp;%s&nbsp;'''", 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"|<span style="font-weight: bold;">%s</span>\n|[[%s|18px|link=]] [[%s|<span style="color:#313131; font-weight:bold;">%s</span>]]\n|align=center|<small>%s<br>%s</small>',
                              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('<div style="margin:3px 0;">\n{| style="width: 100%%;"%s\n|}\n</div>', rows)
end


return p