Modul:MahjongTile

Revisi sejak 12 Oktober 2026 16.50 oleh Root (bicara | kontrib) (←Membuat halaman berisi ' local p = {} local function errorMessage(message) return '<span class="error">MahjongTile: ' .. message .. '</span>' end local function render(args) local tiles = mw.text.trim(args[1] or '') local size = mw.text.trim(args.size or '32') local variant = mw.text.trim(args.variant or 'riichi') :lower() if tiles == '' then return errorMessage('Please supply tile notation.') end if variant ~= 'riichi' and variant...')
(beda) ← Revisi sebelumnya | Revisi terkini (beda) | Revisi selanjutnya → (beda)

Dokumentasi untuk modul ini dapat dibuat di Modul:MahjongTile/doc


local p = {}

local function errorMessage(message)
    return '<span class="error">MahjongTile: ' ..
        message .. '</span>'
end

local function render(args)
    local tiles = mw.text.trim(args[1] or '')
    local size = mw.text.trim(args.size or '32')
    local variant = mw.text.trim(args.variant or 'riichi')
        :lower()

    if tiles == '' then
        return errorMessage('Please supply tile notation.')
    end

    if variant ~= 'riichi' and variant ~= 'mcr' then
        return errorMessage('variant must be riichi or mcr.')
    end

    local width = tonumber(size)
    if not width or width < 12 or width > 120 then
        return errorMessage('size must be 12–120 pixels.')
    end

    tiles = tiles:gsub('%s+', '')

    local output = {}
    local position = 1

    while position <= #tiles do
        local rest = tiles:sub(position)
        local group, suit = rest:match('^(%d+)([mpszf])')

        if not group then
            return errorMessage('Invalid notation near "' ..
                rest .. '".')
        end

        for i = 1, #group do
            local n = tonumber(group:sub(i, i))
            local valid = false

            if suit == 'm' or suit == 'p' or suit == 's' then
                valid = (n >= 1 and n <= 9) or n == 0
            elseif suit == 'z' then
                valid = n >= 1 and n <= 7
            elseif suit == 'f' then
                valid = n >= 1 and n <= 8
            end

            if not valid then
                return errorMessage('Invalid tile number ' ..
                    n .. ' for suit ' .. suit .. '.')
            end

            if suit == 'f' and variant == 'riichi' then
                return errorMessage(
                    'Flower tiles require variant=mcr.')
            end

            local filename = 'Mahjong-' .. n .. suit .. '.svg'
            local label

            if suit == 'z' then
                local honors = {
                    [1] = 'East wind',
                    [2] = 'South wind',
                    [3] = 'West wind',
                    [4] = 'North wind',
                    [5] = 'White dragon',
                    [6] = 'Green dragon',
                    [7] = 'Red dragon'
                }
                label = honors[n]
            elseif suit == 'f' then
                label = 'Flower or season ' .. n
            else
                local names = {
                    m = 'characters',
                    p = 'circles',
                    s = 'bamboo'
                }
                if n == 0 then
                    label = 'Red five, ' .. names[suit]
                else
                    label = n .. ' of ' .. names[suit]
                end
            end

            table.insert(output, '[[File:' .. filename ..
                '|' .. tostring(width) .. 'px|' .. label .. ']]')
        end

        position = position + #group + 1
    end

    return table.concat(output, '&nbsp;')
end

function p.main(frame)
    local args = frame.args
    if frame:getParent() then
        args = frame:getParent().args
    end
    return render(args)
end

return p