Skip to content

Commit

Permalink
Add support for non team opponents in HDB participantGrabber (#3355)
Browse files Browse the repository at this point in the history
* add support for non team opponents

* enable in sc2

* fix resolve: pass along date

* enable in bw

* fixerino

* unlock participantGrabber
  • Loading branch information
hjpalpha authored Oct 9, 2023
1 parent cff052f commit c00e444
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
61 changes: 36 additions & 25 deletions components/hidden_data_box/commons/hidden_data_box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Array = require('Module:Array')
local Class = require('Module:Class')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local Namespace = require('Module:Namespace')
local ReferenceCleaner = require('Module:ReferenceCleaner')
local String = require('Module:StringUtils')
Expand All @@ -23,12 +25,13 @@ local INVALID_TIER_WARNING = '${tierString} is not a known Liquipedia '
local INVALID_PARENT = '${parent} is not a Liquipedia Tournament[[Category:Pages with invalid parent]]'
local DEFAULT_TIER_TYPE = 'general'

local Opponent = Lua.import('Module:OpponentLibraries', {requireDevIfEnabled = true}).Opponent

---Entry point
---@param args table?
---@return string
function HiddenDataBox.run(args)
args = args or {}
args.participantGrabber = Logic.nilOr(Logic.readBoolOrNil(args.participantGrabber), true)
local doQuery = not Logic.readBool(args.noQuery)

local warnings
Expand All @@ -47,17 +50,13 @@ function HiddenDataBox.run(args)

if not queryResult[1] and Namespace.isMain() then
table.insert(warnings, String.interpolate(INVALID_PARENT, {parent = parent}))
elseif args.participantGrabber then
local participants = HiddenDataBox._fetchParticipants(parent)

Table.iter.forEachPair(participants, function (participant, players)
-- TODO: An improvement would be called TeamCard module for this
-- Would need a rework for the function that does it however
local participantResolved = mw.ext.TeamLiquidIntegration.resolve_redirect(participant)
else
local date = HiddenDataBox.cleanDate(args.date, args.sdate) or queryResult.startdate or
Variables.varDefault('tournament_startdate') or HiddenDataBox.cleanDate(args.edate) or
queryResult.enddate or Variables.varDefault('tournament_enddate')

Table.iter.forEachPair(players, function(key, value)
HiddenDataBox.setWikiVariableForParticipantKey(participant, participantResolved, key, value)
end)
Array.forEach(HiddenDataBox._fetchPlacements(parent), function(placement)
HiddenDataBox._setWikiVariablesFromPlacement(placement, date)
end)
end

Expand Down Expand Up @@ -124,23 +123,17 @@ end

---Fetches participant information from the parent page
---@param parent string
---@return {[string]: {[string]: string}}
function HiddenDataBox._fetchParticipants(parent)
---@return {opponentname: string, opponenttype: OpponentType, opponentplayers: table<string, string>}[]
function HiddenDataBox._fetchPlacements(parent)
local placements = mw.ext.LiquipediaDB.lpdb('placement', {
conditions = '[[pagename::' .. parent .. ']]',
conditions = '[[pagename::' .. parent .. ']] AND [[opponenttype::!' .. Opponent.literal .. ']]',
limit = 1000,
query = 'players, participant',
query = 'opponentplayers, opponentname, opponenttype',
})

return
Table.map(
Table.filter(placements, function(placement)
return String.isNotEmpty(placement.participant) and placement.participant:lower() ~= 'tbd'
end),
function(_, placement)
return placement.participant, placement.players
end
)
return Array.filter(placements, function(placement)
return String.isNotEmpty(placement.opponentname) and placement.opponentname:lower() ~= 'tbd'
end)
end

-- overridable so that wikis can add custom vars
Expand All @@ -149,12 +142,30 @@ end
function HiddenDataBox.addCustomVariables(args, queryResult)
end

---@param placement {opponentname: string, opponenttype: OpponentType, opponentplayers: table<string, string>}
---@param date string
function HiddenDataBox._setWikiVariablesFromPlacement(placement, date)
if Opponent.typeIsParty(placement.opponenttype) then
---Opponent.resolve with syncPlayer enabled sets wiki variables as needed
Opponent.resolve(Opponent.fromLpdbStruct(placement), date, {syncPlayer = true})
return
end

-- TODO: An improvement would be called TeamCard module for this
-- Would need a rework for the function that does it however
local participant = placement.opponentname
local participantResolved = mw.ext.TeamLiquidIntegration.resolve_redirect(participant)
Table.iter.forEachPair(placement.opponentplayers, function(key, value)
HiddenDataBox._setWikiVariableForParticipantKey(participant, participantResolved, key, value)
end)
end

-- overridable so that wikis can add custom vars
---@param participant string
---@param participantResolved string
---@param key string
---@param value string|number
function HiddenDataBox.setWikiVariableForParticipantKey(participant, participantResolved, key, value)
function HiddenDataBox._setWikiVariableForParticipantKey(participant, participantResolved, key, value)
Variables.varDefine(participant .. '_' .. key, value)
if participant ~= participantResolved then
Variables.varDefine(participantResolved .. '_' .. key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local CustomHiddenDataBox = {}
function CustomHiddenDataBox.run(args)
args = args or {}
args.game = Game.name{game = args.game}
args.participantGrabber = false

BasicHiddenDataBox.addCustomVariables = CustomHiddenDataBox.addCustomVariables

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local CustomHiddenDataBox = {}
function CustomHiddenDataBox.run(args)
args = args or {}
args.game = Game.name{game = args.game}
args.participantGrabber = false

BasicHiddenDataBox.addCustomVariables = CustomHiddenDataBox.addCustomVariables

Expand Down

0 comments on commit c00e444

Please sign in to comment.