Module:DidYouKnow: Difference between revisions

no edit summary
No edit summary
No edit summary
 
Line 3: Line 3:
-- CONFIG
-- CONFIG
local DATA_PAGE = "Template:Did you know/Data"
local DATA_PAGE = "Template:Did you know/Data"
local DEFAULT_COUNT = 5
local MAX_COUNT = 15
-- Simple deterministic shuffle
local function shuffle(t)
    for i = #t, 2, -1 do
        local j = math.random(i)
        t[i], t[j] = t[j], t[i]
    end
end


function p.main(frame)
function p.main(frame)
    local args = frame.args
    local count = tonumber(args.count) or DEFAULT_COUNT
    if count > MAX_COUNT then
        count = MAX_COUNT
    end
     local title = mw.title.new(DATA_PAGE)
     local title = mw.title.new(DATA_PAGE)
     if not title or not title.exists then
     if not title or not title.exists then
Line 29: Line 45:
     end
     end


     -- Safe random seed
     -- Daily seed (YYYYMMDD)
     math.randomseed(os.time())
    local today = os.date("%Y%m%d")
     math.random() -- throw away first value
     math.randomseed(tonumber(today))
     math.random() -- discard first value
 
    -- Shuffle and select
    shuffle(items)
 
    if count > #items then
        count = #items
    end
 
    -- Build output list
    local out = {}
    table.insert(out, "<ul class='did-you-know-list'>")
 
    for i = 1, count do
        table.insert(out, "<li>" .. items[i] .. "</li>")
    end
 
    table.insert(out, "</ul>")


     local choice = items[math.random(#items)]
     return table.concat(out, "\n")
    return choice
end
end


return p
return p