Full refactor of codebase

This commit is contained in:
Jonas Hahn
2025-09-18 16:51:57 +02:00
parent 0e6e48cd7b
commit 4168e92601
34 changed files with 1176 additions and 1062 deletions

61
src/handlers/history.go Normal file
View File

@@ -0,0 +1,61 @@
package handlers
import (
"fmt"
"github.com/ascyii/qrank/src/repository"
"github.com/ascyii/qrank/src/templates"
"github.com/ascyii/qrank/src/utils"
"github.com/gin-gonic/gin"
)
func getHistory(c *gin.Context) {
// Load recent games
var games []repository.Game
repository.GetDB().Preload("Table").Order("created_at desc").Find(&games)
type userElo struct {
repository.User
DeltaElo string
Color string
}
type gRow struct {
repository.Game
PlayersA []userElo
PlayersB []userElo
WinnerIsA bool
}
var rows []gRow
for _, g := range games {
var gps []repository.GameUser
repository.GetDB().Model(&repository.GameUser{}).Preload("User").Where("game_id = ?", g.ID).Find(&gps)
var a, b []userElo
for _, gp := range gps {
var eloColor, eloFloatString string
eloFloat := utils.RoundFloat(gp.DeltaElo, 2)
if eloFloat > 0 {
eloColor = "green"
eloFloatString = fmt.Sprintf("+%.2f", eloFloat)
} else if eloFloat < 0 {
eloColor = "red"
eloFloatString = fmt.Sprintf("%.2f", eloFloat)
} else {
eloColor = "black"
eloFloatString = fmt.Sprintf("%.2f", eloFloat)
}
if gp.Side == "A" {
a = append(a, userElo{gp.User, eloFloatString, eloColor})
} else {
b = append(b, userElo{gp.User, eloFloatString, eloColor})
}
}
rows = append(rows, gRow{Game: g, PlayersA: a, PlayersB: b, WinnerIsA: g.ScoreA > g.ScoreB})
}
templates.Render(c, "history", gin.H{"Games": rows})
}