Skip to content

Commit

Permalink
feat(match2): Make use of standardProcessMaps on aoe (#5215)
Browse files Browse the repository at this point in the history
* mapDisplayName support

* aoe use `standardProcessMaps`

* Update components/match2/wikis/ageofempires/match_group_input_custom.lua

Co-authored-by: Rikard Blixt <[email protected]>

* set `getPlayersOfMapOpponent` Parser instead of custom module

---------

Co-authored-by: Rikard Blixt <[email protected]>
  • Loading branch information
hjpalpha and Rathoz authored Dec 19, 2024
1 parent ed36b2b commit 467388e
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 56 deletions.
17 changes: 13 additions & 4 deletions components/match2/commons/match_group_input_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1159,14 +1159,15 @@ 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, mapIndex: integer, match: table): string?
---@field getMapName? fun(game: table, mapIndex: integer, match: table): string?, 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 getGame? fun(match: table, map:table): string?
---@field ADD_SUB_GROUP? boolean
---@field BREAK_ON_EMPTY? boolean

Expand All @@ -1175,14 +1176,15 @@ end
--- The Parser injection may optionally have the following functions:
--- - calculateMapScore(map): fun(opponentIndex): integer?
--- - getExtraData(match, map, opponents): table?
--- - getMapName(map): string?
--- - getMapName(map, mapIndex, match): string?, string?
--- - getMapMode(match, map, opponents): string?
--- - getPlayersOfMapOpponent(map, opponent, opponentIndex): table[]?
--- - getPatch(game): string?
--- - mapIsFinished(map, opponents): boolean
--- - extendMapOpponent(map, opponentIndex): table
--- - getMapBestOf(map): integer?
--- - computeOpponentScore(props, autoScore): integer?, string?
--- - getGame(match, map): string?
---
--- Additionally, the Parser may have the following properties:
--- - ADD_SUB_GROUP boolean?
Expand All @@ -1207,7 +1209,7 @@ function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser)
end

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

if Parser.getMapBestOf then
Expand All @@ -1224,6 +1226,10 @@ function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser)
map.patch = Parser.getPatch(map)
end

if Parser.getGame then
map.game = Parser.getGame(match, map)
end

map.opponents = Array.map(opponents, function(opponent, opponentIndex)
local computeOpponentScore = Parser.computeOpponentScore or MatchGroupInputUtil.computeOpponentScore
local score, status = computeOpponentScore({
Expand Down Expand Up @@ -1254,7 +1260,10 @@ function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser)
map.winner = MatchGroupInputUtil.getWinner(map.status, winnerInput, map.opponents)
end

map.extradata = Parser.getExtraData and Parser.getExtraData(match, map, opponents) or nil
map.extradata = Table.merge(
{displayname = map.mapDisplayName},
Parser.getExtraData and Parser.getExtraData(match, map, opponents) or nil
)

table.insert(maps, map)
match[key] = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ end
function MapFunctions.getExtraData(match, map, opponents)
local extradata = {
comment = map.comment,
displayname = map.mapDisplayName,
header = map.header,
server = map.server,
}
Expand Down Expand Up @@ -420,10 +419,11 @@ end
---@param gameIndex table
---@param match table
---@return string?
---@return string?
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)
return mw.ext.TeamLiquidIntegration.resolve_redirect(game.map), game.mapDisplayName
elseif mapName then
return TBD
end
Expand Down
68 changes: 22 additions & 46 deletions components/match2/wikis/ageofempires/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ local Opponent = Lua.import('Module:Opponent')
local Streams = Lua.import('Module:Links/Stream')

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

local OPPONENT_CONFIG = {
resolveRedirect = true,
Expand Down Expand Up @@ -148,43 +150,7 @@ end
---@param opponents table[]
---@return table[]
function CustomMatchGroupInput.extractMaps(match, opponents)
local maps = {}
for key, map in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
if map.map == nil and map.winner == nil then
break
end
local finishedInput = map.finished --[[@as string?]]
local winnerInput = map.winner --[[@as string?]]

map.extradata = MapFunctions.getExtraData(match, map, opponents)
map.map, map.extradata.displayname = CustomMatchGroupInput._getMapName(map, match.mapsInfo)
map.extradata.mapmode = Table.extract(map, 'mode')

Table.mergeInto(map, MatchGroupInputUtil.getTournamentContext(map, match))

map.finished = MatchGroupInputUtil.mapIsFinished(map)
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],
}, CustomMatchGroupInput.calculateMapScore(map.winner, map.finished))
local players = CustomMatchGroupInput.getPlayersOfMapOpponent(map, opponent, opponentIndex)
return {score = score, status = status, players = players}
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 @@ -268,10 +234,13 @@ function CustomMatchGroupInput.getHeadToHeadLink(match, opponents)
end

