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): start standardization of processMap #5049

Merged
merged 12 commits into from
Nov 7, 2024
59 changes: 52 additions & 7 deletions components/match2/commons/match_group_input_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1079,10 +1079,10 @@ end

---@class MatchParserInterface
---@field extractMaps fun(match: table, opponents: table[], mapProps: any?): table[]
---@field getBestOf fun(bestOfInput: string|integer, maps: table[]): integer
---@field calculateMatchScore? fun(maps: table[], opponents: table[]): fun(opponentIndex: integer): integer
---@field getBestOf fun(bestOfInput: string|integer|nil, maps: table[]): integer?
---@field calculateMatchScore? fun(maps: table[], opponents: table[]): fun(opponentIndex: integer): integer?
---@field removeUnsetMaps? fun(maps: table[]): table[]
---@field getExtraData? fun(match: table, games: table[], opponents: table[]): table
---@field getExtraData? fun(match: table, games: table[], opponents: table[]): table?
---@field adjustOpponent? fun(opponent: MGIParsedOpponent, opponentIndex: integer)
---@field getLinks? fun(match: table, games: table[]): table
---@field DEFAULT_MODE? string
Expand All @@ -1093,14 +1093,14 @@ end
---
--- The Parser injection must have the following functions:
--- - extractMaps(match, opponents, mapProps): table[]
--- - getBestOf(bestOfInput, maps): integer
--- - getBestOf(bestOfInput, maps): integer?
---
--- It may optionally have the following functions:
--- - calculateMatchScore(maps, opponents): fun(opponentIndex): integer
--- - calculateMatchScore(maps, opponents): fun(opponentIndex): integer?
--- - removeUnsetMaps(maps): table[]
--- - getExtraData(match, games, opponents): table
--- - getExtraData(match, games, opponents): table?
--- - adjustOpponent(opponent, opponentIndex)
--- - getLinks
--- - getLinks(match, games): table?
---
--- Additionally, the Parser may have the following properties:
--- - DEFAULT_MODE: string
Expand Down Expand Up @@ -1165,4 +1165,49 @@ function MatchGroupInputUtil.standardProcessMatch(match, Parser, mapProps)
return match
end

---@class MapParserInterface
---@field calculateMapScore? fun(map: table): fun(opponentIndex: integer): integer?
---@field getExtraData? fun(match: table, games: table, opponents: table[]): table?

--- It may optionally have the following functions:
--- - calculateMatchScore(map): fun(opponentIndex): integer?
--- - getExtraData(match, map, opponents): table?
---@param match table
---@param opponents table[]
---@param Parser MapParserInterface
---@param mapProps any?
Rathoz marked this conversation as resolved.
Show resolved Hide resolved
---@return table
function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser, mapProps)
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.extradata = Parser.getExtraData and Parser.getExtraData(match, map, opponents) or nil
map.finished = MatchGroupInputUtil.mapIsFinished(map)
Rathoz marked this conversation as resolved.
Show resolved Hide resolved

local opponentInfo = Array.map(opponents, function(_, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
opponentIndex = opponentIndex,
score = map['score' .. opponentIndex],
}, Parser.calculateMapScore and Parser.calculateMapScore(map) or nil)
return {score = score, status = status}
end)

map.scores = Array.map(opponentInfo, Operator.property('score'))
if map.finished then
map.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponentInfo)
map.walkover = MatchGroupInputUtil.getWalkover(map.resulttype, opponentInfo)
map.winner = MatchGroupInputUtil.getWinner(map.resulttype, winnerInput, opponentInfo)
hjpalpha marked this conversation as resolved.
Show resolved Hide resolved
end

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

return maps
end
Rathoz marked this conversation as resolved.
Show resolved Hide resolved

return MatchGroupInputUtil
54 changes: 8 additions & 46 deletions components/match2/wikis/rainbowsix/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ local Array = require('Module:Array')
local CharacterNames = require('Module:CharacterNames')
local FnUtil = require('Module:FnUtil')
local Lua = require('Module:Lua')
local Operator = require('Module:Operator')
local Table = require('Module:Table')

local MatchGroupInputUtil = Lua.import('Module:MatchGroup/Input/Util')

Expand All @@ -38,45 +36,15 @@ 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.extradata = MapFunctions.getExtraData(map, #opponents)
map.finished = MatchGroupInputUtil.mapIsFinished(map)

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

map.scores = Array.map(opponentInfo, Operator.property('score'))
if map.finished then
map.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponentInfo)
map.walkover = MatchGroupInputUtil.getWalkover(map.resulttype, opponentInfo)
map.winner = MatchGroupInputUtil.getWinner(map.resulttype, winnerInput, opponentInfo)
end

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

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

-- Template:Map sets a default map name so we can count the number of maps.
-- These maps however shouldn't be stored
-- The keepMap function will check if a map should be kept
---@param games table[]
---@return table[]
function MatchFunctions.removeUnsetMaps(games)
return Array.filter(games, MapFunctions.keepMap)
return Array.filter(games, function(map)
return map.map ~= nil
end)
end

---@param maps table[]
Expand All @@ -101,18 +69,12 @@ end
-- map related functions
--

