Skip to content

Commit

Permalink
feat: new achievements function returns objects
Browse files Browse the repository at this point in the history
  • Loading branch information
surskitt committed Feb 2, 2024
1 parent 4d45e20 commit 9a10b31
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
30 changes: 12 additions & 18 deletions steam.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"

Expand Down Expand Up @@ -193,26 +192,22 @@ func getAchievements(apiKey, id string, appId int, client *http.Client) (*player
return j, nil
}

func newestAchievement(am map[string]*playerAchievementsRes) string {
game := ""
func newestAchievement(am map[string]*playerAchievementsRes) (*playerAchievementsRes, playerAchievement) {
game := &playerAchievementsRes{}
newest := playerAchievement{
UnlockTime: 0,
}

for _, as := range am {
for _, a := range as.PlayerStats.Achievements {
if a.UnlockTime > newest.UnlockTime {
game = as.PlayerStats.GameName
game = as
newest = a
}
}
}

if newest.UnlockTime == 0 {
return ""
}

return fmt.Sprintf("%s - %s (%s)", game, newest.Name, newest.Description)
return game, newest
}

func getAchievementCount(as *playerAchievementsRes) string {
Expand Down Expand Up @@ -254,11 +249,14 @@ func steamLastAchievement(apiKey, user string, client *http.Client) (string, err
return "", err
}

log.Println(recentlyPlayed.Ids())

achievementsMap := make(map[string]*playerAchievementsRes)
for _, i := range recentlyPlayed.Ids() {
as, err := getAchievements(apiKey, id, i, client)

if errors.Is(profileNotPublicErr)(err) {
return "Error: profile is not public", nil
}

if err != nil {
return "", err
}
Expand All @@ -267,19 +265,15 @@ func steamLastAchievement(apiKey, user string, client *http.Client) (string, err
achievementsMap[game] = as
}

if errors.Is(profileNotPublicErr)(err) {
return "Error: profile is not public", nil
}

if err != nil {
return "", err
}

n := newestAchievement(achievementsMap)
game, newest := newestAchievement(achievementsMap)

if n == "" {
if newest.UnlockTime == 0 {
return fmt.Sprintf("%s has no recently unlocked steam achievements", user), nil
}

return fmt.Sprintf("%s's last steam achievement: %s", user, n), nil
return fmt.Sprintf("%s's last steam achievement: %s - %s (%s)", user, game.PlayerStats.GameName, newest.Name, newest.Description), nil
}
12 changes: 6 additions & 6 deletions steam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,17 @@ func TestNewestAchievement(t *testing.T) {
{
name: "One game",
m: makeResMap(1),
out: "SUPERHOT: MIND CONTROL DELETE - MORE ()",
out: "MORE",
},
{
name: "Two ids passed, newest first",
m: makeResMap(1, 2),
out: "SUPERHOT: MIND CONTROL DELETE - MORE ()",
out: "MORE",
},
{
name: "Two ids passed, newest second",
m: makeResMap(2, 1),
out: "SUPERHOT: MIND CONTROL DELETE - MORE ()",
out: "MORE",
},
{
name: "One id, no achievements",
Expand All @@ -408,15 +408,15 @@ func TestNewestAchievement(t *testing.T) {
{
name: "Two ids, first no achivements",
m: makeResMap(3, 1),
out: "SUPERHOT: MIND CONTROL DELETE - MORE ()",
out: "MORE",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
out := newestAchievement(tc.m)
_, out := newestAchievement(tc.m)

assert.Equal(t, tc.out, out)
assert.Equal(t, tc.out, out.Name)
})
}
}
Expand Down

0 comments on commit 9a10b31

Please sign in to comment.