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
65 changes: 55 additions & 10 deletions components/match2/commons/match_group_input_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1099,10 +1099,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 getHeadToHeadLink? fun(match: table, opponents: table[]): string?
Expand All @@ -1122,17 +1122,17 @@ 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(match, games)
--- - getHeadToHeadLink(match, opponents)
--- - readDate(match)
--- - getMode(opponents)
--- - getLinks(match, games): table?
--- - getHeadToHeadLink(match, opponents): string?
--- - readDate(match): table
--- - getMode(opponents): string?
---
--- Additionally, the Parser may have the following properties:
--- - DEFAULT_MODE: string
Expand Down Expand Up @@ -1202,4 +1202,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
---@return table
function MatchGroupInputUtil.standardProcessMaps(match, opponents, Parser)
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.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

map.extradata = Parser.getExtraData and Parser.getExtraData(match, map, opponents) or nil

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

return maps
end

return MatchGroupInputUtil
48 changes: 12 additions & 36 deletions components/match2/wikis/arenafps/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 All @@ -34,42 +32,18 @@ end
--

---@param match table
---@param opponents MGIParsedOpponent[]
---@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)
Rathoz marked this conversation as resolved.
Show resolved Hide resolved
return Array.filter(games, function(map)
return map.map ~= nil
end)
end

---@param bestofInput string|integer?
Expand Down Expand Up @@ -97,9 +71,11 @@ end
-- map related functions
--

---@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,
}
Expand Down
63 changes: 13 additions & 50 deletions components/match2/wikis/battlerite/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

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

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

local CustomMatchGroupInput = {}
local MatchFunctions = {}
local MapFunctions = {}

MatchFunctions.getBestOf = MatchGroupInputUtil.getBestOf
MatchFunctions.OPPONENT_CONFIG = {
resolveRedirect = true,
pagifyTeamNames = true,
Expand All @@ -33,50 +32,20 @@ end
--
-- match related functions
--
MatchFunctions.getBestOf = MatchGroupInputUtil.getBestOf

-- 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)
end

---@param match table
---@param opponents MGIParsedOpponent[]
---@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],
})
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 maps table[]
Expand All @@ -99,17 +68,11 @@ 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

---@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)
return {
comment = map.comment,
}
Expand Down
54 changes: 14 additions & 40 deletions components/match2/wikis/callofduty/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,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 Variables = require('Module:Variables')

local MatchGroupInputUtil = Lua.import('Module:MatchGroup/Input/Util')
Expand All @@ -30,48 +28,23 @@ function CustomMatchGroupInput.processMatch(match, options)
return MatchGroupInputUtil.standardProcessMatch(match, MatchFunctions)
end

--
-- match related functions
--
---@param match table
---@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 maps
return MatchGroupInputUtil.standardProcessMaps(match, opponents, MapFunctions)
end

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

---@param maps table[]
---@return fun(opponentIndex: integer): integer?
Expand Down Expand Up @@ -101,10 +74,11 @@ end
-- map related functions
--

-- Parse extradata information
---@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,
}
Expand Down
Loading
Loading