Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions lua/wikis/commons/Standings/Parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ function StandingsParser.calculateTiebreakerValues(opponentsInRound, tiebreakerI
return
end
Array.forEach(opponentsInRound, function(opponent)
local value = tiebreaker:valueOf(opponentsInRound, opponent)
opponent.extradata.tiebreakerValues[tiebreakerId] = {
value = tiebreaker:valueOf(opponentsInRound, opponent),
display = tiebreaker:display(opponentsInRound, opponent),
value = value,
display = tiebreaker:display(opponentsInRound, opponent, value),
}
end)
end)
Expand Down
56 changes: 46 additions & 10 deletions lua/wikis/commons/Standings/Tiebreaker/Buchholz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local Lua = require('Module:Lua')

local Array = Lua.import('Module:Array')
local Class = Lua.import('Module:Class')
local FnUtil = Lua.import('Module:FnUtil')

local Opponent = Lua.import('Module:Opponent/Custom')

Expand All @@ -17,23 +18,58 @@ local TiebreakerInterface = Lua.import('Module:Standings/Tiebreaker/Interface')
---@class TiebreakerBuchholz : StandingsTiebreaker
local TiebreakerBuchholz = Class.new(TiebreakerInterface)

---@param state TiebreakerOpponent[]
---Build a set of enemy names for an opponent, memoized per opponent entry table.
---Falls back to Opponent.same for renamed-team detection and aliases the result.
---@param opponent TiebreakerOpponent
---@return integer
function TiebreakerBuchholz:valueOf(state, opponent)
local enemies = Array.flatMap(opponent.matches, function(match)
---@return table<string, true>
local getEnemyNames = FnUtil.memoize(function(opponent)
local ownName = Opponent.toName(opponent.opponent)
local enemyNames = {}
Array.forEach(opponent.matches, function(match)
if not match.finished then
return {}
return
end
return Array.filter(match.opponents, function (opp)
return not Opponent.same(opp, opponent.opponent)
Array.forEach(match.opponents, function(matchOpponent)
local name = Opponent.toName(matchOpponent)
-- Skip self: by name, or by Opponent.same for renamed-team edge cases
if name == ownName then
return
end
if matchOpponent.type == 'team' and Opponent.same(matchOpponent, opponent.opponent) then
return
end
enemyNames[name] = true
end)
end)
return enemyNames
end)

---@param state TiebreakerOpponent[]
---@param opponent TiebreakerOpponent
---@return integer
function TiebreakerBuchholz:valueOf(state, opponent)
local enemyNames = getEnemyNames(opponent)

return Array.reduce(state, function(score, groupMember)
local isEnemy = Array.any(enemies, function(enemy)
return Opponent.same(enemy, groupMember.opponent)
end)
local memberName = Opponent.toName(groupMember.opponent)
local isEnemy = enemyNames[memberName]

-- Fallback for renamed teams: scan match opponents with Opponent.same
if not isEnemy and groupMember.opponent.type == 'team' then
local found = Array.any(opponent.matches, function(match)
if not match.finished then
return false
end
return Array.any(match.opponents, function(matchOpponent)
return Opponent.same(matchOpponent, groupMember.opponent)
end)
end)
if found then
-- Alias so this cost is only paid once per group member
enemyNames[memberName] = true
isEnemy = true
end
end

if not isEnemy then
return score
Expand Down
3 changes: 2 additions & 1 deletion lua/wikis/commons/Standings/Tiebreaker/Game/Diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ end

---@param state TiebreakerOpponent[]
---@param opponent TiebreakerOpponent
---@param value integer?
---@return string
function TiebreakerGameDiff:display(state, opponent)
function TiebreakerGameDiff:display(state, opponent, value)
local games = TiebreakerGameUtil.getGames(opponent)
return games.w .. ' - ' .. games.l
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ end

---@param state TiebreakerOpponent[]
---@param opponent TiebreakerOpponent
---@param value integer?
---@return string
function TiebreakerRoundDiff:display(state, opponent)
function TiebreakerRoundDiff:display(state, opponent, value)
local rounds = TiebreakerRoundUtil.getRounds(opponent)
return rounds.w .. ' - ' .. rounds.l
end
Expand Down
5 changes: 3 additions & 2 deletions lua/wikis/commons/Standings/Tiebreaker/Game/WinRate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ end

---@param state TiebreakerOpponent[]
---@param opponent TiebreakerOpponent
---@param value integer?
---@return string
function TiebreakerGameWinRate:display(state, opponent)
function TiebreakerGameWinRate:display(state, opponent, value)
local games = TiebreakerGameUtil.getGames(opponent)
if games == 0 then
return '-'
end
return MathUtil.formatPercentage(self:valueOf(state, opponent), 2)
return MathUtil.formatPercentage(value ~= nil and value or self:valueOf(state, opponent), 2)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value ~= nil and is redundant

end

return TiebreakerGameWinRate
8 changes: 6 additions & 2 deletions lua/wikis/commons/Standings/Tiebreaker/Interface.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ end

---@param state TiebreakerOpponent[]
---@param opponent TiebreakerOpponent
---@param value integer?
---@return string
function StandingsTiebreaker:display(state, opponent)
return tostring(self:valueOf(state, opponent))
function StandingsTiebreaker:display(state, opponent, value)
if value == nil then
value = self:valueOf(state, opponent)
end
return tostring(value)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return tostring(value or self:valueOf(state, opponent))

plus kick the if above

end

---@return 'full'|'ml'|'h2h'
Expand Down
3 changes: 2 additions & 1 deletion lua/wikis/commons/Standings/Tiebreaker/Match/Diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ end

---@param state TiebreakerOpponent[]
---@param opponent TiebreakerOpponent
---@param value integer?
---@return string
function TiebreakerMatchDiff:display(state, opponent)
function TiebreakerMatchDiff:display(state, opponent, value)
return opponent.match.w .. ' - ' .. opponent.match.l
end

Expand Down
5 changes: 3 additions & 2 deletions lua/wikis/commons/Standings/Tiebreaker/Match/WinRate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ end

---@param state TiebreakerOpponent[]
---@param opponent TiebreakerOpponent
---@param value integer?
---@return string
function TiebreakerMatchWinRate:display(state, opponent)
return MathUtil.formatPercentage(self:valueOf(state, opponent), 2)
function TiebreakerMatchWinRate:display(state, opponent, value)
return MathUtil.formatPercentage(value ~= nil and value or self:valueOf(state, opponent), 2)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

end

return TiebreakerMatchWinRate
Loading