mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-15 06:02:13 +00:00
feat: add canonical matchmaking rating engine
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUpdateRatingMatchesCanonicalGlicko2Example(t *testing.T) {
|
||||
current := Rating{Value: 1500, RD: 200, Volatility: 0.06}
|
||||
opponents := []Opponent{
|
||||
{PlayerID: "a", Rating: Rating{Value: 1400, RD: 30, Volatility: 0.06}, Score: 1, Weight: 1},
|
||||
{PlayerID: "b", Rating: Rating{Value: 1550, RD: 100, Volatility: 0.06}, Score: 0, Weight: 1},
|
||||
{PlayerID: "c", Rating: Rating{Value: 1700, RD: 300, Volatility: 0.06}, Score: 0, Weight: 1},
|
||||
}
|
||||
updated, err := UpdateRating(current, opponents, time.Unix(100000, 0))
|
||||
if err != nil { t.Fatal(err) }
|
||||
if math.Abs(updated.Value-1464.06) > 0.1 || math.Abs(updated.RD-151.52) > 0.1 || math.Abs(updated.Volatility-0.05999) > 0.0001 {
|
||||
t.Fatalf("canonical vector mismatch: %+v", updated)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRatingInactivityRaisesRDWithoutChangingRating(t *testing.T) {
|
||||
now := time.Unix(100000, 0)
|
||||
current := Rating{Value: 1600, RD: 100, Volatility: 0.06, LastRatedAt: now}
|
||||
updated, err := UpdateRating(current, nil, now.Add(48*time.Hour+time.Hour))
|
||||
if err != nil { t.Fatal(err) }
|
||||
if updated.Value != current.Value || updated.RD <= current.RD || updated.RD > GlickoInitialRD { t.Fatalf("bad inactivity update: %+v", updated) }
|
||||
}
|
||||
|
||||
func TestOpponentWeightHelpersAreExactAndDeterministic(t *testing.T) {
|
||||
opponents := []Opponent{{PlayerID: "c", Rating: Rating{Value: 1700}}, {PlayerID: "a", Rating: Rating{Value: 1400}}, {PlayerID: "b", Rating: Rating{Value: 1550}}}
|
||||
ranked, err := RankedOpponents(opponents)
|
||||
if err != nil { t.Fatal(err) }
|
||||
if ranked[0].PlayerID != "a" || ranked[0].Weight != 1.0/3.0 { t.Fatalf("ranked weighting/order wrong: %+v", ranked) }
|
||||
reordered, err := RankedOpponents([]Opponent{opponents[1], opponents[0], opponents[2]})
|
||||
if err != nil { t.Fatal(err) }
|
||||
for i := range ranked { if ranked[i].PlayerID != reordered[i].PlayerID { t.Fatal("input order changed opponent order") } }
|
||||
casual, err := CasualOpponents(opponents[:2])
|
||||
if err != nil { t.Fatal(err) }
|
||||
if casual[0].Weight != 0.5 || casual[1].Weight != 0.5 { t.Fatalf("casual weighting wrong: %+v", casual) }
|
||||
}
|
||||
|
||||
func TestRatingRejectsInvalidStateAndBadScore(t *testing.T) {
|
||||
_, err := UpdateRating(Rating{Value: 1500, RD: 0, Volatility: 0.06}, nil, time.Now())
|
||||
if err == nil { t.Fatal("accepted zero RD") }
|
||||
_, err = UpdateRating(Rating{Value: 1500, RD: 200, Volatility: 0.06}, []Opponent{{Rating: Rating{Value: 1500, RD: 100, Volatility: 0.06}, Weight: 1, Score: 2}}, time.Now())
|
||||
if err == nil { t.Fatal("accepted score outside [0,1]") }
|
||||
}
|
||||
Reference in New Issue
Block a user