Skip to content

Commit

Permalink
feat(match2): switch crafts from participants to mapOpponents (#5111)
Browse files Browse the repository at this point in the history
* fill up gaps

* fix mapMode

* MatchGroup/Util customs

* legacy storage shit

* unused

* fixes

* break line

* make use of #5112

* fixes

* a bit better

* more fixing

* more use of #5112
  • Loading branch information
hjpalpha authored Dec 3, 2024
1 parent 21fddc6 commit 7933169
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function MapFunctions.getTeamMapPlayers(mapInput, opponent, opponentIndex)
return Logic.nilIfEmpty(mapInput['t' .. opponentIndex .. 'p' .. playerIndex])
end)

local participants, unattachedParticipants = MatchGroupInputUtil.parseParticipants(
local mapPlayers = MatchGroupInputUtil.parseMapPlayers(
opponent.match2players,
players,
function(playerIndex)
Expand All @@ -298,21 +298,21 @@ function MapFunctions.getTeamMapPlayers(mapInput, opponent, opponentIndex)
end
)

Array.forEach(unattachedParticipants, function(participant)
local name = mapInput['t' .. opponentIndex .. 'p' .. participant.position]
Array.forEach(mapPlayers, function(player, playerIndex)
if Logic.isEmpty(player) then return end
local name = mapInput['t' .. opponentIndex .. 'p' .. player.position]
local nameUpper = name:upper()
local isTBD = nameUpper == TBD or nameUpper == TBA

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

return participants
return mapPlayers
end

---@param mapInput table
Expand Down Expand Up @@ -349,9 +349,7 @@ end
---@param opponents table[]
---@return string
function MapFunctions.getMapMode(match, map, opponents)
local playerCounts = Array.map(map.opponents or {}, function(opponent)
return Table.size(opponent.players or {})
end)
local playerCounts = Array.map(map.opponents or {}, MapFunctions.getMapOpponentSize)

local modeParts = Array.map(playerCounts, function(count, opponentIndex)
if count == 0 then
Expand Down Expand Up @@ -384,7 +382,7 @@ function MapFunctions.getExtraData(match, map, opponents)

if #opponents ~= 2 then
return extradata
elseif Array.any(map.opponents, function(mapOpponent) return Table.size(mapOpponent.players or {}) ~= 1 end) then
elseif Array.any(map.opponents, function(opponent) return MapFunctions.getMapOpponentSize(opponent) ~= 1 end) then
return extradata
end

Expand Down Expand Up @@ -429,4 +427,10 @@ function MapFunctions.getMapName(game)
end
end

---@param opponent table
---@return integer
function MapFunctions.getMapOpponentSize(opponent)
return Table.size(Array.filter(opponent.players or {}, Logic.isNotEmpty))
end

return StarcraftMatchGroupInput
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ function StarcraftMatchGroupUtil.matchFromRecord(record)
-- Add additional fields to opponents
StarcraftMatchGroupUtil.populateOpponents(match)

-- Compute game.opponents by looking up game.participants in match.opponents
for _, game in ipairs(match.games) do
-- Adjust game.opponents by looking up game.opponents.players in match.opponents
Array.forEach(match.games, function(game)
game.opponents = StarcraftMatchGroupUtil.computeGameOpponents(game, match.opponents)
game.extradata = game.extradata or {}
end
end)

-- Determine whether the match is a team match with different players each game
match.opponentMode = Array.any(match.opponents, function(opponent)
Expand Down Expand Up @@ -138,79 +138,36 @@ function StarcraftMatchGroupUtil.populateOpponents(match)
end
end

---Computes game.opponents by looking up matchOpponents.players on each participant.
---@param game StarcraftMatchGroupUtilGame
---@param matchOpponents StarcraftStandardOpponent[]
---@return StarcraftMatchGroupUtilGameOpponent[]
function StarcraftMatchGroupUtil.computeGameOpponents(game, matchOpponents)
local function playerFromParticipant(opponentIndex, matchPlayerIndex, participant)
local matchPlayer = matchOpponents[opponentIndex].players[matchPlayerIndex]
if matchPlayer then
return Table.merge(matchPlayer, {
matchPlayerIndex = matchPlayerIndex,
faction = participant.faction,
position = tonumber(participant.position),
})
else
return {
displayName = 'TBD',
matchPlayerIndex = matchPlayerIndex,
faction = Faction.defaultFaction,
}
end
end

-- Convert participants list to players array
local opponentPlayers = {}
for key, participant in pairs(game.participants) do
local opponentIndex, matchPlayerIndex = key:match('(%d+)_(%d+)')
opponentIndex = tonumber(opponentIndex)
-- opponentIndex can not be nil due to the format of the participants keys
---@cast opponentIndex -nil
matchPlayerIndex = tonumber(matchPlayerIndex)

local player = playerFromParticipant(opponentIndex, matchPlayerIndex, participant)

if not opponentPlayers[opponentIndex] then
opponentPlayers[opponentIndex] = {}
end
table.insert(opponentPlayers[opponentIndex], player)
end

local modeParts = mw.text.split(game.mode or '', 'v')

-- Create game opponents
local opponents = {}
for opponentIndex = 1, #modeParts do
local opponent = {
isArchon = modeParts[opponentIndex] == 'Archon',
isSpecialArchon = modeParts[opponentIndex]:match('^%dS$'),
placement = tonumber(Table.extract(game.extradata, 'placement' .. opponentIndex)),
players = opponentPlayers[opponentIndex] or {},
score = game.scores[opponentIndex],
}
if opponent.placement and (opponent.placement < 1 or 99 <= opponent.placement) then
opponent.placement = nil
end
table.insert(opponents, opponent)
end
return Array.map(game.opponents, function(mapOpponent, opponentIndex)
local mode = modeParts[opponentIndex]
local players = Array.map(mapOpponent.players, function(player, playerIndex)
if Logic.isEmpty(player) then return end
local matchPlayer = (matchOpponents[opponentIndex].players or {})[playerIndex] or {}
return Table.merge({displayName = 'TBD'}, matchPlayer, {
faction = player.faction,
position = tonumber(player.position),
matchPlayerIndex = playerIndex,
})
end) --[[@as table[] ]]

-- Sort players in game opponents
for _, opponent in pairs(opponents) do
if opponent.isSpecialArchon then
local isSpecialArchon = mode:match('^%dS$')
if isSpecialArchon then
-- Team melee: Sort players by the order they were inputted
table.sort(opponent.players, function(a, b)
return a.position < b.position
end)
else
-- Sort players by the order they appear in the match opponent players list
table.sort(opponent.players, function(a, b)
return a.matchPlayerIndex < b.matchPlayerIndex
end)
table.sort(players, function(a, b) return a.position < b.position end)
end
end

return opponents
return Table.merge(mapOpponent, {
isArchon = mode == 'Archon',
isSpecialArchon = isSpecialArchon,
players = players,
})
end)
end

---Group games on the subgroup field to form submatches
Expand Down
27 changes: 15 additions & 12 deletions components/match2/wikis/starcraft/match_legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

local MatchLegacy = {}

local json = require('Module:Json')
local Json = require('Module:Json')
local Logic = require('Module:Logic')
local String = require('Module:StringUtils')
local Table = require('Module:Table')
local Template = require('Module:Template')
Expand All @@ -33,13 +34,13 @@ end
function MatchLegacy._storeGames(match, match2)
local games = ''
for gameIndex, game in ipairs(match2.match2games or {}) do
game.extradata = json.parseIfString(game.extradata or '{}') or game.extradata
game.extradata = Json.parseIfString(game.extradata or '{}') or game.extradata

if game.mode == '1v1' then
game.opponent1 = game.extradata.opponent1
game.opponent2 = game.extradata.opponent2
game.date = match.date
local scores = json.parseIfString(game.scores or '{}') or {}
local scores = Json.parseIfString(game.scores or '{}') or {}
game.opponent1score = scores[1] or 0
game.opponent2score = scores[2] or 0

Expand All @@ -48,7 +49,8 @@ function MatchLegacy._storeGames(match, match2)

-- participants holds additional playerdata per match, e.g. the faction (=race)
-- participants is stored as opponentID_playerID, so e.g. for opponent2, player1 it is "2_1"
local playerdata = json.parseIfString(game.participants or '{}') or game.participants
local playerdata = Table.mapValues(Json.parseIfString(game.participants or '{}') or game.participants or {},
Logic.nilIfEmpty)
for key, item in pairs(playerdata) do
local keyArray = mw.text.split(key or '', '_')
local l = tonumber(keyArray[2])
Expand All @@ -63,7 +65,7 @@ function MatchLegacy._storeGames(match, match2)
end
game.extradata.gamenumber = gameIndex

game.extradata = json.stringify(game.extradata)
game.extradata = Json.stringify(game.extradata)
local res = mw.ext.LiquipediaDB.lpdb_game(
'legacygame_' .. match2.match2id .. gameIndex,
game
Expand All @@ -75,11 +77,12 @@ function MatchLegacy._storeGames(match, match2)

submatch.opponent1 = game.extradata.opponent1
submatch.opponent2 = game.extradata.opponent2
local scores = json.parseIfString(game.scores or '{}') or {}
local scores = Json.parseIfString(game.scores or '{}') or {}
submatch.opponent1score = scores[1] or 0
submatch.opponent2score = scores[2] or 0
submatch.extradata = {}
local playerdata = json.parseIfString(game.participants or '{}') or game.participants
local playerdata = Table.mapValues(Json.parseIfString(game.participants or '{}') or game.participants,
Logic.nilIfEmpty)
for key, item in pairs(playerdata) do
local keyArray = mw.text.split(key or '', '_')
local l = tonumber(keyArray[2])
Expand Down Expand Up @@ -109,7 +112,7 @@ function MatchLegacy._storeGames(match, match2)
submatch.liquipediatier = match2.liquipediatier
submatch.type = match2.type
submatch.game = match2.game
submatch.extradata = json.stringify(submatch.extradata --[[@as table]])
submatch.extradata = Json.stringify(submatch.extradata --[[@as table]])

mw.ext.LiquipediaDB.lpdb_match(
'legacymatch_' .. match2.match2id .. gameIndex,
Expand All @@ -130,7 +133,7 @@ function MatchLegacy._convertParameters(match2)
end

match.staticid = match2.match2id
match.extradata = json.parseIfString(match.extradata) or {}
match.extradata = Json.parseIfString(match.extradata) or {}
local opponent1 = match2.match2opponents[1] or {}
local opponent1match2players = opponent1.match2players or {}
local opponent2 = match2.match2opponents[2] or {}
Expand All @@ -145,14 +148,14 @@ function MatchLegacy._convertParameters(match2)
match.opponent1score = (tonumber(opponent1.score or 0) or 0) >= 0 and opponent1.score or 0
match.opponent1flag = player.flag
match.extradata.opponent1name = player.displayname
player.extradata = json.parseIfString(player.extradata or '{}') or player.extradata
player.extradata = Json.parseIfString(player.extradata or '{}') or player.extradata
match.extradata.opponent1race = player.extradata.faction
player = opponent2match2players[1] or {}
match.opponent2 = player.name
match.opponent2score = (tonumber(opponent2.score or 0) or 0) >= 0 and opponent2.score or 0
match.opponent2flag = player.flag
match.extradata.opponent2name = player.displayname
player.extradata = json.parseIfString(player.extradata or '{}') or player.extradata
player.extradata = Json.parseIfString(player.extradata or '{}') or player.extradata
match.extradata.opponent2race = player.extradata.faction
elseif opponent1.type == 'team' then
match.opponent1 = Template.safeExpand(
Expand All @@ -177,7 +180,7 @@ function MatchLegacy._convertParameters(match2)
match.walkover = match.winner
end
match.extradata.bestof = match2.bestof ~= 0 and tostring(match2.bestof) or ''
match.extradata = json.stringify(match.extradata)
match.extradata = Json.stringify(match.extradata)
else
return nil, false
end
Expand Down
32 changes: 18 additions & 14 deletions components/match2/wikis/stormgate/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function MapFunctions.getTeamMapPlayers(mapInput, opponent, opponentIndex)
return Logic.nilIfEmpty(mapInput['t' .. opponentIndex .. 'p' .. playerIndex])
end)

local participants, unattachedParticipants = MatchGroupInputUtil.parseParticipants(
local mapPlayers = MatchGroupInputUtil.parseMapPlayers(
opponent.match2players,
players,
function(playerIndex)
Expand Down Expand Up @@ -235,21 +235,21 @@ function MapFunctions.getTeamMapPlayers(mapInput, opponent, opponentIndex)
end
)

Array.forEach(unattachedParticipants, function(participant)
local name = mapInput['t' .. opponentIndex .. 'p' .. participant.position]
Array.forEach(mapPlayers, function(player, playerIndex)
if Logic.isEmpty(player) then return end
local name = mapInput['t' .. opponentIndex .. 'p' .. player.position]
local nameUpper = name:upper()
local isTBD = nameUpper == TBD

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

return participants
return mapPlayers
end

---@param mapInput table
Expand Down Expand Up @@ -283,9 +283,7 @@ end
---@param opponents table[]
---@return string
function MapFunctions.getMapMode(match, map, opponents)
local playerCounts = Array.map(map.opponents or {}, function(opponent)
return Table.size(opponent.players or {})
end)
local playerCounts = Array.map(map.opponents or {}, MapFunctions.getMapOpponentSize)

local modeParts = Array.map(playerCounts, function(count, opponentIndex)
if count == 0 then
Expand All @@ -311,7 +309,7 @@ function MapFunctions.getExtraData(match, map, opponents)

if #opponents ~= 2 then
return extradata
elseif Array.any(map.opponents, function(mapOpponent) return Table.size(mapOpponent.players or {}) ~= 1 end) then
elseif Array.any(map.opponents, function(opponent) return MapFunctions.getMapOpponentSize(opponent) ~= 1 end) then
return extradata
end

Expand Down Expand Up @@ -371,4 +369,10 @@ function MapFunctions.readHeroes(heroesInput, faction, playerName, ignoreFaction
end)
end

---@param opponent table
---@return integer
function MapFunctions.getMapOpponentSize(opponent)
return Table.size(Array.filter(opponent.players or {}, Logic.isNotEmpty))
end

return CustomMatchGroupInput
Loading

0 comments on commit 7933169

Please sign in to comment.