Module:GroupDraw
From eSportsAmaze
More actions
Documentation for this module may be created at Module:GroupDraw/doc
local p = {}
local html = mw.html
local cargo = mw.ext.cargo
local function buildLogo(teamName, tourneyData, masterData)
local lightLogo = "Shield_team.png"
local darkLogo = "Shield_team_dark.png"
if tourneyData and tourneyData.image and tourneyData.image ~= "" then
lightLogo = tourneyData.image
darkLogo = (tourneyData.image_dark and tourneyData.image_dark ~= "") and tourneyData.image_dark or lightLogo
elseif masterData then
if masterData.image and masterData.image ~= "" then
lightLogo = masterData.image
else
lightLogo = teamName .. ".png"
end
if masterData.image_dark and masterData.image_dark ~= "" then
darkLogo = masterData.image_dark
else
darkLogo = lightLogo
end
end
local container = html.create('span'):addClass('team-logo-wrapper')
container:tag('span'):addClass('logo-lightmode'):wikitext('[[File:' .. lightLogo .. '|30px|link=|class=team-logo]]')
container:tag('span'):addClass('logo-darkmode'):wikitext('[[File:' .. darkLogo .. '|30px|link=|class=team-logo]]')
return tostring(container)
end
function p.main(frame)
local args = frame:getParent().args
local tournamentName = args.tournament or mw.title.getCurrentTitle().text
local groupsInfo = {}
local allTeams = {}
local maxRows = 0
for i = 1, 20 do
local gName = args['g' .. i]
local gTeamsStr = args['g' .. i .. 'teams']
if gName and gTeamsStr then
local teamList = mw.text.split(gTeamsStr, ",")
local cleanedTeams = {}
for _, tName in ipairs(teamList) do
local trimmedTeam = mw.text.trim(tName)
if trimmedTeam ~= "" then
table.insert(cleanedTeams, trimmedTeam)
table.insert(allTeams, "'" .. trimmedTeam:gsub("'", "\\'") .. "'")
end
end
table.insert(groupsInfo, { title = gName, squad = cleanedTeams })
if #cleanedTeams > maxRows then maxRows = #cleanedTeams end
end
end
if #groupsInfo == 0 then return "" end
local teamSql = table.concat(allTeams, ",")
local tourneyDb = {}
if cargo and cargo.query and teamSql ~= "" then
local tResults = cargo.query("Tournament_Teams", "team, display_name, image, image_dark", { where = "tournament='" .. tournamentName:gsub("'", "\\'") .. "' AND team IN (" .. teamSql .. ")", limit = 500 })
for _, rowData in ipairs(tResults) do tourneyDb[rowData.team] = rowData end
end
local masterDb = {}
if cargo and cargo.query and teamSql ~= "" then
local mResults = cargo.query("Teams", "name, image, image_dark", { where = "name IN (" .. teamSql .. ")", limit = 500 })
for _, rowData in ipairs(mResults) do masterDb[rowData.name] = rowData end
end
local mainContainer = html.create('div'):addClass('group-draw-wrapper')
local drawTable = mainContainer:tag('table'):addClass('wikitable group-draw-table'):css('width', '100%'):css('border-collapse', 'collapse')
local topHeader = drawTable:tag('tr')
for _, grp in ipairs(groupsInfo) do
topHeader:tag('th'):wikitext(grp.title):css('text-align', 'center'):css('background-color', '#f2f6fa')
end
for rIndex = 1, maxRows do
local tableRow = drawTable:tag('tr')
for _, grp in ipairs(groupsInfo) do
local cellContent = tableRow:tag('td')
local targetTeam = grp.squad[rIndex]
if targetTeam then
local tData = tourneyDb[targetTeam]
local mData = masterDb[targetTeam]
local displayName = targetTeam
if tData and tData.display_name and tData.display_name ~= "" then
displayName = tData.display_name
end
local generatedLogo = buildLogo(targetTeam, tData, mData)
local cellDiv = cellContent:tag('div'):css('display', 'flex'):css('align-items', 'center'):css('gap', '10px')
cellDiv:wikitext(generatedLogo .. ' [[' .. targetTeam .. '|' .. displayName .. ']]')
end
end
end
return tostring(mainContainer)
end
return p