We're currently performing an overhaul of our entire tournament results system so results will be missing from team and player pages for a while, and there may be some errors on tournament pages. Please bear with us as we complete this update.

Module:TeamClass

From Call of Duty Esports Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:TeamClass/doc

local util_args = require("Module:ArgsUtil")
local util_table = require("Module:TableUtil")
local util_vars = require("Module:VarsUtil")
local cache = require('mw.ext.LuaCache')
local lang = mw.getLanguage('en')

local p = require('Module:FileEntityAbstract'):finalExtends()
local h = {}

p.objectType = 'Team'
p.cssClass = 'team-object'
p.defaultlength = 'link'
p.imagelength = 'file'
p.filePattern = '%slogo std.png'
p.squarePattern = '%slogo square.png'
p.imagesizes = { long = 60, medium = 48, short = 36, default = 60 }
p.luaCachePrefix = 'teamnames_08_'

function p:init(str, opts)
	if not str then
		self.is_nil = true
		return
	end
	if not opts then opts = {} end
	local vars = self:getFromVariableOrCache(str:gsub('_', ' '))
	self.vars = util_table.shallowClone(vars)
	if opts.suffix then
		self.linkTarget = opts.link or (self.vars.link .. '/' .. opts.suffix)
	else
		self.linkTarget = opts.link or self.vars.link
	end
	if opts.short then
		self.vars.short = opts.short
	end
	self.linked = not self.vars.exception
	
end

function p:getFromVariableOrCache(str)
	local key = self.luaCachePrefix .. lang:lc(str)
	return util_vars.getObject(key) or h.getFromCacheOrTeamnames(str, key)
end

function h.getFromCacheOrTeamnames(str, key)
	local vars = cache.get(key) or h.getFromTeamnames(str, key)
	if vars.exists == nil then
		vars.exists = true
	end
	util_vars.setObject(key, vars)
	return vars
end

function h.getFromTeamnames(str, key)
	local vars_init = util_args.lookupVars(str, mw.loadData('Module:Teamnames'), true)
	local vars
	if vars_init then
		vars = util_table.shallowClone(vars_init)
		vars.file = vars.link or ''
		vars.exception = util_args.nilToFalse(vars.exception)
		vars.exists = true
	else
		vars = {
			link = str,
			long = str,
			medium = str,
			short = mw.ustring.upper(mw.ustring.sub(str:gsub('Team ',''), 1, 4)),
			file = '',
			exception = false,
			exists = false,
		}
	end
	cache.set(key, vars)
	return vars
end

function p:square(opts)
	if self.is_nil then return end
	return self:_file(opts, nil, self.squarePattern)
end

function p:_file(opts, link, file)
	return ('[[File:%s|link=%s|%spx%s]]'):format(
		file or self.filePattern:format(self:get(self.imagelength)),
		link,
		opts.size or self.imagesizes[opts.len or 'default'] or self.imagesizes.default,
		self:getImageClass()
	)
end

function p:getImageClass()
	local imageClass = self:get('class')
	if not imageClass then return '' end
	return ('|class=%s-logo-std'):format(imageClass)
end

return p