Skip to content

Commit

Permalink
Array.forEach <3
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckeLucky committed Dec 16, 2024
1 parent 032ef8c commit bdefea8
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions components/match2/wikis/hearthstone/match_group_util_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,18 @@ function CustomMatchGroupUtil.matchFromRecord(record)
return opponent.type == Opponent.team end
)

local extradata = match.extradata
---@cast extradata table
if match.isTeamMatch then
-- Compute submatches
match.submatches = Array.map(
CustomMatchGroupUtil.groupBySubmatch(match.games),
function(games) return CustomMatchGroupUtil.constructSubmatch(games) end
)

-- Extract submatch headers from extradata
for _, submatch in pairs(match.submatches) do
local extradata = match.extradata
---@cast extradata table
Array.forEach(match.submatches, function (submatch)
submatch.header = Table.extract(extradata, 'subgroup' .. submatch.subgroup .. 'header')
end
end)
end

return match
Expand Down Expand Up @@ -86,15 +85,15 @@ function CustomMatchGroupUtil.groupBySubmatch(matchGames)
local previousSubgroup = nil
local currentGames = nil
local submatchGames = {}
for _, game in ipairs(matchGames) do
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
end)
return submatchGames
end

Expand All @@ -106,18 +105,19 @@ function CustomMatchGroupUtil.constructSubmatch(games)

-- Sum up scores
local scores = {}
for opponentIndex, _ in pairs(opponents) do
Array.forEach(opponents, function (_, opponentIndex)
scores[opponentIndex] = 0
end
for _, game in pairs(games) do
end)

Array.forEach(games, function (game)
if game.map and String.startsWith(game.map, 'Submatch') and not game.resultType then
for opponentIndex, score in pairs(scores) do
scores[opponentIndex] = score + (tonumber(game.scores[opponentIndex]) or 0)
end
Array.forEach(scores, function (score, index)
scores[index] = score + (tonumber(game.scores[index]) or 0)
end)
elseif game.winner then
scores[game.winner] = (scores[game.winner] or 0) + 1
end
end
end)

-- Compute winner if all games have been played, skipped, or defaulted
local allPlayed = Array.all(games, function(game)
Expand All @@ -140,10 +140,10 @@ function CustomMatchGroupUtil.constructSubmatch(games)
-- Set resultType and walkover if every game is a walkover
local walkovers = {}
local resultTypes = {}
for _, game in pairs(games) do
Array.forEach(games, function (game)
resultTypes[game.resultType or ''] = true
walkovers[game.walkover or ''] = true
end
end)
local walkover
local uniqueResult = Table.uniqueKey(resultTypes)
if uniqueResult == 'default' then
Expand Down

0 comments on commit bdefea8

Please sign in to comment.