Skip to content

Commit

Permalink
refactor(match2): mapVeto into widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Rathoz committed Oct 30, 2024
1 parent 3b09f5d commit 3695b0b
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 194 deletions.
201 changes: 24 additions & 177 deletions components/match2/commons/match_summary_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,7 @@ local OpponentLibraries = require('Module:OpponentLibraries')
local Opponent = OpponentLibraries.Opponent
local OpponentDisplay = OpponentLibraries.OpponentDisplay

local ARROW_LEFT = '[[File:Arrow sans left.svg|15x15px|link=|Left team starts]]'
local ARROW_RIGHT = '[[File:Arrow sans right.svg|15x15px|link=|Right team starts]]'
local DEFAULT_VETO_TYPE_TO_TEXT = {
ban = 'BAN',
pick = 'PICK',
decider = 'DECIDER',
defaultban = 'DEFAULT BAN',
}
local TBD = Abbreviation.make('TBD', 'To Be Determined')
local VETO_DECIDER = 'decider'

---just a base class to avoid anno warnings
---@class MatchSummaryRowInterface
Expand Down Expand Up @@ -352,164 +343,13 @@ function Match:create()
return self.root
end

-- Map Veto Class
---@class VetoDisplay: MatchSummaryRowInterface
---@operator call: self
---@field root Html
---@field table Html
---@field vetoTypeToText table
---@field game string?
---@field emptyMapDisplay string
local MapVeto = Class.new(
function(self, options)
self.root = mw.html.create('div')
:addClass('brkts-popup-mapveto')

self.table = self.root:tag('table')
:addClass('wikitable-striped')
:addClass('collapsible')
:addClass('collapsed')

self.vetoTypeToText = options.vetoTypeToText or DEFAULT_VETO_TYPE_TO_TEXT
self.game = options.game
self.emptyMapDisplay = options.emptyMapDisplay or TBD

self:createHeader()
end
)

---@return self
function MapVeto:createHeader()
self.table:tag('tr')
:tag('th'):css('width','33%'):done()
:tag('th'):css('width','34%'):wikitext('Map Veto'):done()
:tag('th'):css('width','33%'):done()
return self
end

---@param firstVeto number?
---@param format string?
---@return self
function MapVeto:vetoStart(firstVeto, format)
format = format and ('Veto format: ' .. format)
local textLeft
local textCenter
local textRight
if firstVeto == 1 then
textLeft = '<b>Start Map Veto</b>'
textCenter = ARROW_LEFT
textRight = format
elseif firstVeto == 2 then
textLeft = format
textCenter = ARROW_RIGHT
textRight = '<b>Start Map Veto</b>'
else
return self
end

self.table:tag('tr'):addClass('brkts-popup-mapveto-vetostart')
:tag('th'):wikitext(textLeft):done()
:tag('th'):wikitext(textCenter):done()
:tag('th'):wikitext(textRight):done()

return self
end

---@param map string?
---@return self
function MapVeto:addDecider(map)
local row = mw.html.create('tr'):addClass('brkts-popup-mapveto-vetoround')

self:addColumnVetoType(row, 'brkts-popup-mapveto-decider', self.vetoTypeToText.decider)
self:addColumnVetoMap(row, self:displayMap(map))
self:addColumnVetoType(row, 'brkts-popup-mapveto-decider', self.vetoTypeToText.decider)

self.table:node(row)
return self
end

---@param vetoType string?
---@param map1 string?
---@param map2 string?
---@return self
function MapVeto:addRound(vetoType, map1, map2)
map1, map2 = self:displayMaps(map1, map2)

local vetoText = self.vetoTypeToText[vetoType]

if not vetoText then return self end

local class = 'brkts-popup-mapveto-' .. vetoType

local row = mw.html.create('tr'):addClass('brkts-popup-mapveto-vetoround')

self:addColumnVetoMap(row, map1)
self:addColumnVetoType(row, class, vetoText)
self:addColumnVetoMap(row, map2)

self.table:node(row)
return self
end

