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

Module:Rankings: Difference between revisions

From eSportsAmaze
No edit summary
No edit summary
Line 10: Line 10:
     if not y then return 0 end
     if not y then return 0 end
     return os.time({year=y, month=m, day=d, hour=12, min=0, sec=0})
     return os.time({year=y, month=m, day=d, hour=12, min=0, sec=0})
end
-- Smart Logo Builder (ZERO Expensive Calls)
local function buildLogo(teamName, tData, mData)
    local lightFile = "Shield_team.png"
    local darkFile = "Shield_team_dark.png"
    if tData and tData.image and tData.image ~= "" then
        lightFile = tData.image; darkFile = (tData.image_dark and tData.image_dark ~= "") and tData.image_dark or lightFile
    elseif mData and mData.image and mData.image ~= "" then
        lightFile = mData.image; darkFile = (mData.image_dark and mData.image_dark ~= "") and mData.image_dark or lightFile
    end
    local container = html.create('span'):addClass('team-logo-wrapper')
    container:tag('span'):addClass('logo-lightmode'):wikitext('[[File:' .. lightFile .. '|24px|link=|class=team-logo]]')
    container:tag('span'):addClass('logo-darkmode'):wikitext('[[File:' .. darkFile .. '|24px|link=|class=team-logo]]')
    return tostring(container)
end
end


Line 67: Line 50:
end
end


function p.main(frame)
-- ==========================================
    local args = frame:getParent().args
-- EXPORT 1: API ENGINE (Math & Logic Only)
    if not args.type then args = frame.args end
-- ==========================================
    local statType = (args.type or "team"):lower()
function p.getRankings(statType, targetDateStr)
    local limit = tonumber(args.limit) or 100
   
    local targetDateStr = args.as_of_date
     local targetTs = os.time()  
     local targetTs = os.time()  
     if targetDateStr and targetDateStr ~= "" then
     if targetDateStr and targetDateStr ~= "" then
Line 80: Line 60:
     end
     end
      
      
     local root = html.create('div'):addClass('standings-wrapper')
     statType = (statType or "team"):lower()
    local tbl = root:tag('table'):addClass('standings-card-table stats-card-table')
     local list = {}
 
      
     local uniqueTeams = {}
     local tData = {}
    local pData = {}
 
     if statType == "team" then
     if statType == "team" then
         local results = cargo.query("RankingData_Team", "tournament, tier, end_date, team, rank", { orderBy = "end_date DESC", limit = 5000 })
         local results = cargo.query("RankingData_Team", "tournament, tier, end_date, team, rank", { orderBy = "end_date DESC", limit = 5000 })
         if not results or #results == 0 then return 'No data.' end
         if not results then return {} end
          
         local tData = {}
         for _, r in ipairs(results) do
         for _, r in ipairs(results) do
             local t = r.team
             local t = r.team
             local endTs = getTimestamps(r.end_date)
             local endTs = getTimestamps(r.end_date)
           
             if t and t ~= "" and endTs > 0 and endTs <= targetTs then
             if t and t ~= "" and endTs > 0 and endTs <= targetTs then
                 if not tData[t] then  
                 if not tData[t] then tData[t] = { name = t, team = t, points = 0, events = 0, latest_tourney = r.tournament } end
                    tData[t] = { team = t, points = 0, events = 0, latest_tourney = r.tournament }  
                    uniqueTeams[t] = true
                end
               
                 local diff = os.difftime(targetTs, endTs)
                 local diff = os.difftime(targetTs, endTs)
                 local days = math.max(0, math.floor(diff / 86400))
                 local days = math.max(0, math.floor(diff / 86400))
               
                 local finalPts = getTeamBasePts(r.tier, tonumber(r.rank) or 99) * getTeamDecay(days)
                 local base = getTeamBasePts(r.tier, tonumber(r.rank) or 99)
                local decay = getTeamDecay(days)
                local finalPts = base * decay
               
                 if finalPts > 0 then
                 if finalPts > 0 then
                     tData[t].points = tData[t].points + finalPts
                     tData[t].points = tData[t].points + finalPts
