Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(match2): Make use of standardProcessMaps #5210

Merged
merged 22 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions components/match2/commons/match_group_input_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1159,11 +1159,14 @@ end
---@class MapParserInterface
---@field calculateMapScore? fun(map: table): fun(opponentIndex: integer): integer?
---@field getExtraData? fun(match: table, game: table, opponents: table[]): table?
---@field getMapName? fun(game: table): string?
---@field getMapName? fun(game: table, mapIndex: integer, match: table): string?
---@field getMapMode? fun(match: table, game: table, opponents: table[]): string?
---@field getPlayersOfMapOpponent? fun(game: table, opponent:table, opponentIndex: integer): table[]
---@field getPatch? fun(game: table): string?
---@field mapIsFinished? fun(map: table, opponents: table[], finishedInput: string?, winnerInput: string?): boolean
---@field extendMapOpponent? fun(map: table, opponentIndex: integer): table
---@field getMapBestOf? fun(map: table): integer?
---@field computeOpponentScore? fun(props: table, autoScore?: fun(opponentIndex: integer):integer?): integer?, string?
---@field ADD_SUB_GROUP? boolean
---@field BREAK_ON_EMPTY? boolean

Expand All @@ -1177,6 +1180,9 @@ end
--- - getPlayersOfMapOpponent(map, opponent, opponentIndex): table[]?
--- - getPatch(game): string?
--- - mapIsFinished(map, opponents): boolean
--- - extendMapOpponent(map, opponentIndex): table
--- - getMapBestOf(map): integer?
--- - computeOpponentScore(props, autoScore): integer?, string?
---
--- Additionally, the Parser may have the following properties:
--- - ADD_SUB_GROUP boolean?
Expand All @@ -1188,8 +1194,8 @@ end
function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser)
local maps = {}
local subGroup = 0
for key, map in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
if Parser.BREAK_ON_EMPTY and Logic.isEmpty(map) then
for key, map, mapIndex in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
if Parser.BREAK_ON_EMPTY and Logic.isDeepEmpty(map) then
break
end
local finishedInput = map.finished --[[@as string?]]
Expand All @@ -1201,7 +1207,11 @@ function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser)
end

if Parser.getMapName then
map.map = Parser.getMapName(map)
map.map = Parser.getMapName(map, mapIndex, match)
end

if Parser.getMapBestOf then
map.bestof = Parser.getMapBestOf(map)
end

if Parser.mapIsFinished then
Expand All @@ -1215,7 +1225,8 @@ function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser)
end

map.opponents = Array.map(opponents, function(opponent, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
local computeOpponentScore = Parser.computeOpponentScore or MatchGroupInputUtil.computeOpponentScore
local score, status = computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
opponentIndex = opponentIndex,
Expand All @@ -1224,7 +1235,12 @@ function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser)
local players = Parser.getPlayersOfMapOpponent
and Parser.getPlayersOfMapOpponent(map, opponent, opponentIndex)
or nil
return {score = score, status = status, players = players}

local mapOpponent = {score = score, status = status, players = players}
if not Parser.extendMapOpponent then
return mapOpponent
end
return Table.merge(Parser.extendMapOpponent(map, opponentIndex), mapOpponent)
end)

-- needs map.opponents available!
Expand Down Expand Up @@ -1463,6 +1479,7 @@ function MatchGroupInputUtil.calculatePlacementOfOpponents(opponents)
end

