Leaguepedia | League of Legends Esports Wiki
[checked revision][checked revision]
([ST] just check if isIngame == isIngame() instead of that silly boolean expression)
([ST] self.modifier)
 
(37 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
local lang = mw.getLanguage('en')
 
local lang = mw.getLanguage('en')
   
local CompoundEntityAbstract = require('Module:CompoundEntityAbstract')
+
local EntityListAbstract = require('Module:EntityListAbstract')
   
local p = CompoundEntityAbstract:extends()
+
local p = EntityListAbstract:finalExtends()
 
local h = {}
 
local h = {}
   
p.Entity = require('Module:Role2')
+
p.Entity = require('Module:Role')
   
function p:init(str, sep, opts)
+
function p:init(str, opts)
 
if not opts then opts = {} end
sep = sep or '%s*;%s*'
 
self:super('init', str, sep, opts)
+
self.opts = opts
 
opts.sep = opts.sep or ';'
  +
self:super('init', str, opts)
  +
self.modifier = opts.modifier
  +
if self.is_nil and opts.modifier then
  +
-- case when we have a modifier but no string (e.g. solely sub or trainee)
  +
self.objs = { self.Entity(nil, opts) }
  +
self.is_nil = false
 
end
 
if self.is_nil then return end
 
if self.is_nil then return end
  +
self.ingame = self:filterIngameRoles(true)
 
self.staff = self:filterIngameRoles(false)
+
self.isFiltered = false
self.hasIngame = #self.ingame > 0
 
self.hasStaff = #self.staff > 0
 
self.ingameOnly = not self.hasStaff
 
self.staffOnly = not self.hasStaff
 
 
end
 
end
   
function p:filterIngameRoles(isIngame)
+
function p:names(opts)
 
if self.is_nil then return end
 
if self.is_nil then return end
local filteredRoles = {}
+
if not opts then opts = {} end
  +
local opts2 = mw.clone(opts)
for _, role in ipairs(self) do
 
 
local tbl = {}
if isIngame == role:isIngame() then
 
  +
filteredRoles[#filteredRoles+1] = role
 
 
for i, obj in ipairs(self) do
 
tbl[#tbl+1] = obj:name(opts2)
  +
  +
-- skip the prefix starting with the second item in the list
  +
-- for example, we want to write Sub/Sup/Assistant Coach
  +
-- instead of Sub/Sup/Sub/Assistant coach
  +
  +
-- however we'd want to write Assistant Coach/Sub/Sup
  +
if obj:isIngame() then
  +
opts2.skip_prefix = true
 
end
 
end
 
end
 
end
 
return util_table.concat(tbl, opts.sep)
return filteredRoles
 
 
end
 
end
   
function p:ingameImages(sep, opts)
+
function p:sentence()
 
if self.is_nil then return end
 
if self.is_nil then return end
local tbl = {}
+
local nouns = {}
 
local preps = {}
for _, obj in ipairs(self.ingame) do
 
 
local phrases = {}
tbl[#tbl+1] = obj:image(opts)
 
  +
local prep = self.objs[1]:get('prep')
  +
  +
-- we have to print a preposition next to each word only if
  +
-- they disagree in the prepositions used, e.g. mid laner for and co-owner of
  +
-- but mid laner and manager for
  +
-- so track whether we've changed preposition at all;
  +
-- if so, then concat and return the phrases
  +
-- if not then just return the nouns
  +
local usePreps = false
  +
for i, obj in ipairs(self) do
  +
nouns[i] = obj:name{len='sentence'}
  +
phrases[i] = ('%s %s'):format(obj:name{len='sentence'}, obj:get('prep'))
  +
if prep ~= obj:get('prep') then
  +
usePreps = true
 
end
 
end
  +
if not usePreps then
 
return util_table.printList(nouns) .. ' ' .. prep
 
end
 
end
return util_table.concat(tbl, sep or '')
+
return util_table.printList(phrases)
 
end
 
end
   
function p:ingameNames(length, sep, opts)
+
function p:sortnumber()
 
if self.is_nil then return end
 
if self.is_nil then return end
  +
return self.objs[1]:sortnumber()
local tbl = {}
 
for _, obj in ipairs(self.ingame) do
 
tbl[#tbl+1] = obj:name(length, opts)
 
end
 
return util_table.concat(tbl, sep)
 
 
end
 
end
   
function p:staffNames(length, sep, opts)
+
function p:ingame()
  +
return self:getFilterKey('ingame')
if self.is_nil then return end
 
local tbl = {}
 
for _, obj in ipairs(self.staff) do
 
tbl[#tbl+1] = obj:name(length, opts)
 
end
 
return util_table.concat(tbl, sep)
 
 
end
 
end
   
function p:serialize()
+
function p:staff()
  +
return self:getFilterKey('staff')
local tbl = {}
 
for _, obj in ipairs(self) do
 
tbl[#tbl+1] = obj:serialize()
 
end
 
-- same separator as we use in p:init
 
return util_table.concat(tbl, ';')
 
 
end
 
end
   
function p:sortnumber()
+
function p:hasIngame()
return self.objs[1]:sortnumber()
+
return self:getFilterKey('hasIngame')
  +
end
  +
  +
function p:hasStaff()
  +
return self:getFilterKey('hasStaff')
  +
end
  +
  +
function p:isIngameOnly()
  +
return self:getFilterKey('isIngameOnly')
  +
end
  +
  +
function p:isStaffOnly()
  +
return self:getFilterKey('isStaffOnly')
  +
end
  +
  +
function p:getFilterKey(key)
  +
if self.isFiltered then return self.filters[key] end
  +
self:getFilters()
  +
return self.filters[key]
  +
end
  +
  +
function p:getFilters()
  +
optsIngame = mw.clone(self.opts)
  +
optsStaff = mw.clone(self.opts)
  +
optsStaff.modifier = nil
  +
optsStaff.trainee = nil
  +
optsStaff.Trainee = nil
  +
optsStaff.sub = nil
  +
optsStaff.Sub = nil
  +
optsIngame.alreadyCast = true
  +
optsStaff.alreadyCast = true
 
local ingame = p(h.filterIngameRoles(self, true), optsIngame)
  +
local staff = p(h.filterIngameRoles(self, false), optsStaff)
  +
self.filters = {
  +
ingame = ingame,
  +
staff = staff,
 
hasIngame = ingame:exists(),
 
hasStaff = staff:exists(),
  +
isIngameOnly = not staff:exists(),
  +
isStaffOnly = not ingame:exists(),
  +
}
  +
self.isFiltered = true
  +
end
  +
  +
function h.filterIngameRoles(objs, isIngame)
  +
local filteredRoles = {}
 
for _, role in ipairs(objs) do
 
if isIngame == role:isIngame() then
 
filteredRoles[#filteredRoles+1] = role
  +
end
  +
end
 
return filteredRoles
 
end
 
end
   

Latest revision as of 00:12, 5 December 2020

Edit the documentation or categories for this module.


local util_args = require('Module:ArgsUtil')
local util_html = require("Module:HtmlUtil")
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 lang = mw.getLanguage('en')

local EntityListAbstract = require('Module:EntityListAbstract')

local p = EntityListAbstract:finalExtends()
local h = {}

p.Entity = require('Module:Role')

function p:init(str, opts)
	if not opts then opts = {} end
	self.opts = opts
	opts.sep = opts.sep or ';'
	self:super('init', str, opts)
	self.modifier = opts.modifier
	if self.is_nil and opts.modifier then
		-- case when we have a modifier but no string (e.g. solely sub or trainee)
		self.objs = { self.Entity(nil, opts) }
		self.is_nil = false
	end
	if self.is_nil then return end

	self.isFiltered = false	
end

function p:names(opts)
	if self.is_nil then return end
	if not opts then opts = {} end
	local opts2 = mw.clone(opts)
	local tbl = {}
	
	for i, obj in ipairs(self) do
		tbl[#tbl+1] = obj:name(opts2)
		
		-- skip the prefix starting with the second item in the list
		-- for example, we want to write Sub/Sup/Assistant Coach
		-- instead of Sub/Sup/Sub/Assistant coach
		
		-- however we'd want to write Assistant Coach/Sub/Sup
		if obj:isIngame() then
			opts2.skip_prefix = true
		end
	end
	return util_table.concat(tbl, opts.sep)
end

function p:sentence()
	if self.is_nil then return end
	local nouns = {}
	local preps = {}
	local phrases = {}
	local prep = self.objs[1]:get('prep')
	
	-- we have to print a preposition next to each word only if
	-- they disagree in the prepositions used, e.g. mid laner for and co-owner of
	-- but mid laner and manager for
	-- so track whether we've changed preposition at all;
	-- if so, then concat and return the phrases
	-- if not then just return the nouns
	local usePreps = false
	for i, obj in ipairs(self) do
		nouns[i] = obj:name{len='sentence'}
		phrases[i] = ('%s %s'):format(obj:name{len='sentence'}, obj:get('prep'))
		if prep ~= obj:get('prep') then
			usePreps = true
		end
	end
	if not usePreps then
		return util_table.printList(nouns) .. ' ' .. prep
	end
	return util_table.printList(phrases)
end

function p:sortnumber()
	if self.is_nil then return end
	return self.objs[1]:sortnumber()
end

function p:ingame()
	return self:getFilterKey('ingame')
end

function p:staff()
	return self:getFilterKey('staff')
end

function p:hasIngame()
	return self:getFilterKey('hasIngame')
end

function p:hasStaff()
	return self:getFilterKey('hasStaff')
end

function p:isIngameOnly()
	return self:getFilterKey('isIngameOnly')
end

function p:isStaffOnly()
	return self:getFilterKey('isStaffOnly')
end

function p:getFilterKey(key)
	if self.isFiltered then return self.filters[key] end
	self:getFilters()
	return self.filters[key]
end

function p:getFilters()
	optsIngame = mw.clone(self.opts)
	optsStaff = mw.clone(self.opts)
	optsStaff.modifier = nil
	optsStaff.trainee = nil
	optsStaff.Trainee = nil
	optsStaff.sub = nil
	optsStaff.Sub = nil
	optsIngame.alreadyCast = true
	optsStaff.alreadyCast = true
	local ingame = p(h.filterIngameRoles(self, true), optsIngame)
	local staff = p(h.filterIngameRoles(self, false), optsStaff)
	self.filters = {
		ingame = ingame,
		staff = staff,
		hasIngame = ingame:exists(),
		hasStaff = staff:exists(),
		isIngameOnly = not staff:exists(),
		isStaffOnly = not ingame:exists(),
	}
	self.isFiltered = true
end

function h.filterIngameRoles(objs, isIngame)
	local filteredRoles = {}
	for _, role in ipairs(objs) do
		if isIngame == role:isIngame() then
			filteredRoles[#filteredRoles+1] = role
		end
	end
	return filteredRoles
end

return p