---@param map1 string?
---@param map2 string?
---@return string
---@return string
function MapVeto:displayMaps(map1, map2)
if Logic.isEmpty(map1) and Logic.isEmpty(map2) then
return TBD, TBD
end

return self:displayMap(map1), self:displayMap(map2)
end

---@param map string?
---@return string
function MapVeto:displayMap(map)
if not map then
return self.emptyMapDisplay
end
if not self.game then
return Page.makeInternalLink(map) or self.emptyMapDisplay
end
return Page.makeInternalLink(map, map .. '/' .. self.game) or self.emptyMapDisplay
end

---@param row Html
---@param styleClass string
---@param vetoText string
---@return self
function MapVeto:addColumnVetoType(row, styleClass, vetoText)
row:tag('td')
:tag('span')
:addClass(styleClass)
:addClass('brkts-popup-mapveto-vetotype')
:wikitext(vetoText)
return self
end

---@param row Html
---@param map string
---@return self
function MapVeto:addColumnVetoMap(row, map)
row:tag('td'):wikitext(map)
return self
end

---@return Html
function MapVeto:create()
return self.root
end

---@class MatchSummary
---@operator call(string?):MatchSummary
---@field Header MatchSummaryHeader
---@field Body MatchSummaryBody
---@field Row MatchSummaryRow
---@field Footer MatchSummaryFooter
---@field Match MatchSummaryMatch
---@field MapVeto VetoDisplay
---@field DEFAULT_VETO_TYPE_TO_TEXT table
---@field matches Html[]?
---@field headerElement Html?
---@field root Html?
Expand All @@ -519,8 +359,6 @@ MatchSummary.Body = Body
MatchSummary.Row = Row
MatchSummary.Footer = Footer
MatchSummary.Match = Match
MatchSummary.MapVeto = MapVeto
MatchSummary.DEFAULT_VETO_TYPE_TO_TEXT = DEFAULT_VETO_TYPE_TO_TEXT

---@param width string?
---@return MatchSummary
Expand Down Expand Up @@ -737,27 +575,36 @@ function MatchSummary.defaultGetByMatchId(CustomMatchSummary, args, options)
return matchSummary:create()
end

---@param vetoData table
---@param options {game: string?, vetoTypeToText:table?, emptyMapDisplay: string?}?
---@return VetoDisplay?
function MatchSummary.defaultMapVetoDisplay(vetoData, options)
if Logic.isEmpty(vetoData) then
---@param mapVetoes table
---@param options {game: string?, emptyMapDisplay: string?}?
---@return {firstVeto: integer?, vetoFormat: string?, vetoRounds: table[]}?
function MatchSummary.preProcessMapVeto(mapVetoes, options)
if Logic.isEmpty(mapVetoes) then
return
end

local mapVeto = MapVeto(options or {})
Array.forEach(vetoData, function(vetoRound)
if vetoRound.vetostart then
mapVeto:vetoStart(tonumber(vetoRound.vetostart), vetoRound.format)
options = options or {}
local mapInputToDisplay = function(map)
if Logic.isEmpty(map) then
return {name = options.emptyMapDisplay or TBD}
end
if vetoRound.type == VETO_DECIDER then
mapVeto:addDecider(vetoRound.decider)
else
mapVeto:addRound(vetoRound.type, vetoRound.team1, vetoRound.team2)
if options.game then
return {name = map, link = map .. '/' .. options.game}
end
end)
return {name = map, link = map}
end

return mapVeto
return {
firstVeto = tonumber(mapVetoes[1].vetostart),
vetoFormat = mapVetoes[1].format,
vetoRounds = Array.map(mapVetoes, function(vetoRound)
return {
type = vetoRound.type,
map1 = mapInputToDisplay(vetoRound.team1 or vetoRound.decider),
map2 = mapInputToDisplay(vetoRound.team2),
}
end)
}
end

---@param games table[]
Expand Down
4 changes: 1 addition & 3 deletions components/match2/wikis/counterstrike/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,10 @@ function CustomMatchSummary.createBody(match)
end
local showCountdown = match.timestamp ~= DateExt.defaultTimestamp

