Module:PlayerTooltip

-- local util_args = require('Module:ArgsUtil') local util_cargo = require("Module:CargoUtil") local util_html = require("Module:HtmlUtil") local util_map = require('Module:MapUtil') 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 i18n = require("Module:I18nUtil") local lang = mw.getLanguage('en')

local m_team = require('Module:Team')

local ROWS = { 'Name', 'Country', 'Birthdate', 'Residency', 'Role' }

local h = {} local p = {}

function p.main(frame) local args = util_args.merge i18n.init('PlayerTooltip') local player = args.page or mw.title.getCurrentTitle.text if util_title.isRedirect(player) then return '' end if not mw.title.new(player).exists then return '' end local data = h.queryPlayerData(player) if not data then return '' end h.formatData(data) return h.makeOutput(data) end

function h.queryPlayerData(player) local query = { tables = { 'Players=P', 'PlayerRedirects=PR', 'PlayerImages=PI', 'Tournaments=T', 'Tenures=Ten', },		join = { 'P.OverviewPage=PR.OverviewPage', 'PR.AllName=PI.Link', 'PI.Tournament=T.OverviewPage', 'P.OverviewPage=Ten.NameLeave', },		where = { ('P.OverviewPage="%s"'):format(player), 'PI.IsProfileImage IS NULL OR PI.IsProfileImage="1"', -- this next line is commented because it's completely incorrect, but it would be remiss -- to not acknowledge why it seems to make sense - -- we only want to retrieve *current* AutoTeam results. -- however, including this line means that AutoTeam free agents have no results -- that's not okay because we need to return something from Players table still -- 'Ten.IsCurrent="1" OR P.IsAutoTeam="0"', },		fields = { 'P.ID', 'P.Name', 'COALESCE(P.NationalityPrimary, P.Country)=Country [country]', 'P.Birthdate', 'P.Residency', 'P.Role', 'P.Team', 'P.IsRetired [boolean]', 'P.IsAutoTeam [boolean]', 'PR.AllName', 'COALESCE(P.Image, PI.FileName)=Image', -- fields that are needed so we can groupBy them in our oneToMany -- so that we don't add incorrect extra f/a / TBD lines: 'Ten.IsCurrent', -- in the case that a player isn't properly set up with auto image, even though they should be: 'PI.FileName', },		oneToMany = { groupBy = { 'Image', 'AllName', 'FileName', 'IsCurrent' }, fields = { Teams = { 'CONCAT(CASE WHEN Ten.IsCurrent="1" THEN Ten.Team ELSE "" END)=TenureTeam', }			}		},		-- we want to put non-current tenures at the very end -- then we care about getting the most recent image -- finally we order by PR.AllName simply to ensure that the same name appears -- consecutively, so that oneToMany works as desired orderBy = 'Ten.IsCurrent DESC, COALESCE(PI.SortDate, T.Date) DESC, PR.AllName DESC', }	util_cargo.logQuery(query) return util_cargo.getOneRow(query) end

function h.formatData(data) if data.Country then data.Country = data.Country:flair end data.Team = h.getTeamDisplay(data) end

function h.getTeamDisplay(data) if data.IsRetired then return m_team.rightlong('retired') end if data.Team and not data.IsAutoTeam then return m_team.rightlong(data.Team) elseif not data.IsAutoTeam then return m_team.rightlong('free agent') elseif #data.Teams == 0 or not data.Teams[1].TenureTeam then return m_team.rightlong('free agent') end return util_table.concat(data.Teams, ' ', h.formatOneTeam) end

function h.formatOneTeam(row) return m_team.rightlong(row.TenureTeam) end

function h.makeOutput(data) local output = mw.html.create('div') :addClass('tippingover-tooltip') :addClass('tooltip-div') :addClass('player-tooltip') h.printImage(output, data.Image) h.printRight(output, data) return output end

function h.printRight(output, data) local div = output:tag('div') :addClass('player-tooltip-info') h.printPlayer(div, data) h.printInfo(div, data) h.printTeam(div, data) end

function h.printPlayer(output, data) output:tag('div') :addClass('player-tooltip-player') :wikitext(data.ID) end

function h.printImage(output, image) if not image or image == 'Unknown Infobox Image - Player.png' then return end output:tag('div') :addClass('player-tooltip-image') :wikitext((''):format(image)) end

function h.printInfo(div, data) local tbl = div:tag('table') :addClass('player-tooltip-info-table') util_html.printVerticalData(tbl, data, ROWS) end

function h.printTeam(output, data) output:tag('div') :addClass('player-tooltip-team') :wikitext(data.Team) end

return p --