Skip to content

Commit

Permalink
refactor(match2): rework and simplify setPlacement function to be non…
Browse files Browse the repository at this point in the history
…-mutating (#4841)

* refactor(match2): rework and simplify setPlacement function to be non-mutating

* do cs, fifa and tft
  • Loading branch information
Rathoz authored Oct 10, 2024
1 parent fa16b43 commit fdcb640
Show file tree
Hide file tree
Showing 44 changed files with 145 additions and 84 deletions.
57 changes: 16 additions & 41 deletions components/match2/commons/match_group_input_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -845,50 +845,25 @@ function MatchGroupInputUtil.getDefaultWinner(opponents)
return idx > 0 and idx or -1
end

-- Set the field 'placement' for the two participants in the opponenets list.
-- Set the placementWinner field to the winner, and placementLoser to the other team
-- Special cases:
-- If Winner = 0, that means draw, and placementLoser isn't used. Both teams will get placementWinner
-- If Winner = -1, that mean no team won, and placementWinner isn't used. Both teams will get placementLoser
---@param opponents MGIParsedOpponent[]
---@param winner integer?
---@param placementWinner integer
---@param placementLoser integer

--- Calculate the correct value of the 'placement' for the two-opponent matches/games.
--- Cases:
--- If Winner = OpponentIndex, return 1
--- If Winner = 0, means it was a draw, return 1
--- If Winner = -1, means that mean no team won, returns 2
--- Otherwise return 2
---@param resultType string?
---@return MGIParsedOpponent[]
function MatchGroupInputUtil.setPlacement(opponents, winner, placementWinner, placementLoser, resultType)
if not opponents or #opponents ~= 2 or resultType == MatchGroupInputUtil.RESULT_TYPE.NOT_PLAYED then
return opponents
---@param winner integer?
---@param opponentIndex integer
---@return integer?
function MatchGroupInputUtil.placementFromWinner(resultType, winner, opponentIndex)
if resultType == MatchGroupInputUtil.RESULT_TYPE.NOT_PLAYED then
return nil
end

local loserIdx
local winnerIdx
if winner == 1 then
winnerIdx = 1
loserIdx = 2
elseif winner == 2 then
winnerIdx = 2
loserIdx = 1
elseif winner == 0 then
-- Draw; idx of winner/loser doesn't matter
-- since loser and winner gets the same placement
placementLoser = placementWinner
winnerIdx = 1
loserIdx = 2
elseif winner == -1 then
-- No Winner (both loses). For example if both teams DQ.
-- idx's doesn't matter
placementWinner = placementLoser
winnerIdx = 1
loserIdx = 2
else
error('setPlacement: Unexpected winner: ' .. tostring(winner))
return opponents
if winner == 0 or winner == opponentIndex then
return 1
end
opponents[winnerIdx].placement = placementWinner
opponents[loserIdx].placement = placementLoser

return opponents
return 2
end

---@param match table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ function StarcraftMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

Table.mergeInto(match, MatchGroupInputUtil.getTournamentContext(match))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Opponent.toLegacyMode(opponents[1].type, opponents[2].type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

Table.mergeInto(match, MatchGroupInputUtil.getTournamentContext(match))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

Table.mergeInto(match, MatchGroupInputUtil.getTournamentContext(match))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', 'team'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', 'team'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', DEFAULT_MODE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', 'team'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

Table.mergeInto(match, MatchGroupInputUtil.getTournamentContext(match))
Expand Down
4 changes: 3 additions & 1 deletion components/match2/wikis/dota2/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ function CustomMatchGroupInput.processMatchWithoutStandalone(MatchParser, match)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', 'solo'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

Table.mergeInto(match, MatchGroupInputUtil.getTournamentContext(match))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
4 changes: 3 additions & 1 deletion components/match2/wikis/halo/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
4 changes: 3 additions & 1 deletion components/match2/wikis/heroes/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', DEFAULT_MODE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', DEFAULT_MODE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ function CustomMatchGroupInput.processMatchWithoutStandalone(MatchParser, match)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
4 changes: 3 additions & 1 deletion components/match2/wikis/magic/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ function CustomMatchGroupInput.processMatch(match)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', 'solo'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', 'team'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode', 'team'))
Expand Down
4 changes: 3 additions & 1 deletion components/match2/wikis/osu/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ function CustomMatchGroupInput.processMatch(match, options)
match.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponents)
match.walkover = MatchGroupInputUtil.getWalkover(match.resulttype, opponents)
match.winner = MatchGroupInputUtil.getWinner(match.resulttype, winnerInput, opponents)
MatchGroupInputUtil.setPlacement(opponents, match.winner, 1, 2, match.resulttype)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.resulttype, match.winner, opponentIndex)
end)
end

match.mode = Logic.emptyOr(match.mode, Variables.varDefault('tournament_mode'), DEFAULT_MODE)
Expand Down
Loading

0 comments on commit fdcb640

Please sign in to comment.