local mapVeto = MatchSummary.defaultMapVetoDisplay(match.extradata.mapveto, {game = match.game})

return MatchSummaryWidgets.Body{children = WidgetUtil.collect(
showCountdown and MatchSummaryWidgets.Row{children = DisplayHelper.MatchCountdownBlock(match)} or nil,
Array.map(match.games, CustomMatchSummary._createMap),
mapVeto and mapVeto:create() or nil,
MatchSummaryWidgets.MapVeto(MatchSummary.preProcessMapVeto(match.extradata.mapveto, {game = match.game})),
MatchSummaryWidgets.MatchComment{children = matchStatusText} or nil
)}
end
Expand Down
3 changes: 1 addition & 2 deletions components/match2/wikis/criticalops/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ end
---@return MatchSummaryBody
function CustomMatchSummary.createBody(match)
local showCountdown = match.timestamp ~= DateExt.defaultTimestamp
local mapVeto = MatchSummary.defaultMapVetoDisplay(match.extradata.mapveto, {game = match.game})

return MatchSummaryWidgets.Body{children = WidgetUtil.collect(
showCountdown and MatchSummaryWidgets.Row{children = DisplayHelper.MatchCountdownBlock(match)} or nil,
Array.map(match.games, CustomMatchSummary._createMap),
mapVeto and mapVeto:create() or nil
MatchSummaryWidgets.MapVeto(MatchSummary.preProcessMapVeto(match.extradata.mapveto, {game = match.game}))
)}
end

Expand Down
3 changes: 1 addition & 2 deletions components/match2/wikis/heroes/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ end
function CustomMatchSummary.createBody(match)
local showCountdown = match.timestamp ~= DateExt.defaultTimestamp
local characterBansData = MatchSummary.buildCharacterBanData(match.games, MAX_NUM_BANS)
local mapVeto = MatchSummary.defaultMapVetoDisplay(match.extradata.mapveto, {emptyMapDisplay = FP})

return MatchSummaryWidgets.Body{children = WidgetUtil.collect(
showCountdown and MatchSummaryWidgets.Row{children = DisplayHelper.MatchCountdownBlock(match)} or nil,
Array.map(match.games, FnUtil.curry(CustomMatchSummary._createGame, match.date)),
MatchSummaryWidgets.Mvp(match.extradata.mvp),
MatchSummaryWidgets.CharacterBanTable{bans = characterBansData, date = match.date},
MatchSummaryWidgets.Casters{casters = match.extradata.casters},
mapVeto and mapVeto:create() or nil
MatchSummaryWidgets.MapVeto(MatchSummary.preProcessMapVeto(match.extradata.mapveto, {emptyMapDisplay = FP}))
)}
end

Expand Down
3 changes: 1 addition & 2 deletions components/match2/wikis/rainbowsix/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,13 @@ end
---@return MatchSummaryBody
function CustomMatchSummary.createBody(match)
local showCountdown = match.timestamp ~= DateExt.defaultTimestamp
local mapVeto = MatchSummary.defaultMapVetoDisplay(match.extradata.mapveto, {game = 'siege'})

return MatchSummaryWidgets.Body{children = WidgetUtil.collect(
showCountdown and MatchSummaryWidgets.Row{children = DisplayHelper.MatchCountdownBlock(match)} or nil,
Array.map(match.games, CustomMatchSummary._createMap),
MatchSummaryWidgets.Mvp(match.extradata.mvp),
MatchSummaryWidgets.Casters{casters = match.extradata.casters},
mapVeto and mapVeto:create() or nil
MatchSummaryWidgets.MapVeto(MatchSummary.preProcessMapVeto(match.extradata.mapveto, {game = 'siege'}))
)}
end

Expand Down
3 changes: 1 addition & 2 deletions components/match2/wikis/splatoon/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ end
---@return MatchSummaryBody
function CustomMatchSummary.createBody(match)
local showCountdown = match.timestamp ~= DateExt.defaultTimestamp
local mapVeto = MatchSummary.defaultMapVetoDisplay(match.extradata.mapveto)