Line 114: Line 81:
             end
             end
         end
         end
        for _, d in pairs(tData) do if d.points > 0 then table.insert(list, d) end end


     elseif statType == "player" then
     elseif statType == "player" then
         local results = cargo.query("RankingData_Player", "tournament, tier, end_date, player, team, finishes, mvp_tourney, igl, survivor, mvp_finals, emerging", { orderBy = "end_date DESC", limit = 5000 })
         local results = cargo.query("RankingData_Player", "tournament, tier, end_date, player, team, finishes, mvp_tourney, igl, survivor, mvp_finals, emerging", { orderBy = "end_date DESC", limit = 5000 })
         if not results or #results == 0 then return 'No data.' end
         if not results then return {} end
          
         local pData = {}
         for _, r in ipairs(results) do
         for _, r in ipairs(results) do
             local p = r.player
             local p = r.player
             local endTs = getTimestamps(r.end_date)
             local endTs = getTimestamps(r.end_date)
           
             if p and p ~= "" and endTs > 0 and endTs <= targetTs then
             if p and p ~= "" and endTs > 0 and endTs <= targetTs then
                 if not pData[p] then  
                 if not pData[p] then pData[p] = { name = p, player = p, team = r.team, latest_tourney = r.tournament, points = 0, events = 0 }  
                    pData[p] = { player = p, team = r.team, latest_tourney = r.tournament, points = 0, events = 0 }  
                    if r.team and r.team ~= "" then uniqueTeams[r.team] = true end
                 else
                 else
                     if (not pData[p].team or pData[p].team == "") and r.team and r.team ~= "" then
                     if (not pData[p].team or pData[p].team == "") and r.team and r.team ~= "" then pData[p].team = r.team end
                        pData[p].team = r.team
                        uniqueTeams[r.team] = true
                    end
                 end
                 end
                  
                  
                 local diff = os.difftime(targetTs, endTs)
                 local diff = os.difftime(targetTs, endTs)
                 local days = math.max(0, math.floor(diff / 86400))
                 local days = math.max(0, math.floor(diff / 86400))
                local decay = getPlayerDecay(days)
                  
                  
                 local tierLow = (r.tier or ""):lower()
                 local t = (r.tier or ""):lower()
                 local mult = 1
                 local mult = 1
                 if tierLow:find("publisher") then mult = 2 elseif tierLow:find("tier 1") then mult = 1.5 end
                 if t:find("publisher") then mult = 2 elseif t:find("tier 1") then mult = 1.5 end
                  
                  
                 local base = (tonumber(r.finishes) or 0) * mult
                 local base = (tonumber(r.finishes) or 0) * mult
Line 149: Line 110:
                 if tonumber(r.emerging) == 1 then base = base + 5 end
                 if tonumber(r.emerging) == 1 then base = base + 5 end
                  
                  
                 local finalPts = base * decay
                 local finalPts = base * getPlayerDecay(days)
                 if finalPts > 0 then
                 if finalPts > 0 then
                     pData[p].points = pData[p].points + finalPts
                     pData[p].points = pData[p].points + finalPts
Line 156: Line 117:
             end
             end
         end
         end
        for _, d in pairs(pData) do if d.points > 0 then table.insert(list, d) end end
    end
   
    table.sort(list, function(a, b) return a.points > b.points end)
    for i, d in ipairs(list) do d.rank = i end
    return list
end
-- ==========================================
-- EXPORT 2: SINGLE RANK LOOKUP
-- ==========================================
function p.getRank(name, statType, targetDateStr)
    local list = p.getRankings(statType, targetDateStr)
    for _, d in ipairs(list) do
        if d.name:lower() == name:lower() then return d.rank, d.points end
     end
     end
    return nil, nil
end
-- ==========================================
-- HTML VISUAL RENDERER (For Rankings Page)
-- ==========================================
local function buildLogo(teamName, tData, mData)
    local lightFile = "Shield_team.png"
    local darkFile = "Shield_team_dark.png"
    if tData and tData.image and tData.image ~= "" then lightFile = tData.image; darkFile = (tData.image_dark and tData.image_dark ~= "") and tData.image_dark or lightFile
    elseif mData and mData.image and mData.image ~= "" then lightFile = mData.image; darkFile = (mData.image_dark and mData.image_dark ~= "") and mData.image_dark or lightFile end
    local container = html.create('span'):addClass('team-logo-wrapper')
    container:tag('span'):addClass('logo-lightmode'):wikitext('[[File:' .. lightFile .. '|24px|link=|class=team-logo]]')
    container:tag('span'):addClass('logo-darkmode'):wikitext('[[File:' .. darkFile .. '|24px|link=|class=team-logo]]')
    return tostring(container)
