Hello there! We are conducting a survey to better understand the user experience in making a first edit. If you have ever made an edit on Gamepedia, please fill out the survey. Thank you!

Module:TeamResults

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search
To edit the documentation or categories for this module, click here. This module has an i18n file. Click here to edit it.
local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_esports = require('Module:EsportsUtil')
local util_html = require('Module:HTMLUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local util_toggle = require('Module:ToggleUtil')
local util_tournament = require('Module:TournamentUtil')
local util_vars = require('Module:VarsUtil')
local i18n = require('Module:i18nUtil')
local Reload = require('Module:Reload')

local sep = '%s*;;%s*'
local sep2 = '%s*,%s*'

local m_team = require('Module:Team')
local Sprite = require('Module:Sprite').sprite
local lang = mw.getLanguage('en')
local placement = require('Module:Placement')
local PopupButton = require('Module:PopupButton')

local TOGGLE_DATA = {
	order = { 'Local', 'USD', 'Euros' },
	sep = ' • ',
	section = 'teamres-togglers-currency',
	all = 'teamres-currency-all',
}

local COLUMNS = { 'Date', 'Place', 'Prize', 'Event', 'LastResult', 'Roster' }

local CURRENCIES = { 'Local', 'USD', 'Euros' }

local h = {}
local s = {}
local p = {}

function p.main(frame)
	local args = util_args.merge(true)
	i18n.initGlobalFromFile('TeamResults')
	
	local team = args[1] or mw.title.getCurrentTitle().baseText
	local teamlink = m_team.teamlinkname(team)
	
	local result = h.doQuery(teamlink, args.show, args)	
	
	return h.introSentence(args.show), h.reload(args), h.makeOutput(args, team, result)
end

function s.RoleSprite(id)
	return Sprite{
		id,
		size = '15',
		type = 'Role',
		notext = 'Yes',
		nolink = 'Yes',
	}
end

function s.LeagueSprite(id)
	return Sprite{
		id,
		type = 'League'
	}
end

function h.doQuery(team, show, args)
	-- use_redirect_table will be false in self namespace
	local tbl = {
		tables = h.getTables(),
		join = h.getJoin(),
		where = h.getWhere(args, team),
		fields = h.getFields(),
		groupBy = 'TournamentResults.UniqueLine',
		orderBy = 'TournamentResults.Date DESC',
		limit = h.getLimit(show)
	}
	return util_cargo.queryAndCast(tbl)
end

function h.getTables()
	local tbl = {
		"TournamentRosters", 
		"TournamentResults", 
		"Tournaments"
	}
	return table.concat(tbl, ', ')
end

function h.getJoin()
	local tbl = {
		"TournamentResults.PageAndTeam = TournamentRosters.PageAndTeam",
		"TournamentResults._pageName=Tournaments._pageName"
	}
	return table.concat(tbl, ', ')
end

function h.getWhere(args, team)
	local tbl = {
		('TournamentResults.Team="%s"'):format(team),
		args.minplacement and ('Place_Number <= "%s"'):format(args.minplacement),
	}
	return util_cargo.concatWhere(tbl)
end

function h.getFields()
	return {
		"TournamentResults.Team=Team",
		"TournamentResults.Prize_Markup=PrizeMarkup",
		"TournamentResults.PrizeOther=PrizeOther",
		"TournamentResults.Prize=Prize",
		"TournamentResults.PrizeUnit=PrizeUnit",
		"TournamentResults.Prize_USD=Prize_USD",
		"TournamentResults.Prize_Euro=Prize_Euro",
		"TournamentResults.Date=Date",
		"TournamentResults.Place=Place",
		"TournamentResults.Event=Event",
		"TournamentResults.Phase=Phase",
		"CONCAT(TournamentResults._pageName)=EventLink",
		"TournamentRosters.RosterLinks__full=RosterLinks",
		"TournamentRosters.Roster=RosterNames",
		"TournamentRosters.Roles=Roles",
		"TournamentResults.LastResult=LastResult",
		"TournamentResults.LastOpponent_Markup=LastOpponent",
		"Tournaments.League=League",
	}
end

function h.getLimit(show)
	if show == 'overviewpage' then
		return 10
	elseif show == 'everything' then
		return 500
	end
	return nil
end

function h.addLastResult(tr, row, team)
	local td = tr:tag('td'):addClass('achievements-lastresult-cell')
	local div = td:tag('div'):addClass('achievements-lastresult-outer')
	div:tag('span'):addClass('achievements-lastresult-inner'):wikitext(row.LastResult)
	div:wikitext(row.LastOpponent)
	local div_right = div:tag('div'):addClass('achievements-lastresult-right')
	PopupButton.tth(div_right, row.EventLink, row.Event, team)
end

function h.makeHeaderText(team)
	local text = mw.html.create()
	text:wikitext(i18n.print('tResults',m_team.teamname(team)))
		:wikitext(' (')
	util_toggle.printOptionFromListTogglers(text, TOGGLE_DATA)
	text:wikitext(')')
	return tostring(text)
end

function h.currencyDisplay(tr, row)
	local currencydisplay = { Local = row.PrizeMarkup }
	if row.Prize then
		row.PrizeMarkup = row.PrizeMarkup or row.Prize
		if row.Prize_USD and row.Prize_USD ~= '' then
			currencydisplay.USD = '$ ' .. lang:formatNum(tonumber(row.Prize_USD))
		else
			currencydisplay.USD = '<span style="font-style:italic;">' .. row.PrizeMarkup .. '</span>'
		end
		if row.Prize_Euro and row.Prize_Euro ~= '' then
			currencydisplay.Euros = '€ ' .. lang:formatNum(tonumber(row.Prize_Euro))
		else
			currencydisplay.Euros = '<span style="font-style:italic;">' .. row.PrizeMarkup .. '</span>'
		end
	end
	local td = tr:tag('td')
		:addClass('achievements-prize-cell')
	for _, v in ipairs(CURRENCIES) do
		h.printOneCurrency(currencydisplay, td, v)
	end
end

function h.printOneCurrency(displays, td, this)
	local span = td:tag('span')
	span:wikitext(displays[this])
	util_toggle.oflCellClasses(span, TOGGLE_DATA, this)
end

function h.introSentence(show)
	if show == 'overviewpage' and not Reload.isReload() then
		return "''" .. i18n.print('seeall', mw.title.getCurrentTitle().rootText) .. "''"
	end
	return ''
end

function h.reload(args)
	return Reload.intro('FilterTournamentResults', 'TeamResults', args)
end

function h.makeOutput(args, team, result)
	local output = Reload.wrapper()
	local tbl = output:tag('div'):tag('table')
	tbl:addClass("wikitable sortable hoverable-rows"):css({ ["font-size"] = "90%" })
	util_html.makeColspanHeader(tbl, h.makeHeaderText(team), #COLUMNS)
	util_html.headerFromI18n(tbl, COLUMNS)
		
	local totalprizeUSD = 0
	local totalprizeEuro = 0
	
	for _,row in ipairs(result) do
		local tr = tbl:tag("tr")
		tr:tag("td"):addClass('achievements-date'):wikitext(row["Date"]):done()
			:node(placement.makeNode(row.Place):addClass('achievements-place'))
		h.currencyDisplay(tr, row)
		tdEvent = tr:tag("td")
			:wikitext(s.LeagueSprite(row.League))
			:wikitext(' ', util_text.intLink(row.EventLink, row.Event))
		if row.Phase then
			tdEvent:wikitext(" - " .. row["Phase"])
		end 
		h.addLastResult(tr, row, team)
		
		tr:tag("td"):wikitext(h.getTeammates(row.RosterLinks, row.Roles))
	end
	
	h.printTotalEarnings(tbl, result)
	return output
end

function h.getTeammates(linksstr, rolesstr)
	local teammates = {}
	local links = util_text.split(linksstr, sep)
	local roles =rolesstr and util_text.split(rolesstr, sep) or ''
	for i, link in ipairs(links) do
		local thisrole = util_args.splitMapConcat(roles[i] or '', nil, s.RoleSprite, nil)
		teammates[#teammates+1] = ("%s&nbsp;%s"):format(thisrole, util_esports.playerLinked(link))
	end
	return table.concat(teammates, ", ")
end

function h.printTotalEarnings(tbl, result)
	local td = tbl:tag("tr")
		:tag("th"):attr("colspan",#COLUMNS)
		:wikitext(i18n.print('totalPrize'))
		:wikitext(h.getTotalEarnings(result))
end

function h.getTotalEarnings(result)
	local earnings = {}
	for _, row in ipairs(result) do
		if row.PrizeUnit then
			earnings[row.PrizeUnit] = (earnings[row.PrizeUnit] or 0) + (tonumber(row.Prize,10) or 0)
		end
	end
	local earningstable = {}
	
	for currency, amount in pairs(earnings) do
		earningstable[#earningstable+1] = currency .. " " .. lang:formatNum(amount)
	end
	return util_table.concat(earningstable," &#8226; ")
end

return p