This page is directors-only protected so that only directors can edit it.

Module:DidYouKnow: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(forgot to uncomment a line commented for debugging)
(explanatory comment on the random seed bit)
Line 5: Line 5:
local s = ""
local s = ""
local length = #facts.facts
local length = #facts.facts
-- Different random seed every day (displays the same set of facts for a day, then a different set the next day, etc.)
math.randomseed(math.floor(os.time()/(60*60*24)))
math.randomseed(math.floor(os.time()/(60*60*24)))

Revision as of 11:06, 26 December 2018

Implements {{DidYouKnow}}. All individual hooks are stored and can be added at Module:DidYouKnow/facts.

[view] [edit] [history] [refresh]The above documentation is transcluded from Module:DidYouKnow/doc.
local p = {}
local facts = require("Module:DidYouKnow/facts")

function p.getFacts()
	local s = ""
	local length = #facts.facts
	-- Different random seed every day (displays the same set of facts for a day, then a different set the next day, etc.)
	math.randomseed(math.floor(os.time()/(60*60*24)))
	
	-- Ensure duplicate facts are not displayed
	local chosen = {}
	while #chosen < 5 do
		local random = math.random(1, length)
		local isPresent = false
		
		for i, v in ipairs(chosen) do
			if v == random then
				isPresent = true
				break
			end
		end
		
		if not isPresent then
			table.insert(chosen, random)
		end
	end
	
	-- Now we actually get to write the output.
	local result = {}
	for i, v in ipairs(chosen) do
		result[i] = "* ... that " .. facts.facts[v] .. "?"
	end
	return table.concat(result, "\n")
end

-- List all facts. Intended to allow easier debugging.
function p.getAllFacts()
	local result = {}
	local length = #facts.facts
	
	for i = 1, length do
		result[i] = "* ... that " .. facts.facts[i] .. "?"
	end
	return table.concat(result, "\n")
end

return p