Module:PlayerCurrentTeam

From Halo Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require("Module:CargoUtil")
local util_esports = require("Module:EsportsUtil")
local util_html = require("Module:HtmlUtil")
local util_map = require("Module:MapUtil")
local util_sort = require("Module:SortUtil")
local util_source = require("Module:SourceUtil")
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 i18n = require('Module:i18nUtil')

local ListOfRoles = require('Module:ListOfRoles')
local OD = require('Module:OrderedDict')

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

local h = {}

local p = {}

function p.test(frame)
	local args = util_args.merge(true)
	local result = p.main(args[1])
	local output = mw.html.create('table')
		:addClass('wikitable')
	for _, row in ipairs(result) do
		output:tag('tr')
			:tag('td'):wikitext(row.team)
			:tag('td'):wikitext(row.role)
			:tag('td'):wikitext(tostring(row.sub))
			:tag('td'):wikitext(tostring(row.trainee))
	end
	return output
end

function p.main(player)
	local listOfChanges = h.queryForChanges(player)
	h.processChangesRows(listOfChanges)
	if not next(listOfChanges) then return { last = {} } end
	local netStatuses = h.computeNetStatuses(listOfChanges)
	local listOfTeams = h.getOutputListOfTeamsAndRoles(netStatuses)
	listOfTeams.last = h.getLastTeamList(listOfChanges)
	return listOfTeams
end

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

function h.getQuery(player)
	local query = {
		tables = h.getTables(),
		join = h.getJoin(),
		where = h.getWhere(player),
		fields = h.getFields(),
		orderBy = 'RC.Date_Sort ASC, News.N_LineInDate ASC, RC.N_LineInNews ASC',
	}
	return query
end

function h.getTables()
	local ret = {
		'RosterChanges=RC',
		'NewsItems=News',
		'PlayerRedirects=PR',
	}
	return ret
end

function h.getJoin()
	local ret = {
		'RC.NewsId=News.NewsId',
		'RC.Player=PR.AllName',
	}
	return ret
end

function h.getWhere(player)
	local where = {
		('PR._pageName="%s"'):format(player)
	}
	return util_cargo.concatWhere(where)
end

function h.getFields()
	local ret = {
		'RC.Date_Sort',
		'RC.Player',
		'PR._pageName=PlayerLink',
		'RC.Team',
		'RC.Role',
		'RC.RoleModifier',
		'RC.Direction',
		'RC.CurrentTeamPriority=Priority',
		'News.Date_Display',
		'News.Region',
		'News._pageName',
	}
	return ret
end

function h.processChangesRows(listOfChanges)
	-- util_map.rowsInPlace(listOfChanges, h.processOneChangeRow)
end

-- function h.processOneChangeRow(row)
-- 	for _, v in ipairs('Team', 'Role', 'RoleModifier') do
-- 		row[v .. 'row.Direction'] = row[v]
-- 	end
-- end

function h.computeNetStatuses(listOfChanges)
	local currentTeamStatuses = {}
	for _, row in ipairs(listOfChanges) do
		h.updateEntryForThisTeam(currentTeamStatuses, row)
	end
	return currentTeamStatuses
end

function h.updateEntryForThisTeam(currentTeamStatuses, row)
	if currentTeamStatuses[row.Team] then
		row.Priority = row.Priority or currentTeamStatuses[row.Team].Priority
	end
	currentTeamStatuses[row.Team] = row
end

function h.getOutputListOfTeamsAndRoles(netStatuses)
	local teamsAndRoles = {}
	for _, teamStatus in pairs(netStatuses) do
		h.addTeamToOutputIfNeeded(teamsAndRoles, teamStatus)
	end
	util_sort.tablesByKeys(teamsAndRoles, 'Priority', true)
	return teamsAndRoles
end

function h.addTeamToOutputIfNeeded(teamsAndRoles, teamStatus)
	if teamStatus.Direction ~= 'Join' then return end
	teamsAndRoles[#teamsAndRoles+1] = {
		team = teamStatus.Team,
		sub = teamStatus.RoleModifier == 'Sub',
		trainee = teamStatus.RoleModifier == 'Trainee',
		role = teamStatus.Role,
		Priority = teamStatus.Priority,
	}
end

function h.getLastTeamList(listOfChanges)
	return h.getLastTeamInfo(listOfChanges[#listOfChanges])
end

function h.getLastTeamInfo(row)
	local ret = {
		team = row.Team,
		role = row.Role,
		sub = row.RoleModifier == 'Sub',
		trainee = row.RoleModifier == 'Trainee',
	}
	return ret
end

return p