feat: form matches from queue projections

This commit is contained in:
Josh Creek
2026-08-31 21:36:00 +01:00
parent 02a11704ce
commit 4dcf98cbb0
4 changed files with 82 additions and 2 deletions
+43 -1
View File
@@ -2,6 +2,7 @@ package domain
import (
"fmt"
"math"
"sort"
"time"
)
@@ -33,6 +34,33 @@ type Selection struct {
TotalWaitSeconds float64
}
type MatchFormation struct {
Selection Selection
Teams Teams
}
// FormFromQueue is the queue-backed matcher boundary. Queue.Candidates owns
// expiry and ordering; this method chooses the oldest projected candidate as
// the anchor, then forms and partitions one deterministic match.
func FormFromQueue(queue *Queue, size int, now time.Time) (MatchFormation, error) {
if queue == nil {
return MatchFormation{}, fmt.Errorf("queue is required")
}
candidates := queue.Candidates(now)
if len(candidates) == 0 {
return MatchFormation{}, fmt.Errorf("queue is empty")
}
selection, err := SelectCandidates(candidates[0], candidates[1:], size, now)
if err != nil {
return MatchFormation{}, err
}
teams, err := PartitionTeams(selection.Players)
if err != nil {
return MatchFormation{}, err
}
return MatchFormation{Selection: selection, Teams: teams}, nil
}
func RatingTolerance(waitSeconds float64) float64 {
if waitSeconds < 0 {
waitSeconds = 0
@@ -50,9 +78,11 @@ func SelectCandidates(anchor Candidate, candidates []Candidate, size int, now ti
}
pool := make([]Candidate, 0, len(candidates)+1)
seen := map[string]bool{}
seenPlayers := map[string]bool{}
add := func(candidate Candidate) {
if candidate.TicketID != "" && !seen[candidate.TicketID] {
if validCandidate(candidate) && !seen[candidate.TicketID] && !seenPlayers[candidate.PlayerID] {
seen[candidate.TicketID] = true
seenPlayers[candidate.PlayerID] = true
pool = append(pool, candidate)
}
}
@@ -97,6 +127,18 @@ func SelectCandidates(anchor Candidate, candidates []Candidate, size int, now ti
return best, nil
}
func validCandidate(candidate Candidate) bool {
if candidate.TicketID == "" || candidate.PlayerID == "" || candidate.EnqueuedAt.IsZero() || math.IsNaN(candidate.Rating) || math.IsInf(candidate.Rating, 0) {
return false
}
for region, rtt := range candidate.PredictedRTT {
if region != "EU" && region != "NA" || math.IsNaN(rtt) || math.IsInf(rtt, 0) || rtt < 0 {
return false
}
}
return len(candidate.PredictedRTT) > 0
}
func compatibleSet(players []Candidate, now time.Time) bool {
regions := commonRegions(players)
if len(regions) == 0 {
+35
View File
@@ -66,3 +66,38 @@ func TestSelectCandidatesRejectsNoCommonRegion(t *testing.T) {
t.Fatal("selected players without a common <=100ms region")
}
}
func TestFormFromQueueUsesServerProjectionAndBalancesTeams(t *testing.T) {
now := time.Unix(100000, 0)
queue := NewQueue()
for _, id := range []string{"c", "a", "b", "d"} {
candidate := candidate(id, 1500, time.Second, 40, 45, now)
if _, err := queue.Create(candidate.PlayerID, candidate.TicketID, "create-key-"+id+"-123456", candidate, now.Add(-time.Duration(len(id))*time.Millisecond)); err != nil {
t.Fatal(err)
}
}
formation, err := FormFromQueue(queue, 4, now)
if err != nil {
t.Fatal(err)
}
if formation.Selection.Players[0].TicketID != "a" || formation.Selection.Region != "EU" || len(formation.Teams.Team0) != 2 || len(formation.Teams.Team1) != 2 {
t.Fatalf("formation = %+v", formation)
}
seen := map[string]bool{}
for _, player := range append(formation.Teams.Team0, formation.Teams.Team1...) {
if seen[player.PlayerID] {
t.Fatalf("duplicate player in teams: %s", player.PlayerID)
}
seen[player.PlayerID] = true
}
}
func TestSelectCandidatesRejectsMalformedCandidateInsteadOfTrustingIt(t *testing.T) {
now := time.Unix(100000, 0)
anchor := candidate("a", 1500, 0, 40, 40, now)
malformed := candidate("b", 1500, 0, 40, 40, now)
malformed.PlayerID = anchor.PlayerID
if _, err := SelectCandidates(anchor, []Candidate{malformed}, 2, now); err == nil {
t.Fatal("duplicate player candidate accepted")
}
}