Module:PlayerPentakills

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 RegionList = require('Module:RegionList')

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 i18n.init('PlayerPentakills') h.castArgs(args) h.setColumns(args) local player = h.getPlayer(args) -- TODO: Or redirect target local result = h.doQuery(args, player) -- return is a hack to avoid having extra whitespace when this is printed on its own line if #result == 0 then return '' end util_map.inPlace(result, h.processRow) return h.makeOutput(result, args) end

function h.castArgs(args) args.region = RegionList(args.region) 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 = { 'Pentakills=PK', 'PlayerRedirects=PR', 'Tournaments=T' }, fields = { 'DateDisplay', 'T.OverviewPage=OverviewPage', 'Team [team]', 'TeamVs [team]', 'PK.Name=ID', 'Link', 'Champion [champion]', 'Role [role]', 'Win [boolean]', 'Kills=K', 'Deaths=D', 'Assists=A', 'ScoreboardLink', 'Vod', 'COALESCE(PK.Tournament, T.StandardName)=Tournament', 'T._pageName=_pageName', 'COALESCE(PK.Region,T.Region)=Region [region]', 'PK._pageName=_pageName', },		where = h.getWhere(args, player), join = 'PK.Link=PR.AllName, PK.OverviewPage=T.OverviewPage', orderBy = 'PK.DateSort DESC', }	util_vars.log(query) return util_cargo.queryAndCast(query) end

function h.getWhere(args, player) local tbl = { h.getPlayerWhereCondition(player), util_cargo.whereFromArg('PK.Win = "%s"', args.iswin), util_cargo.whereFromCompoundEntity('T.Region="%s"', args.region), 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 = row.Region:flair, Tournament = util_text.intLink(row.OverviewPage, row.Tournament), Team = row.Team:imagelink, TeamVs = row.TeamVs:imagelink, Champion = row.Champion:flairlink, Position = row.Role:get('short'), 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, args) local tbl = mw.html.create('table') :addClass('wikitable') util_html.printHeaderFromI18n(tbl, COLUMNS) util_html.printRowsByList(tbl, result, COLUMNS) return h.heading(args.playerpage), tbl, h.fullListLink(args.playerpage) end

function h.heading(playerpage) if not util_args.castAsBool(playerpage) then return '' end return mw.html.create('h3'):wikitext(i18n.default('playerHeading')) end

function h.fullListLink(playerpage) if not util_args.castAsBool(playerpage) then return '' end return ("\n:%s"):format(i18n.print("FullList")) end

return p