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

Module:Directory: Difference between revisions

From eSportsAmaze
Created page with "local p = {} local cargo = mw.ext.cargo local html = mw.html 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() -- Setup the query based on the type local queryTable, queryFields, querySort if dirType == "team" then queryTable = "Teams" queryFields = "_pageName=PageName, name, country" querySort = "name ASC" else..."
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
local cargo = mw.ext.cargo
local cargo = mw.ext.cargo
local html = mw.html
local html = mw.html
local function sqlEscape(s)
    if not s then return "" end
    return s:gsub("\\", "\\\\"):gsub("'", "\\'")
end


function p.main(frame)
function p.main(frame)
Line 8: Line 13:
     local dirType = (args.type or "player"):lower()
     local dirType = (args.type or "player"):lower()
      
      
    -- Setup the query based on the type
     local queryTable, queryFields, querySort
     local queryTable, queryFields, querySort
     if dirType == "team" then
     if dirType == "team" then
Line 20: Line 24:
     end
     end
      
      
     -- Fetch up to 5000 records so we can build the complete A-Z list
     -- Add dynamic filtering for Game and Status
     local results = cargo.query(queryTable, queryFields, { orderBy = querySort, limit = 5000 })
    local whereParts = {}
     if not results or #results == 0 then return "No data found in the database." end
    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 groups = {}
Line 28: Line 41:
     local letterSet = {}
     local letterSet = {}
      
      
    -- Group results by their first letter
     for _, r in ipairs(results) do
     for _, r in ipairs(results) do
         local sortName = (dirType == "team") and r.name or r.id
         local sortName = (dirType == "team") and r.name or r.id
         if sortName and sortName ~= "" then
         if sortName and sortName ~= "" then
            -- Get first character and capitalize it
             local firstChar = mw.ustring.upper(mw.ustring.sub(sortName, 1, 1))
             local firstChar = mw.ustring.upper(mw.ustring.sub(sortName, 1, 1))
            -- If it's a number or symbol, group it under "#"
             if not firstChar:match("%a") then firstChar = "#" end
             if not firstChar:match("%a") then firstChar = "#" end
              
              
Line 46: Line 56:
     end
     end
      
      
    -- Sort the letters alphabetically, forcing "#" to the very front
     table.sort(letters, function(a, b)
     table.sort(letters, function(a, b)
         if a == "#" then return true end
         if a == "#" then return true end
Line 58: Line 67:
     local topBar = root:tag('div'):addClass('dir-index')
     local topBar = root:tag('div'):addClass('dir-index')
     for _, l in ipairs(letters) do
     for _, l in ipairs(letters) do
         topBar:tag('a'):attr('href', '#dir-letter-' .. l):addClass('dir-letter-btn'):wikitext(l)
         topBar:tag('span'):addClass('dir-letter-btn'):wikitext('[[#dir-letter-' .. l .. '|' .. l .. ']]')
     end
     end
      
      
Line 64: Line 73:
     local content = root:tag('div'):addClass('dir-content')
     local content = root:tag('div'):addClass('dir-content')
     for _, l in ipairs(letters) do
     for _, l in ipairs(letters) do
        -- Section Header (A, B, C...)
         content:tag('h3'):attr('id', 'dir-letter-' .. l):addClass('dir-section-title'):wikitext(l)
         content:tag('h3'):attr('id', 'dir-letter-' .. l):addClass('dir-section-title'):wikitext(l)
          
          
Line 72: Line 80:
              
              
             if dirType == "team" then
             if dirType == "team" then
                -- TEAM FORMAT: TeamName (Country)
                 local text = "'''[[" .. r.PageName .. "|" .. r.name .. "]]'''"
                 local text = "'''[[" .. r.PageName .. "|" .. r.name .. "]]'''"
                 if r.country and r.country ~= "" then  
                 if r.country and r.country ~= "" then  
Line 79: Line 86:
                 li:wikitext(text)
                 li:wikitext(text)
             else
             else
                -- PLAYER FORMAT: PlayerName - Real Name / Team
                 local text = "'''[[" .. r.PageName .. "|" .. r.id .. "]]'''"
                 local text = "'''[[" .. r.PageName .. "|" .. r.id .. "]]'''"
                 local meta = {}
                 local meta = {}

Latest revision as of 07:39, 21 May 2026

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