Skip to content

Commit

Permalink
improve handling opponent handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckeLucky committed Dec 18, 2024
1 parent eabac47 commit 1a146c5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 90 deletions.
92 changes: 43 additions & 49 deletions components/match2/wikis/hearthstone/match_group_util_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
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 MatchGroupUtil = Lua.import('Module:MatchGroup/Util')
-- can not use `Module:OpponentLibraries`/`Module:Opponent/Custom` to avoid loop
local Opponent = Lua.import('Module:Opponent')
Expand All @@ -19,10 +21,12 @@ local SCORE_STATUS = 'S'

local CustomMatchGroupUtil = Table.deepCopy(MatchGroupUtil)

---@class HearthstoneMatchGroupUtilGameOpponent: GameOpponent
---@field placement number?

---@class HearthstoneMatchGroupUtilSubmatch
---@field games MatchGroupUtilGame[]
---@field opponents GameOpponent[]
---@field scores table<number|string, number|string>
---@field opponents HearthstoneMatchGroupUtilGameOpponent[]
---@field subgroup number
---@field winner number?
---@field header string?
Expand All @@ -45,20 +49,22 @@ function CustomMatchGroupUtil.matchFromRecord(record)
return opponent.type == Opponent.team end
)

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

local extradata = match.extradata
---@cast extradata table
Array.forEach(match.submatches, function (submatch)
submatch.header = Table.extract(extradata, 'subgroup' .. submatch.subgroup .. 'header')
end)
if not match.isTeamMatch then
return match
end

-- Compute submatches
match.submatches = Array.map(
CustomMatchGroupUtil.groupBySubmatch(match.games),
function(games) return CustomMatchGroupUtil.constructSubmatch(games) end
)

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 Down Expand Up @@ -102,46 +108,34 @@ end
function CustomMatchGroupUtil.constructSubmatch(games)
local firstGame = games[1]
local opponents = Table.deepCopy(firstGame.opponents)
local scores = {}
local winner = nil

if string.find(firstGame.map or '', '^[sS]ubmatch %d+$') then
Array.forEach(firstGame.opponents, function (opponent, opponentIndex)
if opponent.status and opponent.status ~= SCORE_STATUS then
scores[opponentIndex] = opponent.status
else
scores[opponentIndex] = opponent.score
end
end)
winner = firstGame.winner
else
local allPlayed = true
scores = {0, 0}
-- Sum up scores
Array.forEach(games, function (game)
if game.winner then
scores[game.winner] = (scores[game.winner] or 0) + 1
end
allPlayed = game.winner ~= nil
end)

if allPlayed then
local diff = (scores[1] or 0) - (scores[2] or 0)
if diff < 0 then
winner = 2
elseif diff == 0 then
winner = 0
else
winner = 1
end
end
local isSubmatch = string.find(firstGame.map or '', '^[sS]ubmatch %d+$')
if isSubmatch then
games = {firstGame}
end

---@param opponent table
local getOpponentScoreAndStatus = function(opponent, opponentIndex)
local statuses = Array.unique(Array.map(games, function(game)
return game.opponents[opponentIndex].status
end))
opponent.status = #statuses == 1 and statuses[1] ~= SCORE_STATUS and statuses[1] or SCORE_STATUS
opponent.score = isSubmatch and firstGame.scores[opponentIndex] or Array.reduce(Array.map(games, function(game)
return (game.winner == opponentIndex and 1 or 0)
end), Operator.add)
end

Array.forEach(opponents, getOpponentScoreAndStatus)

local allPlayed = Array.all(games, function (game) return game.winner ~= nil end)
local winner = allPlayed and MatchGroupInputUtil.getWinner('', nil, opponents) or nil
Array.forEach(opponents, function(opponent, opponentIndex)
opponent.placement = MatchGroupInputUtil.placementFromWinner('', winner, opponentIndex)
end)

return {
games = games,
opponents = opponents,
scores = scores,
subgroup = games[1].subgroup,
subgroup = firstGame.subgroup,
winner = winner,
}
end
Expand Down
51 changes: 10 additions & 41 deletions components/match2/wikis/hearthstone/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local WidgetUtil = Lua.import('Module:Widget/Util')

local OpponentLibraries = require('Module:OpponentLibraries')
local Opponent = OpponentLibraries.Opponent
local OpponentDisplay = OpponentLibraries.OpponentDisplay

local CustomMatchSummary = {}

Expand Down Expand Up @@ -79,52 +78,22 @@ function CustomMatchSummary._submatchHasDetails(submatch)
end

---@param submatch HearthstoneMatchGroupUtilSubmatch
---@return Widget
---@return Html
function CustomMatchSummary.TeamSubMatchOpponnetRow(submatch)
local opponents = submatch.opponents or {{}, {}}

local createOpponent = function(opponentIndex)
local players = (opponents[opponentIndex] or {}).players or {}
Array.forEach(opponents, function (opponent, opponentIndex)
local players = opponent.players or {}
if Logic.isEmpty(players) then
players = Opponent.tbd(Opponent.solo).players
end
return OpponentDisplay.BlockOpponent{
flip = opponentIndex == 1,
opponent = {players = players, type = Opponent.partyTypes[math.max(#players, 1)]},
showLink = true,
overflow = 'ellipsis',
}
end

---@param opponentIndex any
---@return Html
local createScore = function(opponentIndex)
local isWinner = opponentIndex == submatch.winner
return OpponentDisplay.BlockScore{
isWinner = isWinner,
scoreText = (submatch.scores or {})[opponentIndex],
}
end
---@cast players -nil
opponent.type = Opponent.partyTypes[math.max(#players, 1)]
opponent.players = players
end)

return HtmlWidgets.Div{
classes = {'brkts-popup-header-dev'},
css = {['justify-content'] = 'center', margin = 'auto'},
children = WidgetUtil.collect(
HtmlWidgets.Div{
classes = {'brkts-popup-header-opponent', 'brkts-popup-header-opponent-left'},
children = {
createOpponent(1),
createScore(1):addClass('brkts-popup-header-opponent-score-left'),
},
},
HtmlWidgets.Div{
classes = {'brkts-popup-header-opponent', 'brkts-popup-header-opponent-right'},
children = {
createScore(2):addClass('brkts-popup-header-opponent-score-right'),
createOpponent(2),
},
}
)
return HtmlWidgets.Div {
css = {margin = 'auto'},
children = MatchSummary.createDefaultHeader({opponents = opponents}):create()
}
end

Expand Down

0 comments on commit 1a146c5

Please sign in to comment.