-- Check if a map should be discarded due to being redundant
---@param map table
---@return boolean
function MapFunctions.keepMap(map)
return map.map ~= nil
end

-- Parse extradata information, particularally info about halfs and operator bans
---@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 = {
comment = map.comment,
t1firstside = {rt = map.t1firstside, ot = map.t1firstsideot},
Expand All @@ -121,7 +83,7 @@ function MapFunctions.getExtraData(map, opponentCount)
}

local getCharacterName = FnUtil.curry(MatchGroupInputUtil.getCharacterName, CharacterNames)
Array.forEach(Array.range(1, opponentCount), function(opponentIndex)
Array.forEach(opponents, function(_, opponentIndex)
extradata['t' .. opponentIndex .. 'bans'] = Array.map(Array.range(1, MAX_NUM_BANS), function(banIndex)
local ban = map['t' .. opponentIndex .. 'ban' .. banIndex]
return getCharacterName(ban) or ''
Expand Down
53 changes: 14 additions & 39 deletions components/match2/wikis/tft/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

local Array = require('Module:Array')
local Lua = require('Module:Lua')
local Operator = require('Module:Operator')
local Table = require('Module:Table')
local Variables = require('Module:Variables')

local MatchGroupInputUtil = Lua.import('Module:MatchGroup/Input/Util')
Expand Down Expand Up @@ -53,51 +51,28 @@ function MatchFunctions.getBestOf(bestofInput)
return bestof
end

---@param games table[]
---@return table[]
function MatchFunctions.removeUnsetMaps(games)
return Array.filter(games, function(map)
return map.map ~= nil
end)
end

---@param match table
---@param opponents table[]
---@return table[]
function MatchFunctions.extractMaps(match, opponents)
local maps = {}
for mapKey, map in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
if not map.map then
break
end
local finishedInput = map.finished --[[@as string?]]
local winnerInput = map.winner --[[@as string?]]

map.finished = MatchGroupInputUtil.mapIsFinished(map)

local opponentInfo = Array.map(opponents, function(_, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
opponentIndex = opponentIndex,
score = map['score' .. opponentIndex],
}, MapFunctions.calculateMapScore(map.winner, map.finished))
return {score = score, status = status}
end)

map.scores = Array.map(opponentInfo, Operator.property('score'))
if map.finished then
map.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponentInfo)
map.walkover = MatchGroupInputUtil.getWalkover(map.resulttype, opponentInfo)
map.winner = MatchGroupInputUtil.getWinner(map.resulttype, winnerInput, opponentInfo)
end

map.extradata = MapFunctions.getExtradata(map)

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

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

---@param mapInput table
---@param match table
---@param map table
---@param opponents table[]
---@return table
function MapFunctions.getExtradata(mapInput)
function MapFunctions.getExtraData(match, map, opponents)
return {
comment = mapInput.comment,
comment = map.comment,
}
end

Expand Down
63 changes: 25 additions & 38 deletions components/match2/wikis/trackmania/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
local Array = require('Module:Array')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local Operator = require('Module:Operator')
local Table = require('Module:Table')

local MatchGroupInputUtil = Lua.import('Module:MatchGroup/Input/Util')
local Opponent = Lua.import('Module:Opponent')
Expand All @@ -35,39 +33,24 @@ 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
if not map.map then
break
end
local finishedInput = map.finished --[[@as string?]]
local winnerInput = map.winner --[[@as string?]]

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

local opponentInfo = 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}
end)

map.scores = Array.map(opponentInfo, Operator.property('score'))
if map.finished then
map.resulttype = MatchGroupInputUtil.getResultType(winnerInput, finishedInput, opponentInfo)
map.walkover = MatchGroupInputUtil.getWalkover(map.resulttype, opponentInfo)
map.winner = MatchGroupInputUtil.getWinner(map.resulttype, winnerInput, opponentInfo)
end

table.insert(maps, map)
match[key] = nil
end
return MatchGroupInputUtil.standardProcessMaps(match, opponents, MapFunctions)
end

return maps
---@param games table[]
---@return table[]
function MatchFunctions.removeUnsetMaps(games)
return Array.filter(games, function(map)
return map.map ~= nil
end)
end

---@param opponent MGIParsedOpponent
---@param opponentIndex integer
function MatchFunctions.adjustOpponent(opponent, opponentIndex)
opponent.extradata = CustomMatchGroupInput.getOpponentExtradata(opponent)
if opponent.extradata.additionalScores then
opponent.score = CustomMatchGroupInput._getSetWins(opponent)
end
end

---@param opponent table
Expand Down Expand Up @@ -105,10 +88,12 @@ function MatchFunctions.isFeatured(match)
end

---@param match table
---@param games table[]
---@param opponents table[]
---@return table
function MatchFunctions.getExtraData(match)
local opponent1 = match.opponent1 or {}
local opponent2 = match.opponent2 or {}
function MatchFunctions.getExtraData(match, games, opponents)
local opponent1 = opponents[1] or {}
local opponent2 = opponents[2] or {}

return {
isfeatured = MatchFunctions.isFeatured(match),
Expand All @@ -118,9 +103,11 @@ function MatchFunctions.getExtraData(match)
}
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,
overtime = Logic.readBool(map.overtime)
Expand Down
Loading
Loading