Skip to content

Commit

Permalink
add GetUserRecentlyPlayedGames endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 1, 2024
1 parent d50fcfa commit 2ffe416
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ For convenience, the API docs and examples can be found in the tables below
|`GetUserGameRankAndScore(string,int)`|Get metadata about how a user has performed on a given game.|[docs](https://api-docs.retroachievements.org/v1/get-user-game-rank-and-score.html) \| [example](examples/user/getusergamerankandscore/getusergamerankandscore.go)|
|`GetUserPoints(string)`|Get a user's total hardcore and softcore points.|[docs](https://api-docs.retroachievements.org/v1/get-user-points.html) \| [example](examples/user/getuserpoints/getuserpoints.go)|
|`GetUserProgress(string,[]int)`|Get a user's progress on a list of specified games.|[docs](https://api-docs.retroachievements.org/v1/get-user-progress.html) \| [example](examples/user/getuserprogress/getuserprogress.go)|
|`GetUserRecentlyPlayedGames(string,int,int)`|Get a list of games a user has recently played.|[docs](https://api-docs.retroachievements.org/v1/get-user-recently-played-games.html) \| [example](examples/user/getuserrecentlyplayedgames/getuserrecentlyplayedgames.go)|

<h3>Game</h3>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Package getuserrecentachievements provides an example for a users achievements in the last X minutes
package main

import (
"fmt"
"os"

"github.com/joshraphael/go-retroachievements"
)

/*
Test script, add RA_API_KEY to your env and use `go run getuserrecentachievements.go`
*/
func main() {
secret := os.Getenv("RA_API_KEY")

client := retroachievements.NewClient(secret)

resp, err := client.GetUserRecentlyPlayedGames("jamiras", 10, 0)
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", resp)
}
14 changes: 14 additions & 0 deletions http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func GameID(gameId int) RequestDetail {
})
}

// Count adds a count to the query parameters
func Count(count int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["c"] = strconv.Itoa(count)
})
}

// Offset adds a offset to the query parameters
func Offset(offset int) RequestDetail {
return requestDetailFn(func(r *Request) {
r.Params["o"] = strconv.Itoa(offset)
})
}

// AwardMetadata adds a target game id to the query parameters
func AwardMetadata(awardMetadata bool) RequestDetail {
return requestDetailFn(func(r *Request) {
Expand Down
4 changes: 4 additions & 0 deletions http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestNewRequest(t *testing.T) {
raHttp.IDs([]int{2837, 4535}),
raHttp.GameID(345),
raHttp.AwardMetadata(true),
raHttp.Count(20),
raHttp.Offset(34),
)

expected := &raHttp.Request{
Expand All @@ -46,6 +48,8 @@ func TestNewRequest(t *testing.T) {
"i": "2837,4535",
"g": "345",
"a": "1",
"c": "20",
"o": "34",
},
}

Expand Down
14 changes: 14 additions & 0 deletions models/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ type Progress struct {
NumAchievedHardcore int `json:"NumAchievedHardcore"`
ScoreAchievedHardcore int `json:"ScoreAchievedHardcore"`
}

type UserRecentlyPlayed struct {
Progress
GameID int `json:"GameID"`
ConsoleID int `json:"ConsoleID"`
ConsoleName string `json:"ConsoleName"`
Title string `json:"Title"`
ImageIcon string `json:"ImageIcon"`
ImageTitle string `json:"ImageTitle"`
ImageIngame string `json:"ImageIngame"`
ImageBoxArt string `json:"ImageBoxArt"`
LastPlayed DateTime `json:"LastPlayed"`
AchievementsTotal int `json:"AchievementsTotal"`
}
31 changes: 31 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,34 @@ func (c *Client) GetUserProgress(username string, gameIDs []int) (map[string]mod
}
return *progress, nil
}

// GetUserRecentlyPlayedGames get a list of games a user has recently played.
func (c *Client) GetUserRecentlyPlayedGames(username string, count int, offset int) ([]models.UserRecentlyPlayed, error) {
numCount := count
if count <= 0 {
numCount = 10
}
if count > 50 {
numCount = 50
}
numOffset := offset
if offset < 0 {
numOffset = 0
}
resp, err := c.do(
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserRecentlyPlayedGames.php"),
raHttp.APIToken(c.Secret),
raHttp.Username(username),
raHttp.Count(numCount),
raHttp.Offset(numOffset),
)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
recentlyPlayed, err := raHttp.ResponseList[models.UserRecentlyPlayed](resp)
if err != nil {
return nil, fmt.Errorf("parsing response list: %w", err)
}
return recentlyPlayed, nil
}

0 comments on commit 2ffe416

Please sign in to comment.