Module:TeamRename

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_news = require("Module:NewsUtil") local util_sentence = require("Module:SentenceUtil") local util_table = require("Module:TableUtil") local util_title = require("Module:TitleUtil") local util_text = require("Module:TextUtil") local util_vars = require("Module:VarsUtil") local i18n = require("Module:I18nUtil")

local TEAM_ARGS = { 'original', 'new' } local DIRECTIONS = { 'Leave', 'Join' } local PLAYER_INDEX = 1

local m_team = require('Module:Team')

local h = {}

local p = {} function p.main(frame) local args = util_args.merge util_cargo.setStoreNamespace('Data') h.castArgs(args) i18n.init('TeamRename') util_news.setId args.Sentence = h.writeSentence(args) local players = util_news.getPlayersFromArg(args.players) h.storeRenameCargo(args) local newsCargo = h.getNewsCargo(args, players) util_cargo.store(newsCargo) if args.store_rosters then h.storeRosterChangeCargo(args, players) end return util_news.makeSentenceOutput(args, newsCargo) end

function h.castArgs(args) args.new = m_team.teamlinkname(args.new) args.original = m_team.teamlinkname(args.original) args.store_rosters = util_args.castAsBool(args.store_rosters) args.same_page = util_args.castAsBool(args.same_page) end

function h.writeSentence(args) return util_sentence.makeReplacements(		args.custom or i18n.default(args.type or 'rename'),		h.getReplacements(args)	) end

function h.getReplacements(args) local ret = { OLD = m_team.rightmediumlinked(args.original), NEW = m_team.rightmediumlinked(args.new), SLOT = h.getSlotText(args) }	return ret end

function h.getSlotText(args) if not args.slot then return '' end return i18n.default(		'slot',		util_text.intLinkOrText(args.slot)	) end

-- store cargo function h.storeRenameCargo(args) local tbl = { _table = 'TeamRenames', Date = util_vars.getVar('Date'), OriginalName = m_team.teamlinkname(args.original), NewName = m_team.teamlinkname(args.new), NewsId = util_news.getId, Verb = args.type or 'rename', Slot = util_title.target(args.slot), IsSamePage = args.same_page, }	util_cargo.store(tbl) end

function h.getNewsCargo(args, players) local tbl = { SubjectType = 'Team', Subject = m_team.teamlinkname(args.new), Teams = h.getNewsTeamField(args), Players = players:concatKeys, }	util_table.mergeDontOverwrite(tbl, util_news.getNewsCargoFieldsFromArgs(args)) return tbl end

function h.getNewsTeamField(args) local tbl = { m_team.teamlinkname(args.original), m_team.teamlinkname(args.new), }	return util_table.concat(tbl, ',') end

function h.storeRosterChangeCargo(args, players) players:mapRows(h.storeOnePlayerRosterChangeCargo, args) end

function h.storeOnePlayerRosterChangeCargo(player, args) util_news.storeRosterChangesRow(h.getOnePlayerMove(player, args, 1)) -- if the player immediately changed position/status/etc on team rename, then actually -- we don't want to store a follow-up join after the leave, so quit immediately -- this is governed by `changed_on_team_rename` in RCPlayer. -- the same argument can be used afterwards in the Roster Change to avoid storing the join part of -- a role or position swap. -- players who leave at time of rename should just be listed as leaving the old team -- players who join at the time of rename are irrelevant and would not be included in this line if util_args.castAsBool(player.ChangedOnTeamRename) then return end util_news.storeRosterChangesRow(h.getOnePlayerMove(player, args, 2)) end

function h.getOnePlayerMove(player, args, index) local ret = { Team = m_team.teamlinkname(args[TEAM_ARGS[index]]), N_LineInNews = index, Direction = DIRECTIONS[index], Preload = h.getRosterChangePreload(args), Tags = 'TeamRename', }	util_table.mergeDontOverwrite(		ret,		util_news.getRCFieldsFromPlayerAndArgs(player, args)	) ret.RosterChangeId = ret.NewsId .. '_RC_' .. PLAYER_INDEX PLAYER_INDEX = PLAYER_INDEX + 1 return ret end

function h.getRosterChangePreload(args) return args.same_page and 'team_rename_same_page' or 'team_rename' end

return p