Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(match2): properly attach participants to their playerid #4645

Merged
merged 24 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
36 changes: 36 additions & 0 deletions components/match2/commons/match_group_input_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,42 @@ function MatchGroupInputUtil.getCharacterName(alias, character)
return (assert(alias[character:lower()], 'Invalid character:' .. character))
end

---@param players {name: string?}[]?
---@param playerInput string?
---@param playerLink string?
---@param options {pagifyPlayerNames: boolean?}?
---@return integer?
function MatchGroupInputUtil.findPlayerId(players, playerInput, playerLink, options)
if not players then
return
end

if Logic.isEmpty(playerInput) then
return
end
---@cast playerInput -nil

if not playerLink then
playerLink = mw.ext.TeamLiquidIntegration.resolve_redirect(playerInput)
end
if (options or {}).pagifyPlayerNames then
playerLink = Page.pageifyLink(playerLink) --[[@as string]]
end
hjpalpha marked this conversation as resolved.
Show resolved Hide resolved

local playerLinks = Array.map(players, Operator.property('name'))
local playerIndex = Array.indexOf(playerLinks, FnUtil.curry(Operator.eq, playerLink))
if playerIndex > 0 then
return playerIndex
end

local playerDisplayNames = Array.map(players, Operator.property('displayname'))
playerIndex = Array.indexOf(playerDisplayNames, FnUtil.curry(Operator.eq, playerInput))
if playerIndex > 0 then
return playerIndex
end
mw.log('Player with id ' .. playerInput .. ' not found in opponent data')
end

--- Warning, both match and standalone match may be mutated
---@param match table
---@param standaloneMatch table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ local Flags = require('Module:Flags')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local Operator = require('Module:Operator')
local Page = require('Module:Page')
local String = require('Module:StringUtils')
local Table = require('Module:Table')
local Variables = require('Module:Variables')
Expand Down Expand Up @@ -401,23 +400,22 @@ function MapFunctions.getTeamParticipants(mapInput, opponent, opponentIndex)
local isTBD = nameInput:upper() == TBD or nameInput:upper() == TBA

local link = participantInput.link or Variables.varDefault(nameInput .. '_page') or nameInput
link = Page.pageifyLink(link) --[[@as string -- can't be nil as input isn't nil]]

local playerIndex = MapFunctions.getPlayerIndex(players, link, nameInput)
local playerId = MatchGroupInputUtil.findPlayerId(players, nameInput, link, OPPONENT_CONFIG)
Rathoz marked this conversation as resolved.
Show resolved Hide resolved

-- in case we have a TBD or a player not known in match2players inster a new player in match2players
if isTBD or playerIndex == 0 then
if isTBD or not playerId then
table.insert(players, {
name = isTBD and TBD or link,
displayname = isTBD and TBD or nameInput,
extradata = {faction = participantInput.faction or Faction.defaultFaction},
})
playerIndex = #players
playerId = #players
end
Rathoz marked this conversation as resolved.
Show resolved Hide resolved

local player = players[playerIndex]
local player = players[playerId]

