Module:TeamResults

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 util_vars = require('Module:VarsUtil') local i18n = require('Module:i18nUtil')

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 = ' &#8226; ', 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 = {} 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) -- 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", "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(team) local tbl = { ('TournamentResults.Team="%s"'):format(team) }	return util_table.concat(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 = ' ' .. row.PrizeMarkup .. ' '		end if row.Prize_Euro and row.Prize_Euro ~= '' then currencydisplay.Euros = '€ ' .. lang:formatNum(tonumber(row.Prize_Euro)) else currencydisplay.Euros = ' ' .. row.PrizeMarkup .. ' '		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) i18n.initGlobalFromFile('TeamResults') 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 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)) 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) 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] = s.RoleSprite(role) end thisrole = table.concat(thisroles,"") teammates[#teammates+1] = thisrole .. " " .. 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(i18n.print('totalPrize')) 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 "''" .. i18n.print('seeall', mw.title.getCurrentTitle.rootText) .. "''"	end return '' end

return p