Module:SentenceUtil

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

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

local util_args = require('Module:ArgsUtil')
local util_esports = require("Module:EsportsUtil")
local util_map = require("Module:MapUtil")
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require('Module:i18nUtil')

local h = {}

local p = {}

function p.getConjugation(word, n)
	if n > 1 then return i18n.default(('conj_%s_plural'):format(word:lower())) end
	return i18n.default(('conj_%s_singular'):format(word:lower()))
end

function p.makeReplacements(sentence, replacements)
	if not sentence then return nil end
	if not replacements then replacements = {} end
	local old = sentence
	for k, v in pairs(replacements) do
		sentence = sentence:gsub('%$' .. k .. '%$', v or 'unknown')
	end
	if sentence == old then
		return sentence
	end
	return p.makeReplacements(sentence, replacements)
end

function p.players(players)
	if not players then error('Nil players link sent to SentenceUtil') end
	return util_table.printList(util_map.arraySafe(players, h.playerWithRole, players))
end

function p.playersFromArray(players)
	if not players then error('Nil players link sent to SentenceUtil') end
	return util_table.printList(util_map.arraySafe(players, util_esports.playerWithRole))
end

function h.playerWithRole(player, players)
	return util_esports.playerWithRole(players[player])
end

function p.playersPossessive(players)
	return util_text.possessive(
		p.players(players),
		h.playersPossessiveLastCharacter(players[#players], players[players[#players]])
	)
end

function h.playersPossessiveLastCharacter(lastPlayerName, lastPlayerObj)
	local staffNames = lastPlayerObj.RolesStaff and lastPlayerObj.RolesStaff:names{len='name'}
	if staffNames then return staffNames end
	return util_esports.playerDisplay(lastPlayerName)
end

function p.playersWithSwap(players)
	return util_table.printList(util_map.arraySafe(players, h.playerLinkedWithSwap, players))
end

function p.playersPossessiveWithSwap(players)
	return util_text.possessive(p.playersWithSwap(players))
end

function h.playerLinkedWithSwap(player, players)
	local playerData = players[player]
	if not playerData.RoleStart or not playerData.RoleEnd then
		return util_esports.playerMaybeUnlinked(player, playerData.unlinked)
	end
	return ('%s (%s)'):format(
		util_esports.playerMaybeUnlinked(player, playerData.unlinked),
		h.roleSwapText(playerData)
	)
end

function h.roleSwapText(playerData)
	return i18n.default(
		'role_swap',
		h.roleNameOrInput(playerData, 'Leave'),
		h.roleNameOrInput(playerData, 'Join')
	)
end

function h.roleNameOrInput(playerData, when)
	return playerData[when].RoleDisplay
end

return p