Module:RandomItemSequence: Difference between revisions

(Removed reseeding flag as it was causing a bug)
No edit summary
Line 13: Line 13:
end
end


-- render function with optional reseeding
-- render function
-- frame: standard MediaWiki frame
function p.render(frame)
function p.render(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local itemsParam = args.items or ""
     local itemsParam = args.items or ""
Line 34: Line 32:
     shuffle(items)
     shuffle(items)


     return frame:expandTemplate{
     -- Generate wikitext directly instead of expandTemplate
        title = "ItemSlot",
    local output = {}
         args = {
    for _, item in ipairs(items) do
            item = table.concat(items, "; "),
         table.insert(output, string.format("{{ItemSlot|item=%s|tooltip=%s|link=%s}}", item, tooltip, link))
            tooltip = tooltip,
    end
            link = link
 
        }
     return table.concat(output, "\n")
     }
end
end


return p
return p

Revision as of 14:18, 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 wikitext directly instead of expandTemplate
    local output = {}
    for _, item in ipairs(items) do
        table.insert(output, string.format("{{ItemSlot|item=%s|tooltip=%s|link=%s}}", item, tooltip, link))
    end

    return table.concat(output, "\n")
end

return p