return MatchSummaryWidgets.Body{children = WidgetUtil.collect(
showCountdown and MatchSummaryWidgets.Row{children = DisplayHelper.MatchCountdownBlock(match)} or nil,
Array.map(match.games, CustomMatchSummary._createGame),
MatchSummaryWidgets.Mvp(match.extradata.mvp),
mapVeto and mapVeto:create() or nil
MatchSummaryWidgets.MapVeto(MatchSummary.preProcessMapVeto(match.extradata.mapveto))
)}
end

Expand Down
3 changes: 1 addition & 2 deletions components/match2/wikis/valorant/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,13 @@ end
---@return MatchSummaryBody
function CustomMatchSummary.createBody(match)
local showCountdown = match.timestamp ~= DateExt.defaultTimestamp
local mapVeto = MatchSummary.defaultMapVetoDisplay(match.extradata.mapveto)

return MatchSummaryWidgets.Body{children = WidgetUtil.collect(
showCountdown and MatchSummaryWidgets.Row{children = DisplayHelper.MatchCountdownBlock(match)} or nil,
Array.map(match.games, CustomMatchSummary._createMap),
MatchSummaryWidgets.Mvp(match.extradata.mvp),
MatchSummaryWidgets.Casters{casters = match.extradata.casters},
mapVeto and mapVeto:create() or nil
MatchSummaryWidgets.MapVeto(MatchSummary.preProcessMapVeto(match.extradata.mapveto))
)}
end

Expand Down
3 changes: 1 addition & 2 deletions components/match2/wikis/worldoftanks/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ end
---@return MatchSummaryBody
function CustomMatchSummary.createBody(match)
local showCountdown = match.timestamp ~= DateExt.defaultTimestamp
local mapVeto = MatchSummary.defaultMapVetoDisplay(match.extradata.mapveto, {emptyMapDisplay = NONE})

return MatchSummaryWidgets.Body{children = WidgetUtil.collect(
showCountdown and MatchSummaryWidgets.Row{children = DisplayHelper.MatchCountdownBlock(match)} or nil,
Array.map(match.games, CustomMatchSummary._createMapRow),
MatchSummaryWidgets.Mvp(match.extradata.mvp),
MatchSummaryWidgets.Casters{casters = match.extradata.casters},
mapVeto and mapVeto:create() or nil
MatchSummaryWidgets.MapVeto(MatchSummary.preProcessMapVeto(match.extradata.mapveto, {emptyMapDisplay = NONE}))
)}
end

Expand Down
3 changes: 1 addition & 2 deletions components/match2/wikis/zula/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ end
---@return MatchSummaryBody
function CustomMatchSummary.createBody(match)
local showCountdown = match.timestamp ~= DateExt.defaultTimestamp
local mapVeto = MatchSummary.defaultMapVetoDisplay(match.extradata.mapveto)

return MatchSummaryWidgets.Body{children = WidgetUtil.collect(
showCountdown and MatchSummaryWidgets.Row{children = DisplayHelper.MatchCountdownBlock(match)} or nil,
Array.map(match.games, CustomMatchSummary._createMap),
mapVeto and mapVeto:create() or nil
MatchSummaryWidgets.MapVeto(MatchSummary.preProcessMapVeto(match.extradata.mapveto))
)}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Widgets.Character = Lua.import('Module:Widget/Match/Summary/Character')
Widgets.GameCenter = Lua.import('Module:Widget/Match/Summary/GameCenter')
Widgets.GameComment = Lua.import('Module:Widget/Match/Summary/GameComment')
Widgets.GameWinLossIndicator = Lua.import('Module:Widget/Match/Summary/GameWinLossIndicator')
Widgets.MapVeto = Lua.import('Module:Widget/Match/Summary/MapVeto')
Widgets.MatchComment = Lua.import('Module:Widget/Match/Summary/MatchComment')
Widgets.MatchPageLink = Lua.import('Module:Widget/Match/Summary/MatchPageLink')
Widgets.Mvp = Lua.import('Module:Widget/Match/Summary/Mvp')
Expand Down
Loading

0 comments on commit 3695b0b

Please sign in to comment.