Module:RandomItemSequence: Difference between revisions

No edit summary
No edit summary
Line 14: Line 14:


-- render function
-- render function
-- `frame` is the standard MediaWiki frame object
-- `args.items` is a semicolon-separated list of item names
function p.render(frame)
function p.render(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
Line 20: Line 22:
     local link = args.link or ""
     local link = args.link or ""


    -- Split items
     local items = {}
     local items = {}
     for item in string.gmatch(itemsParam, '([^;]+)') do
     for item in string.gmatch(itemsParam, "([^;]+)") do
         item = item:gsub("^%s*(.-)%s*$", "%1")
         item = item:gsub("^%s*(.-)%s*$", "%1") -- trim whitespace
         table.insert(items, item)
         table.insert(items, item)
     end
     end
Line 30: Line 33:
     end
     end


    -- Shuffle items
     shuffle(items)
     shuffle(items)


     -- Generate a dummy unique value to force MediaWiki to treat each template expansion as distinct
     -- Render each ItemSlot individually via expandTemplate
     local unique = tostring(os.clock() + Random.random(1e6))
    -- This is one call per item, fully processed by MediaWiki
     local output = {}
    for _, item in ipairs(items) do
        table.insert(output, frame:expandTemplate{
            title = "ItemSlot",
            args = {
                item = item,
                tooltip = tooltip,
                link = link
            }
        })
    end


     return frame:expandTemplate{
    -- Concatenate all ItemSlots into one HTML block
        title = "ItemSlot",
     return table.concat(output, "\n")
        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:30, 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
-- `frame` is the standard MediaWiki frame object
-- `args.items` is a semicolon-separated list of item names
function p.render(frame)
    local args = frame:getParent().args
    local itemsParam = args.items or ""
    local tooltip = args.tooltip or ""
    local link = args.link or ""

    -- Split items
    local items = {}
    for item in string.gmatch(itemsParam, "([^;]+)") do
        item = item:gsub("^%s*(.-)%s*$", "%1") -- trim whitespace
        table.insert(items, item)
    end

    if #items == 0 then
        return ""
    end

    -- Shuffle items
    shuffle(items)

    -- Render each ItemSlot individually via expandTemplate
    -- This is one call per item, fully processed by MediaWiki
    local output = {}
    for _, item in ipairs(items) do
        table.insert(output, frame:expandTemplate{
            title = "ItemSlot",
            args = {
                item = item,
                tooltip = tooltip,
                link = link
            }
        })
    end

    -- Concatenate all ItemSlots into one HTML block
    return table.concat(output, "\n")
end

return p