Skip to content

Commit

Permalink
feat: add subcommand for points
Browse files Browse the repository at this point in the history
  • Loading branch information
surskitt committed Aug 31, 2024
1 parent 7eb75d0 commit afc7077
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
57 changes: 51 additions & 6 deletions retroachivements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
32 changes: 32 additions & 0 deletions retroachivements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit afc7077

Please sign in to comment.