Module:ContractData

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 lang = mw.getLanguage('en') 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('ContractData') util_news.setId local players = util_news.getPlayersFromArg(args.players) local playersByDate = h.getPlayersByDate(players) args.Sentence = h.writeSentence(args, playersByDate) h.storeContractCargo(args, players) local newsCargo = h.getNewsCargo(args, players) util_cargo.store(newsCargo) return util_news.makeSentenceOutput(args, newsCargo) end

function h.castArgs(args) args.team = m_team.teamlinkname(args.team) end

function h.getPlayersByDate(players) local byDate = {} for _, player, playerData in ipairs(players) do		util_table.initDict(byDate, playerData.ContractUntil) util_table.pushDict(byDate[playerData.ContractUntil], player, playerData) end return byDate end

function h.writeSentence(args, playersByDate) return util_table.concatDict(playersByDate, ' ', h.getSentencePart, args) end

function h.getSentencePart(oneDateOfPlayers, args) return util_sentence.makeReplacements(		args.custom or i18n.default('sentence'),		h.getReplacements(args, oneDateOfPlayers)	) end

function h.getReplacements(args, oneDateOfPlayers) local firstPlayer = oneDateOfPlayers[oneDateOfPlayers[1]] local ret = { PLAYERS_POSS = util_sentence.playersPossessive(oneDateOfPlayers), CONTRACT = util_sentence.getConjugation('contract', #oneDateOfPlayers), EXPIRE = util_sentence.getConjugation('expire', #oneDateOfPlayers), ARE = util_sentence.getConjugation('are', #oneDateOfPlayers), DATE = lang:formatDate('j F Y', firstPlayer.ContractUntil), }	return ret end

function h.storeContractCargo(args, players) players:mapRows(h.storeOneContractCargo, args) end

function h.storeOneContractCargo(player, args) local tbl = { _table = 'Contracts', Team = args.team, Player = player.Player, ContractEnd = player.ContractUntil, NewsId = util_news.getId, }	util_cargo.store(tbl) end

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

function h.getPreload(args) if args.display_date then return 'contract_data_fuzzy' end return 'contract_data' end

return p