Module:PlayerPentakills

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:PlayerPentakills/doc

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

local m_champion = require('Module:Champion')
local m_region = require('Module:Region')
local m_role = require('Module:Role')
local m_team = require('Module:Team')

local i18n = require('Module:I18nUtil')

local COLUMNS = { 'Date', 'Region', 'Tournament', 'Team', 'TeamVs', 'Champion', 'Position', 'WL', 'KDA', 'Scoreboard', 'VOD' }

local h = {}

local p = {}
function p.main(frame)
	local args = util_args.merge(true)
	i18n.initGlobalFromFile('PlayerPentakills')
	h.setColumns(args)
	local player = h.getPlayer(args)
	-- TODO: Or redirect target
	local result = h.doQuery(args, player)
	util_vars.log(result)
	util_table.mapInPlace(result, h.processRow)
	return h.makeOutput(result)
end

function h.setColumns(args)
	if util_args.castAsBool(args.noplayer) then
		table.insert(COLUMNS, 2, 'Player')
	end
	if util_args.castAsBool(args.debug) then
		COLUMNS[#COLUMNS+1] = '_pageName'
	end
end

function h.getPlayer(args)
	if util_args.castAsBool(args.noplayer) then
		return nil
	end
	return args.player or mw.title.getCurrentTitle().text
end

function h.doQuery(args, player)
	local query = {
		tables = { 'Tournaments=T', 'Pentakills=PK', 'PlayerRedirects=PR' },
		fields = h.fields,
		where = h.getWhere(args, player),
		join = 'T.OverviewPage=PK.OverviewPage, PK.Link=PR.AllName',
		orderBy = 'PK.DateSort DESC',
		types = {
			Win = 'boolean'
		},
	}
	util_vars.log(query)
	return util_cargo.queryAndCast(query)
end

h.fields = { 'DateDisplay', 'PK.Region=Region',
	'T.OverviewPage=OverviewPage',
	'Team', 'TeamVs', 'PK.Name=ID', 'Link', 'Champion', 'Role', 'Win', 'Kills=K', 'Deaths=D', 'Assists=A',
	'ScoreboardLink',
	'Vod',
	'PK.Tournament=Tournament',
	'T.StandardName=Tournament2',
	'T._pageName=_pageName',
	'T.Region=Region2',
	'PK._pageName=_pageName',
}

function h.getWhere(args, player)
	local tbl = {
		h.getPlayerWhereCondition(player),
		util_cargo.whereFromArg('PK.Win = "%s"', args.iswin),
		h.getDateWhereCondition(args),
		h.getTournamentWhereCondition(args.tournament)
	}
	return util_cargo.concatWhere(tbl)
end

function h.getPlayerWhereCondition(player)
	if not player then return nil end
	-- if mw.title.new(player, '').exists then
	-- 	return util_cargo.whereFromArg('(PR._pageName = "%s")', player)
	-- end
	return util_cargo.whereFromArg('(PK.Link = "%s")', player)
end

function h.getDateWhereCondition(args)
	if args.year then
		return ('(PK.DateSort <= "%s" AND PK.DateSort >= "%s")'):format(
			('%s-12-31'):format(args.year),
			('%s-01-01'):format(args.year)
		)
	end
	return nil
end

function h.getTournamentWhereCondition(tournament)
	if not tournament then return nil end
	return ('T.OverviewPage="%s"'):format(util_title.target(tournament))
end

function h.processRow(row)
	local processed = {
		Date = row.DateDisplay,
		Player = util_text.intLinkOrText(row.Link, row.ID),
		Region = m_region.rightmedium(row.Region2 or row.Region),
		Tournament = util_text.intLink(row.OverviewPage, row.Tournament2 or row.Tournament),
		Team = m_team.onlyimagelinked(row.Team),
		TeamVs = m_team.onlyimagelinked(row.TeamVs),
		Champion = m_champion.rightlonglinked(row.Champion),
		Position = m_role.shortname(row.Role),
		WL = row.Win and 'W' or 'L',
		KDA = ('%s/%s/%s'):format(row.K or '??', row.D or '??', row.A or '??'),
		Scoreboard = util_text.intLink(row.ScoreboardLink) or '',
		VOD = util_text.extLink(row.Vod) or '',
		_pageName = row._pageName,
	}
	return processed
end

function h.makeOutput(result)
	local tbl = mw.html.create('table')
		:addClass('wikitable')
	util_html.headerFromI18n(tbl, COLUMNS)
	util_html.printRowsByList(tbl, result, COLUMNS)
	return tbl, ("\n:''%s''"):format(i18n.print("FullList"))
end

return p