---@param map table
---@param mapsInfo {name: string, link: string}[]?
---@param mapIndex integer
---@param match table
---@return string?
---@return string?
function CustomMatchGroupInput._getMapName(map, mapsInfo)
function MapFunctions.getMapName(map, mapIndex, match)
---@type {name: string, link: string}[]?
local mapsInfo = match.mapsInfo
if String.isEmpty(map.map) or map.map == 'TBD' then
return
end
Expand All @@ -294,7 +263,7 @@ end
---@param opponent table
---@param opponentIndex integer
---@return {civ: string?, flag: string?, displayName: string?, pageName: string?}[]
function CustomMatchGroupInput.getPlayersOfMapOpponent(map, opponent, opponentIndex)
function MapFunctions.getPlayersOfMapOpponent(map, opponent, opponentIndex)
local players
if opponent.type == Opponent.team then
players = Array.parseCommaSeparatedString(map['players' .. opponentIndex])
Expand Down Expand Up @@ -325,14 +294,13 @@ function CustomMatchGroupInput.getPlayersOfMapOpponent(map, opponent, opponentIn

end

---@param winnerInput string|integer|nil
---@param finished boolean
---@param map table
---@return fun(opponentIndex: integer): integer?
function CustomMatchGroupInput.calculateMapScore(winnerInput, finished)
local winner = tonumber(winnerInput)
function MapFunctions.calculateMapScore(map)
local winner = tonumber(map.winner)
return function(opponentIndex)
-- TODO Better to check if map has started, rather than finished, for a more correct handling
if not winner and not finished then
if not winner and not map.finished then
return
end
return winner == opponentIndex and 1 or 0
Expand All @@ -346,7 +314,15 @@ end
function MapFunctions.getExtraData(match, map, opponents)
return {
comment = map.comment,
mapmode = Table.extract(map, 'mode'),
}
end

---@param match table
---@param map table
---@return string?
function MapFunctions.getGame(match, map)
return Logic.emptyOr(map.game, match.game, Variables.varDefault('tournament_game'))
end

return CustomMatchGroupInput
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ end
---@param mapIndex integer
---@param match table
---@return string?
---@return string?
function MapFunctions.getMapName(map, mapIndex, match)
if String.isNotEmpty(map.map) and map.map ~= 'TBD' then
return mw.ext.TeamLiquidIntegration.resolve_redirect(map.map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ end
---@param mapIndex integer
---@param match table
---@return string?
---@return string?
function MapFunctions.getMapName(map, mapIndex, match)
return nil
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ end
---@param mapIndex table
---@param match table
---@return string?
---@return string?
function MapFunctions.getMapName(map, mapIndex, match)
return nil
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ end
---@param mapIndex integer
---@param match table
---@return string
---@return string?
function MapFunctions.getMapName(map, mapIndex, match)
if Logic.readBool(match.hasSubmatches) then
-- generic map name (not displayed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ end
---@param mapIndex integer
---@param match table
---@return string?
---@return string?
function MapFunctions.getMapName(map, mapIndex, match)
if map.map == DUMMY_MAP then
return nil
Expand Down
1 change: 1 addition & 0 deletions components/match2/wikis/magic/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ end
---@param mapIndex integer
---@param match table
---@return string?
---@return string?
function MapFunctions.getMapName(map, mapIndex, match)
return nil
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ end
---@param gameIndex integer
---@param match table
---@return string?
---@return string?
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)
return mw.ext.TeamLiquidIntegration.resolve_redirect(game.map), game.mapDisplayName
elseif mapName then
return TBD
end
Expand Down Expand Up @@ -305,7 +306,6 @@ end
function MapFunctions.getExtraData(match, map, opponents)
local extradata = {
comment = map.comment,
displayname = map.mapDisplayName,
header = map.header,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ end
---@param mapIndex integer
---@param match table
---@return string?
---@return string?
function MapFunctions.getMapName(map, mapIndex, match)
return nil
end
Expand Down
4 changes: 2 additions & 2 deletions components/match2/wikis/warcraft/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ end
---@param gameIndex integer
---@param match table
---@return string?
---@return string?
function MapFunctions.getMapName(game, gameIndex, match)
return (MapsData[(game.map or ''):lower()] or {}).name or game.map
return (MapsData[(game.map or ''):lower()] or {}).name or game.map, game.mapDisplayName
end

---@param map table
Expand Down Expand Up @@ -337,7 +338,6 @@ end
function MapFunctions.getExtraData(match, map, opponents)
local extradata = {
comment = map.comment,
displayname = map.mapDisplayName,
header = map.header,
}

Expand Down

0 comments on commit 467388e

Please sign in to comment.