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
Line 8: Line 8:
     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 19:
     end
     end
      
      
    -- Fetch up to 5000 records so we can build the complete A-Z list
     local results = cargo.query(queryTable, queryFields, { orderBy = querySort, limit = 5000 })
     local results = cargo.query(queryTable, queryFields, { orderBy = querySort, limit = 5000 })
     if not results or #results == 0 then return "No data found in the database." end
     if not results or #results == 0 then return "No data found in the database." end
Line 28: Line 26:
     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 41:
     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 55: Line 49:
     local root = html.create('div'):addClass('dir-wrapper')
     local root = html.create('div'):addClass('dir-wrapper')
      
      
     -- 1. Build the Top A-Z Index Filter
     -- 1. Build the Top A-Z Index Filter (FIXED: Using Wikitext Links)
     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 58:
     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 65:
              
              
             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 71:
                 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 = {}

Revision as of 07:30, 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

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
    
    local results = cargo.query(queryTable, queryFields, { orderBy = querySort, limit = 5000 })
    if not results or #results == 0 then return "No data found in the database." 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 (FIXED: Using Wikitext Links)
    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