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
18 changes: 18 additions & 0 deletions apps/matchmakingserver/battleground/battleground.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ func (b *Battleground) TeamForInvitedPlayer(playerGUID uint64, realmID uint32) (
return false, TeamAny
}

// IsActivePlayer reports whether the player is a seated participant.
func (b *Battleground) IsActivePlayer(playerGUID uint64, realmID uint32) bool {
unwrappedGUID := guid.PlayerUnwrapped{
RealmID: uint16(realmID),
LowGUID: guid.LowType(playerGUID),
Comment on lines +199 to +203
}

for _, guids := range b.ActivePlayersPerTeam {
for _, g := range guids {
if g == unwrappedGUID {
return true
}
}
}

return false
}

func (b *Battleground) RemovePlayer(playerGUID uint64, realmID uint32) {
unwrappedGUID := guid.PlayerUnwrapped{
RealmID: uint16(realmID),
Expand Down
19 changes: 19 additions & 0 deletions apps/matchmakingserver/battleground/battleground_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ func TestBackfillSlotsForTeamCountsInvitedPlayers(t *testing.T) {
t.Errorf("alliance slots = %d, want 0 (invited player holds the slot)", got)
}
}

// TestIsActivePlayer locks the guard semantics: only seated participants are
// active — realm-aware, and never for unknown players.
func TestIsActivePlayer(t *testing.T) {
bg := bgWithCounts(5, 10, 2, 1)

if !bg.IsActivePlayer(1, 1) {
t.Error("seated alliance player must be active")
}
if !bg.IsActivePlayer(100, 1) {
t.Error("seated horde player must be active")
}
if bg.IsActivePlayer(1, 2) {
t.Error("same low GUID on another realm must not be active")
}
if bg.IsActivePlayer(999, 1) {
t.Error("unknown player must not be active")
}
}
14 changes: 14 additions & 0 deletions apps/matchmakingserver/service/battleground-queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func (q *GenericBattlegroundQueue) process(ctx context.Context) error {
if freeSlotsAlliance > 0 {
groupsToInvite := q.findGroupsForGivenSlots(freeSlotsAlliance, battleground.TeamAlliance)
if len(groupsToInvite) > 0 {
log.Info().
Uint32("instance", bg.InstanceID).
Uint8("slots", freeSlotsAlliance).
Int("active", len(bg.ActivePlayersPerTeam[battleground.TeamAlliance])).
Int("invited", len(bg.InvitedPlayersPerTeam[battleground.TeamAlliance])).
Int("groups", len(groupsToInvite)).
Msg("backfill invites alliance")
q.inviteGroupsToBG(groupsToInvite, &bg, battleground.TeamAlliance)
}
}
Expand All @@ -169,6 +176,13 @@ func (q *GenericBattlegroundQueue) process(ctx context.Context) error {
if freeSlotsHorde > 0 {
groupsToInvite := q.findGroupsForGivenSlots(freeSlotsHorde, battleground.TeamHorde)
if len(groupsToInvite) > 0 {
log.Info().
Uint32("instance", bg.InstanceID).
Uint8("slots", freeSlotsHorde).
Int("active", len(bg.ActivePlayersPerTeam[battleground.TeamHorde])).
Int("invited", len(bg.InvitedPlayersPerTeam[battleground.TeamHorde])).
Int("groups", len(groupsToInvite)).
Msg("backfill invites horde")
q.inviteGroupsToBG(groupsToInvite, &bg, battleground.TeamHorde)
}
}
Expand Down
28 changes: 28 additions & 0 deletions apps/matchmakingserver/service/battleground_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,12 @@ func (s *battleGroundService) PlayerJoinedBattleground(ctx context.Context, play
return nil
})
if err != nil {
// A rejected confirmation desyncs this service's team counts from the
// real battleground — the exact failure behind an observed 11v10.
log.Warn().Err(err).
Uint64("player", playerGUID).
Uint32("instance", instanceID).
Msg("join confirmation rejected")
return err
}

Expand Down Expand Up @@ -766,6 +772,13 @@ func (s *battleGroundService) RemovePlayerFromQueue(ctx context.Context, playerG
continue
}

// "Remove from queue" must never yank a seated participant out of
// a running match — active players leave through
// PlayerLeftBattleground only. Only invite/queue links are undone.
if bg.IsActivePlayer(playerGUID, realmID) {
continue
Comment on lines +775 to +779
}

err = s.PlayerLeftBattleground(ctx, playerGUID, realmID, link.BattlegroundKey.InstanceID, link.BattlegroundKey.BattlegroupID != 0)
if err != nil {
return err
Expand Down Expand Up @@ -858,6 +871,21 @@ func (s *battleGroundService) ProcessExpiredBattlegroundInvites(ctx context.Cont
continue
}

log.Info().
Uint64("player", uint64(invite.GUID.LowGUID)).
Uint32("instance", bg.InstanceID).
Msg("battleground invite expired")

// Unlink only this battleground: RemovePlayerFromQueue
// removed the player from every battleground of the same
// queue type, including one where they are an active
// participant of a running match.
s.removeBattlegroundLinkForPlayer(BattlegroundKey{
RealmID: bg.RealmID,
InstanceID: bg.InstanceID,
BattlegroupID: bg.BattleGroupID,
}, uint64(invite.GUID.LowGUID), uint32(invite.GUID.RealmID))
Comment on lines +883 to +887

slot := uint8(0) // TODO: provide real slot
err = s.eventsProducer.InviteExpired(&events.MatchmakingEventPlayersInviteExpiredPayload{
RealmID: uint32(invite.GUID.RealmID),
Expand Down
36 changes: 36 additions & 0 deletions apps/matchmakingserver/service/battleground_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,39 @@ func TestBattlegroundStatusChangedEndedPurgesInvitedLinks(t *testing.T) {
assert.Empty(t, s.GetQueueOrBattlegroundLinkForPlayer(QueuesByRealmAndPlayerKey{activePlayer}))
assert.Empty(t, s.GetQueueOrBattlegroundLinkForPlayer(QueuesByRealmAndPlayerKey{invitedPlayer}))
}

// TestRemovePlayerFromQueueKeepsActiveParticipant locks the guard: "remove
// from queue" must never yank a seated participant out of a running match.
// A regression would either mutate the battleground state or panic in
// PlayerLeftBattleground (nil events producer in this fixture).
func TestRemovePlayerFromQueueKeepsActiveParticipant(t *testing.T) {
playerGUID := uint64(12345)
realmID := uint32(1)

bg := &battleground.Battleground{
InstanceID: 101,
RealmID: realmID,
QueueTypeID: 2,
}
bg.ActivePlayersPerTeam[battleground.TeamAlliance] = append(
bg.ActivePlayersPerTeam[battleground.TeamAlliance],
guid.PlayerUnwrapped{RealmID: uint16(realmID), LowGUID: guid.LowType(playerGUID)},
)

bgRepo := repo.NewBattlegroundInMemRepo()
assert.NoError(t, bgRepo.SaveBattleground(context.Background(), bg))

service := &battleGroundService{
playersQueueOrBattleground: make(map[QueuesByRealmAndPlayerKey][]QueueOrBattlegroundLink),
battlegroundsRepo: bgRepo,
}
key := QueuesByRealmAndPlayerKey{guid.PlayerUnwrapped{RealmID: uint16(realmID), LowGUID: guid.LowType(playerGUID)}}
service.playersQueueOrBattleground[key] = []QueueOrBattlegroundLink{
{BattlegroundKey: &BattlegroundKey{InstanceID: 101, RealmID: realmID}},
}

assert.NoError(t, service.RemovePlayerFromQueue(context.Background(), playerGUID, realmID, bg.QueueTypeID))

assert.True(t, bg.IsActivePlayer(playerGUID, realmID), "active player must stay seated")
assert.Len(t, service.playersQueueOrBattleground[key], 1, "battleground link must be kept")
}
Loading