Module:TeamResults

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_cargo = require('Module:CargoUtil')
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 sep = '%s*;;%s*'
local sep2 = '%s*,%s*'

local m_team = require('Module:Team')
local m_league = require('Module:League')
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', 'Pl', 'Prize', 'Event', 'Last Result', 'Roster' }

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

local h = {}

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

function h.doQuery(team, show)
	-- use_redirect_table will be false in self namespace
	local tbl = {
		tables = h.getTables(),
		join = h.getJoin(),
		where = h.getWhere(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", 
		"InfoboxTournament"
	}
	return table.concat(tbl, ', ')
end

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

function h.getWhere(team)
	local tbl = {
		('TournamentResults.TeamLink="%s"'):format(team)
	}
	return util_table.concat(tbl)
end

function h.getFields()
	return {
		"TournamentResults.TeamLink=TeamLink",
		"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",
		"InfoboxTournament.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(m_team.teamname(team))
		:wikitext(' Tournament Results - Show Prize as (')
	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
		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 ~= '' 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

local p = {}

function p.teamResults( frame )
	local args = util_args.merge(true)
	
	local team = args[1] or mw.title.getCurrentTitle().baseText
	local teamlink = m_team.teamlinkname(team)
	local earnings = {}
	
	local result = h.doQuery(teamlink, args.show)
	
	local output = mw.html.create('div')
	local tbl = output:tag('table')
	tbl:addClass("wikitable sortable"):css({ ["font-size"] = "90%" })
	util_html.makeColspanHeader(tbl, h.makeHeaderText(team), #COLUMNS)
	util_html.makeHeader(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))
		h.currencyDisplay(tr, row)
		tdEvent = tr:tag("td")
			:wikitext(m_league.onlyimage(row.League))
			:wikitext(' ', util_text.intLink(row.EventLink, row.Event))
		if row.Phase then
			tdEvent:wikitext(" - " .. row["Phase"])
		end 
		h.addLastResult(tr, row, team)
		
		local teammates = {}
		local teammatenames = mw.text.split(row.RosterNames or '',";;")
		local teammatelinks = mw.text.split(row.RosterLinks or '',";;")
		local roles = mw.text.split(row.Roles or '',"%s*;;%s*")
		
		if teammatelinks[1] ~= "" then
			for index,teammatelink in ipairs(teammatelinks) do
				thisroles = {}
				for i, role in ipairs(mw.text.split(roles[index] or '','%s*,%s*')) do
					thisroles[i] = h.RoleSprite(role)
				end
				thisrole = table.concat(thisroles,"")
				teammates[#teammates+1] = thisrole .. "&nbsp;[[" .. teammatelink .. "|" .. teammatenames[index] .. "]]"
			end
		end
		tr:tag("td"):wikitext(table.concat(teammates, ", "))
		if row.Prize and row.PrizeUnit then
			earnings[row.PrizeUnit] = (earnings[row.PrizeUnit] or 0) + tonumber(row.Prize,10)
		end
		tr:done()
	end
	
	td = tbl:tag("tr")
		:tag("th"):attr("colspan",#COLUMNS):wikitext("Total Prize: ")
	
	local earningstable = {}
	
	for currency, amount in pairs(earnings) do
		earningstable[#earningstable+1] = currency .. " " .. lang:formatNum(amount)
	end
	
	td:wikitext(table.concat(earningstable," &#8226; ")):done()
	
	return p.intro(args.show), output
end

function p.intro(show)
	if show == 'overviewpage' then
		return string.format(":''This table shows up to the 10 most recent results. For complete results, [[%s/Tournament Results|click here]].''",mw.title.getCurrentTitle().rootText)
	end
	return ''
end

return p