go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/experiments/matchmaker/pkg/sim/balanced_mm_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package sim
     9  
    10  import (
    11  	"math/rand"
    12  	"testing"
    13  
    14  	"go.charczuk.com/sdk/assert"
    15  	"go.charczuk.com/sdk/uuid"
    16  )
    17  
    18  func Test_BalancedMatchmaker_basic(t *testing.T) {
    19  	rs := rand.NewSource(1234)
    20  	r := rand.New(rs)
    21  	bmm := new(BalancedMatchmaker)
    22  	bmm.Init(r)
    23  
    24  	for _, tier := range rankTiers {
    25  		for x := 0; x < 13; x++ {
    26  			rating := int(randomNormal(r, float64(tier.MeanRating), 50))
    27  			bmm.Queue(mockPlayerWithRating(rating))
    28  		}
    29  	}
    30  
    31  	originalTotalPlayers := bmm.QueueLen()
    32  
    33  	games := bmm.FormGames(MatchmakerConfig{
    34  		TeamSize: 6,
    35  		MaxGames: 5,
    36  	})
    37  
    38  	assert.ItsEqual(t, 5, len(games))
    39  	assert.ItsEqual(t, originalTotalPlayers, bmm.QueueLen()+len(gamePlayers(games)))
    40  }
    41  
    42  func mockPlayerWithRating(rating int) *Player {
    43  	return &Player{
    44  		ID:     uuid.V4(),
    45  		Rating: rating,
    46  	}
    47  }
    48  
    49  func gamePlayers(games []MatchmakerGame) (out []*Player) {
    50  	for _, g := range games {
    51  		for _, p := range g.TeamA {
    52  			out = append(out, p)
    53  		}
    54  		for _, p := range g.TeamB {
    55  			out = append(out, p)
    56  		}
    57  	}
    58  	return
    59  }