Module:DidYouKnow/sandbox: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
Dinoguy1000 (talk | contribs) (isPresent variable isn't needed) |
Dinoguy1000 (talk | contribs) (this variable is never used) |
||
Line 17: | Line 17: | ||
local random = math.random(1, length) |
local random = math.random(1, length) |
||
for |
for _, v in ipairs(chosen) do |
||
if v == random then |
if v == random then |
||
table.insert(chosen, random) |
table.insert(chosen, random) |
Latest revision as of 14:55, 1 January 2024
local p = {}
local facts = require("Module:DidYouKnow/facts")
local function formatFact(factText)
return "* ... that " .. factText .. "?"
end
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))*2) -- *2 so that we will have a different set of facts compared to Fandom
-- Ensure duplicate facts are not displayed
local chosen = {}
while #chosen < 8 do
local random = math.random(1, length)
for _, v in ipairs(chosen) do
if v == random then
table.insert(chosen, random)
break
end
end
end
-- Now we actually get to write the output.
local result = {}
for i, v in ipairs(chosen) do
result[i] = formatFact(facts.facts[v])
end
return table.concat(result, "\n")
end
local function formatFactList(factTable)
local facts = factTable.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()
return formatFactList(facts)
end
function p.listEditcopyFacts()
local editcopyFacts = require("Module:DidYouKnow/facts/editcopy")
return formatFactList(editcopyFacts)
end
return p