Module:RandomItemSequence: Difference between revisions
Faintfulness (talk | contribs) No edit summary |
Faintfulness (talk | contribs) No edit summary |
||
| Line 32: | Line 32: | ||
shuffle(items) | shuffle(items) | ||
-- Generate | -- Generate a dummy unique value to force MediaWiki to treat each template expansion as distinct | ||
local | local unique = tostring(os.clock() + Random.random(1e6)) | ||
return table.concat( | return frame:expandTemplate{ | ||
title = "ItemSlot", | |||
args = { | |||
item = table.concat(items, "; "), | |||
tooltip = tooltip, | |||
link = link, | |||
_dummy = unique -- ignored by ItemSlot, only for cache-busting | |||
} | |||
} | |||
end | end | ||
return p | return p | ||
Revision as of 14:22, 28 December 2025
Documentation for this module may be created at Module:RandomItemSequence/doc
local p = {}
local Random = require('Module:Random')
-- Seed once per page parse
Random.seed(os.time())
-- Fisher–Yates shuffle
local function shuffle(t)
for i = #t, 2, -1 do
local j = Random.random(i)
t[i], t[j] = t[j], t[i]
end
end
-- render function
function p.render(frame)
local args = frame:getParent().args
local itemsParam = args.items or ""
local tooltip = args.tooltip or ""
local link = args.link or ""
local items = {}
for item in string.gmatch(itemsParam, '([^;]+)') do
item = item:gsub("^%s*(.-)%s*$", "%1")
table.insert(items, item)
end
if #items == 0 then
return ""
end
shuffle(items)
-- Generate a dummy unique value to force MediaWiki to treat each template expansion as distinct
local unique = tostring(os.clock() + Random.random(1e6))
return frame:expandTemplate{
title = "ItemSlot",
args = {
item = table.concat(items, "; "),
tooltip = tooltip,
link = link,
_dummy = unique -- ignored by ItemSlot, only for cache-busting
}
}
end
return p