end


     local tourneyDb = {}
function p.main(frame)
     local masterDb = {}
    local args = frame:getParent().args
    local teamListQuoted = {}
    if not args.type then args = frame.args end
    local statType = (args.type or "team"):lower()
    local limit = tonumber(args.limit) or 100
   
    local list = p.getRankings(statType, args.as_of_date)
    if #list == 0 then return 'No data.' end
 
     local uniqueTeams = {}
    for _, d in ipairs(list) do
        if d.team and d.team ~= "" then uniqueTeams[d.team] = true end
    end
   
     local tourneyDb = {}; local masterDb = {}; local teamListQuoted = {}
     for t, _ in pairs(uniqueTeams) do table.insert(teamListQuoted, "'" .. sqlEscape(t) .. "'") end
     for t, _ in pairs(uniqueTeams) do table.insert(teamListQuoted, "'" .. sqlEscape(t) .. "'") end
     local teamSql = table.concat(teamListQuoted, ",")
     local teamSql = table.concat(teamListQuoted, ",")
Line 167: Line 171:
         local mResults = cargo.query("Teams", "name, image, image_dark", { where = "name IN (" .. teamSql .. ")", limit = 500 })
         local mResults = cargo.query("Teams", "name, image, image_dark", { where = "name IN (" .. teamSql .. ")", limit = 500 })
         if mResults then for _, row in ipairs(mResults) do masterDb[row.name:lower()] = row end end
         if mResults then for _, row in ipairs(mResults) do masterDb[row.name:lower()] = row end end
       
         local tResults = cargo.query("Tournament_Teams", "tournament, team, image, image_dark", { where = "team IN (" .. teamSql .. ")", limit = 5000 })
         local tResults = cargo.query("Tournament_Teams", "tournament, team, image, image_dark", { where = "team IN (" .. teamSql .. ")", limit = 5000 })
         if tResults then  
         if tResults then for _, row in ipairs(tResults) do tourneyDb[row.tournament:lower() .. "|" .. row.team:lower()] = row end end
            for _, row in ipairs(tResults) do  
                tourneyDb[row.tournament:lower() .. "|" .. row.team:lower()] = row  
            end  
        end
     end
     end


    local root = html.create('div'):addClass('standings-wrapper')
    local tbl = root:tag('table'):addClass('standings-card-table stats-card-table')
    local th = tbl:tag('tr')
   
     if statType == "team" then
     if statType == "team" then
        local list = {}
        for _, d in pairs(tData) do if d.points > 0 then table.insert(list, d) end end
        table.sort(list, function(a, b) return a.points > b.points end)
       
        local th = tbl:tag('tr')
         th:tag('th'):addClass('sticky-col sticky-1'):wikitext('Rank')
         th:tag('th'):addClass('sticky-col sticky-1'):wikitext('Rank')
         th:tag('th'):addClass('sticky-col sticky-2 stat-team'):wikitext('Team')
         th:tag('th'):addClass('sticky-col sticky-2 stat-team'):wikitext('Team')
Line 194: Line 192:
             local tD = tourneyDb[d.latest_tourney:lower() .. "|" .. d.team:lower()]
             local tD = tourneyDb[d.latest_tourney:lower() .. "|" .. d.team:lower()]
             local mD = masterDb[d.team:lower()]
             local mD = masterDb[d.team:lower()]
           
             local teamCell = tr:tag('td'):addClass('sticky-col sticky-2 stat-team team-name-cell')
             local teamCell = tr:tag('td'):addClass('sticky-col sticky-2 stat-team team-name-cell')
             teamCell:wikitext(buildLogo(d.team, tD, mD))
             teamCell:wikitext(buildLogo(d.team, tD, mD))
