Module:Team

local function getGlobalDate local globalDate = mw.ext.VariablesLua.var('date') if not(globalDate and globalDate ~= '') then globalDate = mw.ext.VariablesLua.var('tournament_edate') if not(globalDate and globalDate ~= '') then globalDate = mw.ext.VariablesLua.var('tournament_sdate') if not(globalDate and globalDate ~= '') then globalDate = os.date( '%F' ) end end end return globalDate end

local function getNullMessage(name) mw.log('Missing team: ' .. name) return ' No team template exists for name "' .. name .. '". ' .. '' end

local custom = { ['team'] = function (name, std) return '  ' .. name .. '  ' end, ['team2'] = function (name, std) return ' ' .. name .. '   ' end, ['teamshort'] = function (name, std) return '  ' .. name .. '  ' end, ['team2short'] = function (name, std) return ' ' .. name .. '   ' end, ['teambracket'] = function (name, std) return '  ' .. name .. ' ' end, ['teamicon'] = function (name, std) return '   ' end, ['teampart'] = function (name, std) return '   ' end, ['iconfile'] = function (_, std) return std end, ['teampage'] = function (name, _) return name end }

local function getOverride(form, name, data) if name == nil then return nil end local overrides = mw.loadData('Module:Team/override') if overrides.templates[name:lower] then return overrides.templates[name:lower][form:lower] elseif overrides.game[name:lower] and data then return custom[form:lower](data, overrides.games[mw.ext.VariablesLua.var('tournament_game', ''):lower]) elseif overrides.games[name:lower] and data then return custom[form:lower](data, overrides.games[name:lower]) elseif name:lower == 'noteam' then return overrides.templates[''][form:lower] else return nil end end

local function getTemplate(form, name, date, skipOverride) if not(skipOverride) then local override = getOverride(form, name, date) if override then return override end end if mw.ext.TeamTemplate.teamexists(name) then return mw.ext.TeamTemplate[form](name, date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(mw.text.trim(name)) then return mw.ext.TeamTemplate[form](mw.text.trim(name), date or getGlobalDate) .. ''	elseif mw.ext.TeamTemplate.teamexists(name:gsub("_", " ")) then return mw.ext.TeamTemplate[form](name:gsub("_", " "), date or getGlobalDate) .. ''	elseif mw.ext.TeamTemplate.teamexists(name:gsub(" ", "_")) then return mw.ext.TeamTemplate[form](name:gsub(" ", "_"), date or getGlobalDate) .. ''	else return nil end end

local p = { ['team'] = function (_, name, date) return getTemplate('team', name, date) or getNullMessage(name) end,

['team2'] = function (_, name, date) return getTemplate('team2', name, date) or getNullMessage(name) end,

['short'] = function (_, name, date) return getTemplate('teamshort', name, date) or (' ' .. name .. ' (missing) ' .. '') end,

['short2'] = function (_, name, date) return getTemplate('team2short', name, date) or (' (missing) ' .. name .. ' ' .. '') end,

['bracket'] = function (_, name, date, skipOverride) return getTemplate('teambracket', name, date, skipOverride) or (' Missing: ' .. name .. ' ' .. '') end,

['bracketShort'] = function (_, name, date, skipOverride) if not(skipOverride) then local override = getOverride('teambracket', name, date) if override then return override end end local output if mw.ext.TeamTemplate.teamexists(name) then output = mw.ext.TeamTemplate.raw(name, date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(mw.text.trim(name)) then output = mw.ext.TeamTemplate.raw(mw.text.trim(name), date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(name:gsub("_", " ")) then output = mw.ext.TeamTemplate.raw(name:gsub("_", " "), date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(name:gsub(" ", "_")) then output = mw.ext.TeamTemplate.raw(name:gsub(" ", "_"), date or getGlobalDate) else mw.log('Missing team: ' .. name) return ' Missing: ' .. name .. ' ' .. ''		end if output.image ~= '' then return '  ' .. output.shortname .. ' '		else return '  ' .. output.shortname .. ' '		end end,

['icon'] = function (_, name, date) return getTemplate('teamicon', name, date) or getNullMessage(name) end,

['iconFile'] = function (_, name, date) local output = getOverride('iconfile', name, date) if output then return output elseif mw.ext.TeamTemplate.teamexists(name) then output = mw.ext.TeamTemplate.raw(name, date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(mw.text.trim(name)) then output = mw.ext.TeamTemplate.raw(mw.text.trim(name), date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(name:gsub("_", " ")) then output = mw.ext.TeamTemplate.raw(name:gsub("_", " "), date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(name:gsub(" ", "_")) then output = mw.ext.TeamTemplate.raw(name:gsub(" ", "_"), date or getGlobalDate) else mw.log('Missing team: ' .. name .. ' (icon)') return mw.loadData('Module:Team/override').games[''] end return output.image ~= '' and output.image or output.legacyimage end,

['imageFile'] = function (_, name, date) local output if mw.ext.TeamTemplate.teamexists(name) then output = mw.ext.TeamTemplate.raw(name, date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(mw.text.trim(name)) then output = mw.ext.TeamTemplate.raw(mw.text.trim(name), date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(name:gsub("_", " ")) then output = mw.ext.TeamTemplate.raw(name:gsub("_", " "), date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(name:gsub(" ", "_")) then output = mw.ext.TeamTemplate.raw(name:gsub(" ", "_"), date or getGlobalDate) else mw.log('Missing team: ' .. name .. ' (icon)') return nil end return output.image ~= '' and output.image or nil end,

['part'] = function (_, name, date) return getTemplate('teampart', name, date) or getNullMessage(name) end,

['page'] = function (_, name, date) local override = getOverride('teampage', name, date) if override then return override elseif mw.ext.TeamTemplate.teamexists(name) then return mw.ext.TeamTemplate.teampage(name, date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(mw.text.trim(name)) then return mw.ext.TeamTemplate.teampage(mw.text.trim(name), date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(name:gsub("_", " ")) then return mw.ext.TeamTemplate.teampage(name:gsub("_", " "), date or getGlobalDate) elseif mw.ext.TeamTemplate.teamexists(name:gsub(" ", "_")) then return mw.ext.TeamTemplate.teampage(name:gsub(" ", "_"), date or getGlobalDate) else return name end end,

['override'] = getOverride, }

function p.get(frame) local args = {} for k, v in pairs(frame.args) do		if v ~= '' then args[k] = mw.text.trim(v) end end if args[1] then return p[args[1]](frame, args[2] or '', args[3]) else return ' Invalid team template form specified. ' .. ''	end end

function p.queryDB(type, name, date) if mw.ext.TeamTemplate.teamexists(name) then return mw.ext.TeamTemplate[type](name, date) else return nil end end

function p.queryRaw(name, date) if mw.ext.TeamTemplate.teamexists(name) then return mw.ext.TeamTemplate.raw(name, date) else return nil end end

function p.queryHistorical(name) return mw.ext.TeamTemplate.raw_historical(name) end

function p.queryHistoricalNames(name) if mw.ext.TeamTemplate.teamexists(name) then local index = mw.ext.TeamTemplate.raw_historical(name) if index then local outputKeyed, output = {}, {} for _, template in pairs(index) do				outputKeyed[mw.ext.TeamTemplate.teampage(template)] = true end for template, _ in pairs(outputKeyed) do				table.insert(output, template) end return output else return {mw.ext.TeamTemplate.teampage(name)} end else return nil end end

return p