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
24 changes: 4 additions & 20 deletions apps/matchmakingserver/battleground/battleground.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,13 @@ func (b *Battleground) FreeSlotsForTeam(team PVPTeam) uint8 {
}

// BackfillSlotsForTeam returns how many players the team can accept from the
// queue without outgrowing the opposite team: a running battleground only
// refills a team up to the opposite team's headcount (or MinPlayersPerTeam if
// bigger), capped by MaxPlayersPerTeam. A balanced battleground accepts no
// newcomers, so fresh groups stay queued and can pop a new instance instead
// of stacking one team of an in-progress match.
// queue: up to MaxPlayersPerTeam, the retail behavior — a battleground in
// progress keeps accepting joiners while a team is below the cap, regardless
// of the balance (joining a losing match is allowed on retail too).
func (b *Battleground) BackfillSlotsForTeam(team PVPTeam) uint8 {
other := TeamAlliance
if team == TeamAlliance {
other = TeamHorde
}

teamCount := len(b.ActivePlayersPerTeam[team]) + len(b.InvitedPlayersPerTeam[team])
otherCount := len(b.ActivePlayersPerTeam[other]) + len(b.InvitedPlayersPerTeam[other])

target := int(b.MinPlayersPerTeam)
if otherCount > target {
target = otherCount
}
if target > int(b.MaxPlayersPerTeam) {
target = int(b.MaxPlayersPerTeam)
}

r := target - teamCount
r := int(b.MaxPlayersPerTeam) - teamCount
if r < 0 {
return 0
}
Expand Down
14 changes: 7 additions & 7 deletions apps/matchmakingserver/battleground/battleground_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func TestBackfillSlotsForTeam(t *testing.T) {
expectedAlliance uint8
expectedHorde uint8
}{
{"balanced full match accepts nobody", 5, 5, 0, 0},
{"short team refills up to min", 4, 5, 1, 0},
{"both short refill to min", 3, 4, 2, 1},
{"team refills up to bigger opposite team", 5, 8, 3, 0},
{"match at min keeps filling to max", 5, 5, 5, 5},
{"short team refills toward max", 4, 5, 6, 5},
{"both short refill toward max", 3, 4, 7, 6},
{"unbalanced still fills both to max", 5, 8, 5, 2},
{"never past max", 5, 10, 5, 0},
{"balanced above min accepts nobody", 8, 8, 0, 0},
{"empty bg refills to min", 0, 0, 5, 5},
{"balanced above min keeps filling", 8, 8, 2, 2},
{"empty bg refills to max", 0, 0, 10, 10},
}

for _, c := range cases {
Expand All @@ -50,7 +50,7 @@ func TestBackfillSlotsForTeam(t *testing.T) {
}

func TestBackfillSlotsForTeamCountsInvitedPlayers(t *testing.T) {
bg := bgWithCounts(5, 10, 4, 5)
bg := bgWithCounts(5, 10, 9, 5)
bg.InvitedPlayersPerTeam[TeamAlliance] = append(bg.InvitedPlayersPerTeam[TeamAlliance], InvitedPlayer{GUID: guid.PlayerUnwrapped{RealmID: 1, LowGUID: 50}})

if got := bg.BackfillSlotsForTeam(TeamAlliance); got != 0 {
Expand Down
45 changes: 24 additions & 21 deletions apps/matchmakingserver/service/battleground-queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func TestGenericBattlegroundQueue_FillInExistingBG(t *testing.T) {
mockService.AssertExpectations(t)
}

// TestGenericBattlegroundQueue_BalancedBGDoesNotAbsorbNewGroups reproduces the
// 10v5 bug: a balanced in-progress battleground (5v5, max 10) must not absorb a
// fresh 5-player group on one team; the group stays queued and pops a new
// instance once the opposite faction is available.
func TestGenericBattlegroundQueue_BalancedBGDoesNotAbsorbNewGroups(t *testing.T) {
// TestGenericBattlegroundQueue_BalancedBGBackfillsTowardMax asserts the retail
// behavior: an in-progress battleground below MaxPlayersPerTeam keeps
// absorbing queued groups regardless of balance — the transient imbalance is
// then corrected by the opposite faction's own backfill (humans or bot fill).
func TestGenericBattlegroundQueue_BalancedBGBackfillsTowardMax(t *testing.T) {
template := repo.BattlegroundTemplate{
MinPlayersPerTeam: 5,
MaxPlayersPerTeam: 10,
Expand All @@ -152,24 +152,26 @@ func TestGenericBattlegroundQueue_BalancedBGDoesNotAbsorbNewGroups(t *testing.T)
mockService := new(mocks.BattleGroundService)
mockService.On("BattlegroundsThatNeedPlayers", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]battleground.Battleground{runningBG}, nil)
mockService.On("TemplateForQueueTypeID", mock.Anything, mock.Anything).Return(template, nil)
// No InviteGroups expectation: inviting into the running BG must not happen.
invites := 0
var invitedGroups []service.QueuedGroup
var invitedTeam battleground.PVPTeam
mockService.On("InviteGroups", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
invites++
invitedGroups = args.Get(1).([]service.QueuedGroup)
invitedTeam = args.Get(3).(battleground.PVPTeam)
}).Return(nil)

newBGCreated := false
queue := service.NewGenericBattlegroundQueue(mockService, bgCreatorMock(func(ctx context.Context, template repo.BattlegroundTemplate, queueType battleground.QueueTypeID, bracketID service.BracketID, realmID, battlegroupID uint32, allianceGroups, hordeGroups []service.QueuedGroup) error {
newBGCreated = true
return nil
}), repo.BattlegroundTemplate{TypeID: 1}, 1, 1, 1)

// 5-player alliance group queues while the balanced match runs.
// 5-player alliance group queues while the 5v5 match runs: it is invited
// into the running instance (5 free alliance slots up to the max of 10).
assert.NoError(t, queue.AddQueuedGroup(groupWithMembers(5, battleground.TeamAlliance)))
assert.False(t, newBGCreated, "no horde in queue yet")
assert.Len(t, queue.GetAllQueuedGroups(), 1, "group must stay in queue")

// Horde players arrive (e.g. the bot fill): a NEW instance pops.
for i := 0; i < 5; i++ {
assert.NoError(t, queue.AddQueuedGroup(groupWithMembers(1, battleground.TeamHorde)))
}
assert.True(t, newBGCreated, "second instance must be created")
assert.Equal(t, 1, invites, "group must backfill into the running battleground exactly once")
assert.Equal(t, battleground.TeamAlliance, invitedTeam, "backfill must target the queued group's team")
assert.Len(t, invitedGroups, 1, "exactly the one queued group is invited")
assert.Len(t, queue.GetAllQueuedGroups(), 0, "invited group leaves the queue")

mockService.AssertExpectations(t)
}
Expand All @@ -187,11 +189,12 @@ func TestGenericBattlegroundQueue_BackfillCountsLeaders(t *testing.T) {
MinPlayersPerTeam: template.MinPlayersPerTeam,
MaxPlayersPerTeam: template.MaxPlayersPerTeam,
}
for i := uint32(1); i <= 5; i++ {
if i <= 4 {
runningBG.ActivePlayersPerTeam[battleground.TeamAlliance] = append(runningBG.ActivePlayersPerTeam[battleground.TeamAlliance], getGUID(1, i))
// 9v5 with max 10: exactly one free alliance slot (retail backfill).
for i := uint32(1); i <= 9; i++ {
runningBG.ActivePlayersPerTeam[battleground.TeamAlliance] = append(runningBG.ActivePlayersPerTeam[battleground.TeamAlliance], getGUID(1, i))
if i <= 5 {
runningBG.ActivePlayersPerTeam[battleground.TeamHorde] = append(runningBG.ActivePlayersPerTeam[battleground.TeamHorde], getGUID(1, i+100))
}
runningBG.ActivePlayersPerTeam[battleground.TeamHorde] = append(runningBG.ActivePlayersPerTeam[battleground.TeamHorde], getGUID(1, i+100))
}

mockService := new(mocks.BattleGroundService)
Expand Down
Loading