Module:TeamRoster/NewsCargo

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

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

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_news = require("Module:NewsUtil")
local util_sentence = require("Module:SentenceUtil")
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require("Module:I18nUtil")
local SENTENCES = require('Module:RosterChangeData/Sentences')
local lang = mw.getLanguage('en')
local h = {}
local p = {}
function p.main(playerData, teamData, args, ns)
	if util_args.castAsBool(args.nocargo) then return end
	if not util_vars.getBool('is_pickup') then return end
	util_cargo.setStoreNamespace(ns or 'Data')
	i18n.init('TeamRoster/NewsCargo')
	local tournamentData = h.getTournamentData()
	h.storeOneDirection(playerData, teamData, 'Join', tournamentData)
	if not util_vars.getBool('is_over') then return end
	h.storeOneDirection(playerData, teamData, 'Leave', tournamentData)
end

function h.getTournamentData()
	local query = {
		tables = { 'Tournaments' },
		where = { ('OverviewPage="%s"'):format(h.getTitle()) },
		fields = {
			'COALESCE(DateStart,Date)=DateJoin',
			'Date=DateLeave',
			'COALESCE(StandardName, Name)=Name',
			'_pageName',
			'Region',
		},
	}
	return util_cargo.getOneRow(query) or {}
end

function h.getTitle()
	return mw.title.getCurrentTitle().text:gsub('/Participants', '')
end

function h.storeOneDirection(playerData, teamData, when, tournamentData)
	local players = util_map.copy(playerData, h.extractPlayer)
	local sentence = h.writeSentence(players, teamData, when, date, tournamentData)
	if when == 'Join' or tournamentData.DateJoin ~= tournamentData.DateLeave then
		util_news.setId()
		util_vars.resetGlobalIndex('N_LineInNews')
		h.storeNews(players, teamData, sentence, when, tournamentData)
	end
	util_map.rowsInPlace(playerData, h.storeOneRosterChange, when, tournamentData)
end

function h.extractPlayer(row)
	-- for util_esports.playerWithRole
	local player = {
		player = row.Link,
		role = row.Role,
	}
	return player
end

function h.writeSentence(players, teamData, when, date, tournamentData)
	return util_sentence.makeReplacements(
		i18n.default(h.pickSentence(tournamentData) .. when),
		h.getReplacements(players, teamData, date, tournamentData)
	)
end

function h.pickSentence(tournamentData)
	if tournamentData.DateJoin == tournamentData.DateLeave then
		return 'sentenceSameDay'
	end
	return 'sentence'
end

function h.getReplacements(players, teamData, date, tournamentData)
	local replacements = {
		PLAYERS = util_sentence.playersFromArray(players),
		JOIN = util_sentence.getConjugation('join', #players),
		LEAVE = util_sentence.getConjugation('leave', #players),
		ENDDATE = lang:formatDate('F j', tournamentData.DateLeave),
		EVENT = util_text.intLink(tournamentData._pageName, tournamentData.Name),
	}
	return replacements
end

function h.storeNews(players, teamData, sentence, when, tournamentData)
	local tempArgs = {
		tournament = h.getTitle(),
		team = teamData.Team,
		display_date = tournamentData['Date' .. when],
		date = tournamentData['Date' .. when],
		region = tournamentData.Region,
		Sentence = sentence,
		no_frontpage = 'Yes',
		no_portal = 'Yes',
		players = util_map.concatField(players, 'player', ',')
	}
	local toStore = util_news.getNewsCargoFieldsFromArgs(tempArgs)
	toStore.SubjectType = 'Team'
	toStore.Subject = tempArgs.team
	toStore.SubjectLink = tempArgs.team
	toStore.Date_Sort = tournamentData['Date' .. when]
	util_cargo.store(toStore)
end

function h.storeOneRosterChange(row, when, tournamentData)
	util_vars.setVar('Date', tournamentData['Date' .. when])
	local toStore = {
		_table = 'RosterChanges',
		Date_Sort = tournamentData['Date' .. when],
		Player = row.Link,
		Direction = when,
		Team = row.Team,
		Role = row.Role,
		Roles = row.Role,
		RolesIngame = row.Role:ingame(),
		RolesStaff = row.Role:staff(),
		Status = 'pickup',
		CurrentTeamPriority = 10, -- just a big number to make it last
		Tournaments = tournamentData._pageName,
		Preload = ('pickup_' .. when):lower(),
		N_LineInNews = util_vars.setGlobalIndex('N_LineInNews'),
	}
	toStore.PreloadSortNumber = SENTENCES.priority[toStore.Preload]
	util_news.storeRosterChangesRow(toStore)
end

return p