Module:Random

From Infernum Mod Wiki
Jump to navigation Jump to search

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

local p = {}

local trim = mw.text.trim

local function convertListToArray(list, separator)
	local result = {}
	local text = mw.text.split(list, separator)
	
	for i = 1, #text, 1 do
		text[i] = trim(text[i])
		if not (text[i] == "" or not text[i]) then
			result[#result + 1] = text[i]
		end
	end
	
	return result
end

-- for modules
function p.go(args)
	if args['list'] and trim(args['list']) ~= '' then
		args = convertListToArray(args['list'], ',')
	end
	math.randomseed(os.time())
	local tableWithOptions = {}
	
	for k, v in pairs(args) do
		if type(k) == 'number' and type(v) == 'string' and trim(v) ~= '' then
			tableWithOptions[#tableWithOptions + 1] = trim(v)
		end
	end
	
	return tableWithOptions[math.random(#tableWithOptions)]
end

-- for the template
function p._random(frame)
	args = frame:getParent().args
	return p.go(args)
end

return p