Module:PlayerLeagueHistory

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_sort = require('Module:SortUtil') local util_table = require('Module:TableUtil') local util_vars = require('Module:VarsUtil')

local h = {}

local p = {}

function p._main(player) -- leagues here are actually League Groups not leagues -- this will plug into Infobox Player and Cargo will be written there local gameData = h.getGameData(player) local byLeagueGroup = h.groupByLeagueGroup(gameData) return h.makeCargoStores(player, byLeagueGroup) end

function h.getGameData(player) return util_cargo.queryAndCast(h.getQuery(player)) end

function h.getQuery(player) local query = { tables = h.tables, join = h.join, fields = h.fields, where = h.getWhere(player), }	util_cargo.logQuery(query) return query end

h.tables = { 'LeagueGroups__Leagues=L', 'LeagueGroups=LG', 'Tournaments=IT', 'ScoreboardPlayers=SP', 'PlayerRedirects=PR', } h.join = { 'L._rowID=LG._ID', 'L._value=IT.League', 'IT.OverviewPage=SP.OverviewPage', 'SP.Link=PR.AllName', } h.fields = { 'PR.OverviewPage', 'LG.LongName=LeagueGroup', 'IT.OverviewPage=Tournament', 'SP.Team=Team', 'IT.Date=Date', }

function h.getWhere(player) local tbl = { 'LG.Leagues__full IS NOT NULL', ('PR.OverviewPage="%s"'):format(player), 'IT.IsPlayoffs="0"', 'IT.IsQualifier="0"', }	return util_cargo.concatWhere(tbl) end

-- group function h.groupByLeagueGroup(gameData) local byLeagueGroup = {} for _, row in ipairs(gameData) do		h.addLeagueGroup(byLeagueGroup, row) end return byLeagueGroup end

function h.addLeagueGroup(byLeagueGroup, row) byLeagueGroup.total = 1 + (byLeagueGroup.total or 0) util_table.initDict(byLeagueGroup, row.LeagueGroup) h.addTournamentToLeagueGroup(byLeagueGroup[row.LeagueGroup], row) end

function h.addTournamentToLeagueGroup(leagueGroup, row) leagueGroup.total = 1 + (leagueGroup.total or 0) util_table.initDict(leagueGroup, row.Tournament) h.addTeamToParent(leagueGroup, row) h.addTeamToParent(leagueGroup[row.Tournament], row) leagueGroup[row.Tournament].date = row.Date end

function h.addTeamToParent(parent, row) util_table.initTable(parent, 'teams') util_table.initDict(parent.teams, row.Team) parent.teams[row.Team][#parent.teams[row.Team]+1] = row end

-- "output" function h.makeCargoStores(player, byLeagueGroup) local ret = {} for i, leagueGroup in ipairs(byLeagueGroup) do		ret[#ret+1] = { _table = 'PlayerLeagueHistory', Player = player, League = leagueGroup, Teams = util_table.concat(byLeagueGroup[leagueGroup].teams), LeagueHistory = h.getLeagueHistoryBlob(byLeagueGroup[leagueGroup]), TotalGames = byLeagueGroup[leagueGroup].total }	end return ret end

function h.getLeagueHistoryBlob(leagueGroup) return util_table.concat(h.getLeagueHistoryBlobTable(leagueGroup), ';;;') end

function h.getLeagueHistoryBlobTable(leagueGroup) local ret = {} util_sort.dictByKeys(leagueGroup, 'date', true) for _, tournament in ipairs(leagueGroup) do		ret[#ret+1] = h.getLeagueAndTeams(tournament, leagueGroup[tournament]) end return ret end

function h.getLeagueAndTeams(key, tournament) return ('%s::%s'):format(key, util_table.concat(tournament.teams, ';;')) end

return p