Line 202: Line 199:
             tr:tag('td'):addClass('stat-col'):css('font-weight','800'):css('color','#00509d'):css('font-size','1.1em'):wikitext(string.format("%.2f", d.points))
             tr:tag('td'):addClass('stat-col'):css('font-weight','800'):css('color','#00509d'):css('font-size','1.1em'):wikitext(string.format("%.2f", d.points))
         end
         end
     elseif statType == "player" then
     elseif statType == "player" then
        local list = {}
        for _, d in pairs(pData) do if d.points > 0 then table.insert(list, d) end end
        table.sort(list, function(a, b) return a.points > b.points end)
       
        local th = tbl:tag('tr')
         th:tag('th'):addClass('sticky-col sticky-1'):wikitext('Rank')
         th:tag('th'):addClass('sticky-col sticky-1'):wikitext('Rank')
         th:tag('th'):addClass('sticky-col sticky-logo'):wikitext('Team')
         th:tag('th'):addClass('sticky-col sticky-logo'):wikitext('Team')
Line 239: Line 230:
         end
         end
     end
     end
   
     return tostring(root)
     return tostring(root)
end
end


return p
return p

Revision as of 17:41, 22 May 2026

Documentation for this module may be created at Module:Rankings/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

local function getTimestamps(dateStr)
    if not dateStr or dateStr == "" then return 0 end
    local y, m, d = dateStr:match("(%d+)-(%d+)-(%d+)")
    if not y then return 0 end
    return os.time({year=y, month=m, day=d, hour=12, min=0, sec=0})
end

local function getTeamBasePts(tier, rank)
    local t = (tier or ""):lower()
    local col = 0
    if t:find("publisher") then col = 1
    elseif t:find("tier 1") then col = 2
    elseif t:find("tier 2") then col = 3
    elseif t:find("tier 3") then col = 4
    else return 0 end

    if rank == 1 then return ({1000, 800, 600, 400})[col]
    elseif rank == 2 then return ({800, 700, 500, 350})[col]
    elseif rank == 3 then return ({700, 600, 400, 300})[col]
    elseif rank == 4 then return ({600, 500, 300, 250})[col]
    elseif rank == 5 then return ({500, 400, 250, 200})[col]
    elseif rank >= 6 and rank <= 10 then return ({400, 300, 200, 150})[col]
    elseif rank >= 11 and rank <= 20 then return ({300, 200, 150, 100})[col]
    elseif rank >= 21 and rank <= 30 then return ({200, 100, 75, 50})[col]
    elseif rank >= 31 and rank <= 48 then return ({100, 50, 35, 25})[col]
    end return 0
end

local function getTeamDecay(days)
    if days <= 180 then return 1
    elseif days <= 270 then return 0.75
    elseif days <= 365 then return 0.5
    elseif days <= 1095 then return 0.1
    else return 0 end
end

local function getPlayerDecay(days)
    if days <= 180 then return 1
    elseif days <= 240 then return 0.75
    elseif days <= 300 then return 0.5
    elseif days <= 365 then return 0.25
    elseif days <= 1095 then return 0.1
    else return 0 end
end

