Module:PlayerPRStore

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_table = require('Module:TableUtil')

local MEDALS = { 'Gold', 'Silver', 'Bronze', 'Copper' } local QUERIES = { 'All', 2018, 2019, 2020, 2021, All = false, -- no further where condition will get removed in util_cargo [2018] = '(T.Date >= "2018-01-01" AND T.Date <= "2018-12-31")', [2019] = '(T.Date >= "2019-01-01" AND T.Date <= "2019-12-31")', [2020] = '(T.Date >= "2020-01-01" AND T.Date <= "2020-12-31")', [2021] = '(T.Date >= "2021-01-01" AND T.Date <= "2021-12-31")', }

local h = {}

local p = {} function p._main(player) -- the cargo is deprecated local ret = { cargo = {}, display = {} } for k, tag in ipairs(QUERIES) do		ret.cargo[#ret.cargo+1] = h.getReturnedTable(tag, QUERIES[tag], player) ret.display[#ret.display+1] = tag ret.display[tag] = ret.cargo[#ret.cargo] end return ret end

function h.getReturnedTable(tag, where, player) local ret = { -- this table is being DELETED -- you must recreate it if you want to use it		_table = 'PlayerPR', Tag = tag, }	local data = h.getData(where, player) if data.Score == 0 then return {} end util_table.merge(ret, data) return ret end

function h.getData(where, player) local data = util_cargo.queryAndCast(h.getQuery(where, player)) return h.getProcessedData(data) end

function h.getQuery(where, player) local ret = { tables = { 'Tournaments=T', 'TournamentResults=Res', 'PlayerRedirects=PR' }, fields = { 'PRPoints', 'Place' }, join = 'T._pageName = Res.OverviewPage, Res.RosterLinks HOLDS PR.AllName', where = h.getWhere(where, player), types = { PRPoints = 'number', Place = 'number', },		limit = 9999, }	return ret end

function h.getWhere(where, player) local tbl = { ('PR._pageName="%s"'):format(mw.title.getCurrentTitle.text), 'Res.RosterLinks HOLDS PR.AllName', where, }	return util_cargo.concatWhere(tbl) end

function h.getProcessedData(data) local totals = { Score = 0, Gold = 0, Silver = 0, Bronze = 0, Copper = 0, }	h.countData(data, totals) return totals end

function h.countData(data, totals) for _, row in ipairs(data) do		h.addRowToTotals(row, totals) end end

function h.addRowToTotals(row, totals) h.addMedals(MEDALS[row.Place], totals) totals.Score = totals.Score + (row.PRPoints or 0) end

function h.addMedals(medal, totals) if not medal then return end totals[medal] = totals[medal] + 1 end

return p