Skip to content

Commit

Permalink
feat(match2): standardize aoe's match parsing (#5273)
Browse files Browse the repository at this point in the history
* feat(match2): standardize aoe's match parrsing

* linebreak
  • Loading branch information
Rathoz authored Jan 3, 2025
1 parent d8c49ea commit 30d1337
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 59 deletions.
6 changes: 5 additions & 1 deletion components/match2/commons/match_group_input_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,8 @@ end
---@field getHeadToHeadLink? fun(match: table, opponents: table[]): string?
---@field readDate? readDateFunction
---@field getMode? fun(opponents: table[]): string
---@field readOpponent? fun(match: table, opponentIndex: integer, opponentConfig: readOpponentOptions?):
---MGIParsedOpponent
---@field DEFAULT_MODE? string
---@field DATE_FALLBACKS? string[]
---@field OPPONENT_CONFIG? readOpponentOptions
Expand All @@ -1093,6 +1095,7 @@ end
--- - getHeadToHeadLink(match, opponents): string?
--- - readDate(match): table
--- - getMode(opponents): string?
--- - readOpponent(match, opponentIndex, opponentConfig): MGIParsedOpponent
---
--- Additionally, the Parser may have the following properties:
--- - DEFAULT_MODE: string
Expand All @@ -1110,8 +1113,9 @@ function MatchGroupInputUtil.standardProcessMatch(match, Parser, FfaParser, mapP
local dateProps = MatchGroupInputUtil.getMatchDate(Parser, matchInput)
Table.mergeInto(match, dateProps)

local readOpponent = Parser.readOpponent or MatchGroupInputUtil.readOpponent
local opponents = Array.mapIndexes(function(opponentIndex)
local opponent = MatchGroupInputUtil.readOpponent(match, opponentIndex, Parser.OPPONENT_CONFIG)
local opponent = readOpponent(match, opponentIndex, Parser.OPPONENT_CONFIG)
if opponent and Parser.adjustOpponent then
Parser.adjustOpponent(opponent, opponentIndex)
end
Expand Down
83 changes: 25 additions & 58 deletions components/match2/wikis/ageofempires/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,78 +20,36 @@ local Variables = require('Module:Variables')

local MatchGroupInputUtil = Lua.import('Module:MatchGroup/Input/Util')
local Opponent = Lua.import('Module:Opponent')
local Streams = Lua.import('Module:Links/Stream')

local CustomMatchGroupInput = {}
local MapFunctions = {
BREAK_ON_EMPTY = true,
}

local OPPONENT_CONFIG = {
resolveRedirect = true,
pagifyTeamNames = true,
}

local MatchFunctions = {
OPPONENT_CONFIG = OPPONENT_CONFIG,
}
local MapFunctions = {
BREAK_ON_EMPTY = true,
}

---@param match table
---@param options table?
---@return table
function CustomMatchGroupInput.processMatch(match, options)
assert(not Logic.readBool(match.ffa), 'FFA is not yet supported in AoE match2.')
Table.mergeInto(match, MatchGroupInputUtil.getTournamentContext(match))
match.game, match.mapsInfo = CustomMatchGroupInput._getMapsAndGame(match)

Table.mergeInto(match, MatchGroupInputUtil.getMatchDate({}, match))

local opponents = Array.mapIndexes(function(opponentIndex)
return CustomMatchGroupInput.readOpponent(match, opponentIndex, OPPONENT_CONFIG)
end)
match.game, match.mapsInfo = MatchFunctions.getGameAndMapsFromTournament(match)

local games = CustomMatchGroupInput.extractMaps(match, opponents)
match.links = MatchGroupInputUtil.getLinks(match)
match.links.headtohead = CustomMatchGroupInput.getHeadToHeadLink(match, opponents)

local autoScoreFunction = MatchGroupInputUtil.canUseAutoScore(match, games)
and CustomMatchGroupInput.calculateMatchScore(games)
or nil

Array.forEach(opponents, function(opponent, opponentIndex)
opponent.score, opponent.status = MatchGroupInputUtil.computeOpponentScore({
walkover = match.walkover,
winner = match.winner,
opponentIndex = opponentIndex,
score = opponent.score,
}, autoScoreFunction)
end)
match.bestof = CustomMatchGroupInput.getBestOf(match.bestof)

local winnerInput = match.winner --[[@as string?]]
local finishedInput = match.finished --[[@as string?]]
match.finished = MatchGroupInputUtil.matchIsFinished(match, games, opponents)

if match.finished then
match.status = MatchGroupInputUtil.getMatchStatus(winnerInput, finishedInput)
match.winner = MatchGroupInputUtil.getWinner(match.status, winnerInput, opponents)
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner(match.status, match.winner, opponentIndex)
end)
end

match.mode = Opponent.toLegacyMode(opponents[1].type, opponents[2].type)
match.stream = Streams.processStreams(match)

match.games = games
match.opponents = opponents

match.extradata = CustomMatchGroupInput._getExtraData(match)

return match
return MatchGroupInputUtil.standardProcessMatch(match, MatchFunctions)
end

---@param match table
---@param opponentIndex integer
---@param options readOpponentOptions
---@return table?
function CustomMatchGroupInput.readOpponent(match, opponentIndex, options)
function MatchFunctions.readOpponent(match, opponentIndex, options)
options = options or {}
local opponentInput = Json.parseIfString(Table.extract(match, 'opponent' .. opponentIndex))
if not opponentInput then
Expand Down Expand Up @@ -149,13 +107,14 @@ end
---@param match table
---@param opponents table[]
---@return table[]
function CustomMatchGroupInput.extractMaps(match, opponents)
function MatchFunctions.extractMaps(match, opponents)
return MatchGroupInputUtil.standardProcessMaps(match, opponents, MapFunctions)
end

---@param bestofInput string|integer?
---@param maps table[]
---@return integer?
function CustomMatchGroupInput.getBestOf(bestofInput)
function MatchFunctions.getBestOf(bestofInput, maps)
local bestof = tonumber(bestofInput) or tonumber(Variables.varDefault('bestof'))

if bestof then
Expand All @@ -165,9 +124,15 @@ function CustomMatchGroupInput.getBestOf(bestofInput)
return bestof
end

---@param opponents table[]
---@return string
function MatchFunctions.getMode(opponents)
return Opponent.toLegacyMode(opponents[1].type, opponents[2].type)
end

---@param match table
---@return string?, table?
function CustomMatchGroupInput._getMapsAndGame(match)
function MatchFunctions.getGameAndMapsFromTournament(match)
local mapsInfo = Json.parseIfString(Variables.varDefault('tournament_maps'))

if Logic.isNotEmpty(mapsInfo) and match.game then
Expand Down Expand Up @@ -196,15 +161,17 @@ end

---@param maps table[]
---@return fun(opponentIndex: integer): integer
function CustomMatchGroupInput.calculateMatchScore(maps)
function MatchFunctions.calculateMatchScore(maps)
return function(opponentIndex)
return MatchGroupInputUtil.computeMatchScoreFromMapWinners(maps, opponentIndex)
end
end

---@param match table
---@param games table[]
---@param opponents table[]
---@return table
function CustomMatchGroupInput._getExtraData(match)
function MatchFunctions.getExtraData(match, games, opponents)
return {
casters = MatchGroupInputUtil.readCasters(match, {noSort = true}),
}
Expand All @@ -213,7 +180,7 @@ end
---@param match table
---@param opponents table[]
---@return string?
function CustomMatchGroupInput.getHeadToHeadLink(match, opponents)
function MatchFunctions.getHeadToHeadLink(match, opponents)
if opponents[1].type ~= Opponent.solo or opponents[2].type ~= Opponent.solo then
return
end
Expand Down

0 comments on commit 30d1337

Please sign in to comment.