Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hjpalpha committed Sep 13, 2024
1 parent f1d7bf1 commit 33daf93
Showing 1 changed file with 49 additions and 69 deletions.
118 changes: 49 additions & 69 deletions components/match2/wikis/warcraft/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,79 +320,59 @@ end
---@param opponentIndex integer
---@return table<string, WarcraftParticipant>
function MapFunctions.getTeamParticipants(mapInput, opponent, opponentIndex)
local players = opponent.match2players

---@type {input:string,faction:string?,link:string?,heroes:string?,heroesCheckDisabled:boolean,playedRandom:boolean}[]
local participantsList = Array.mapIndexes(function(playerIndex)
local prefix = 't' .. opponentIndex .. 'p' .. playerIndex

if Logic.isEmpty(mapInput[prefix]) then return end

return {
input = mapInput[prefix],
link = Logic.nilIfEmpty(mapInput[prefix .. 'link']),
faction = Faction.read(mapInput[prefix .. 'race']),
heroes = mapInput[prefix .. 'heroes'],
heroesCheckDisabled = Logic.readBool(mapInput[prefix .. 'heroesNoCheck']),
playedRandom = Logic.readBool(mapInput[prefix .. 'random']),
}
local players = Array.mapIndexes(function(playerIndex)
return Logic.nilIfEmpty(mapInput['t' .. opponentIndex .. 'p' .. playerIndex])
end)

local participants = {}

Array.forEach(participantsList, function(participantInput, position)
local nameInput = participantInput.input

local isTBD = nameInput:upper() == TBD

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)

-- 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
table.insert(players, {
name = isTBD and TBD or link,
displayname = isTBD and TBD or nameInput,
extradata = {faction = participantInput.faction or Faction.defaultFaction},
})
playerIndex = #players
end

local player = players[playerIndex]
local faction = participantInput.faction or player.extradata.faction

participants[opponentIndex .. '_' .. playerIndex] = {
faction = faction,
player = link,
position = position,
flag = Flags.CountryName(player.flag),
random = participantInput.playedRandom,
heroes = MapFunctions.readHeroes(
participantInput.heroes,
faction,
link,
participantInput.heroesCheckDisabled
),
}
local participants, unattachedParticipants = MatchGroupInputUtil.parseParticipants(
opponent.match2players,
players,
function(playerIndex)
local prefix = 't' .. opponentIndex .. 'p' .. playerIndex
return {
name = mapInput[prefix],
link = Logic.nilIfEmpty(mapInput[prefix .. 'link']) or Variables.varDefault(mapInput[prefix] .. '_page'),
faction = Faction.read(mapInput[prefix .. 'race']),
heroes = mapInput[prefix .. 'heroes'],
heroesCheckDisabled = Logic.readBool(mapInput[prefix .. 'heroesNoCheck']),
playedRandom = Logic.readBool(mapInput[prefix .. 'random']),
}
end,
function(playerIndex, playerIdData, playerInputData)
local faction = playerInputData.faction or (playerIdData.extradata or {}).faction or Faction.defaultFaction
local link = playerIdData.name or playerInputData.link
return {
faction = faction,
player = link,
flag = Flags.CountryName(playerIdData.flag),
position = playerIndex,
random = playerInputData.playedRandom,
heroes = MapFunctions.readHeroes(
playerInputData.heroes,
faction,
link,
playerInputData.heroesCheckDisabled
),
}
end,
OPPONENT_CONFIG
)

Array.forEach(unattachedParticipants, function(participant)
local name = mapInput['t' .. opponentIndex .. 'p' .. participant.position]
local nameUpper = name:upper()
local isTBD = nameUpper == TBD

table.insert(opponent.match2players, {
name = isTBD and TBD or participant.player,
displayname = isTBD and TBD or name,
flag = participant.flag,
extradata = {faction = participant.faction},
})
participants[#opponent.match2players] = participant
end)

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)
return Table.map(participants, MatchGroupInputUtil.prefixPartcipants(opponentIndex))
end

---@param mapInput table
Expand Down

0 comments on commit 33daf93

Please sign in to comment.