Module:RandomItemSequence: Difference between revisions

(Lua script for randomized flavor seasoning template)
 
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 2: Line 2:
local Random = require('Module:Random')
local Random = require('Module:Random')


local berries = {
-- Fisher–Yates shuffle
     "Cheri Berry",
local function shuffle(t)
    "Aspear Berry",
     for i = #t, 2, -1 do
    "Chesto Berry",
        local j = Random.random(i)
    "Rawst Berry",
        t[i], t[j] = t[j], t[i]
    "Pecha Berry",
     end
    "Razz Berry",
end
    "Pinap Berry",
    "Bluk Berry",
    "Wepear Berry",
    "Nanab Berry",
    "Oran Berry",
    "Persim Berry",
    "Leppa Berry",
    "Lum Berry",
    "Sitrus Berry",
    "Eggant Berry",
    "Hopo Berry",
    "Figy Berry",
    "Iapapa Berry",
    "Wiki Berry",
    "Aguav Berry",
    "Mago Berry",
    "Touga Berry",
    "Nomel Berry",
    "Cornn Berry",
    "Rabuta Berry",
    "Magost Berry",
    "Spelon Berry",
    "Belue Berry",
    "Pamtre Berry",
    "Durin Berry",
    "Watmel Berry",
    "Pomeg Berry",
    "Kelpsy Berry",
    "Qualot Berry",
    "Hondew Berry",
    "Grepa Berry",
    "Tamato Berry",
    "Occa Berry",
    "Yache Berry",
    "Passho Berry",
    "Rindo Berry",
    "Shuca Berry",
    "Chople Berry",
    "Payapa Berry",
    "Kebia Berry",
    "Coba Berry",
    "Wacan Berry",
    "Tanga Berry",
    "Colbur Berry",
    "Charti Berry",
    "Haban Berry",
    "Kasib Berry",
    "Babiri Berry",
    "Chilan Berry",
    "Roseli Berry",
    "Liechi Berry",
    "Apicot Berry",
    "Ganlon Berry",
    "Petaya Berry",
    "Salac Berry",
    "Lansat Berry",
    "Starf Berry",
    "Kee Berry",
    "Maranga Berry",
    "Enigma Berry",
    "Rowap Berry",
    "Micle Berry",
    "Jaboca Berry",
     "Custap Berry"
}


function p.slot(frame)
function p.render(frame)
local berry = berries[ Random.random(#berries) ]
    local args = frame:getParent().args
return string.format(
    local itemsParam = args.items or ""
"{{ItemSlot|item=%s|tooltip=Any Flavor Seasoning}}",
    local tooltip = args.tooltip or ""
berry
    local link = args.link or ""
)
 
    -- Only reseed if the editor provided a valid numeric seed
    local seed = tonumber(args.reseed)
    if seed then
        Random.seed(seed)
    end
 
    local items = {}
    for item in string.gmatch(itemsParam, '([^;]+)') do
        item = item:gsub("^%s*(.-)%s*$", "%1") -- trim spaces
        table.insert(items, item)
    end
 
    if #items == 0 then
        return ""
    end
 
    shuffle(items)
 
    return frame:expandTemplate{
        title = "ItemSlot",
        args = {
            item = table.concat(items, "; "),
            tooltip = tooltip,
            link = link
        }
    }
end
end


return p
return p

Latest revision as of 19:01, 28 December 2025

Documentation for this module may be created at Module:RandomItemSequence/doc

local p = {}
local Random = require('Module:Random')

-- 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

function p.render(frame)
    local args = frame:getParent().args
    local itemsParam = args.items or ""
    local tooltip = args.tooltip or ""
    local link = args.link or ""

    -- Only reseed if the editor provided a valid numeric seed
    local seed = tonumber(args.reseed)
    if seed then
        Random.seed(seed)
    end

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

    if #items == 0 then
        return ""
    end

    shuffle(items)

    return frame:expandTemplate{
        title = "ItemSlot",
        args = {
            item = table.concat(items, "; "),
            tooltip = tooltip,
            link = link
        }
    }
end

return p