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 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_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
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 = { 'Pentakills', 'PlayerRedirects=PR' },
		fields = h.fields,
		where = h.makeWhere(args, player),
		join = 'PR.AllName = Pentakills.Link',
		orderBy = 'Pentakills.DateSort DESC',
		types = {
			Win = 'boolean'
		},
		groupBy = 'Pentakills._pageName, Pentakills.N',
	}
	return util_cargo.queryAndCast(query)
end

h.fields = { 'N', 'DateDisplay', 'Region', 'Tournament', 'TournamentLink', 'Team', 'TeamVs', 'ID', 'Link', 'Champion', 'Role', 'Win', 'K', 'D', 'A', 'ScoreboardLink', 'Vod' }

function h.makeWhere(args, player)
	local tbl = {
		util_cargo.whereFromArg('(PR._pageName = BINARY "%s" OR PR._pageName IS NULL)', player),
		util_cargo.whereFromArg('Pentakills.Win = "%s"', args.iswin),
		('(PR.AllName = BINARY Pentakills.Link OR PR.AllName IS NULL)')
	}
	return util_cargo.concatWhere(tbl)
end

function h.processRow(row)
	local processed = {
		Date = row.DateDisplay,
		Player = util_text.intLinkOrText(row.Link, row.ID),
		Region = m_region.rightmedium(row.Region),
		Tournament = util_text.intLink(row.TournamentLink, 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 '',
	}
	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