Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 06:21, 27 January 2026 by Esportsamaze (talk | contribs)

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

local p = {}
local html = mw.html
local lang = mw.getContentLanguage()

-- ============================================================
-- HELPER FUNCTIONS
-- ============================================================

-- Helper: Currency Formatter (Indian Format)
local function formatCurrency(amount)
    if not amount or amount == "" or amount == "0" then return "TBD" end
    
    -- Check for currency symbol ($ or ₹)
    local currency = "₹" 
    if string.find(amount, "%$") then currency = "$" end
    
    -- Clean string
    local n = tostring(amount):gsub(",", ""):gsub("₹", ""):gsub("%$", "")
    
    -- Format Logic
    if #n <= 3 then return currency .. " " .. n end
    
    local last3 = n:sub(-3)
    local rest = n:sub(1, -4)
    local formattedRest = rest:reverse():gsub("(%d%d)", "%1,"):reverse()
    if formattedRest:sub(1, 1) == "," then formattedRest = formattedRest:sub(2) end
    
    return '<span class="pz-prize">' .. currency .. " " .. formattedRest .. "," .. last3 .. '</span>'
end

-- Helper: Get Clean Title (Removes "BGMI/Tournaments/" path)
local function getCleanTitle(pageLink)
    if not pageLink then return "" end
    local title = mw.title.new(pageLink)
    if title then 
        return title.subpageText -- Returns only text after the last "/"
    end
    return pageLink
end

-- Helper: Logo Logic (Light/Dark/Shield)
local function getLogo(pageName, image, imageDark)
    local lightFile = (image ~= "" and image) or (pageName .. '.png')
    local darkFile = (imageDark ~= "" and imageDark) or (pageName .. '_dark.png')
    
    local hasLight = mw.title.new('File:' .. lightFile).exists
    local hasDark = mw.title.new('File:' .. darkFile).exists
    
    local container = html.create('div'):addClass('infobox-image')
    
    -- Light Mode
    local lDiv = container:tag('div'):addClass('logo-lightmode')
    if hasLight then
        lDiv:wikitext('[[File:' .. lightFile .. '|200px]]')
    else
        lDiv:wikitext('[[File:Shield_team.png|200px]]')
    end
    
    -- Dark Mode
    local dDiv = container:tag('div'):addClass('logo-darkmode')
    if hasDark then
        dDiv:wikitext('[[File:' .. darkFile .. '|200px]]')
    elseif hasLight then
        dDiv:wikitext('[[File:' .. lightFile .. '|200px]]')
    else
        dDiv:wikitext('[[File:Shield_team_dark.png|200px]]')
    end
    
    return container
end

-- Helper: Small Logo for Lists
local function getSmallLogo(teamName)
    if not teamName or teamName == "" then return "" end
    local clean = teamName:gsub("'", "")
    local light = clean .. '.png'
    local dark = clean .. '_dark.png'
    
    local container = html.create('span'):addClass('tr-logo')
    
    if mw.title.new('File:' .. light).exists then
        container:wikitext('[[File:' .. light .. '|link=|class=logo-lightmode]]')
    else
        container:wikitext('[[File:Shield_team.png|link=|class=logo-lightmode]]')
    end
    
    if mw.title.new('File:' .. dark).exists then
        container:wikitext('[[File:' .. dark .. '|link=|class=logo-darkmode]]')
    elseif mw.title.new('File:' .. light).exists then
        container:wikitext('[[File:' .. light .. '|link=|class=logo-darkmode]]')
    else
        container:wikitext('[[File:Shield_team_dark.png|link=|class=logo-darkmode]]')
    end
    return tostring(container)
end

-- Helper: Date Ranger (Jan 20 – Jan 25, 2026)
local function formatDateRange(startStr, endStr)
    if not startStr or startStr == "" then return "TBA" end
    
    -- If there is an end date
    if endStr and endStr ~= "" and endStr ~= startStr then
        local s = lang:formatDate('d M', startStr)
        local e = lang:formatDate('d M, Y', endStr)
        return s .. " –<br>" .. e -- <br> helps mobile layout
    else
        -- Single date only
        return lang:formatDate('d M, Y', startStr)
    end
end

