Module:Infobox/Player

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 lang = mw.getLanguage('en')
local m_team = require('Module:Team')
local m_role = require('Module:Role')
local m_champion = require('Module:Champion')
local m_region = require('Module:Region')
local m_country = require('Module:Country')
local social = require('Module:Infobox/Social')
local TeamHistory = require('Module:Infobox/TeamHist').teamHist
local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_game = require('Module:GameUtil')
local util_html = require('Module:HtmlUtil')
local util_infobox = require('Module:InfoboxUtil')
local util_map = require('Module:MapUtil')
local util_sentence = require("Module:SentenceUtil")
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 PlayerCurrentTeam = require('Module:PlayerCurrentTeam').main
local IntroSentence = require('Module:IntroSentence/Player').main

local NOIMAGE = 'Unknown Infobox Image - Player.png'

local InfoboxData = require('Module:InfoboxDataClass')
local InfoboxPlayer = InfoboxData:extends()

InfoboxPlayer.type = 'Player'

InfoboxPlayer.LAYOUT = {
	tabs = 'PlayerTabsHeader',
	sections = { 'Background Information', 'Competitive', 'Social Media & Links', 'Team History' },
	contents = {
		{ 'Name', 'Country', 'Nationality', 'Birthday', 'Died', 'Residency', 'ResidencyPrev' },
		{ 'Team', 'Role', 'PreviousRole', 'Champions', 'CompIDs', 'SoloIDs' },
		{ 'social', social = 'wide' },
		{ 'teamhist', teamhist = 'wide' },
	},
}

local DIRECT_STORE = { ID = 'id', Stream = 'stream', Twitter = 'twitter', Facebook = 'facebook', Askfm = 'askfm', Reddit = 'reddit', Youtube = 'youtube', Weibo = 'weibo', Vk = 'vk', Instagram = 'instagram', IsRetired = 'isretired',
}

local h = {} -- helper functions
local p = {}
function p.main(args)
	args._title = args.id
	return InfoboxPlayer:init(args)
end

function InfoboxPlayer:getProcessed(args)
	if util_args.castAsBool(args.checkboxAutoTeams) then
		args.team = nil
		args.team2 = nil
	end
	local processed = {
		pagename = mw.title.getCurrentTitle().text,
		bday = h.getBirthdayAndAgeIfAppropriate(args),
		died = h.getDeathDateIfNeeded(args),
		lc = args.id and lang:lcfirst(args.id) == args.id,
		role = args.role and m_role.rolename(args.role),
		hasAutoTeams = util_args.castAsBool(args.checkboxAutoTeams),
		nationality = args.nationality and util_map.split(args.nationality, nil),
		image = h.getImage(args),
	}
	processed.entity = processed.lc and util_text.lcfirst(processed.pagename) or processed.pagename
	local teamhist, last = TeamHistory(args)
	processed.teams = h.getTeamsTable(args, processed, last)
	processed.teamhist = not processed.hasAutoTeams and teamhist
	processed.roleLast = h.getCargoLastRole(args, processed)
	return processed
end

function h.getImage(args)
	if not util_args.castAsBool(args.checkboxAutoImage) then
		return util_infobox.getFile(args.image, NOIMAGE)
	end
	local image = h.queryForImage(args)
	return image or NOIMAGE
end

function h.getBirthdayAndAgeIfAppropriate(args)
	local bday = util_infobox.makeBday(args.birth_date_year, args.birth_date_month, args.birth_date_day)
	if args.died then
		bday.age = nil
		bday.displayandage = bday.display
	end
	return bday
end

function h.getDeathDateIfNeeded(args)
	if not args.died then return {} end
	return util_infobox.makeBday(args.birth_date_year, args.birth_date_month, args.birth_date_day, args.died)
end

function h.getTeamsTable(args, processed, last)
	if processed.hasAutoTeams then
		return PlayerCurrentTeam(mw.title.getCurrentTitle().text)
	end
	local teams = h.getLegacyTeams(args, processed)
	teams.last = last or {}
	return teams
end

function h.getLegacyTeams(args, processed)
	local ret = util_map.inPlace(
		util_args.numberedArgsToTable(args, 'team') or {},
		h.getOneLegacyTeam, args, processed
	)
	return ret
end

function h.getOneLegacyTeam(team, args, processed)
	local ret = {
		team = m_team.teamlinkname(team),
		role = processed.role,
		sub = util_args.castAsBool(args.checkboxIsSub),
	}
	return ret
end

function h.getCargoLastRole(args, processed)
	if not processed.teams then return args.role end
	if processed.teams.last.role then 
		return m_role.roleonly(processed.teams.last.role) or args.role
	end
	return args.role
end

