Module:EsportsUtil

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.


local util_args = require('Module:ArgsUtil')
local util_math = require('Module:MathUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local util_title = require('Module:TitleUtil')
local util_vars = require('Module:VarsUtil')
local m_country = require('Module:Country')
local m_team = require('Module:Team')

local defaultsep = '%s*,%s*'

local p = {}

p.outcomes = {
	w = 'Win',
	win = 'Win',
	l = 'Loss',
	loss = 'Loss',
	lost = 'Loss'
}

function p.getOutcomeName(str)
	if not str then return nil end
	if not p.outcomes[str:lower()] then
		error('Invalid outcome of ' .. str)
	end
	return p.outcomes[str:lower()]
end

function p.getOverviewPage(str)
	if str then
		return util_title.target(str:gsub('_',' '))
	else
		local var = util_vars.getVar('overviewpage')
		return var or mw.title.getCurrentTitle().text
	end
end

function p.otherTeamN(n)
	-- teams are Team1 and Team2
	-- returns 2 if 1 is input, returns 1 if 2 is input
	local num = tonumber(n)
	return 3 - num
end

function p.makeLinks(links, display)
	if not links then links = display end
	local tbl = {}
	for k, v in ipairs(display) do
		if not links[k] or links[k] == '' then
			tbl[k] = ('[[%s]]'):format(v)
		else
			tbl[k] = ('[[%s|%s]]'):format(links[k], v)
		end
	end
	return tbl
end

function p.concatLinks(links, display, sep)
	if not links then links = display end
	return table.concat(p.makeLinks(links, display), sep or ',')
end

function p.concatLinksFromStrings(links, display, sep1, sep2)
	if not links and not display then
		return nil
	end
	if not links then links = display end
	local links_tbl = util_text.split(links, sep1 or defaultsep)
	local display_tbl = util_text.split(display, sep1 or defaultsep)
	return p.concatLinks(links_tbl, display_tbl, sep2 or ', ')
end

function p.ucfirstLink(link, display)
	if not link and not display then return nil end
	local lang = mw.getLanguage('en')
	link = link or display
	return lang:ucfirst(link)
end

function p.ucfirstLinks(links, displays, sep)
	if not links and not displays then return nil end
	local lang = mw.getLanguage('en')
	links = links or displays
	sep = sep or defaultsep
	local tbl = util_text.split(links, sep)
	for k, v in ipairs(tbl) do
		tbl[k] = lang:ucfirst(v)
	end
	return table.concat(tbl, ',')
end

function p.winrate(w, l, round)
	if w + l == 0 then
		return 0
	end
	if round then
		return util_math.roundnum(w / (w + l) * 100, round)
	else
		return 100 * w / (w + l)
	end
end	

function p.winrateRanked(w, l, round)
	return (w - l) * 100 + (w + l)
end

function p.addTeamHighlighter(tbl, team)
	if team == 'TBD' then return end
	tbl:addClass('teamhighlight')
		:addClass('teamhighlighter')
		:attr('data-teamhighlight',team)
end

function p.calculateKDA(k, d, a, round)
	k = tonumber(k)
	d = tonumber(d)
	a = tonumber(a)
	if not (k and d and a) then
		error('Attempting to calculate KDA, missing value')
	end
	if d == 0 then
		return k + a
	end
	return util_math.roundnum((k + a) / d, round or .01)
end

function p.KDA(k, d, a)
	return ('%s/%s/%s'):format(k or '', d or '', a or '')
end

function p.roundedGold(gold)
	if not gold then return nil end
	gold = tonumber(gold)
	local output = util_math.roundnum(gold / 1000, .1)
	return ('%sk'):format(output)
end

function p.standingsClass(str)
	return 'standings-' .. str
end

function p.standingsClasses(str)
	return util_args.splitMapConcat(str, ' ', p.standingsClass, ' ')
end

function p.placementIcon(str)
	local deserialized = util_math.deserializeRangeToTable(str)
	if not deserialized[1] then
		return str
	end
	if deserialized[1] < 4 then
		return ('[[File:PlacementIcon%s.png|25px|link=]] %s'):format(
			deserialized[1] or '',
			table.concat(deserialized,'-')
		)
	else
		return table.concat(deserialized,'-')
	end
end

function p.playerMarkup(settings)
	local tbl = {
		settings.flag and m_country.onlyimage(settings.flag),
		settings.team and m_team.onlyimagelinkedshort(settings.team),
		settings.player and p.playerLinked(settings.player) or 'TBD',
	}
	util_table.removeFalseEntries(tbl, 3)
	return util_table.concat(tbl, '')
end
	
function p.flagPlayerMandatory(settings)
	local tbl = {
		m_country.onlyimage(settings.flag),
		settings.team and m_team.onlyimagelinkedshort(settings.team),
		settings.player and p.playerLinked(settings.player) or 'TBD',
	}
	util_table.removeFalseEntries(tbl, 3)
	return util_table.concat(tbl, '')
end

function p.teamPlayerMandatory(settings)
	local tbl = {
		settings.flag and m_country.onlyimage(settings.flag),
		m_team.onlyimagelinkedshort(settings.team),
		settings.player and p.playerLinked(settings.player) or 'TBD',
	}
	util_table.removeFalseEntries(tbl, 3)
	return util_table.concat(tbl, '')
end

function p.teamFlagPlayerMandatory(settings)
	local tbl = {
		m_country.onlyimage(settings.flag),
		m_team.onlyimagelinked(settings.team),
		settings.player and p.playerLinked(settings.player) or 'TBD',
	}
	return util_table.concat(tbl, '')
end

function p.printDiff(td, diff)
	if diff == 0 then
		td:tag('span'):addClass('stat-diff diff-neutral'):wikitext('▬')
	elseif diff > 0 then
		td:tag('span'):addClass('stat-diff diff-up'):wikitext('▲')
		td:wikitext(diff)
	else
		td:tag('span'):addClass('stat-diff diff-down'):wikitext('▼')
		td:wikitext(diff * -1)
	end
end

function p.diff(diff)
	local td = mw.html.create('span')
	p.printDiff(td, diff)
	return tostring(td)
end

function p.calendarLink(link)
	return util_text.link(link, '<span class="calendar-icon"></span>')
end

function p.calendarIntLink(link)
	return util_text.intLink(link, '<span class="calendar-icon"></span>')
end

function p.calendarExtLink(link)
	return util_text.extLink(link, '<span class="calendar-icon"></span>')
end

function p.playerDisplay(str)
	return str:gsub('%s*%(.*%)','')
end

function p.playersDisplay(str, sep)
	return util_args.splitMapConcat(str, sep, p.playerDisplay)
end

function p.escapeLink(str)
	return str
		:gsub('^_+','')
		:gsub('_+$','')
		:gsub('_+', ' ')
		:gsub(' +', ' ')
end

function p.playersLink(str, sep)
	if not str then return nil end
	return util_text.ucfirstMap(p.escapeLink(str), sep)
end

function p.playerLink(str)
	if not str then return nil end
	return util_text.ucfirst(p.escapeLink(str))
end

function p.playerLinked(str)
	if not str then return nil end
	return util_text.intLink(p.escapeLink(str), p.playerDisplay(str))
end

function p.playersLinked(str, sep)
	if not str then return nil end
	return util_args.splitMapConcat(str, sep, p.playerLinked, ', ')
end

function p.placeNumber(str)
	if str == 'DNS' then
		return 9999
	end
	return util_math.deserialize(str)
end

return p