-- Helper: Add Infobox Row
local function addRow(container, label, value)
    if value and value ~= "" then
        container:tag('div'):addClass('infobox-row')
            :tag('div'):addClass('infobox-label'):wikitext(label):done()
            :tag('div'):addClass('infobox-data'):wikitext(value):done()
    end
end

-- ============================================================
-- MAIN 1: TOURNAMENT INFOBOX (Preserved)
-- ============================================================
function p.infobox(frame)
    local args = frame:getParent().args
    local page = args.name or mw.title.getCurrentTitle().text
    local cleanName = mw.title.getCurrentTitle().subpageText
    
    local root = html.create('div'):addClass('infobox')
    
    -- Header
    root:tag('div'):addClass('infobox-header'):wikitext(args.name or cleanName)
    
    -- Logo
    root:node(getLogo(page, args.image, args.image_dark))
    
    -- Data Container
    local data = root:tag('div'):addClass('infobox-data-container')
    
    if args.series then
        addRow(data, "Series", '[[' .. args.series .. ']]')
    end
    
    addRow(data, "Organizer", args.organizer)
    addRow(data, "Type", args.type)
    addRow(data, "Location", args.location)
    
    -- Use formatting but strip the span for infobox text compatibility if needed
    -- or just pass raw if you prefer plain text. Using formatted here:
    if args.prizepool then
         addRow(data, "Prize Pool", formatCurrency(args.prizepool))
    end
    
    addRow(data, "Start Date", args.start_date)
    addRow(data, "End Date", args.end_date)
    
    if args.winner then
        addRow(data, "Winner", "'''[[" .. args.winner .. "]]'''")
    end

    return tostring(root)
end

-- ============================================================
-- MAIN 2: LIST ROW (Tournaments Page) - NEW CARD DESIGN
-- ============================================================
function p.listRow(frame)
    local args = frame.args -- Arguments passed via #invoke
    local page = args.Page or ""
    local name = getCleanTitle(page)
    local startDate = args.start_date or ""
    local endDate = args.end_date or ""
    
    -- Container (Div instead of Tr)
    local row = html.create('div'):addClass('tourney-row')
    
    -- 1. Date Column
    row:tag('div'):addClass('tr-date')
       :wikitext(formatDateRange(startDate, endDate))
        
    -- 2. Info Column (Name + Organizer)
    local info = row:tag('div'):addClass('tr-info')
    info:tag('div'):addClass('tr-name'):wikitext('[[' .. page .. '|' .. name .. ']]')
    
    if args.organizer then
        info:tag('div'):addClass('tr-org'):wikitext(args.organizer)
    end
        
    -- 3. Winner Column
    local winDiv = row:tag('div'):addClass('tr-winner mobile-hide')
    if args.winner and args.winner ~= "" then
        winDiv:wikitext(getSmallLogo(args.winner))
        winDiv:tag('span'):wikitext(args.winner)
    else
        winDiv:tag('span'):addClass('dim-text'):wikitext('-')
    end
        
    -- 4. Prize Pool Column
    local prizeDiv = row:tag('div'):addClass('tr-prize')
    prizeDiv:wikitext(formatCurrency(args.prize_pool))
        
    return tostring(row)
end

-- ============================================================
-- MAIN 3: LIST ROW MAIN (Homepage) - COMPACT CARD
-- ============================================================
function p.listRowMain(frame)
    local args = frame.args
    local page = args.Page or ""
    local name = getCleanTitle(page)
    local startDate = args.start_date or ""
    local endDate = args.end_date or ""
    
    local row = html.create('div'):addClass('tourney-row tr-compact')
    
    -- 1. Date Column (Simplified)
    row:tag('div'):addClass('tr-date')
       :wikitext(formatDateRange(startDate, endDate))
        
    -- 2. Info Column
    local info = row:tag('div'):addClass('tr-info')
    info:tag('div'):addClass('tr-name'):wikitext('[[' .. page .. '|' .. name .. ']]')
    if args.organizer then
         info:tag('div'):addClass('tr-org'):wikitext(args.organizer)
    end
        
    -- 3. Prize Pool Column
    local prizeDiv = row:tag('div'):addClass('tr-prize')
    prizeDiv:wikitext(formatCurrency(args.prize_pool))
        
    return tostring(row)
end

return p