function InfoboxPlayer:getDisplay(args, processed)
	local social = social.makeSocialSection(args)
	local display = {
		title = args.id or processed.pagename,
		image = not util_args.castAsBool(args.noteamhist) and processed.image,
		notice = h.getNotice(args),
		
		-- background info
		Name = args.name and (args.name .. (args.nativename and (' (%s)'):format(args.nativename) or '')),
		Country = args.country and m_country.rightlong(args.country),
		Nationality = util_table.concat(processed.nationality, '<br>', m_country.rightlong),
		Birthday = processed.bday.displayandage,
		Died = processed.died.displayandage,
		Residency = args.residency and m_region.rightmedium(args.residency),
		ResidencyPrev = util_table.concatFromArgs(args, 'residency-prev', '<br>', m_region.rightmedium),
		
		-- competitive
		Team = h.getCurrentTeam(processed.teams),
		Role = processed.role,
		PreviousRole = args.prev1 and util_table.concatFromArgs(args, 'prev', '<br>', m_role.roleonly),
		Champions = util_table.concatFromArgs(args, 'favchamp', '', m_champion.onlyimagelinked),
		CompIDs = util_table.concatFromArgs(args, 'compID',', '),
		SoloIDs = args.ids,
		
		-- social
		social = next(social) and tostring(util_html.blockBox(social)),
		teamhist = processed.teamhist,
		
		class = h.getClass(args, processed)
	}
	return display
end

function h.queryForImage(args)
	local query = {
		tables = {
			'PlayerRedirects=PR',
			'PlayerImages=PI',
			'Tournaments=T',
		},
		join = {
			'PR.AllName=PI.Link',
			'PI.Tournament=T.OverviewPage',
		},
		where = h.getImageWhere(args),
		fields = { 'PI.FileName' },
		orderBy = 'COALESCE(PI.SortDate, T.DateStart, T.Date) DESC',
	}
	return util_cargo.getOneResult(query)
end

function h.getImageWhere(args)
	local where = {
		('PR._pageName="%s"'):format(mw.title.getCurrentTitle().text),
		('PI._pageName IS NOT NULL'),
		('PI.IsProfileImage="1"'),
	}
	return where
end

function h.getNotice(args)
	if args.died then
		return 'Player is deceased.'
	end
	if util_args.castAsBool(args.isretired) then
		return 'Player has retired.'
	end
	return nil
end

function h.getCurrentTeam(teams)
	if #teams == 0 then return nil end
	return util_table.concat(
		util_map.extractField(teams, 'team', m_team.rightmediumlinked),
		'<br>'
	)
end

function h.getClass(args, processed)
	local listOfClasses = {
		processed.hasAutoTeams and 'infobox-player-narrow',
		util_args.castAsBool(args.noteamhist) and 'infobox-low-content',
	}
	return util_table.concat(listOfClasses, ' ')
end

