Skip to content

Commit

Permalink
refactor(match2): move h2h links to MGI (#4942)
Browse files Browse the repository at this point in the history
* refactor(match2): move h2h links to MGI

* fix rl typo

* forgot to add aoe link data

* type typo and remove special parsing

* aoe typo

* fix wc3 and sc
  • Loading branch information
Rathoz authored Oct 23, 2024
1 parent 4d4b0e1 commit 89b8d7a
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 190 deletions.
4 changes: 2 additions & 2 deletions components/match2/commons/match_group_input_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1036,13 +1036,13 @@ function MatchGroupInputUtil.prefixPartcipants(opponentIndex)
end

---@param match table
---@return table<string, string>
---@return table<string, string?>
function MatchGroupInputUtil.getLinks(match)
local links = Links.transform(match)
return Table.mapValues(
Links.makeFullLinksForTableItems(links, 'match', false),
String.nilIfEmpty
) --[[@as table<string, string>]]
)
end

--- Warning, both match and standalone match may be mutated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function StarcraftMatchGroupInput.processMatch(match, options)

local games = MatchFunctions.extractMaps(match, opponents)
match.links = MatchGroupInputUtil.getLinks(match)
match.links.headtohead = MatchFunctions.getHeadToHeadLink(match, opponents)

local autoScoreFunction = MatchGroupInputUtil.canUseAutoScore(match, games)
and MatchFunctions.calculateMatchScore(games, opponents)
Expand Down Expand Up @@ -217,10 +218,8 @@ end
function MatchFunctions.getExtraData(match, numberOfGames)
local extradata = {
casters = MatchGroupInputUtil.readCasters(match, {noSort = true}),
headtohead = tostring(Logic.readBool(Logic.emptyOr(match.headtohead, Variables.varDefault('headtohead')))),
ffa = 'false',
}
Variables.varDefine('headtohead', extradata.headtohead)

for prefix, vetoMap, vetoIndex in Table.iter.pairsByPrefix(match, 'veto') do
MatchFunctions.getVeto(extradata, vetoMap, match, prefix, vetoIndex)
Expand All @@ -233,6 +232,27 @@ function MatchFunctions.getExtraData(match, numberOfGames)
return extradata
end

---@param match table
---@param opponents table[]
---@return string?
function MatchFunctions.getHeadToHeadLink(match, opponents)
local showH2H = Logic.readBool(Logic.emptyOr(match.headtohead, Variables.varDefault('headtohead')))
Variables.varDefine('headtohead', tostring(showH2H))

if not showH2H or #opponents ~= 2 or Array.any(opponents, function(opponent)
return opponent.type ~= Opponent.solo or not ((opponent.match2players or {})[1] or {}).name end)
then
return
end

return (tostring(mw.uri.fullUrl('Special:RunQuery/Match_history'))
.. '?pfRunQueryFormName=Match+history&Head_to_head_query%5Bplayer%5D='
.. opponents[1].players[1].name
.. '&Head_to_head_query%5Bopponent%5D='
.. opponents[2].players[1].name
.. '&wpRunQuery=Run+query'):gsub(' ', '_')
end

---@param extradata table
---@param map string
---@param match table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ local StarcraftMatchGroupUtil = Table.deepCopy(MatchGroupUtil)

---@class StarcraftMatchGroupUtilMatch: MatchGroupUtilMatch
---@field games StarcraftMatchGroupUtilGame[]
---@field headToHead boolean
---@field isFfa boolean
---@field noScore boolean?
---@field opponentMode 'uniform'|'team'
Expand Down Expand Up @@ -109,7 +108,6 @@ function StarcraftMatchGroupUtil.matchFromRecord(record)
end

