Module:EsportsUtil

local util_args = require('Module:ArgsUtil') local util_math = require('Module:MathUtil') 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 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'):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, d, a) 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 (' %s'):format(			deserialized[1] or '',			table.concat(deserialized,'-')		) else return table.concat(deserialized,'-') end end

function p.teamAndPlayer(team, link, player) return m_team.onlyimagelinkedshort(team) .. (util_text.intLink(link, player) or 'TBD') 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) return str:gsub('%s*%(.*%)','') end

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

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

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

function p.playerLinked(str) if not str then return nil end return util_text.intLink(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