Full refactor of codebase
This commit is contained in:
61
src/handlers/history.go
Normal file
61
src/handlers/history.go
Normal 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})
|
||||
}
|
||||
Reference in New Issue
Block a user