Module:Team ID

From Dota 2 Wiki
Jump to: navigation, search

Documentation for Module:Team ID Jump to code ↴ [ edit | purge ]

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


Dependencies

local p = {}
local cargo = mw.ext.cargo
local caching = require('Module:Cache').auto
local fileExists = require('Module:FileExists')
local getArgs = require('Module:Arguments').getArgs
local tooltip = require('Module:Tooltip')._main
local yesno = require('Module:Yesno')

local specials = {
  ["tbd"] = { "TBD", "To be decided" },
  ["tba"] = { "TBD", "To be announced" },
  ["grp. s."] = { "Grp. S.", "Group Stage (W/D/L)" },
  ["grp.s."] = { "Grp. S.", "Group Stage (W/D/L)" },
  ["grp"] = { "Grp. S.", "Group Stage (W/D/L)" },
  ["league"] = { "League Play", "League Play (W/D/L)" },
  ["league play"] = { "League Play", "League Play (W/D/L)" },
}


function p.main(frame)
  local args = getArgs(frame, {
    wrappers = {
      'Template:Team ID'
    }
  })
  return caching(p._main, args, 'teamid')
end

function p._main(args)
  assert(args and args[1], 'No team name entered')
  -- Check if a special rule exists for this input.
  local special = specials[string.lower(args[1])]
  if special then
    local values = special
    return tostring(tooltip({ values[1], values[2] }))
  else
    local icon = 'Team icon Default.png'
    local name = args[1]
    
    -- Attempt to get the data through Cargo.
    -- _pageName matches are prioritized over matches in the names table.
    -- If multiple teams have the same name in their names table the first one is used.
    local cargo_info = cargo.query('professional_teams', 'icon, name', { where='_pageName="' .. args[1] .. '"', groupBy='_pageID' })[1]
    if not cargo_info then
      cargo_info = cargo.query('professional_teams', 'icon, name', { where='names HOLDS "' .. args[1] .. '"', groupBy='_pageID' })[1]
    end
    
    if cargo_info then
      if cargo_info['icon'] ~= '' or nil then icon = cargo_info['icon'] end
      if cargo_info['name'] ~= '' or nil then name = cargo_info['name'] end
    else
      if fileExists('File:Team icon ' .. args[1] .. '.png') then
        icon = 'Team icon ' .. args[1] .. '.png'
      end
    end

    local image = string.format('[[File:%s|%s|link=%s|class=noprint|%s]]', icon, (args[2] or '20x20px'), args[1], name)
    local link = string.format('[[%s|%s]]', args[1], name)

    if yesno(args.reverse) then
      return string.format('<span class="image-link">%s %s</span>', link, image)
    else
      return string.format('<span class="image-link">%s %s</span>', image, link)
    end
  end
end


return p