-- ==========================================
-- EXPORT 1: API ENGINE (Math & Logic Only)
-- ==========================================
function p.getRankings(statType, targetDateStr)
    local targetTs = os.time() 
    if targetDateStr and targetDateStr ~= "" then
        local targetVal = getTimestamps(targetDateStr)
        if targetVal > 0 then targetTs = targetVal end
    end
    
    statType = (statType or "team"):lower()
    local list = {}
    
    if statType == "team" then
        local results = cargo.query("RankingData_Team", "tournament, tier, end_date, team, rank", { orderBy = "end_date DESC", limit = 5000 })
        if not results then return {} end
        local tData = {}
        for _, r in ipairs(results) do
            local t = r.team
            local endTs = getTimestamps(r.end_date)
            if t and t ~= "" and endTs > 0 and endTs <= targetTs then
                if not tData[t] then tData[t] = { name = t, team = t, points = 0, events = 0, latest_tourney = r.tournament } end
                local diff = os.difftime(targetTs, endTs)
                local days = math.max(0, math.floor(diff / 86400))
                local finalPts = getTeamBasePts(r.tier, tonumber(r.rank) or 99) * getTeamDecay(days)
                if finalPts > 0 then
                    tData[t].points = tData[t].points + finalPts
                    tData[t].events = tData[t].events + 1
                end
            end
        end
        for _, d in pairs(tData) do if d.points > 0 then table.insert(list, d) end end

    elseif statType == "player" then
        local results = cargo.query("RankingData_Player", "tournament, tier, end_date, player, team, finishes, mvp_tourney, igl, survivor, mvp_finals, emerging", { orderBy = "end_date DESC", limit = 5000 })
        if not results then return {} end
        local pData = {}
        for _, r in ipairs(results) do
            local p = r.player
            local endTs = getTimestamps(r.end_date)
            if p and p ~= "" and endTs > 0 and endTs <= targetTs then
                if not pData[p] then pData[p] = { name = p, player = p, team = r.team, latest_tourney = r.tournament, points = 0, events = 0 } 
                else
                    if (not pData[p].team or pData[p].team == "") and r.team and r.team ~= "" then pData[p].team = r.team end
                end
                
                local diff = os.difftime(targetTs, endTs)
                local days = math.max(0, math.floor(diff / 86400))
                
                local t = (r.tier or ""):lower()
                local mult = 1
                if t:find("publisher") then mult = 2 elseif t:find("tier 1") then mult = 1.5 end
                
                local base = (tonumber(r.finishes) or 0) * mult
                if tonumber(r.mvp_tourney) == 1 then base = base + 20 end
                if tonumber(r.igl) == 1 then base = base + 10 end
                if tonumber(r.survivor) == 1 then base = base + 10 end
                if tonumber(r.mvp_finals) == 1 then base = base + 10 end
                if tonumber(r.emerging) == 1 then base = base + 5 end
                
                local finalPts = base * getPlayerDecay(days)
                if finalPts > 0 then
                    pData[p].points = pData[p].points + finalPts
                    pData[p].events = pData[p].events + 1
                end
            end
        end
        for _, d in pairs(pData) do if d.points > 0 then table.insert(list, d) end end
    end
    
    table.sort(list, function(a, b) return a.points > b.points end)
    for i, d in ipairs(list) do d.rank = i end
    return list
end

-- ==========================================
-- EXPORT 2: SINGLE RANK LOOKUP
-- ==========================================
function p.getRank(name, statType, targetDateStr)
    local list = p.getRankings(statType, targetDateStr)
    for _, d in ipairs(list) do
        if d.name:lower() == name:lower() then return d.rank, d.points end
    end
    return nil, nil
end

-- ==========================================
-- HTML VISUAL RENDERER (For Rankings Page)
-- ==========================================
local function buildLogo(teamName, tData, mData)
    local lightFile = "Shield_team.png"
    local darkFile = "Shield_team_dark.png"
    if tData and tData.image and tData.image ~= "" then lightFile = tData.image; darkFile = (tData.image_dark and tData.image_dark ~= "") and tData.image_dark or lightFile
    elseif mData and mData.image and mData.image ~= "" then lightFile = mData.image; darkFile = (mData.image_dark and mData.image_dark ~= "") and mData.image_dark or lightFile end
    local container = html.create('span'):addClass('team-logo-wrapper')
    container:tag('span'):addClass('logo-lightmode'):wikitext('[[File:' .. lightFile .. '|24px|link=|class=team-logo]]')
    container:tag('span'):addClass('logo-darkmode'):wikitext('[[File:' .. darkFile .. '|24px|link=|class=team-logo]]')
    return tostring(container)
end