-- Misc
match.headToHead = Logic.readBool(Table.extract(extradata, 'headtohead'))
match.isFfa = Logic.readBool(Table.extract(extradata, 'ffa'))
match.noScore = Logic.readBoolOrNil(Table.extract(extradata, 'noscore'))
match.casters = String.nilIfEmpty(Table.extract(extradata, 'casters'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,6 @@ function StarcraftMatchSummary.MatchSummaryContainer(args)
return matchSummary:create()
end

---@param match StarcraftMatchGroupUtilMatch
---@param footer MatchSummaryFooter
---@return MatchSummaryFooter
function StarcraftMatchSummary.addToFooter(match, footer)
footer = MatchSummary.addVodsToFooter(match, footer)

if not match.headToHead or #match.opponents ~= 2 or Array.any(match.opponents, function(opponent)
return opponent.type ~= Opponent.solo or not ((opponent.players or {})[1] or {}).pageName end)
then
return footer:addLinks(match.links)
end
match.links.headtohead = tostring(mw.uri.fullUrl('Special:RunQuery/Match_history'))
.. '?pfRunQueryFormName=Match+history&Head_to_head_query%5Bplayer%5D='
.. match.opponents[1].players[1].pageName
.. '&Head_to_head_query%5Bopponent%5D='
.. match.opponents[2].players[1].pageName
.. '&wpRunQuery=Run+query'
match.links.headtohead = string.gsub(match.links.headtohead, ' ', '_')

return footer:addLinks(match.links)
end

---@param match StarcraftMatchGroupUtilMatch
---@return MatchSummaryBody
function StarcraftMatchSummary.createBody(match)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function CustomMatchGroupInput.processMatch(match, options)

local games = CustomMatchGroupInput.extractMaps(match, opponents)
match.links = MatchGroupInputUtil.getLinks(match)
match.links.headtohead = CustomMatchGroupInput.getHeadToHeadLink(match, opponents)

local autoScoreFunction = MatchGroupInputUtil.canUseAutoScore(match, games)
and CustomMatchGroupInput.calculateMatchScore(games)
Expand Down Expand Up @@ -241,11 +242,33 @@ end
---@return table
function CustomMatchGroupInput._getExtraData(match)
return {
headtohead = Logic.emptyOr(match.headtohead, Variables.varDefault('tournament_headtohead')),
casters = MatchGroupInputUtil.readCasters(match, {noSort = true}),
}
end

---@param match table
---@param opponents table[]
---@return string?
function CustomMatchGroupInput.getHeadToHeadLink(match, opponents)
if opponents[1].type ~= Opponent.solo or opponents[2].type ~= Opponent.solo then
return
end
if not Logic.readBool(Logic.emptyOr(match.headtohead, Variables.varDefault('tournament_headtohead'))) then
return nil
end
if Opponent.isEmpty(opponents[1]) or Opponent.isEmpty(opponents[2]) then
return nil
end

local player1, player2 =
string.gsub(opponents[1].name, ' ', '_'),
string.gsub(opponents[2].name, ' ', '_')

return tostring(mw.uri.fullUrl('Special:RunQuery/Match_history')) ..
'?pfRunQueryFormName=Match+history&Head_to_head_query%5Bplayer%5D=' ..player1 ..
'&Head_to_head_query%5Bopponent%5D=' .. player2 .. '&wpRunQuery=Run+query'
end

---@param map table
---@param mapsInfo {name: string, link: string}[]?
---@return string?
Expand Down
52 changes: 0 additions & 52 deletions components/match2/wikis/ageofempires/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ local DateExt = require('Module:Date/Ext')
local Faction = require('Module:Faction')
local Game = require('Module:Game')
local Icon = require('Module:Icon')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local MapMode = require('Module:MapMode')
local Operator = require('Module:Operator')
local String = require('Module:StringUtils')
local Table = require('Module:Table')

local DisplayHelper = Lua.import('Module:MatchGroup/Display/Helper')
local MatchSummary = Lua.import('Module:MatchSummary/Base')
Expand All @@ -30,17 +28,6 @@ local GREEN_CHECK = Icon.makeIcon{iconName = 'winner', color = 'forest-green-tex
local DRAW_LINE = Icon.makeIcon{iconName = 'draw', color = 'bright-sun-text', size = 'initial'}
local NO_CHECK = '[[File:NoCheck.png|link=]]'

local LINKDATA = {
mapdraft = {
text = 'Map Draft',
icon = 'File:Map Draft Icon.png'
},
civdraft = {
text = 'Civ Draft',
icon = 'File:Civ Draft Icon.png'
}
}

local CustomMatchSummary = {}

---@param args table
Expand Down Expand Up @@ -91,45 +78,6 @@ function CustomMatchSummary.createBody(match)
return body
end

---@param match MatchGroupUtilMatch
---@param footer MatchSummaryFooter
---@return MatchSummaryFooter
function CustomMatchSummary.addToFooter(match, footer)
footer = MatchSummary.addVodsToFooter(match, footer)

local addLinks = function(linkType)
local currentLinkData = LINKDATA[linkType]
if not currentLinkData then
mw.log('Unknown link: ' .. linkType)
return
end
for _, link in Table.iter.pairsByPrefix(match.links, linkType, {requireIndex = false}) do
footer:addLink(link, currentLinkData.icon, currentLinkData.iconDark, currentLinkData.text)
end
end

addLinks('mapdraft')
addLinks('civdraft')

if not Logic.readBool(match.extradata.headtohead) or not CustomMatchSummary._isSolo(match) then
return footer
end

if not Opponent.isEmpty(match.opponents[1]) and not Opponent.isEmpty(match.opponents[2]) then
local player1, player2 = string.gsub(match.opponents[1].name, ' ', '_'),
string.gsub(match.opponents[2].name, ' ', '_')
footer:addElement(
'[[File:Match Info Stats.png|link=' ..
tostring(mw.uri.fullUrl('Special:RunQuery/Match_history')) ..
'?pfRunQueryFormName=Match+history&Head_to_head_query%5Bplayer%5D=' ..
player1 ..
'&Head_to_head_query%5Bopponent%5D=' .. player2 .. '&wpRunQuery=Run+query|Head-to-head statistics]]'
)
end

return footer
end

---@param match MatchGroupUtilMatch
---@return boolean
function CustomMatchSummary._isSolo(match)
Expand Down
2 changes: 1 addition & 1 deletion components/match2/wikis/dota2/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ end
---@param opponents table[]
---@return table
function MatchFunctions.getLinks(match, games, opponents)
---@type table<string, string|table>
---@type table<string, string|table|nil>
local links = MatchGroupInputUtil.getLinks(match)
links.stratz = {}
links.dotabuff = {}
Expand Down
37 changes: 37 additions & 0 deletions components/match2/wikis/halo/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ local Table = require('Module:Table')
local Variables = require('Module:Variables')

local MatchGroupInputUtil = Lua.import('Module:MatchGroup/Input/Util')
local OpponentLibrary = require('Module:OpponentLibraries')
local Opponent = OpponentLibrary.Opponent

local DEFAULT_BESTOF = 3
local DEFAULT_MODE = 'team'
Expand All @@ -41,6 +43,7 @@ function CustomMatchGroupInput.processMatch(match, options)

local games = MatchFunctions.extractMaps(match, #opponents)
match.links = MatchGroupInputUtil.getLinks(match)
match.links.headtohead = MatchFunctions.getHeadToHeadLink(match, opponents)

match.bestof = MatchFunctions.getBestOf(match.bestof)

Expand Down Expand Up @@ -153,6 +156,40 @@ function MatchFunctions.getExtraData(match)
}
end

---@param match table
---@param opponents table[]
---@return string?
function MatchFunctions.getHeadToHeadLink(match, opponents)
if
opponents[1].type ~= Opponent.team or
opponents[2].type ~= Opponent.team or
not opponents[1].name or
not opponents[2].name then

return nil
end

local team1, team2 = string.gsub(opponents[1].name, ' ', '_'), string.gsub(opponents[2].name, ' ', '_')
local buildQueryFormLink = function(form, template, arguments)
return tostring(mw.uri.fullUrl('Special:RunQuery/' .. form,
mw.uri.buildQueryString(Table.map(arguments, function(key, value) return template .. key, value end))
.. '&_run'
))
end

local headtoheadArgs = {
['[team1]'] = team1,
['[team2]'] = team2,
['[games][is_list]'] = 1,
['[tiers][is_list]'] = 1,
['[fromdate][day]'] = '01',
['[fromdate][month]'] = '01',
['[fromdate][year]'] = string.sub(match.date,1,4)
}

return buildQueryFormLink('Head2head', 'Headtohead', headtoheadArgs)
end

--
-- map related functions
--
Expand Down
39 changes: 0 additions & 39 deletions components/match2/wikis/halo/match_summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ local DisplayHelper = Lua.import('Module:MatchGroup/Display/Helper')
local MatchSummary = Lua.import('Module:MatchSummary/Base')
local MatchSummaryWidgets = Lua.import('Module:Widget/Match/Summary/All')

local OpponentLibrary = require('Module:OpponentLibraries')
local Opponent = OpponentLibrary.Opponent

local GREEN_CHECK = Icon.makeIcon{iconName = 'winner', color = 'forest-green-text', size = '110%'}
local NO_CHECK = '[[File:NoCheck.png|link=]]'

Expand All @@ -32,42 +29,6 @@ function CustomMatchSummary.getByMatchId(args)
return MatchSummary.defaultGetByMatchId(CustomMatchSummary, args)
end

---@param match MatchGroupUtilMatch
---@param footer MatchSummaryFooter
---@return MatchSummaryFooter
function CustomMatchSummary.addToFooter(match, footer)
footer = MatchSummary.addVodsToFooter(match, footer)

if
match.opponents[1].type == Opponent.team and
match.opponents[2].type == Opponent.team and
match.opponents[1].name and
match.opponents[2].name
then
local team1, team2 = string.gsub(match.opponents[1].name, ' ', '_'), string.gsub(match.opponents[2].name, ' ', '_')
local buildQueryFormLink = function(form, template, arguments)
return tostring(mw.uri.fullUrl('Special:RunQuery/' .. form,
mw.uri.buildQueryString(Table.map(arguments, function(key, value) return template .. key, value end))
.. '&_run'
))
end

local headtoheadArgs = {
['[team1]'] = team1,
['[team2]'] = team2,
['[games][is_list]'] = 1,
['[tiers][is_list]'] = 1,
['[fromdate][day]'] = '01',
['[fromdate][month]'] = '01',
['[fromdate][year]'] = string.sub(match.date,1,4)
}

match.links.headtohead = buildQueryFormLink('Head2head', 'Headtohead', headtoheadArgs)
end

return footer:addLinks(match.links)
end

---@param match MatchGroupUtilMatch
---@return MatchSummaryBody
function CustomMatchSummary.createBody(match)
Expand Down
30 changes: 20 additions & 10 deletions components/match2/wikis/rocketleague/match_group_input_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function CustomMatchGroupInput.processMatch(match, options)
end)
local games = CustomMatchGroupInput.extractMaps(match, #opponents)
match.links = MatchGroupInputUtil.getLinks(match)
match.links.headtohead = MatchFunctions.getHeadToHeadLink(match, opponents)

Array.forEach(opponents, function(opponent, opponentIndex)
opponent.extradata = CustomMatchGroupInput.getOpponentExtradata(opponent)
Expand Down Expand Up @@ -162,21 +163,30 @@ end

---@param match table
---@param opponents table[]
---@return table
function MatchFunctions.getExtraData(match, opponents)
local opponent1 = opponents[1]
local opponent2 = opponents[2]
---@return string?
function MatchFunctions.getHeadToHeadLink(match, opponents)
if not Logic.readBool(Logic.emptyOr(match.showh2h, Variables.varDefault('showh2h'))) or
opponents[1].type ~= Opponent.team or
opponents[2].type ~= Opponent.team then

local showh2h = Logic.readBool(Logic.emptyOr(match.showh2h, Variables.varDefault('showh2h')))
and opponent1.type == Opponent.team
and opponent2.type == Opponent.team
return nil
end

local team1, team2 = mw.uri.encode(opponents[1].name), mw.uri.encode(opponents[2].name)
return tostring(mw.uri.fullUrl('Special:RunQuery/Head2head'))
.. '?RunQuery=Run&pfRunQueryFormName=Head2head&Headtohead%5Bteam1%5D='
.. team1 .. '&Headtohead%5Bteam2%5D=' .. team2
end

---@param match table
---@param opponents table[]
---@return table
function MatchFunctions.getExtraData(match, opponents)
return {
showh2h = showh2h,
isfeatured = MatchFunctions.isFeatured(opponents, tonumber(match.liquipediatier)),
casters = MatchGroupInputUtil.readCasters(match),
hasopponent1 = MatchFunctions._checkForNonEmptyOpponent(opponent1),
hasopponent2 = MatchFunctions._checkForNonEmptyOpponent(opponent2),
hasopponent1 = MatchFunctions._checkForNonEmptyOpponent(opponents[1]),
hasopponent2 = MatchFunctions._checkForNonEmptyOpponent(opponents[2]),
liquipediatiertype2 = Variables.varDefault('tournament_tiertype2'),
}
end
Expand Down
Loading

0 comments on commit 89b8d7a

Please sign in to comment.