From 9d02fd39ed3f0e9f9fcd8c44c6e9e2ef11ed5097 Mon Sep 17 00:00:00 2001 From: diamondburned Date: Wed, 20 Mar 2024 18:43:23 -0700 Subject: [PATCH] Fix leaderboard bugs --- server/db/sql_queries.sql | 8 ++++++- server/db/sql_queries.sql.go | 27 +++++++++++++++--------- server/frontend/styles/_leaderboard.scss | 4 ---- server/r_leaderboard.go | 7 +++--- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/server/db/sql_queries.sql b/server/db/sql_queries.sql index b5efe01..b3cde97 100644 --- a/server/db/sql_queries.sql +++ b/server/db/sql_queries.sql @@ -53,7 +53,13 @@ SELECT ORDER BY COALESCE(SUM(team_points.points), 0) DESC; -- name: TeamPointsHistory :many -SELECT * FROM team_points ORDER BY added_at ASC; +SELECT + team_name, + added_at, + SUM(points) AS points + FROM team_points + GROUP BY team_name + ORDER BY added_at ASC; -- name: ListTeams :many SELECT team_name, created_at, accepting_members FROM teams; diff --git a/server/db/sql_queries.sql.go b/server/db/sql_queries.sql.go index 8a6a29b..a263728 100644 --- a/server/db/sql_queries.sql.go +++ b/server/db/sql_queries.sql.go @@ -637,24 +637,31 @@ func (q *Queries) TeamPoints(ctx context.Context) ([]TeamPointsRow, error) { } const teamPointsHistory = `-- name: TeamPointsHistory :many -SELECT team_name, added_at, points, reason FROM team_points ORDER BY added_at ASC +SELECT + team_name, + added_at, + SUM(points) AS points + FROM team_points + GROUP BY team_name + ORDER BY added_at ASC ` -func (q *Queries) TeamPointsHistory(ctx context.Context) ([]TeamPoint, error) { +type TeamPointsHistoryRow struct { + TeamName string + AddedAt time.Time + Points sql.NullFloat64 +} + +func (q *Queries) TeamPointsHistory(ctx context.Context) ([]TeamPointsHistoryRow, error) { rows, err := q.query(ctx, q.teamPointsHistoryStmt, teamPointsHistory) if err != nil { return nil, err } defer rows.Close() - var items []TeamPoint + var items []TeamPointsHistoryRow for rows.Next() { - var i TeamPoint - if err := rows.Scan( - &i.TeamName, - &i.AddedAt, - &i.Points, - &i.Reason, - ); err != nil { + var i TeamPointsHistoryRow + if err := rows.Scan(&i.TeamName, &i.AddedAt, &i.Points); err != nil { return nil, err } items = append(items, i) diff --git a/server/frontend/styles/_leaderboard.scss b/server/frontend/styles/_leaderboard.scss index 076e8a0..0eabd84 100644 --- a/server/frontend/styles/_leaderboard.scss +++ b/server/frontend/styles/_leaderboard.scss @@ -28,10 +28,6 @@ @include highlight-glow; } - tr > :nth-child(2) { - max-width: 4em; - } - .week-of-code-scores { position: relative; font-size: 0; diff --git a/server/r_leaderboard.go b/server/r_leaderboard.go index 5b71ea4..ff547fa 100644 --- a/server/r_leaderboard.go +++ b/server/r_leaderboard.go @@ -2,6 +2,7 @@ package server import ( "fmt" + "math" "net/http" "slices" "strings" @@ -36,7 +37,7 @@ func (t leaderboardTeamPointsTable) TeamPointsTooltip(teamIx int) string { pts := t.Points[teamIx] vals := make([]string, len(t.Reasons)) for i, p := range pts { - vals[i] = fmt.Sprintf("%s: %.0f", t.Reasons[i], p) + vals[i] = fmt.Sprintf("%s: %.0f", t.Reasons[i], math.Floor(p)) } return strings.Join(vals, ", ") } @@ -44,7 +45,6 @@ func (t leaderboardTeamPointsTable) TeamPointsTooltip(teamIx int) string { type leaderboardTeamPointsEvent struct { TeamName string `json:"team_name"` AddedAt time.Time `json:"added_at"` - Reason string `json:"reason"` Points float64 `json:"points"` } @@ -150,8 +150,7 @@ func (s *Server) leaderboard(w http.ResponseWriter, r *http.Request) { events = append(events, leaderboardTeamPointsEvent{ TeamName: row.TeamName, AddedAt: row.AddedAt, - Reason: row.Reason, - Points: row.Points, + Points: row.Points.Float64, }) }