Module:NewsCurrentStatusAbstract

From Call of Duty Esports Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:NewsCurrentStatusAbstract/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 OD = require('Module:OrderedDict')

local LCS = require('Module:LuaClassSystem').class

local h = {}

local p = LCS.abstract()

function p:init(subject)
	self.CONTRACT_DATES = {}
	local listOfChanges = self:queryForChanges(subject)
	self:processChangesRows(listOfChanges)
	if #listOfChanges == 0 then return self:defaultOutput() end
	local netStatuses = self:computeNetStatuses(listOfChanges)
	local listOfSubjects = self:getFinalListOfSubjects(netStatuses)
	return self:makeOutput(listOfSubjects, listOfChanges)
end

function p:queryForChanges(subject)
	return util_cargo.queryAndCast(self:getQuery(subject))
end

function p:getQuery(subject)
	local query = {
		tables = self:getTables(),
		join = self:getJoin(),
		where = self:getWhere(subject),
		fields = self:getFields(),
		orderBy = 'News.Date_Sort ASC, News.N_LineInDate ASC, RC.N_LineInNews ASC',
		complexTypes = {
			Role = {
				type = 'RoleList',
				args = {
					'Roles',
					modifier = 'RoleModifier',
				}
			}
		}
	}
	util_cargo.logQuery(query)
	return query
end

function p:getTables() end
function p:getJoin() end
function p:getWhere() end

function p:getFields()
	local ret = {
		'RC.Date_Sort',
		'RC.Player',
		
		'RC.Team',
		'RC.Roles',
		'RC.RoleModifier',
		'RC.Direction',
		'RC.CurrentTeamPriority=Priority',
		'News.Date_Display',
		'News.Region',
		'News._pageName',
	}
	return ret
end

function p:processChangesRows(listOfChanges)
	util_map.selfRowsInPlace(self, listOfChanges, self.processOneChangeRow)
end

-- get dictionary keyed by subject
function p:computeNetStatuses(listOfChanges)
	local currentStatuses = {}
	for _, row in ipairs(listOfChanges) do
		self:updateEntryForThis(currentStatuses, row)
	end
	return currentStatuses
end

function p:updateEntryForThis(currentStatuses, row)
	if currentStatuses[row.Subject] then
		row.Priority = row.Priority or currentStatuses[row.Subject].Priority
	end
	if row.Subject then
		currentStatuses[row.Subject] = row
	end
end

-- get ordered list of subjects
function p:getFinalListOfSubjects(netStatuses)
	local subjects = {}
	for _, status in pairs(netStatuses) do
		self:addSubjectToOutputIfNeeded(subjects, status)
	end
	util_sort.tablesByKeys(subjects, 'Priority', true)
	return subjects
end

function p:addSubjectToOutputIfNeeded(subjects, status)
	if not self:doWeAddSubjectToOutput(status) then return end
	subjects[#subjects+1] = status
end

function p:doWeAddSubjectToOutput(status)
	return status.Direction == 'Join'
end

function p:makeOutput(listOfSubjects, listOfChanges) end

return p