-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathCustom.lua
More file actions
120 lines (97 loc) · 3.99 KB
/
Custom.lua
File metadata and controls
120 lines (97 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
-- @Liquipedia
-- page=Module:MatchGroup/Util/Custom
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--
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')
local MatchGroupInputUtil = Lua.import('Module:MatchGroup/Input/Util')
local MatchGroupUtil = Lua.import('Module:MatchGroup/Util')
local Opponent = Lua.import('Module:Opponent/Custom')
local SCORE_STATUS = 'S'
local CustomMatchGroupUtil = Table.deepCopy(MatchGroupUtil)
---@class HearthstoneMatchGroupUtilGameOpponent: GameOpponent
---@field placement number?
---@class HearthstoneMatchGroupUtilSubmatch: MatchGroupUtilSubgroup
---@field opponents HearthstoneMatchGroupUtilGameOpponent[]
---@field winner number?
---@field header string?
---@class HearthstoneMatchGroupUtilMatch: MatchGroupUtilMatch
---@field submatches HearthstoneMatchGroupUtilSubmatch[]?
---@field isTeamMatch boolean
---@param record match2
---@return HearthstoneMatchGroupUtilMatch
function CustomMatchGroupUtil.matchFromRecord(record)
local match = MatchGroupUtil.matchFromRecord(record) --[[@as HearthstoneMatchGroupUtilMatch]]
-- Adjust game.opponents by looking up game.opponents.players in match.opponents
Array.forEach(match.games, function(game)
game.opponents = CustomMatchGroupUtil.computeGameOpponents(game, match.opponents)
end)
match.isTeamMatch = Array.any(match.opponents, function(opponent)
return opponent.type == Opponent.team end
)
if not match.isTeamMatch then
return match
end
-- Compute submatches
match.submatches = Array.map(
MatchGroupUtil.groupBySubgroup(match.games),
FnUtil.curry(CustomMatchGroupUtil.constructSubmatch, match)
)
return match
end
---@param game MatchGroupUtilGame
---@param matchOpponents standardOpponent[]
---@return table[]
function CustomMatchGroupUtil.computeGameOpponents(game, matchOpponents)
return Array.map(game.opponents, function (opponent, opponentIndex)
return Table.merge(opponent, {
players = Array.map(game.opponents[opponentIndex].players or {}, function (player, playerIndex)
if Logic.isEmpty(player) then return nil end
return Table.merge(matchOpponents[opponentIndex].players[playerIndex] or {}, player)
end)
})
end)
end
---Constructs a submatch object whose properties are aggregated from that of its games.
---@param match HearthstoneMatchGroupUtilMatch
---@param subgroup MatchGroupUtilSubgroup
---@return HearthstoneMatchGroupUtilSubmatch
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+$')
if isSubmatch then
games = {firstGame}
end
---@param opponent table
---@param opponentIndex number
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)
local matchExtradata = match.extradata or {}
return Table.mergeInto({
header = Table.extract(matchExtradata, 'subgroup' .. subgroup.subgroup .. 'header'),
opponents = opponents,
winner = winner,
}, subgroup)
end
return CustomMatchGroupUtil