Module:Records
From eSportsAmaze
More actions
Documentation for this module may be created at Module:Records/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 getNum(s)
if not s then return 0 end
return tonumber(s:match("%d+")) or 0
end
function p.main(frame)
local args = frame:getParent().args
local tournamentName = args.tournament or mw.title.getCurrentTitle().text
local whereClause = string.format("tournament = '%s'", sqlEscape(tournamentName))
-- ==========================================
-- 1. DAILY RECORDS (Grouped by Day & Team)
-- ==========================================
local dayStats = cargo.query("MatchStats_Team",
"tournament_day, team, SUM(total_pts)=day_total, SUM(elim_pts)=day_elims",
{ where = whereClause, groupBy = "tournament_day, team", limit = 5000 }
)
local days = {}
local dayList = {}
if dayStats and #dayStats > 0 then
for _, r in ipairs(dayStats) do
local day = r.tournament_day
if day and day ~= "" then
if not days[day] then
days[day] = { pts = {team="-", val=-1}, elims = {team="-", val=-1} }
table.insert(dayList, day)
end
local pts = tonumber(r.day_total) or 0
local elims = tonumber(r.day_elims) or 0
if pts > days[day].pts.val then days[day].pts = {team=r.team, val=pts} end
if elims > days[day].elims.val then days[day].elims = {team=r.team, val=elims} end
end
end
end
-- Sort days intelligently (Day 1, Day 2 ... Day 10)
table.sort(dayList, function(a, b) return getNum(a) < getNum(b) end)
-- ==========================================
-- 2. OVERALL MATCH RECORDS
-- ==========================================
local teamMatch = cargo.query("MatchStats_Team", "team, total_pts, elim_pts", { where = whereClause, limit = 5000 })
local playerMatch = cargo.query("MatchStats_Player", "player, team, player_elims, damage", { where = whereClause, limit = 5000 })
local rec = {
t_pts = {name="-", val=-1},
t_elims = {name="-", val=-1},
p_elims = {name="-", team="-", val=-1},
p_dmg = {name="-", team="-", val=-1}
}
if teamMatch then
for _, r in ipairs(teamMatch) do
local pts = tonumber(r.total_pts) or 0
local elims = tonumber(r.elim_pts) or 0
if pts > rec.t_pts.val then rec.t_pts = {name=r.team, val=pts} end
if elims > rec.t_elims.val then rec.t_elims = {name=r.team, val=elims} end
end
end
if playerMatch then
for _, r in ipairs(playerMatch) do
local elims = tonumber(r.player_elims) or 0
local dmg = tonumber(r.damage) or 0
if elims > rec.p_elims.val then rec.p_elims = {name=r.player, team=r.team, val=elims} end
if dmg > rec.p_dmg.val then rec.p_dmg = {name=r.player, team=r.team, val=dmg} end
end
end
-- ==========================================
-- 3. BUILD UI
-- ==========================================
if #dayList == 0 and rec.t_pts.val == -1 then
return "" -- No data yet
end
local root = html.create('div'):addClass('rec-wrapper')
root:tag('div'):addClass('rec-header'):wikitext('🏆 Tournament Records')
local grid = root:tag('div'):addClass('rec-grid')
-- LEFT COLUMN: Overall Single Match Records
local colLeft = grid:tag('div'):addClass('rec-col')
colLeft:tag('div'):addClass('rec-col-title'):wikitext('Single Match Highs')
local function addRecord(parent, icon, title, name, subname, val, suffix)
if val == -1 then return end
local box = parent:tag('div'):addClass('rec-box')
box:tag('div'):addClass('rec-icon'):wikitext(icon)
local info = box:tag('div'):addClass('rec-info')
info:tag('div'):addClass('rec-title'):wikitext(title)
local nameStr = "'''[[" .. name .. "]]'''"
if subname and subname ~= "-" then nameStr = nameStr .. " <span style='font-size:0.75em; color:var(--text-muted);'>(" .. subname .. ")</span>" end
info:tag('div'):addClass('rec-name'):wikitext(nameStr)
box:tag('div'):addClass('rec-val'):wikitext(val .. '<span>' .. suffix .. '</span>')
end
addRecord(colLeft, '🔥', 'Highest Team Points', rec.t_pts.name, nil, rec.t_pts.val, 'pts')
addRecord(colLeft, '⚔️', 'Highest Team Elims', rec.t_elims.name, nil, rec.t_elims.val, 'elims')
addRecord(colLeft, '💀', 'Highest Player Elims', rec.p_elims.name, rec.p_elims.team, rec.p_elims.val, 'elims')
if rec.p_dmg.val > 0 then
addRecord(colLeft, '💥', 'Highest Player Damage', rec.p_dmg.name, rec.p_dmg.team, math.floor(rec.p_dmg.val), 'dmg')
end
-- RIGHT COLUMN: Daily Leaders
local colRight = grid:tag('div'):addClass('rec-col')
colRight:tag('div'):addClass('rec-col-title'):wikitext('Daily Leaders')
local dayListContainer = colRight:tag('div'):addClass('rec-day-list')
for _, day in ipairs(dayList) do
local d = days[day]
local dayRow = dayListContainer:tag('div'):addClass('rec-day-row')
dayRow:tag('div'):addClass('rec-day-name'):wikitext(day)
local dStats = dayRow:tag('div'):addClass('rec-day-stats')
dStats:tag('div'):wikitext("🏅 '''Pts:''' [[" .. d.pts.team .. "]] <span style='color:var(--text-muted)'>(" .. d.pts.val .. ")</span>")
dStats:tag('div'):wikitext("🔫 '''Elims:''' [[" .. d.elims.team .. "]] <span style='color:var(--text-muted)'>(" .. d.elims.val .. ")</span>")
end
return tostring(root)
end
return p