Jump to content

📢 Please make sure to read our Style Guide before editing. For questions, suggestions, or technical issues, please contact Noah.

Module:Infobox Player: Difference between revisions

From CrabCraft Wiki
m fixed categories
store categories in lua script
Line 1: Line 1:
local infobox = require('Module:Infobox')
local p = {}
local p = {}


local function humanLabel(name)
local function humanLabel(name)
     return mw.ustring.gsub(mw.ustring.upper(mw.ustring.sub(name,1,1)) .. mw.ustring.sub(name,2), "%-", " ")
     local s = mw.ustring.gsub(name, "%-", " ")
    return mw.ustring.upper(mw.ustring.sub(s,1,1)) .. mw.ustring.sub(s,2)
end
end
local sections = {
    ["Basic Information"] = {
        "season-joined",
        "nationality",
        "discord",
    },
}


function p.infobox(frame)
function p.infobox(frame)
     local args     = frame:getParent().args
     local args = frame:getParent().args
     local outArgs  = { title = args.username, category1 = args.category1 }
     local used = {} -- track which keys we’ve output
     local count    = 0
    local out = {}
 
    -- start the table
    table.insert(out, '{| class="infobox" style="width:250px;"')
    -- title row
    if args.username and args.username ~= "" then
        table.insert(out, '|-\n! colspan="2" style="font-size:120%; background:#ccc;" | ' .. args.username)
    end
 
    -- helper to output a section header
    local function addHeader(text)
        table.insert(out, '|-\n! colspan="2" style="background:#F4BB44;" | ' .. text)
    end
    -- helper to output one row
    local function addRow(key)
        local val = args[key]
        if val and val ~= "" then
            table.insert(out, '|-\n| ' .. humanLabel(key) .. ' || ' .. val)
            used[key] = true
        end
    end
 
    -- first, emit all fixed sections in order
     for section, keys in pairs(sections) do
        -- check if any of those keys exist
        local has = false
        for _, k in ipairs(keys) do
            if args[k] and args[k] ~= "" then
                has = true; break
            end
        end
        if has then
            addHeader(section)
            for _, k in ipairs(keys) do
                addRow(k)
            end
        end
    end


    -- gather anything the user passed that’s not in 'sections' or 'username'
    local extra = {}
     for key, val in pairs(args) do
     for key, val in pairs(args) do
         if key ~= 'username' and val and val ~= '' and key ~= 'category1' then
         if key ~= 'username' and val and val ~= '' and not used[key] then
             count = count + 1
             table.insert(extra, key)
            outArgs['label'..count] = humanLabel(key)
        end
             outArgs['data' ..count] = val
    end
 
    if #extra > 0 then
        addHeader("Additional Data")
        table.sort(extra)
        for _, k in ipairs(extra) do
             addRow(k)
         end
         end
     end
     end


     outArgs.rows = count
     -- close up
     outArgs.categories = 1
     table.insert(out, '|}')
     return infobox.infobox{
     return table.concat(out, "\n")
        getParent = function() return { args = outArgs } end
    }
end
end


return p
return p

Revision as of 18:11, 21 June 2025

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

local p = {}

local function humanLabel(name)
    local s = mw.ustring.gsub(name, "%-", " ")
    return mw.ustring.upper(mw.ustring.sub(s,1,1)) .. mw.ustring.sub(s,2)
end

local sections = {
    ["Basic Information"] = {
        "season-joined",
        "nationality",
        "discord",
    },
}

function p.infobox(frame)
    local args = frame:getParent().args
    local used = {} -- track which keys we’ve output
    local out = {}

    -- start the table
    table.insert(out, '{| class="infobox" style="width:250px;"')
    -- title row
    if args.username and args.username ~= "" then
        table.insert(out, '|-\n! colspan="2" style="font-size:120%; background:#ccc;" | ' .. args.username)
    end

    -- helper to output a section header
    local function addHeader(text)
        table.insert(out, '|-\n! colspan="2" style="background:#F4BB44;" | ' .. text)
    end
    -- helper to output one row
    local function addRow(key)
        local val = args[key]
        if val and val ~= "" then
            table.insert(out, '|-\n| ' .. humanLabel(key) .. ' || ' .. val)
            used[key] = true
        end
    end

    -- first, emit all fixed sections in order
    for section, keys in pairs(sections) do
        -- check if any of those keys exist
        local has = false
        for _, k in ipairs(keys) do
            if args[k] and args[k] ~= "" then
                has = true; break
            end
        end
        if has then
            addHeader(section)
            for _, k in ipairs(keys) do
                addRow(k)
            end
        end
    end

    -- gather anything the user passed that’s not in 'sections' or 'username'
    local extra = {}
    for key, val in pairs(args) do
        if key ~= 'username' and val and val ~= '' and not used[key] then
            table.insert(extra, key)
        end
    end

    if #extra > 0 then
        addHeader("Additional Data")
        table.sort(extra)
        for _, k in ipairs(extra) do
            addRow(k)
        end
    end

    -- close up
    table.insert(out, '|}')
    return table.concat(out, "\n")
end

return p