Skip to content

Commit

Permalink
refactor GetUserRecentlyPlayedGames
Browse files Browse the repository at this point in the history
  • Loading branch information
joshraphael committed Nov 16, 2024
1 parent 5c04eea commit b2cd193
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ For convenience, the API docs and examples can be found in the tables below
|`GetUserGameRankAndScore()`|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()`|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()`|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)|
|`GetUserRecentlyPlayedGames()`|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
Expand Up @@ -6,6 +6,7 @@ import (
"os"

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

/*
Expand All @@ -16,7 +17,13 @@ func main() {

client := retroachievements.NewClient(secret)

resp, err := client.GetUserRecentlyPlayedGames("jamiras", 10, 0)
count := 10
offset := 0
resp, err := client.GetUserRecentlyPlayedGames(models.GetUserRecentlyPlayedGamesParameters{
Username: "jamiras",
Count: &count,
Offset: &offset,
})
if err != nil {
panic(err)
}
Expand Down
15 changes: 0 additions & 15 deletions models/progress.go

This file was deleted.

32 changes: 32 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ type GetUserPoints struct {
SoftcorePoints int `json:"SoftcorePoints"`
}

// GetUserProgressParameters contains the parameters needed for getting a users progress
type GetUserProgressParameters struct {
// The target username
Username string
Expand All @@ -433,3 +434,34 @@ type GetUserProgress struct {
NumAchievedHardcore int `json:"NumAchievedHardcore"`
ScoreAchievedHardcore int `json:"ScoreAchievedHardcore"`
}

// GetUserRecentlyPlayedGamesParameters contains the parameters needed for getting a users recently played games
type GetUserRecentlyPlayedGamesParameters struct {
// The target username
Username string

// [Optional] The number of games to return
Count *int

// [Optional] The offset from the beginning to start returning records
Offset *int
}

type GetUserRecentlyPlayedGames struct {
NumPossibleAchievements int `json:"NumPossibleAchievements"`
PossibleScore int `json:"PossibleScore"`
NumAchieved int `json:"NumAchieved"`
ScoreAchieved int `json:"ScoreAchieved"`
NumAchievedHardcore int `json:"NumAchievedHardcore"`
ScoreAchievedHardcore int `json:"ScoreAchievedHardcore"`
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"`
}
30 changes: 12 additions & 18 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,30 +221,24 @@ func (c *Client) GetUserProgress(params models.GetUserProgressParameters) (map[s
}

// 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(
func (c *Client) GetUserRecentlyPlayedGames(params models.GetUserRecentlyPlayedGamesParameters) ([]models.GetUserRecentlyPlayedGames, error) {
details := []raHttp.RequestDetail{
raHttp.Method(http.MethodGet),
raHttp.Path("/API/API_GetUserRecentlyPlayedGames.php"),
raHttp.APIToken(c.Secret),
raHttp.Username(username),
raHttp.Count(numCount),
raHttp.Offset(numOffset),
)
raHttp.Username(params.Username),
}
if params.Count != nil {
details = append(details, raHttp.Count(*params.Count))
}
if params.Offset != nil {
details = append(details, raHttp.Offset(*params.Offset))
}
resp, err := c.do(details...)
if err != nil {
return nil, fmt.Errorf("calling endpoint: %w", err)
}
recentlyPlayed, err := raHttp.ResponseList[models.UserRecentlyPlayed](resp)
recentlyPlayed, err := raHttp.ResponseList[models.GetUserRecentlyPlayedGames](resp)
if err != nil {
return nil, fmt.Errorf("parsing response list: %w", err)
}
Expand Down

0 comments on commit b2cd193

Please sign in to comment.