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

-- <nowiki>
local getArgs = require("Dev:Arguments").getArgs
local yesno = require("Dev:Yesno")

local levelTypeData
local p = {}
local function Nth(n)
    local function getSuffix(n)
        local lastTwo, lastOne = n % 100, n % 10
        if lastTwo > 3 and lastTwo < 21 then return "th" end
        if lastOne == 1 then return "st" end
        if lastOne == 2 then return "nd" end
        if lastOne == 3 then return "rd" end
        return "th"
    end
    if tonumber(n) then
        return n .. getSuffix(tonumber(n))
    end
    return n
end

local ordinals = {
    "first",
    "second",
    "third",
    "fourth",
    "fifth",
    "sixth",
    "seventh",
    "eighth",
    "ninth",
    "tenth",
    "eleventh",
    "twelfth",
    "thirteenth",
    "fourteenth",
    "fifteenth",
}
local function nthAndLast(level)
    local max
    local offset = 20
    if level > offset then
        max = 15
    else
        max = 10
        offset = 0
    end
    local n = (level - 1 - offset) % max + 1
    local nth = ordinals[n]
    if n == max then nth = nth .. " and last" end
    return nth
end

-- nthLevelType is counts 
local function nthLevelType(level, isDW)
    if isDW then
        levelTypeData = mw.loadData("Module:LeveltypeDW/List")
    else
        levelTypeData = mw.loadData("Module:Leveltype/List")
    end
    local levelTypes = {
        ["Moves"] = " [[Moves levels|moves level]]",
        ["Jelly"] = " [[Jelly levels|jelly level]]",
        ["Ingredients"] = " [[Ingredients levels|ingredients level]]",
        ["Timed"] = " [[Timed levels|timed level]]",
        ["Candy Order"] = " [[Candy Order levels|candy order level]]",
        ["Mixed"] = " [[Mixed Mode levels|mixed mode level]]",
        ["Rainbow Rapids"] = " [[Rainbow Rapids levels|rainbow rapids level]]",
    }
    local levelType = levelTypeData[level]
    local levelTypeName = levelTypes[levelType]
    local tallies = {}
    for k, v in pairs(levelTypeData) do
        if k <= level then
            tallies[v] = (tallies[v] or 0) + 1
        end
    end
    local tally = tallies[levelType]
    if not levelType or not levelTypeName then return "" end

    local wikitext = {}
    table.insert(wikitext, Nth(tally))
    table.insert(wikitext, levelTypeName)
    if isDW then
        table.insert(wikitext, " in [[Dreamworld]]")
    end

    if levelType == "Mixed" then
        local data = mw.loadData("Module:Mixedorder/List")
        local mixedTypes = {
            ['jelly-order'] = "jelly-order [[Category:Jelly-order levels]]",
            ['jelly-ingredients'] = "jelly-ingredients [[Category:Jelly-ingredients levels]]",
            ['order-ingredients'] = "order-ingredients [[Category:Order-ingredients levels]]",
            ['jelly-rapids'] = "jelly-rapids [[Category:Jelly-rapids levels]]",
            ['rapids-ingredients'] = "rapids-ingredients [[Category:Rapids-ingredients levels]]",
            ['rapids-order'] = "rapids-order [[Category:Rapids-order levels]]",
        }
        local mixedType = data[level]
        local mixedTypeName = mixedTypes[mixedType]
        if mixedType and mixedTypeName then
            local tallies = {}
            for k, v in pairs(data) do
                if k <= level then
                    tallies[v] = (tallies[v] or 0) + 1
                end
            end
            local tally = tallies[mixedType]
            table.insert(wikitext, " (")
            table.insert(wikitext, Nth(tally))
            table.insert(wikitext, " " .. mixedTypeName)
            table.insert(wikitext, ")")
        else
            return "''Unknown mixed type for level "..level.."''[[Category:Levels with missing mixed type|"..level.."]]"
        end
    end
    return table.concat(wikitext)
end

function p.main(frame)
    local args = getArgs(frame, {parentFirst=true})
    local currentTitle = args[1] and mw.title.new(args[1]) or mw.title.getCurrentTitle()
    local PAGENAME = currentTitle.text
    local BASEPAGENAME = currentTitle.baseText
    local SUBPAGENAME = currentTitle.subpageText
    local isDW = SUBPAGENAME == "Dreamworld" or SUBPAGENAME == "Dreamworld (Mobile)"
    local TOTALLEVELSR = tonumber(args.TOTALLEVELSR) or 0
    local WIN10DIF = tonumber(args.WIN10DIF) or 0

    local level = tonumber(string.match(PAGENAME, "(%d+)")) or -1
    local requirement = args.requirement
    local moves = tonumber(args.moves) or 0

    local output = {}
    table.insert(output, "'''"..(title or BASEPAGENAME).."''' ")
    if isDW then
        table.insert(output, "was")
    elseif level > (TOTALLEVELSR + WIN10DIF) then
        table.insert(output, "will be")
    else
        table.insert(output, "is")
    end

    table.insert(output, " the " .. nthAndLast(level) .. " level in ")

    if yesno(args.idea) then
        table.insert(output, args.episode or '')
    else
        local getEpisodeText = require("Module:EpisodeText" .. (isDW and "D" or ""))._main
        local episodeText
        if BASEPAGENAME == "Infobox level" or BASEPAGENAME == "Infobox past level" then
            episodeText = getEpisodeText("E")
        elseif level <= 20 then
            episodeText = getEpisodeText(math.ceil(level / 10))
        else
            episodeText = getEpisodeText(math.ceil((level + 10)/15))
        end
        table.insert(output, "[[" .. (episodeText or "") .. "]]")
    end
    if level > (TOTALLEVELSR + WIN10DIF) then
        table.insert(output, ".")
    else
        table.insert(output, " and the " .. nthLevelType(level, isDW) .. ".")
        if requirement then
            table.insert(output, " To pass this level, you must "..requirement)
            local levelType = levelTypeData and levelTypeData[level]
            if levelType == "Jelly"
            or levelType == "Ingredients"
            or levelType == "Candy Order"
            or levelType == "Mixed"
            or levelType == "Rainbow Rapids" then
                table.insert(output, " in "..moves..(moves == 1 and " [[moves|move]]" or " [[moves]] or fewer"))
            end
            table.insert(output, ".")
        end
        table.insert(output, "\nWhen you complete the level, [[Sugar Crush]] is activated and will score you additional points.")
    end
    return table.concat(output)
end

function p.test()
    local TOTALLEVELSR = 6965
    local WIN10DIF = 90
    mw.log("Level 1746", p.main{"Level 1746", TOTALLEVELSR=TOTALLEVELSR, WIN10DIF=WIN10DIF})
    mw.log("Level 656/Dreamworld", p.main{"Level 656/Dreamworld", TOTALLEVELSR=TOTALLEVELSR, WIN10DIF=WIN10DIF})

    mw.log(656, nthLevelType(656, false))
    mw.log(656, nthLevelType(656, true))
end

return p
-- </nowiki>
Community content is available under CC-BY-SA unless otherwise noted.