---@param match table
---@param opponentCount integer
---@return {placementInfo: table[], settings: table}
function MatchGroupInputUtil.parseSettings(match, opponentCount)
-- Pre-parse Status colors (up/down etc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,10 @@ function MapFunctions.isArchon(mapInput, opponent, opponentIndex)
end

---@param game table
---@param gameIndex table
---@param match table
---@return string?
function MapFunctions.getMapName(game)
function MapFunctions.getMapName(game, gameIndex, match)
local mapName = game.map
if mapName and mapName:upper() ~= TBD then
return mw.ext.TeamLiquidIntegration.resolve_redirect(game.map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ function MapFunctions.calculateMapScore(map)
end

---@param map table
---@param mapIndex integer
---@param match table
---@return string?
function MapFunctions.getMapName(map)
function MapFunctions.getMapName(map, mapIndex, match)
if String.isNotEmpty(map.map) and map.map ~= 'TBD' then
return mw.ext.TeamLiquidIntegration.resolve_redirect(map.map)
end
Expand Down
63 changes: 15 additions & 48 deletions components/match2/wikis/brawlstars/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ local BrawlerNames = mw.loadData('Module:BrawlerNames')
local FnUtil = require('Module:FnUtil')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local Operator = require('Module:Operator')
local String = require('Module:StringUtils')
local Table = require('Module:Table')
local Variables = require('Module:Variables')
Expand All @@ -30,7 +29,9 @@ local DEFAULT_BESTOF_MAP = 3

-- containers for process helper functions
local MatchFunctions = {}
local MapFunctions = {}
local MapFunctions = {
BREAK_ON_EMPTY = true,
}

MatchFunctions.DEFAULT_MODE = 'team'
MatchFunctions.DATE_FALLBACKS = {
Expand All @@ -50,42 +51,11 @@ end
---@param opponents table[]
---@return table[]
function MatchFunctions.extractMaps(match, opponents)
local maps = {}
for key, map, mapIndex in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
if map.map == nil then
break
end
local finishedInput = map.finished --[[@as string?]]
local winnerInput = map.winner --[[@as string?]]

map.vod = map.vod or String.nilIfEmpty(match['vodgame' .. mapIndex])
map.bestof = MapFunctions.getBestOf(map)
map.extradata = MapFunctions.getExtraData(map, #opponents)

map.opponents = Array.map(opponents, function(opponent, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
opponentIndex = opponentIndex,
score = map['score' .. opponentIndex],
})
local players = MapFunctions.getPlayersOfMapOpponent(map, opponent, opponentIndex)
return {score = score, status = status, players = players}
end)

map.finished = MatchGroupInputUtil.mapIsFinished(map, map.opponents)

map.scores = Array.map(map.opponents, Operator.property('score'))
if map.finished then
map.status = MatchGroupInputUtil.getMatchStatus(winnerInput, finishedInput)
map.winner = MatchGroupInputUtil.getWinner(map.status, winnerInput, map.opponents)
end

table.insert(maps, map)
match[key] = nil
end

return maps
local games = MatchGroupInputUtil.standardProcessMaps(match, opponents, MapFunctions)
Array.forEach(games, function(game, gameIndex)
game.vod = game.vod or String.nilIfEmpty(match['vodgame' .. gameIndex])
end)
return games
end

--
Expand Down Expand Up @@ -124,16 +94,17 @@ end

---@param map table
---@return integer
function MapFunctions.getBestOf(map)
function MapFunctions.getMapBestOf(map)
local bestof = tonumber(Logic.emptyOr(map.bestof, Variables.varDefault('map_bestof')))
Variables.varDefine('map_bestof', bestof)
return bestof or DEFAULT_BESTOF_MAP
end

---@param match table
---@param map table
---@param opponentCount integer
---@param opponents table[]
---@return table
function MapFunctions.getExtraData(map, opponentCount)
function MapFunctions.getExtraData(match, map, opponents)
local extradata = {
bestof = map.bestof,
comment = map.comment,
Expand All @@ -143,7 +114,7 @@ function MapFunctions.getExtraData(map, opponentCount)

local bans = {}
local getCharacterName = FnUtil.curry(MatchGroupInputUtil.getCharacterName, BrawlerNames)
for opponentIndex = 1, opponentCount do
for opponentIndex = 1, #opponents do
bans['team' .. opponentIndex] = {}
for _, ban in Table.iter.pairsByPrefix(map, 't' .. opponentIndex .. 'b') do
ban = getCharacterName(ban)
Expand All @@ -163,9 +134,9 @@ end
function MapFunctions.getPlayersOfMapOpponent(map, opponent, opponentIndex)
local getCharacterName = FnUtil.curry(MatchGroupInputUtil.getCharacterName, BrawlerNames)
local players = Array.mapIndexes(function(playerIndex)
return opponent.match2players[playerIndex] or Logic.nilIfEmpty(map['t' .. opponentIndex .. 'c' .. playerIndex])
return map['t' .. opponentIndex .. 'p' .. playerIndex] or map['t' .. opponentIndex .. 'c' .. playerIndex]
end)
local participants, unattachedParticipants = MatchGroupInputUtil.parseParticipants(
return MatchGroupInputUtil.parseMapPlayers(
opponent.match2players,
players,
function(playerIndex)
Expand All @@ -180,10 +151,6 @@ function MapFunctions.getPlayersOfMapOpponent(map, opponent, opponentIndex)
}
end
)
Array.forEach(unattachedParticipants, function(participant)
table.insert(participants, participant)
end)
return participants
end

return CustomMatchGroupInput
55 changes: 17 additions & 38 deletions components/match2/wikis/clashofclans/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
local Array = require('Module:Array')
local Lua = require('Module:Lua')
local MathUtil = require('Module:MathUtil')
local Operator = require('Module:Operator')
local Table = require('Module:Table')
local Variables = require('Module:Variables')

Expand Down Expand Up @@ -52,41 +51,7 @@ end
---@param opponents table[]
---@return table[]
function MatchFunctions.extractMaps(match, opponents)
local maps = {}
for key, map in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
local finishedInput = map.finished --[[@as string?]]
local winnerInput = map.winner --[[@as string?]]

map.map = MapFunctions.getMapName(map)
map.extradata = MapFunctions.getExtraData(map)
map.finished = MatchGroupInputUtil.mapIsFinished(map)

map.opponents = Array.map(opponents, function(_, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
opponentIndex = opponentIndex,
score = map['score' .. opponentIndex],
})
return {
score = score,
status = status,
time = map.extradata.times[opponentIndex],
percentage = map.extradata.percentages[opponentIndex] or 0
}
end)

map.scores = Array.map(map.opponents, Operator.property('score'))
if map.finished then
map.status = MatchGroupInputUtil.getMatchStatus(winnerInput, finishedInput)
map.winner = MatchGroupInputUtil.getWinner(map.status, winnerInput, map.opponents)
end

table.insert(maps, map)
match[key] = nil
end

return maps
return MatchGroupInputUtil.standardProcessMaps(match, opponents, MapFunctions)
end

---@param bestofInput string|integer?
Expand Down Expand Up @@ -114,14 +79,18 @@ end
-- map related functions
--
---@param map table
---@param mapIndex integer
---@param match table
---@return string?
function MapFunctions.getMapName(map)
function MapFunctions.getMapName(map, mapIndex, match)
return nil
end

---@param match table
---@param map table
---@param opponents table[]
---@return table
function MapFunctions.getExtraData(map)
function MapFunctions.getExtraData(match, map, opponents)
return {
comment = map.comment,
times = MapFunctions.readTimes(map),
Expand Down Expand Up @@ -161,4 +130,14 @@ function MapFunctions.readTimes(map)
return timesInSeconds
end

function MapFunctions.extendMapOpponent(map, opponentIndex)
local times = MapFunctions.readTimes(map)
local percentages = MapFunctions.readPercentages(map)

return {
time = times[opponentIndex],
percentage = percentages[opponentIndex] or 0,
}
end

return CustomMatchGroupInput
Loading
Loading