Module:EsportsUtil

local util_args = require('Module:ArgsUtil') local util_map = require('Module:MapUtil') 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_team = require('Module:Team')

local p = {} local h = {}

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) 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.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_map.splitAndConcat(str, ' ', p.standingsClass, ' ') end

function p.playerMarkup(settings) local tbl = { settings.country and settings.country:image, 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.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, ' ') end

function p.calendarIntLink(link) return util_text.intLink(link, ' ') end

function p.calendarExtLink(link) return util_text.extLink(link, ' ') end

function p.playerDisplay(str) -- don't return second values local ret = str:gsub('_', ' '):gsub('%s*%(.*%)','') return ret end

function p.playersDisplay(str, sep) return util_map.splitAndConcat(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(link, name) if not link then link = name end if not link then return nil end if link == 'N/A' then return link end return util_text.intLink(p.escapeLink(link), p.playerDisplay(name or link)) end

function p.playerLinkedOrBold(link, thisName) if not link then return nil end if link:lower == thisName:lower then return ("%s"):format(p.playerDisplay(link)) end return p.playerLinked(link) end

function p.playerUnlinked(str) -- if we want a person specifically NOT linked because they are support staff -- then print the name in a span with a class indicating it's a name if not str then return nil end local output = mw.html.create('span') :addClass('player-name') :wikitext(p.playerDisplay(str)) return tostring(output) end

function p.playerMaybeUnlinked(str, isUnlinked) if isUnlinked then return p.playerUnlinked(str) end return p.playerLinked(str) end

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

function p.playerWithRole(tbl) if not tbl then error('Nil player table sent to EsportsUtil.playerWithRole') end if not tbl.player then return nil end if not tbl.role then return p.playerMaybeUnlinked(tbl.player, tbl.unlinked) end -- role here is assumed to be a RoleList local rolesAndPlayer = { tbl.role:ingame:images({ size=14 }) or '', p.playerMaybeUnlinked(tbl.player, tbl.unlinked), h.staffRoles(tbl.role:staff), }	return util_table.concat(rolesAndPlayer, '') end

function h.staffRoles(staffRole) if not staffRole:exists then return nil end return (' (%s)'):format(staffRole:get('name')) end

function p.boldLinksToThisPlayer(str, player, page) page = page or mw.title.getCurrentTitle.text str = str :gsub('%[%[' .. util_text.escape(player) .. '%|', '[[' .. page .. '|')		:gsub('%[%[' .. util_text.escape(player) .. '%]%]', util_text.intLink(page, player))	return str end

return p