From 9a10b315a91c4529428b29a150ba7a724e4d75b8 Mon Sep 17 00:00:00 2001 From: surskitt Date: Fri, 2 Feb 2024 00:15:16 +0000 Subject: [PATCH] feat: new achievements function returns objects --- steam.go | 30 ++++++++++++------------------ steam_test.go | 12 ++++++------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/steam.go b/steam.go index ac4d8e0..6f93591 100644 --- a/steam.go +++ b/steam.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" "strings" @@ -193,8 +192,8 @@ 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, } @@ -202,17 +201,13 @@ func newestAchievement(am map[string]*playerAchievementsRes) string { 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 { @@ -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 } @@ -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 } diff --git a/steam_test.go b/steam_test.go index 4d2fb5e..56b33c1 100644 --- a/steam_test.go +++ b/steam_test.go @@ -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", @@ -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) }) } }