participants[opponentIndex .. '_' .. playerIndex] = {
participants[opponentIndex .. '_' .. playerId] = {
faction = participantInput.faction or player.extradata.faction,
player = link,
position = position,
Expand All @@ -428,20 +426,6 @@ function MapFunctions.getTeamParticipants(mapInput, opponent, opponentIndex)
return participants
end

---@param players {name: string, displayname: string}
---@param name string
---@param displayName string
---@return integer
function MapFunctions.getPlayerIndex(players, name, displayName)
local playerIndex = Array.indexOf(players, function(player) return player.name == name end)

if playerIndex ~= 0 then
return playerIndex
end

return Array.indexOf(players, function(player) return player.displayname == displayName end)
end

---@param mapInput table
---@param opponent table
---@param opponentIndex integer
Expand Down
35 changes: 19 additions & 16 deletions components/match2/wikis/brawlstars/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function CustomMatchGroupInput.processMatch(match, options)
local opponents = Array.mapIndexes(function(opponentIndex)
return MatchGroupInputUtil.readOpponent(match, opponentIndex, {})
end)
local games = CustomMatchGroupInput.extractMaps(match, #opponents)
local games = CustomMatchGroupInput.extractMaps(match, opponents)
match.bestof = MatchFunctions.getBestOf(match)

local autoScoreFunction = MatchGroupInputUtil.canUseAutoScore(match, games)
Expand Down Expand Up @@ -87,20 +87,20 @@ function CustomMatchGroupInput.processMatch(match, options)
end

---@param match table
---@param opponentCount integer
---@param opponents table[]
---@return table[]
function CustomMatchGroupInput.extractMaps(match, opponentCount)
function CustomMatchGroupInput.extractMaps(match, opponents)
local maps = {}
for key, map, mapIndex in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
local finishedInput = map.finished --[[@as string?]]
local winnerInput = map.winner --[[@as string?]]

map.vod = map.vod or String.nilIfEmpty(match['vodgame' .. mapIndex])
map.bestof = MapFunctions.getBestOf(map)
map.participants = MapFunctions.getParticipants(map, opponentCount)
map.extradata = MapFunctions.getExtraData(map, opponentCount)
map.participants = MapFunctions.getParticipants(map, opponents)
map.extradata = MapFunctions.getExtraData(map, #opponents)

local opponentInfo = Array.map(Array.range(1, opponentCount), function(opponentIndex)
local opponentInfo = Array.map(opponents, function(_, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
Expand Down Expand Up @@ -204,19 +204,22 @@ function MapFunctions.getExtraData(map, opponentCount)
end

---@param map table
---@param opponentCount integer
---@param opponents table[]
---@return table
function MapFunctions.getParticipants(map, opponentCount)
function MapFunctions.getParticipants(map, opponents)
local participants = {}
local getCharacterName = FnUtil.curry(MatchGroupInputUtil.getCharacterName, BrawlerNames)
for opponentIndex = 1, opponentCount do
for _, player, playerIndex in Table.iter.pairsByPrefix(map, 't' .. opponentIndex .. 'p') do
participants[opponentIndex .. '_' .. playerIndex] = {player = player}
end

for _, brawler, pickIndex in Table.iter.pairsByPrefix(map, 't' .. opponentIndex .. 'c') do
participants[opponentIndex .. '_' .. pickIndex] = participants[opponentIndex .. '_' .. pickIndex] or {}
participants[opponentIndex .. '_' .. pickIndex].brawler = getCharacterName(brawler)
for opponentIndex, opponent in ipairs(opponents) do
local players = opponent.match2players or {}
for _, playerName, playerIndex in Table.iter.pairsByPrefix(map, 't' .. opponentIndex .. 'p') do
local player = {
player = playerName,
brawler = getCharacterName(map['t' .. opponentIndex .. 'c' .. playerIndex]),
}
local playerId = MatchGroupInputUtil.findPlayerId(players, player.player)
if playerId then
participants[opponentIndex .. '_' .. playerId] = player
end
end
end

Expand Down
28 changes: 16 additions & 12 deletions components/match2/wikis/dota2/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function CustomMatchGroupInput.processMatchWithoutStandalone(MatchParser, match)
local opponents = Array.mapIndexes(function(opponentIndex)
return MatchGroupInputUtil.readOpponent(match, opponentIndex, OPPONENT_CONFIG)
end)
local games = MatchFunctions.extractMaps(MatchParser, match, #opponents)
local games = MatchFunctions.extractMaps(MatchParser, match, opponents)
match.bestof = MatchGroupInputUtil.getBestOf(match.bestof, games)

local autoScoreFunction = MatchGroupInputUtil.canUseAutoScore(match, games)
Expand Down Expand Up @@ -122,9 +122,9 @@ end

---@param MatchParser Dota2MatchParserInterface
---@param match table
---@param opponentCount integer
---@param opponents table[]
---@return table[]
function MatchFunctions.extractMaps(MatchParser, match, opponentCount)
function MatchFunctions.extractMaps(MatchParser, match, opponents)
local maps = {}
for key, mapInput, mapIndex in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
local map = MatchParser.getMap(mapInput)
Expand All @@ -138,11 +138,11 @@ function MatchFunctions.extractMaps(MatchParser, match, opponentCount)
map.length = MatchParser.getLength(map)
map.vod = map.vod or String.nilIfEmpty(match['vodgame' .. mapIndex])
map.publisherid = map.matchid or String.nilIfEmpty(match['matchid' .. mapIndex])
map.participants = MapFunctions.getParticipants(MatchParser, map, opponentCount)
map.extradata = MapFunctions.getExtraData(MatchParser, map, opponentCount)
map.participants = MapFunctions.getParticipants(MatchParser, map, opponents)
map.extradata = MapFunctions.getExtraData(MatchParser, map, #opponents)

map.finished = MatchGroupInputUtil.mapIsFinished(map)
local opponentInfo = Array.map(Array.range(1, opponentCount), function(opponentIndex)
local opponentInfo = Array.map(opponents, function(_, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
Expand Down Expand Up @@ -262,16 +262,20 @@ end
-- Parse participant information
---@param MatchParser Dota2MatchParserInterface
---@param map table
---@param opponentCount integer
---@param opponents table[]
---@return table
function MapFunctions.getParticipants(MatchParser, map, opponentCount)
function MapFunctions.getParticipants(MatchParser, map, opponents)
local participants = {}
local getCharacterName = FnUtil.curry(MatchGroupInputUtil.getCharacterName, HeroNames)

for opponentIndex = 1, opponentCount do
for playerIndex, participant in ipairs(MatchParser.getParticipants(map, opponentIndex) or {}) do
participant.character = getCharacterName(participant.character)
participants[opponentIndex .. '_' .. playerIndex] = participant
for opponentIndex, opponent in ipairs(opponents) do
local players = opponent.match2players or {}
for _, participant in ipairs(MatchParser.getParticipants(map, opponentIndex) or {}) do
local playerId = MatchGroupInputUtil.findPlayerId(players, participant.id, nil, OPPONENT_CONFIG)
if playerId then
participant.character = getCharacterName(participant.character)
participants[opponentIndex .. '_' .. playerId] = participant
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function CustomMatchGroupInput.processMatchWithoutStandalone(MatchParser, match)
local opponents = Array.mapIndexes(function(opponentIndex)
return MatchGroupInputUtil.readOpponent(match, opponentIndex, OPPONENT_CONFIG)
end)
local games = MatchFunctions.extractMaps(MatchParser, match, #opponents)
local games = MatchFunctions.extractMaps(MatchParser, match, opponents)
match.bestof = MatchGroupInputUtil.getBestOf(match.bestof, games)

local autoScoreFunction = MatchGroupInputUtil.canUseAutoScore(match, games)
Expand Down Expand Up @@ -122,9 +122,9 @@ end

---@param MatchParser LeagueOfLegendsMatchParserInterface
---@param match table
---@param opponentCount integer
---@param opponents table[]
---@return table[]
function MatchFunctions.extractMaps(MatchParser, match, opponentCount)
function MatchFunctions.extractMaps(MatchParser, match, opponents)
local maps = {}
for key, mapInput, mapIndex in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
local map = MatchParser.getMap(mapInput)
Expand All @@ -137,11 +137,11 @@ function MatchFunctions.extractMaps(MatchParser, match, opponentCount)

map.length = MatchParser.getLength(map)
map.vod = map.vod or String.nilIfEmpty(match['vodgame' .. mapIndex])
map.participants = MapFunctions.getParticipants(MatchParser, map, opponentCount)
map.extradata = MapFunctions.getExtraData(MatchParser, map, opponentCount)
map.participants = MapFunctions.getParticipants(MatchParser, map, opponents)
map.extradata = MapFunctions.getExtraData(MatchParser, map, #opponents)

map.finished = MatchGroupInputUtil.mapIsFinished(map)
local opponentInfo = Array.map(Array.range(1, opponentCount), function(opponentIndex)
local opponentInfo = Array.map(opponents, function(_, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
Expand Down Expand Up @@ -244,16 +244,20 @@ end
-- Parse participant information
---@param MatchParser LeagueOfLegendsMatchParserInterface
---@param map table
---@param opponentCount integer
---@param opponents table[]
---@return table
function MapFunctions.getParticipants(MatchParser, map, opponentCount)
function MapFunctions.getParticipants(MatchParser, map, opponents)
local participants = {}
local getCharacterName = FnUtil.curry(MatchGroupInputUtil.getCharacterName, HeroNames)

for opponentIndex = 1, opponentCount do
for playerIndex, participant in ipairs(MatchParser.getParticipants(map, opponentIndex) or {}) do
participant.character = getCharacterName(participant.character)
participants[opponentIndex .. '_' .. playerIndex] = participant
for opponentIndex, opponent in ipairs(opponents) do
local players = opponent.match2players or {}
for _, participant in ipairs(MatchParser.getParticipants(map, opponentIndex) or {}) do
local playerId = MatchGroupInputUtil.findPlayerId(players, participant.id, nil, OPPONENT_CONFIG)
if playerId then
participant.character = getCharacterName(participant.character)
participants[opponentIndex .. '_' .. playerId] = participant
end
end
end

Expand Down
43 changes: 23 additions & 20 deletions components/match2/wikis/valorant/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function CustomMatchGroupInput.processMatch(match, options)
local opponents = Array.mapIndexes(function(opponentIndex)
return MatchGroupInputUtil.readOpponent(match, opponentIndex, {})
end)
local games = CustomMatchGroupInput.extractMaps(match, #opponents)
local games = CustomMatchGroupInput.extractMaps(match, opponents)
match.bestof = MatchGroupInputUtil.getBestOf(nil, games)
games = MatchFunctions.removeUnsetMaps(games)

Expand Down Expand Up @@ -83,19 +83,19 @@ end


---@param match table
---@param opponentCount integer
---@param opponents table[]
---@return table[]
function CustomMatchGroupInput.extractMaps(match, opponentCount)
function CustomMatchGroupInput.extractMaps(match, opponents)
local maps = {}
for key, map in Table.iter.pairsByPrefix(match, 'map', {requireIndex = true}) do
local finishedInput = map.finished --[[@as string?]]
local winnerInput = map.winner --[[@as string?]]

map.participants = MapFunctions.getParticipants(map, opponentCount)
map.participants = MapFunctions.getParticipants(map, opponents)
map.extradata = MapFunctions.getExtraData(map, map.participants)
map.finished = MatchGroupInputUtil.mapIsFinished(map)

local opponentInfo = Array.map(Array.range(1, opponentCount), function(opponentIndex)
local opponentInfo = Array.map(opponents, function(_, opponentIndex)
local score, status = MatchGroupInputUtil.computeOpponentScore({
walkover = map.walkover,
winner = map.winner,
Expand Down Expand Up @@ -212,26 +212,29 @@ function MapFunctions.getExtraData(map, participants)
end

---@param map table
---@param opponentCount integer
---@param opponents table[]
---@return table
function MapFunctions.getParticipants(map, opponentCount)
function MapFunctions.getParticipants(map, opponents)
local participants = {}
local getCharacterName = FnUtil.curry(MatchGroupInputUtil.getCharacterName, AgentNames)

for opponentIdx = 1, opponentCount do
for _, stats, playerIdx in Table.iter.pairsByPrefix(map, 't' .. opponentIdx .. 'p', {requireIndex = true}) do
for opponentIndex, opponent in ipairs(opponents) do
local players = opponent.match2players or {}
for _, stats in Table.iter.pairsByPrefix(map, 't' .. opponentIndex .. 'p', {requireIndex = true}) do
stats = Json.parseIfString(stats)

local participant = {
kills = stats.kills,
deaths = stats.deaths,
assists = stats.assists,
acs = stats.acs,
player = stats.player,
agent = getCharacterName(stats.agent),
}

participants[opponentIdx .. '_' .. playerIdx] = participant
local playerId = MatchGroupInputUtil.findPlayerId(players, stats.player)
if playerId then
local participant = {
kills = stats.kills,
deaths = stats.deaths,
assists = stats.assists,
acs = stats.acs,
player = stats.player,
agent = getCharacterName(stats.agent),
}

participants[opponentIndex .. '_' .. playerId] = participant
end
end
end

Expand Down
Loading