function p.main(frame)
    local args = frame:getParent().args
    if not args.type then args = frame.args end
    local statType = (args.type or "team"):lower()
    local limit = tonumber(args.limit) or 100
    
    local list = p.getRankings(statType, args.as_of_date)
    if #list == 0 then return 'No data.' end

    local uniqueTeams = {}
    for _, d in ipairs(list) do
        if d.team and d.team ~= "" then uniqueTeams[d.team] = true end
    end
    
    local tourneyDb = {}; local masterDb = {}; local teamListQuoted = {}
    for t, _ in pairs(uniqueTeams) do table.insert(teamListQuoted, "'" .. sqlEscape(t) .. "'") end
    local teamSql = table.concat(teamListQuoted, ",")
    
    if teamSql ~= "" and cargo and cargo.query then
        local mResults = cargo.query("Teams", "name, image, image_dark", { where = "name IN (" .. teamSql .. ")", limit = 500 })
        if mResults then for _, row in ipairs(mResults) do masterDb[row.name:lower()] = row end end
        local tResults = cargo.query("Tournament_Teams", "tournament, team, image, image_dark", { where = "team IN (" .. teamSql .. ")", limit = 5000 })
        if tResults then for _, row in ipairs(tResults) do tourneyDb[row.tournament:lower() .. "|" .. row.team:lower()] = row end end
    end

    local root = html.create('div'):addClass('standings-wrapper')
    local tbl = root:tag('table'):addClass('standings-card-table stats-card-table')
    local th = tbl:tag('tr')
    
    if statType == "team" then
        th:tag('th'):addClass('sticky-col sticky-1'):wikitext('Rank')
        th:tag('th'):addClass('sticky-col sticky-2 stat-team'):wikitext('Team')
        th:tag('th'):addClass('stat-col'):wikitext('Events Counted')
        th:tag('th'):addClass('stat-col'):wikitext('Total Points')
        
        for i = 1, math.min(#list, limit) do
            local d = list[i]
            local tr = tbl:tag('tr'):addClass('standings-row')
            tr:tag('td'):addClass('sticky-col sticky-1 dyn-rank'):css('text-align','center'):css('font-weight','800')
            
            local tD = tourneyDb[d.latest_tourney:lower() .. "|" .. d.team:lower()]
            local mD = masterDb[d.team:lower()]
            local teamCell = tr:tag('td'):addClass('sticky-col sticky-2 stat-team team-name-cell')
            teamCell:wikitext(buildLogo(d.team, tD, mD))
            teamCell:tag('span'):css('font-size','0.95em'):css('font-weight','700'):wikitext('[[' .. d.team .. ']]')
            
            tr:tag('td'):addClass('stat-col'):wikitext(d.events)
            tr:tag('td'):addClass('stat-col'):css('font-weight','800'):css('color','#00509d'):css('font-size','1.1em'):wikitext(string.format("%.2f", d.points))
        end
    elseif statType == "player" then
        th:tag('th'):addClass('sticky-col sticky-1'):wikitext('Rank')
        th:tag('th'):addClass('sticky-col sticky-logo'):wikitext('Team')
        th:tag('th'):addClass('sticky-col sticky-player stat-team'):wikitext('Player')
        th:tag('th'):addClass('stat-col'):wikitext('Events Counted')
        th:tag('th'):addClass('stat-col'):wikitext('Total Points')
        
        for i = 1, math.min(#list, limit) do
            local d = list[i]
            local tr = tbl:tag('tr'):addClass('standings-row')
            tr:tag('td'):addClass('sticky-col sticky-1 dyn-rank'):css('text-align','center'):css('font-weight','800')
            
            local logoCell = tr:tag('td'):addClass('sticky-col sticky-logo')
            local pCell = tr:tag('td'):addClass('sticky-col sticky-player stat-team team-name-cell')
            local infoDiv = pCell:tag('div'):css('line-height','1.1')
            infoDiv:tag('div'):addClass('player-name-txt'):wikitext('[[' .. d.player .. ']]')
            
            if d.team and d.team ~= "" then
                local tD = tourneyDb[d.latest_tourney:lower() .. "|" .. d.team:lower()]
                local mD = masterDb[d.team:lower()]
                logoCell:wikitext(buildLogo(d.team, tD, mD))
                infoDiv:tag('div'):css('font-size','0.7em'):css('color','var(--text-muted)'):css('text-transform','uppercase'):css('font-weight','700'):wikitext('[[' .. d.team .. ']]')
            else
                logoCell:wikitext(buildLogo("", nil, nil))
                infoDiv:tag('div'):css('font-size','0.7em'):css('color','var(--text-muted)'):css('text-transform','uppercase'):css('font-weight','700'):css('opacity','0.5'):wikitext('Free Agent')
            end
            
            tr:tag('td'):addClass('stat-col'):wikitext(d.events)
            tr:tag('td'):addClass('stat-col'):css('font-weight','800'):css('color','#00509d'):css('font-size','1.1em'):wikitext(string.format("%.2f", d.points))
        end
    end
    return tostring(root)
end

return p