package store import ( "context" "database/sql" "fmt" "github.com/cosmic-clash/cosmic-clash/server/domain" ) const RankedProfileSelectSQL = `SELECT rating, deviation, volatility, ranked_games, updated_at FROM ratings WHERE player_id = $1` // PostgresRankedProfiles reads the durable rating row api.Service's // RankedProfileProvider needs. A missing row means "this player has no // ranked profile yet" (never queued ranked, or their identity predates any // result) -- that's a real, expected state, not an error, and is reported // the same way the in-memory RankedProfiles map api.Service still falls // back to already did: (zero value, false, nil). // // LastSeasonID and SeasonHistory are deliberately left at their zero values. // The ratings table has no "current season" column, and reconstructing // season history means a second query against ranked_season_rollovers with // its own display semantics to settle -- a real, separate piece of work, // not bundled into this read path speculatively. type PostgresRankedProfiles struct{ DB *sql.DB } func (p PostgresRankedProfiles) Get(ctx context.Context, playerID string) (domain.RankedProfile, bool, error) { if p.DB == nil || playerID == "" { return domain.RankedProfile{}, false, fmt.Errorf("invalid ranked profile lookup") } var profile domain.RankedProfile err := p.DB.QueryRowContext(ctx, RankedProfileSelectSQL, playerID). Scan(&profile.Value, &profile.RD, &profile.Volatility, &profile.RankedGames, &profile.LastRatedAt) if err == sql.ErrNoRows { return domain.RankedProfile{}, false, nil } if err != nil { return domain.RankedProfile{}, false, err } return profile, true, nil }