From afc7077c90b8373b944231a61ed1669c26e89d41 Mon Sep 17 00:00:00 2001 From: surskitt Date: Sat, 31 Aug 2024 22:41:00 +0100 Subject: [PATCH] feat: add subcommand for points --- main.go | 4 ++- retroachivements.go | 57 +++++++++++++++++++++++++++++++++++----- retroachivements_test.go | 32 ++++++++++++++++++++++ 3 files changed, 86 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 0355fa0..81cc619 100644 --- a/main.go +++ b/main.go @@ -101,9 +101,11 @@ func raHandler(client *req.Client, kv *bolt.DB, m *gowon.Message) (string, error return CommandHandler(client, kv, m.Nick, user, raLastGames) case "c", "current": return CommandHandler(client, kv, m.Nick, user, raCurrentStatus) + case "p", "points": + return CommandHandler(client, kv, m.Nick, user, raPoints) } - return "one of [s]et, [a]chievement, [l]ast or [c]urrent must be passed as a command", nil + return "one of [s]et, [a]chievement, [l]ast, [c]urrent or [p]oints must be passed as a command", nil } func main() { diff --git a/retroachivements.go b/retroachivements.go index 08264c4..8935870 100644 --- a/retroachivements.go +++ b/retroachivements.go @@ -15,11 +15,13 @@ const ( raRecentGamesURL = "https://retroachievements.org/API/API_GetUserRecentlyPlayedGames.php" raUserSummaryURL = "https://retroachievements.org/API/API_GetUserSummary.php" - achievementColour = "cyan" - gameColour = "magenta" - pointsColour = "green" - hardcoreColour = "yellow" - richPresenceColour = "yellow" + achievementColour = "cyan" + gameColour = "magenta" + pointsColour = "green" + relaxedPointsColour = "magenta" + hardcoreColour = "yellow" + richPresenceColour = "yellow" + rankColour = "yellow" ) var ( @@ -140,7 +142,12 @@ type UserSummary struct { Title string `json:"Title"` LastPlayed string `json:"LastPlayed"` } `json:"RecentlyPlayed"` - RichPresenceMsg string `json:"RichPresenceMsg"` + RichPresenceMsg string `json:"RichPresenceMsg"` + TotalPoints int `json:"TotalPoints"` + TotalTruePoints int `json:"TotalTruePoints"` + TotalSoftcorePoints int `json:"TotalSoftcorePoints"` + Rank int `json:"Rank"` + TotalRanked int `json:"TotalRanked"` } func (us *UserSummary) IsOnline() bool { @@ -197,3 +204,41 @@ func raCurrentStatus(client *req.Client, user string) (string, error) { return sb.String(), nil } + +func raPoints(client *req.Client, user string) (string, error) { + var j UserSummary + + _, err := client.R(). + SetQueryParam("u", user). + SetSuccessResult(&j). + Get(raUserSummaryURL) + + if err != nil { + return "", err + } + + if j.ID == 0 { + return fmt.Sprintf("User %s not found", user), nil + } + + var sb strings.Builder + + w := func(in, colour string) { + s := colourString(in, colour) + sb.WriteString(s) + } + + sb.WriteString(fmt.Sprintf("%s | ", user)) + + w(fmt.Sprintf("Points: %d (%d)", j.TotalPoints, j.TotalTruePoints), pointsColour) + + sb.WriteString(" | ") + + w(fmt.Sprintf("Relaxed: %d", j.TotalSoftcorePoints), relaxedPointsColour) + + sb.WriteString(" | ") + + w(fmt.Sprintf("Rank: %d/%d", j.Rank, j.TotalRanked), rankColour) + + return sb.String(), nil +} diff --git a/retroachivements_test.go b/retroachivements_test.go index 29347b7..fecd127 100644 --- a/retroachivements_test.go +++ b/retroachivements_test.go @@ -246,3 +246,35 @@ func TestRaCurrentStatus(t *testing.T) { }) } } + +func TestRaPoints(t *testing.T) { + cases := map[string]struct { + jsonfn string + expected string + err error + }{ + "points": { + jsonfn: "summary.json", + expected: "user | {green}Points: 509 (1084){clear} | {magenta}Relaxed: 2376{clear} | {yellow}Rank: 51006/70476{clear}", + err: nil, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + json := openTestFile(t, "API_GetUserSummary", "summary.json") + + client := req.C() + httpmock.ActivateNonDefault(client.GetClient()) + httpmock.RegisterResponder("GET", raUserSummaryURL, func(request *http.Request) (*http.Response, error) { + resp := httpmock.NewBytesResponse(http.StatusOK, json) + return resp, nil + }) + + out, err := raPoints(client, "user") + + assert.Equal(t, tc.expected, out) + assert.ErrorIs(t, tc.err, err) + }) + } +}