Changed to smaller float

This commit is contained in:
Jonas Hahn
2025-09-18 21:15:56 +02:00
parent 4168e92601
commit 15e71a1348
4 changed files with 31 additions and 30 deletions

View File

@@ -26,7 +26,6 @@ func getEnter(c *gin.Context) {
} }
if slug != "" { if slug != "" {
var table *repository.Table var table *repository.Table
repository.GetDB().Model(&repository.Table{}).Where("slug = ?", slug).First(&table) repository.GetDB().Model(&repository.Table{}).Where("slug = ?", slug).First(&table)
@@ -120,7 +119,7 @@ func postEnter(c *gin.Context) {
users := []*repository.User{p1, p2, p3, p4} users := []*repository.User{p1, p2, p3, p4}
sides := []string{p1t, p2t, p3t, p4t} sides := []string{p1t, p2t, p3t, p4t}
var playerbuf []float64 var playerbuf []float32
for i, u := range users { for i, u := range users {
if u != nil { if u != nil {

View File

@@ -10,11 +10,11 @@ import (
type User struct { type User struct {
gorm.Model gorm.Model
Email string Email string `gorm:"unique"`
Username string Username string
Slug string Slug string `gorm:"unique"`
Elo float64 Elo float32
GameCount int GameCount int
WinCount int WinCount int
LossCount int LossCount int
@@ -28,7 +28,7 @@ type Table struct {
gorm.Model gorm.Model
Name string Name string
Slug string Slug string `gorm:"unique"`
GameCount int GameCount int
} }
@@ -55,13 +55,13 @@ type GameUser struct {
User User User User
Side string `gorm:"size:1"` Side string `gorm:"size:1"`
DeltaElo float64 DeltaElo float32
} }
type LoginToken struct { type LoginToken struct {
gorm.Model gorm.Model
Token string Token string `gorm:"unique"`
Email string Email string
ExpiresAt time.Time ExpiresAt time.Time
} }

View File

@@ -19,13 +19,13 @@ import (
) )
var eloCfg = struct { var eloCfg = struct {
Mu0 float64 Mu0 float32
Scale float64 // KNew applies to players during burn-in (first BurnInGames matches). Scale float32 // KNew applies to players during burn-in (first BurnInGames matches).
KNew float64 KNew float32
KStd float64 KStd float32
BurnInGames int BurnInGames int
RatingFloor float64 RatingFloor float32
MaxPerMatchDelta float64 MaxPerMatchDelta float32
MaxGoals int MaxGoals int
}{ }{
Mu0: 1500, Mu0: 1500,
@@ -41,16 +41,16 @@ var eloCfg = struct {
type Team struct { type Team struct {
Players []*repository.GameUser Players []*repository.GameUser
Score int Score int
AverageRating float64 AverageRating float32
} }
func NewTeam(players []*repository.GameUser, score int) *Team { func NewTeam(players []*repository.GameUser, score int) *Team {
var t1s float64 = 0 var t1s float32 = 0
for _, player := range players { for _, player := range players {
t1s += float64(player.User.Elo) t1s += float32(player.User.Elo)
} }
t1a := t1s / float64(len(players)) t1a := t1s / float32(len(players))
return &Team{Players: players, Score: score, AverageRating: t1a} return &Team{Players: players, Score: score, AverageRating: t1a}
} }
@@ -80,9 +80,9 @@ func GetNewElo(t1, t2 *Team) error {
return nil return nil
} }
func CalculateRating(rA, rB float64, goalsA, goalsB, gamesA int) (newrA float64, err error) { func CalculateRating(rA, rB float32, goalsA, goalsB, gamesA int) (newrA float32, err error) {
// Observed score (no draws) // Observed score (no draws)
var sA float64 var sA float32
if goalsA > goalsB { if goalsA > goalsB {
sA = 1.0 sA = 1.0
} else { } else {
@@ -105,7 +105,7 @@ func CalculateRating(rA, rB float64, goalsA, goalsB, gamesA int) (newrA float64,
eA := expected(eloCfg.Scale, rA, rB) eA := expected(eloCfg.Scale, rA, rB)
mov := movFactor(rA, rB, diff) mov := movFactor(rA, rB, diff)
var Keff float64 var Keff float32
if gamesA <= eloCfg.BurnInGames { if gamesA <= eloCfg.BurnInGames {
Keff = eloCfg.KNew Keff = eloCfg.KNew
} else { } else {
@@ -124,13 +124,15 @@ func CalculateRating(rA, rB float64, goalsA, goalsB, gamesA int) (newrA float64,
return rA + delta, nil return rA + delta, nil
} }
// expected returns the win probability for player A against B func expected(scale, rA, rB float32) float32 {
func expected(scale, rA, rB float64) float64 { return float32(
return 1.0 / (1.0 + math.Pow(10.0, -(rA-rB)/scale)) 1.0 / (1.0 + math.Pow(10.0, float64(-(rA-rB)/scale))),
)
} }
// movFactor returns a bounded margin-of-victory multiplier. func movFactor(rA, rB float32, diff int) float32 {
func movFactor(rA, rB float64, diff int) float64 {
fd := float64(diff) fd := float64(diff)
return math.Log(fd+1.0) * 2.2 / (math.Abs(rA-rB)*0.001 + 2.2) return float32(
math.Log(fd+1.0) * 2.2 / (math.Abs(float64(rA-rB))*0.001 + 2.2),
)
} }

View File

@@ -28,9 +28,9 @@ func NameFromEmail(email string) string {
return "" return ""
} }
func RoundFloat(val float64, precision uint) float64 { func RoundFloat(val float32, precision uint) float32 {
ratio := math.Pow(10, float64(precision)) ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio return float32(math.Round(float64(val)*ratio) / ratio)
} }
func MustRandToken(n int) string { func MustRandToken(n int) string {