Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lua/wikis/commons/MatchGroup/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 10 additions & 31 deletions lua/wikis/commons/MatchGroup/Util/Starcraft.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
52 changes: 14 additions & 38 deletions lua/wikis/hearthstone/MatchGroup/Util/Custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -24,18 +25,16 @@ 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?

---@class HearthstoneMatchGroupUtilMatch: MatchGroupUtilMatch
---@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]]
Expand All @@ -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

Expand All @@ -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+$')
Expand Down Expand Up @@ -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
44 changes: 12 additions & 32 deletions lua/wikis/stormgate/MatchGroup/Util/Custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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),
Expand All @@ -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?

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading