package account import ( "context" "errors" "fmt" "github.com/go-jet/jet/v2/postgres" "github.com/go-jet/jet/v2/qrm" "github.com/google/uuid" "scrabble/backend/internal/postgres/jet/backend/model" "scrabble/backend/internal/postgres/jet/backend/table" ) // Stats is a durable account's lifetime record, written by the game domain on each // finish and read for the player's statistics screen. MaxGamePoints is the best // single game's total; MaxWordPoints is the best single move's score (which already // includes every word it formed plus the all-tiles bonus). type Stats struct { Wins int Losses int Draws int MaxGamePoints int MaxWordPoints int } // GetStats returns the lifetime statistics for id. An account with no account_stats // row yet — a guest, or a player who has not finished a game — yields the zero // Stats (all counters zero) rather than an error. func (s *Store) GetStats(ctx context.Context, id uuid.UUID) (Stats, error) { stmt := postgres.SELECT(table.AccountStats.AllColumns). FROM(table.AccountStats). WHERE(table.AccountStats.AccountID.EQ(postgres.UUID(id))). LIMIT(1) var row model.AccountStats if err := stmt.QueryContext(ctx, s.db, &row); err != nil { if errors.Is(err, qrm.ErrNoRows) { return Stats{}, nil } return Stats{}, fmt.Errorf("account: get stats %s: %w", id, err) } return Stats{ Wins: int(row.Wins), Losses: int(row.Losses), Draws: int(row.Draws), MaxGamePoints: int(row.MaxGamePoints), MaxWordPoints: int(row.MaxWordPoints), }, nil }