Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

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

local p = {}
local cargo = mw.ext.cargo
local html = mw.html

local function sqlEscape(s)
    if not s then return "" end
    return s:gsub("\\", "\\\\"):gsub("'", "\\'")
end

function p.main(frame)
    local args = frame:getParent().args
    if not args.type then args = frame.args end
    local dirType = (args.type or "player"):lower()
    
    local queryTable, queryFields, querySort
    if dirType == "team" then
        queryTable = "Teams"
        queryFields = "_pageName=PageName, name, country"
        querySort = "name ASC"
    else
        queryTable = "Players"
        queryFields = "_pageName=PageName, id, real_name, current_team"
        querySort = "id ASC"
    end
    
    -- Add dynamic filtering for Game and Status
    local whereParts = {}
    if args.game and args.game ~= "" then
        table.insert(whereParts, "game = '" .. sqlEscape(args.game) .. "'")
    end
    if args.status and args.status ~= "" then
        table.insert(whereParts, "status = '" .. sqlEscape(args.status) .. "'")
    end
    local whereClause = #whereParts > 0 and table.concat(whereParts, " AND ") or "1=1"
    
    local results = cargo.query(queryTable, queryFields, { where = whereClause, orderBy = querySort, limit = 5000 })
    if not results or #results == 0 then return "<div style='padding:20px; color:var(--text-muted); font-style:italic;'>No data found for this selection.</div>" end
    
    local groups = {}
    local letters = {}
    local letterSet = {}
    
    for _, r in ipairs(results) do
        local sortName = (dirType == "team") and r.name or r.id
        if sortName and sortName ~= "" then
            local firstChar = mw.ustring.upper(mw.ustring.sub(sortName, 1, 1))
            if not firstChar:match("%a") then firstChar = "#" end
            
            if not letterSet[firstChar] then
                letterSet[firstChar] = true
                table.insert(letters, firstChar)
                groups[firstChar] = {}
            end
            table.insert(groups[firstChar], r)
        end
    end
    
    table.sort(letters, function(a, b)
        if a == "#" then return true end
        if b == "#" then return false end
        return a < b
    end)
    
    local root = html.create('div'):addClass('dir-wrapper')
    
    -- 1. Build the Top A-Z Index Filter
    local topBar = root:tag('div'):addClass('dir-index')
    for _, l in ipairs(letters) do
        topBar:tag('span'):addClass('dir-letter-btn'):wikitext('[[#dir-letter-' .. l .. '|' .. l .. ']]')
    end
    
    -- 2. Build the Alphabetical Lists
    local content = root:tag('div'):addClass('dir-content')
    for _, l in ipairs(letters) do
        content:tag('h3'):attr('id', 'dir-letter-' .. l):addClass('dir-section-title'):wikitext(l)
        
        local ul = content:tag('ul'):addClass('dir-list')
        for _, r in ipairs(groups[l]) do
            local li = ul:tag('li')
            
            if dirType == "team" then
                local text = "'''[[" .. r.PageName .. "|" .. r.name .. "]]'''"
                if r.country and r.country ~= "" then 
                    text = text .. " <span class='dir-muted'>(" .. r.country .. ")</span>" 
                end
                li:wikitext(text)
            else
                local text = "'''[[" .. r.PageName .. "|" .. r.id .. "]]'''"
                local meta = {}
                if r.real_name and r.real_name ~= "" then table.insert(meta, r.real_name) end
                if r.current_team and r.current_team ~= "" then table.insert(meta, "''[[" .. r.current_team .. "]]''") end
                
                if #meta > 0 then 
                    text = text .. " <span class='dir-muted'>- " .. table.concat(meta, " / ") .. "</span>" 
                end
                li:wikitext(text)
            end
        end
    end
    
    return tostring(root)
end

return p