Module:Retirement

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 COLUMNS = util_news.COLUMNS

local m_team = require('Module:Team') local RoleList = require('Module:RoleList')

local h = {}

local p = {} function p.main(frame) local args = util_args.merge util_cargo.setStoreNamespace('Data') i18n.init('Retirement') h.validateArgs(args) h.castArgs(args) util_news.setId args.Sentence = h.writeSentence(args) h.storeRetireCargo(args) local newsCargo = h.getNewsCargo(args) util_cargo.store(newsCargo) h.storeRosterChangeCargo(args) return util_news.makeSentenceOutput(args, newsCargo) end

function h.validateArgs(args) if args.team and args.team:lower == 'free agent' then error(i18n.print('error_FreeAgentAsTeam')) end end

function h.castArgs(args) for _, v in ipairs({ 'unretires', 'sub', 'trainee', }) do		args[v] = util_args.castAsBool(args[v]) end args.team = args.team and m_team.teamlinkname(args.team) args.role = RoleList(args.role, {sub = args.sub, trainee = args.trainee}) args.new_role = RoleList(args.new_role, {sub = args.new_sub, trainee = args.new_trainee}) args.new_or_old_role = args.new_role:exists and args.new_role or args.role args.role_display = args.role:names{len='name', sep='/'} args.new_role_display = args.new_role:names{len='name', sep='/'} args.new_or_old_role_display = args.new_or_old_role:names{len='name', sep='/'} end

function h.writeSentence(args) -- currently this only supports 1 player but that could be changed later -- write it in the most general way possible -- ofc, the rest of the code doesn't remotely support having more than 1 player listed but shh local replacements = { PLAYERS = util_esports.playerWithRole(args), RETIRE = util_sentence.getConjugation('retire', 1), COME = util_sentence.getConjugation('come', 1), ARE = util_sentence.getConjugation('are', 1), NEW_ROLE = h.getnew_roleSentence(args), ROLE = args.new_role:names{len='sentence'}, NEW_JOIN = h.getUnretireNewTeam(args), NEW_TEAM = m_team.rightmediumlinked(args.team), }	return util_sentence.makeReplacements(h.getSentencePreload(args), replacements) end

function h.getnew_roleSentence(args) if not args.new_role:exists then return '' end return i18n.default('new_role') end

function h.getUnretireNewTeam(args) if not args.team then return '' end return i18n.default('newteam') end

function h.getSentencePreload(args) if args.unretires then return i18n.default('unretire') end return i18n.default('retire') end

function h.storeRetireCargo(args) local tbl = { _table = 'Retirements', Player = args.player, NewsId = util_news.getId, Unretires = args.unretires, }	util_cargo.store(tbl) end

function h.getNewsCargo(args) local tbl = { Players = args.player, }	util_table.mergeDontOverwrite(		tbl,		h.getTeamNewsCargo(args),		util_news.getNewsCargoFieldsFromArgs(args)	) return tbl end

function h.getTeamNewsCargo(args) if not args.team then return {} end local ret = { SubjectType = 'Team', Subject = args.team, Teams = args.team, }	return ret end

function h.storeRosterChangeCargo(args) if not args.team then return end h.storeRosterChangeCargoLeave(args) h.storeRosterChangeCargoJoin(args) end

function h.storeRosterChangeCargoLeave(args) local tbl = util_news.getRCFieldsFromPlayerAndArgs(h.getPlayerFromArgs(args), args) util_table.merge(		tbl,		h.getOldRoleCargo(args),		h.getAdditionalRosterChangeCargo(args)	) util_news.storeRosterChangesRow(tbl) end

function h.storeRosterChangeCargoJoin(args) if not args.new_role:exists then return end local tbl = util_news.getRCFieldsFromPlayerAndArgs(h.getPlayerFromArgs(args), args) util_table.merge(		tbl,		h.getnew_roleCargo(args),		h.getAdditionalRosterChangeCargo(args)	) util_news.storeRosterChangesRow(tbl) end

function h.getPlayerFromArgs(args) local ret = { Player = args.player, Sub = args.sub, Trainee = args.trainee, Role = args.role, Roles = args.role, }	return ret end

function h.getOldRoleCargo(args) local ret = { Direction = 'Leave', Status = args.status, RoleDisplay = args.role_display, RolesIngame = args.role:ingame:names, RolesStaff = args.role:staff:names, }	return ret end

function h.getnew_roleCargo(args) if args.unretires then return h.getnew_roleUnretiresCargo(args) end local ret = { Role = args.new_role, Roles = args.new_role, RolesIngame = args.new_role:ingame:names, RolesStaff = args.new_role:staff:names, RoleDisplay = args.new_role_display, Direction = 'Join', }	return ret end

function h.getnew_roleUnretiresCargo(args) -- this could be input as either |role= or |new_role= local ret = { Role = args.new_or_old_role, RolesIngame = args.new_or_old_role:ingame:names, RolesStaff = args.new_or_old_role:staff:names, RoleDisplay = args.new_or_old_role_display, Direction = 'Join', }	return ret end

function h.getAdditionalRosterChangeCargo(args) local ret = { Preload = h.getPreload(args.team), }	return ret end

function h.getPreload(team) if not team or team:lower == 'tbd' or team:lower == 'free agent' then return 'retirement_no_team' end return 'retirement' end

return p