function InfoboxPlayer:getCargo(args, processed)
	local cargo = self:super('getCargo', args, processed)
	cargo[#cargo+1] = h.getPlayersCargo(args, processed)
	util_table.mergeArrays(
		cargo,
		h.getPlayerLeagueHistory(args)
	)
	return cargo
end

function h.getPlayersCargo(args, processed)
	local ret = {
		_table = 'Players',
		OverviewPage = mw.title.getCurrentTitle().text,
		Image = h.getImageCargo(args, processed),
		Name = args.name,
		Player = processed.entity,
		NativeName = args.nativename,
		NameAlphabet = args.namealphabet,
		NameFull = args.name and (args.name .. (args.nativename and (' (%s)'):format(args.nativename) or '')),
		Country = args.country and m_country.name(args.country),
		Nationality = util_table.concat(processed.nationality, ','),
		Age = processed.bday.age or '',
		Birthdate = processed.bday.store or '',
		ResidencyFormer = args['residency-prev1'],
		Team = processed.teams[1] and processed.teams[1].team,
		Team2 = processed.teams[2] and processed.teams[2].team,
		TeamSystem = 'PC',
		Team2System = 'PC',
		Residency = args.residency and m_region.long(args.residency),
		Role = args.role,
		FavChamps = util_table.concatFromArgs(args, 'favchamp', ','),
		TeamLast = processed.teams.last and processed.teams.last.team,
		RoleLast = processed.roleLast,
		IsSubstitute = args.checkboxIsSub,
		IsLowercase = processed.lc,
		IsAutoTeam = args.checkboxAutoTeams,
		SoloqueueIds = args.ids,
		IsPersonality = args.page_type == 'Personality',
	}
	h.addUnprocessedArgs(ret, args)
	return ret
end

function h.getImageCargo(args, processed)
	if util_args.castAsBool(args.checkboxAutoImage) then
		return nil
	end
	if processed.image == NOIMAGE then return nil end
	if not processed.image then return nil end
	return processed.image:gsub('_', ' ')
end

function h.addUnprocessedArgs(tbl, args)
	for k, v in pairs(DIRECT_STORE) do
		tbl[k] = args[v]
	end
end

function h.getPlayerLeagueHistory(args)
	if util_args.castAsBool(args.noplh) then return {} end
	if not util_game.store_player_league_history then return {} end
	local plhTable = require('Module:PlayerLeagueHistory')._main(mw.title.getCurrentTitle().text)
	return plhTable
end

function InfoboxPlayer:getDisambigSentence(args, processed)
	-- we can't store "now known as" here but we can store everything else
	-- the "now known as" text is going to have to change, but that's ok maybe
	local replacements = {
		RESIDENCY = m_region.onlyimage(args.Residency),
		LINK = util_text.intLinkOrText(mw.title.getCurrentTitle().text),
		NATIONALITY = args.Country and m_country.localization(args.Country) or '',
		FORMER = not processed.teams[1] and i18n.default('former') or '',
		ROLE = m_role.sentence(processed.roleLast, { sub = args.checkboxIsSub }),
		TEAM = processed.teams[1] and m_team.rightmediumlinked(processed.teams[1].team),
		-- NOW_KNOWN_AS = row.CurrentName and i18n.default('nowKnownAs', row.CurrentName) or '',
	}
	return util_sentence.makeReplacements(i18n.default('disambigSentence'), replacements)
end

function InfoboxPlayer:getVariables(args, processed)
	local tbl = {
		suppressorgnavbox = args.checkboxSuppressOrgNavbox,
		introSentence = h.getIntroSentence(args, processed, 'Display'),
		description = h.getIntroSentence(args, processed, 'Description'),
		checkboxAutoTeams = args.checkboxAutoTeams,
		residency = m_region.medium(args.residency),
		isretired = args.isretired,
		current_id = processed.entity,
		isLowContent = args.noteamhist
	}
	local i = 2
	repeat
		thisteam = 'team' .. i
		tbl[thisteam] = args[thisteam] and m_team.teamlinkname(args[thisteam])
		i = i + 1
	until(not args[thisteam])
	return tbl
end

function h.getIntroSentence(args, processed, sentenceType)
	local data = mw.clone(args)
	local name = mw.text.split(args.name or '', ' ')
	data.firstname = table.remove(name, 1)
	data.lastname = table.concat(name, ' ')
	data.lastteam = processed.teams.last.team
	data.role = args.role or processed.lastrole
	data.teamStr = args.team and m_team.teamlinkname(args.team)
	data.sharesrole = util_args.castAsBool(args.checkboxSharesRole)
	data.isasub = util_args.castAsBool(args.checkboxIsSub)
	data.ispersonality = args.page_type == 'Personality'
	data.page_type = args.page_type
	data.lastteampresent = h.getLastTeamPresent(args, processed)
	data.currentTeamList = processed.teams
	return IntroSentence(data, sentenceType)
end

function h.getLastTeamPresent(args, processed)
	if processed.teams and #processed.teams > 0 then return true end
	return processed.teams.last.date and processed.teams.last.date:find('Present')
end

function InfoboxPlayer:getCategories(args, processed)
	local tbl = {
		'Players',
		args.residency and m_region.adjective(args.residency) .. ' Residents',
		args['residency-prev1'] and ('Former %s Residents'):format(m_region.adjective(args['residency-prev1'])),
		args['residency-prev2'] and ('Former %s Residents'):format(m_region.adjective(args['residency-prev2'])),
		(not args.role) and 'Players Without Role',
		h.getSuspendedCategory(args, processed),
		h.isAFreeAgent(args, processed) and 'Free Agents',
		util_args.castAsBool(args.isretired) and 'Retired Players',
		args.birth_date_year and 'Players Born In ' .. args.birth_date_year,
		processed.role == 'Coach' and 'Coaches'
	}
	
	if args.country then
		if args.page_type == 'Personality' then
			tbl[#tbl+1] = ('%s Personalities'):format(m_country.localization(args.country))
		else
			tbl[#tbl+1] = 'Players Born in ' .. m_country.namethe(args.country)
		end
	end
	return tbl
end

function h.isAFreeAgent(args, processed)
	if not util_args.castAsBool(args.checkboxAutoTeams) then
		if args.team then return false end
		if util_args.castAsBool(args.isretired) then return false end
		return true
	end
	return not h.getCurrentTeam(processed.teams)
end

function h.getSuspendedCategory(args, processed)
	if processed.teams.last.team ~= 'Suspended' then return false end
	if not processed.teams.last.date then return false end
	if not processed.teams.last.date:find('Present') then return false end
	return 'Suspended Players'
end

return p