diff --git a/lua/wikis/commons/MatchGroup/Util.lua b/lua/wikis/commons/MatchGroup/Util.lua index 21a3e75b87d..9aab07c17a4 100644 --- a/lua/wikis/commons/MatchGroup/Util.lua +++ b/lua/wikis/commons/MatchGroup/Util.lua @@ -325,6 +325,9 @@ MatchGroupUtil.types.Match = TypeUtil.struct({ extradata = 'table?', }) +---@class MatchGroupUtilSubgroup +---@field games MatchGroupUtilGame[] +---@field subgroup number ---@class FFAMatchGroupUtilMatch: MatchGroupUtilMatch ---@field games FFAMatchGroupUtilGame[] @@ -771,6 +774,33 @@ function MatchGroupUtil.gameFromRecord(record, opponentCount) } end +---Group games on the subgroup field to form submatches +---@param matchGames MatchGroupUtilGame[] +---@return MatchGroupUtilSubgroup[] +function MatchGroupUtil.groupBySubgroup(matchGames) + local previousSubgroup = nil + ---@type MatchGroupUtilGame[]? + local currentGames = nil + ---@type MatchGroupUtilGame[][] + local submatchGames = {} + Array.forEach(matchGames, function (game) + if previousSubgroup == nil or previousSubgroup ~= game.subgroup then + currentGames = {} + Array.appendWith(submatchGames, currentGames) + previousSubgroup = game.subgroup + end + ---@cast currentGames -nil + Array.appendWith(currentGames, game) + end) + return Array.map(submatchGames, function (games, groupIndex) + ---@type MatchGroupUtilSubgroup + return { + games = games, + subgroup = groupIndex + } + end) +end + ---@param data table ---@return string[] function MatchGroupUtil.computeLowerMatchIdsFromLegacy(data) diff --git a/lua/wikis/commons/MatchGroup/Util/Starcraft.lua b/lua/wikis/commons/MatchGroup/Util/Starcraft.lua index 2ed203cdc77..c655dc72d66 100644 --- a/lua/wikis/commons/MatchGroup/Util/Starcraft.lua +++ b/lua/wikis/commons/MatchGroup/Util/Starcraft.lua @@ -9,6 +9,7 @@ local Lua = require('Module:Lua') local Array = Lua.import('Module:Array') local Faction = Lua.import('Module:Faction') +local FnUtil = Lua.import('Module:FnUtil') local Logic = Lua.import('Module:Logic') local Operator = Lua.import('Module:Operator') local String = Lua.import('Module:StringUtils') @@ -40,12 +41,11 @@ local StarcraftMatchGroupUtil = Table.deepCopy(MatchGroupUtil) ---@field map string ---@field displayName string? ----@class StarcraftMatchGroupUtilSubmatch +---@class StarcraftMatchGroupUtilSubmatch: MatchGroupUtilSubgroup ---@field games StarcraftMatchGroupUtilGame[] ---@field mode string ---@field status string? ---@field opponents StarcraftMatchGroupUtilGameOpponent[] ----@field subgroup number ---@field winner number? ---@field header string? @@ -82,8 +82,8 @@ function StarcraftMatchGroupUtil.matchFromRecord(record) if match.opponentMode == 'team' then -- Compute submatches match.submatches = Array.map( - StarcraftMatchGroupUtil.groupBySubmatch(match.games), - function(games) return StarcraftMatchGroupUtil.constructSubmatch(games, match) end + MatchGroupUtil.groupBySubgroup(match.games), + FnUtil.curry(StarcraftMatchGroupUtil.constructSubmatch, match) ) end @@ -160,31 +160,12 @@ function StarcraftMatchGroupUtil.computeGameOpponents(game, matchOpponents) end) end ----Group games on the subgroup field to form submatches ----@param matchGames StarcraftMatchGroupUtilGame[] ----@return StarcraftMatchGroupUtilGame[][] -function StarcraftMatchGroupUtil.groupBySubmatch(matchGames) - -- Group games on adjacent subgroups - local previousSubgroup = nil - local currentGames = nil - local submatchGames = {} - for _, game in ipairs(matchGames) do - if previousSubgroup == nil or previousSubgroup ~= game.subgroup then - currentGames = {} - table.insert(submatchGames, currentGames) - previousSubgroup = game.subgroup - end - ---@cast currentGames -nil - table.insert(currentGames, game) - end - return submatchGames -end - ---Constructs a submatch object whose properties are aggregated from that of its games. ----@param games StarcraftMatchGroupUtilGame[] ---@param match StarcraftMatchGroupUtilMatch +---@param subgroup MatchGroupUtilSubgroup ---@return StarcraftMatchGroupUtilSubmatch -function StarcraftMatchGroupUtil.constructSubmatch(games, match) +function StarcraftMatchGroupUtil.constructSubmatch(match, subgroup) + local games = subgroup.games local firstGame = games[1] local opponents = Table.deepCopy(firstGame.opponents) local isSubmatch = String.startsWith(firstGame.map or '', 'Submatch') @@ -228,14 +209,12 @@ function StarcraftMatchGroupUtil.constructSubmatch(games, match) opponent.placement = MatchGroupInputUtil.placementFromWinner('', winner, opponentIndex) end) - return { - games = games, + return Table.mergeInto({ mode = firstGame.mode, opponents = opponents, - subgroup = firstGame.subgroup, winner = winner, - header = Table.extract(match.extradata or {}, 'subgroup' .. firstGame.subgroup .. 'header'), - } + header = Table.extract(match.extradata or {}, 'subgroup' .. subgroup.subgroup .. 'header'), + }, subgroup) end ---Determine if a match has details that should be displayed via popup diff --git a/lua/wikis/hearthstone/MatchGroup/Util/Custom.lua b/lua/wikis/hearthstone/MatchGroup/Util/Custom.lua index 6f59cff7e75..e35da40719f 100644 --- a/lua/wikis/hearthstone/MatchGroup/Util/Custom.lua +++ b/lua/wikis/hearthstone/MatchGroup/Util/Custom.lua @@ -8,6 +8,7 @@ local Lua = require('Module:Lua') local Array = Lua.import('Module:Array') +local FnUtil = Lua.import('Module:FnUtil') local Logic = Lua.import('Module:Logic') local Operator = Lua.import('Module:Operator') local Table = Lua.import('Module:Table') @@ -24,10 +25,8 @@ local CustomMatchGroupUtil = Table.deepCopy(MatchGroupUtil) ---@class HearthstoneMatchGroupUtilGameOpponent: GameOpponent ---@field placement number? ----@class HearthstoneMatchGroupUtilSubmatch ----@field games MatchGroupUtilGame[] +---@class HearthstoneMatchGroupUtilSubmatch: MatchGroupUtilSubgroup ---@field opponents HearthstoneMatchGroupUtilGameOpponent[] ----@field subgroup number ---@field winner number? ---@field header string? @@ -35,7 +34,7 @@ local CustomMatchGroupUtil = Table.deepCopy(MatchGroupUtil) ---@field submatches HearthstoneMatchGroupUtilSubmatch[]? ---@field isTeamMatch boolean ----@param record table +---@param record match2 ---@return HearthstoneMatchGroupUtilMatch function CustomMatchGroupUtil.matchFromRecord(record) local match = MatchGroupUtil.matchFromRecord(record) --[[@as HearthstoneMatchGroupUtilMatch]] @@ -55,16 +54,10 @@ function CustomMatchGroupUtil.matchFromRecord(record) -- Compute submatches match.submatches = Array.map( - CustomMatchGroupUtil.groupBySubmatch(match.games), - function(games) return CustomMatchGroupUtil.constructSubmatch(games) end + MatchGroupUtil.groupBySubgroup(match.games), + FnUtil.curry(CustomMatchGroupUtil.constructSubmatch, match) ) - local extradata = match.extradata - ---@cast extradata table - Array.forEach(match.submatches, function (submatch) - submatch.header = Table.extract(extradata, 'subgroup' .. submatch.subgroup .. 'header') - end) - return match end @@ -82,30 +75,12 @@ function CustomMatchGroupUtil.computeGameOpponents(game, matchOpponents) end) end ----Group games on the subgroup field to form submatches ----@param matchGames MatchGroupUtilGame[] ----@return MatchGroupUtilGame[][] -function CustomMatchGroupUtil.groupBySubmatch(matchGames) - -- Group games on adjacent subgroups - local previousSubgroup = nil - local currentGames = nil - local submatchGames = {} - Array.forEach(matchGames, function (game) - if previousSubgroup == nil or previousSubgroup ~= game.subgroup then - currentGames = {} - table.insert(submatchGames, currentGames) - previousSubgroup = game.subgroup - end - ---@cast currentGames -nil - table.insert(currentGames, game) - end) - return submatchGames -end - ---Constructs a submatch object whose properties are aggregated from that of its games. ----@param games MatchGroupUtilGame[] +---@param match HearthstoneMatchGroupUtilMatch +---@param subgroup MatchGroupUtilSubgroup ---@return HearthstoneMatchGroupUtilSubmatch -function CustomMatchGroupUtil.constructSubmatch(games) +function CustomMatchGroupUtil.constructSubmatch(match, subgroup) + local games = subgroup.games local firstGame = games[1] local opponents = Table.deepCopy(firstGame.opponents) local isSubmatch = string.find(firstGame.map or '', '^[sS]ubmatch %d+$') @@ -133,12 +108,13 @@ function CustomMatchGroupUtil.constructSubmatch(games) opponent.placement = MatchGroupInputUtil.placementFromWinner('', winner, opponentIndex) end) - return { - games = games, + local matchExtradata = match.extradata or {} + + return Table.mergeInto({ + header = Table.extract(matchExtradata, 'subgroup' .. subgroup.subgroup .. 'header'), opponents = opponents, - subgroup = firstGame.subgroup, winner = winner, - } + }, subgroup) end return CustomMatchGroupUtil diff --git a/lua/wikis/stormgate/MatchGroup/Util/Custom.lua b/lua/wikis/stormgate/MatchGroup/Util/Custom.lua index 20b26e61e88..785fcfe2786 100644 --- a/lua/wikis/stormgate/MatchGroup/Util/Custom.lua +++ b/lua/wikis/stormgate/MatchGroup/Util/Custom.lua @@ -9,6 +9,7 @@ local Lua = require('Module:Lua') local Array = Lua.import('Module:Array') local Faction = Lua.import('Module:Faction') +local FnUtil = Lua.import('Module:FnUtil') local Logic = Lua.import('Module:Logic') local Operator = Lua.import('Module:Operator') local String = Lua.import('Module:StringUtils') @@ -37,10 +38,11 @@ CustomMatchGroupUtil.types.Player = TypeUtil.extendStruct(MatchGroupUtil.types.P ---@field position integer ---@field random boolean ----@class StormgateMatchGroupUtilGameOpponent:GameOpponent +---@class StormgateMatchGroupUtilGameOpponent: GameOpponent ---@field placement number? ---@field players StormgateMatchGroupUtilGamePlayer[] ---@field score number? + CustomMatchGroupUtil.types.GameOpponent = TypeUtil.struct({ placement = 'number?', players = TypeUtil.array(CustomMatchGroupUtil.types.Player), @@ -55,12 +57,11 @@ CustomMatchGroupUtil.types.GameOpponent = TypeUtil.struct({ ---@field by number? ---@field map string ----@class StormgateMatchGroupUtilSubmatch +---@class StormgateMatchGroupUtilSubmatch: MatchGroupUtilSubgroup ---@field games StormgateMatchGroupUtilGame[] ---@field mode string ---@field opponents StormgateMatchGroupUtilGameOpponent[] ---@field status string? ----@field subgroup number ---@field winner number? ---@field header string? @@ -91,8 +92,8 @@ function CustomMatchGroupUtil.matchFromRecord(record) if not match.isUniformMode then -- Compute submatches match.submatches = Array.map( - CustomMatchGroupUtil.groupBySubmatch(match.games), - function(games) return CustomMatchGroupUtil.constructSubmatch(games, match) end + MatchGroupUtil.groupBySubgroup(match.games), + FnUtil.curry(CustomMatchGroupUtil.constructSubmatch, match) ) end @@ -152,31 +153,12 @@ function CustomMatchGroupUtil.computeGameOpponents(game, matchOpponents) end) end ----Group games on the subgroup field to form submatches ----@param matchGames StormgateMatchGroupUtilGame[] ----@return StormgateMatchGroupUtilGame[][] -function CustomMatchGroupUtil.groupBySubmatch(matchGames) - -- Group games on adjacent subgroups - local previousSubgroup = nil - local currentGames = nil - local submatchGames = {} - for _, game in ipairs(matchGames) do - if previousSubgroup == nil or previousSubgroup ~= game.subgroup then - currentGames = {} - table.insert(submatchGames, currentGames) - previousSubgroup = game.subgroup - end - ---@cast currentGames -nil - table.insert(currentGames, game) - end - return submatchGames -end - ---Constructs a submatch object whose properties are aggregated from that of its games. ----@param games StormgateMatchGroupUtilGame[] ---@param match StormgateMatchGroupUtilMatch +---@param subgroup MatchGroupUtilSubgroup ---@return StormgateMatchGroupUtilSubmatch -function CustomMatchGroupUtil.constructSubmatch(games, match) +function CustomMatchGroupUtil.constructSubmatch(match, subgroup) + local games = subgroup.games local firstGame = games[1] local opponents = Table.deepCopy(firstGame.opponents) local isSubmatch = String.startsWith(firstGame.map or '', 'Submatch') @@ -214,14 +196,12 @@ function CustomMatchGroupUtil.constructSubmatch(games, match) CustomMatchGroupUtil._determineSubmatchPlayerFactions(match, games, opponents, opponentIndex) end) - return { - games = games, + return Table.mergeInto({ mode = firstGame.mode, opponents = opponents, - subgroup = firstGame.subgroup, winner = winner, - header = Table.extract(match.extradata or {}, 'subgroup' .. firstGame.subgroup .. 'header'), - } + header = Table.extract(match.extradata or {}, 'subgroup' .. subgroup.subgroup .. 'header'), + }, subgroup) end ---@param match StormgateMatchGroupUtilMatch diff --git a/lua/wikis/warcraft/MatchGroup/Util/Custom.lua b/lua/wikis/warcraft/MatchGroup/Util/Custom.lua index a7047f1d107..8d8417db6c3 100644 --- a/lua/wikis/warcraft/MatchGroup/Util/Custom.lua +++ b/lua/wikis/warcraft/MatchGroup/Util/Custom.lua @@ -9,6 +9,7 @@ local Lua = require('Module:Lua') local Array = Lua.import('Module:Array') local Faction = Lua.import('Module:Faction') +local FnUtil = Lua.import('Module:FnUtil') local Logic = Lua.import('Module:Logic') local Operator = Lua.import('Module:Operator') local String = Lua.import('Module:StringUtils') @@ -44,12 +45,11 @@ local CustomMatchGroupUtil = Table.deepCopy(MatchGroupUtil) ---@field by number? ---@field map string ----@class WarcraftMatchGroupUtilSubmatch +---@class WarcraftMatchGroupUtilSubmatch: MatchGroupUtilSubgroup ---@field games WarcraftMatchGroupUtilGame[] ---@field mode string ---@field opponents WarcraftMatchGroupUtilGameOpponent[] ---@field status string? ----@field subgroup number ---@field winner number? ---@field header string? @@ -83,8 +83,8 @@ function CustomMatchGroupUtil.matchFromRecord(record) if match.opponentMode == TEAM_DISPLAY_MODE then -- Compute submatches match.submatches = Array.map( - CustomMatchGroupUtil.groupBySubmatch(match.games), - function(games) return CustomMatchGroupUtil.constructSubmatch(games, match) end + MatchGroupUtil.groupBySubgroup(match.games), + FnUtil.curry(CustomMatchGroupUtil.constructSubmatch, match) ) end @@ -145,31 +145,12 @@ function CustomMatchGroupUtil.computeGameOpponents(game, matchOpponents) end) end ----Group games on the subgroup field to form submatches ----@param matchGames WarcraftMatchGroupUtilGame[] ----@return WarcraftMatchGroupUtilGame[][] -function CustomMatchGroupUtil.groupBySubmatch(matchGames) - -- Group games on adjacent subgroups - local previousSubgroup = nil - local currentGames = nil - local submatchGames = {} - for _, game in ipairs(matchGames) do - if previousSubgroup == nil or previousSubgroup ~= game.subgroup then - currentGames = {} - table.insert(submatchGames, currentGames) - previousSubgroup = game.subgroup - end - ---@cast currentGames -nil - table.insert(currentGames, game) - end - return submatchGames -end - ---Constructs a submatch object whose properties are aggregated from that of its games. ----@param games WarcraftMatchGroupUtilGame[] ---@param match WarcraftMatchGroupUtilMatch +---@param subgroup MatchGroupUtilSubgroup ---@return WarcraftMatchGroupUtilSubmatch -function CustomMatchGroupUtil.constructSubmatch(games, match) +function CustomMatchGroupUtil.constructSubmatch(match, subgroup) + local games = subgroup.games local firstGame = games[1] local opponents = Table.deepCopy(firstGame.opponents) local isSubmatch = String.startsWith(firstGame.map or '', 'Submatch') @@ -204,14 +185,12 @@ function CustomMatchGroupUtil.constructSubmatch(games, match) CustomMatchGroupUtil._determineSubmatchPlayerFactions(match, games, opponents, opponentIndex) end) - return { - games = games, + return Table.mergeInto({ mode = firstGame.mode, opponents = opponents, - subgroup = firstGame.subgroup, winner = winner, - header = Table.extract(match.extradata or {}, 'subgroup' .. firstGame.subgroup .. 'header'), - } + header = Table.extract(match.extradata or {}, 'subgroup' .. subgroup.subgroup .. 'header'), + }, subgroup) end ---@param match WarcraftMatchGroupUtilMatch