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

Module:DidYouKnow

Jump to navigation Jump to search

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 factTable = require("Module:DidYouKnow/facts")

local function getAllFacts(factTable, factsType)
	local facts = factTable.facts
	if factsType == 'dungeons' then
		facts = factTable.dungeons
	elseif factsType == 'legends' then
		facts = factTable.legends
	elseif factsType == 'movie' then
		facts = factTable.movie
	elseif factsType == 'storymode' then
		facts = factTable.storymode
	elseif factsType == 'earth' then
		facts = factTable.earth
	end
	return facts
end

local function formatFact(factText)
	return "* ... that " .. factText .. "?"
end

function p.getFacts(frame)
	local facts = getAllFacts(factTable, frame:getParent().args['type'] or 'minecraft')
	local s = ""
	local length = #facts
	local count = math.min(length, tonumber(frame:getParent().args['count']) or 8)
	if frame:getParent().args['random'] then
		math.randomseed(os.time())
	else
		-- 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))*2) -- *2 so that we will have a different set of facts compared to Fandom
	end
	
	-- Ensure duplicate facts are not displayed
	local chosen = {}
	while #chosen < count 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] = formatFact(facts[v])
	end
	return table.concat(result, "\n")
end

local function formatFactList(facts)
	local length = #facts
	local result = {}
	
	for i = 1, length do
		result[i] = formatFact(facts[i])
	end
	
	return table.concat(result, "\n")
end

-- List all facts. Intended to allow easier debugging.
function p.listAllFacts(frame)
	local facts = getAllFacts(factTable, frame.args['type'] or 'minecraft')
	return formatFactList(facts)
end

function p.listEditcopyFacts(frame)
	local editcopyFacts = require("Module:DidYouKnow/facts/editcopy")
	local facts = getAllFacts(editcopyFacts, frame.args['type'] or 'minecraft')
	return formatFactList(facts)
end

return p