Skip to content

Commit

Permalink
feat: add achievement count function
Browse files Browse the repository at this point in the history
  • Loading branch information
surskitt committed Feb 1, 2024
1 parent 103288c commit 4d45e20
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 0 deletions.
23 changes: 23 additions & 0 deletions steam.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,29 @@ func newestAchievement(am map[string]*playerAchievementsRes) string {
return fmt.Sprintf("%s - %s (%s)", game, newest.Name, newest.Description)
}

func getAchievementCount(as *playerAchievementsRes) string {
total := len(as.PlayerStats.Achievements)

achieved := 0
for _, a := range as.PlayerStats.Achievements {
if a.UnlockTime > 0 {
achieved += 1
}
}

c := func(a, t int) string {
if a == t {
return "green"
}

return "yellow"
}

colour := c(total, achieved)

return fmt.Sprintf("{%s}%d/%d{clear}", colour, achieved, total)
}

func steamLastAchievement(apiKey, user string, client *http.Client) (string, error) {
id, err := steamGetId(apiKey, user, client)

Expand Down
53 changes: 53 additions & 0 deletions steam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,59 @@ func TestNewestAchievement(t *testing.T) {
}
}

func TestGetAchievementCount(t *testing.T) {
makeRes := func(id int) *playerAchievementsRes {
j := openTestFile(t, "TestGetAchievementCount", fmt.Sprintf("%d.json", id))

r := playerAchievementsRes{}
err := json.Unmarshal(j, &r)
assert.Nil(t, err)

return &r
}

cases := []struct {
name string
id int
out string
}{
{
name: "Multiple achieved",
id: 1,
out: "{yellow}3/14{clear}",
},
{
name: "One achieved",
id: 2,
out: "{yellow}1/3{clear}",
},
{
name: "No achievements",
id: 3,
out: "{green}0/0{clear}",
},
{
name: "None achieved",
id: 4,
out: "{yellow}0/3{clear}",
},
{
name: "All achieved",
id: 5,
out: "{green}3/3{clear}",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
r := makeRes(tc.id)
out := getAchievementCount(r)

assert.Equal(t, tc.out, out)
})
}
}

func TestSteamLastAchievement(t *testing.T) {
cases := []struct {
name string
Expand Down
1 change: 1 addition & 0 deletions testdata/TestGetAchievementCount/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"playerstats":{"steamID":"76561198009303675","gameName":"SUPERHOT: MIND CONTROL DELETE","achievements":[{"apiname":"achievement_1_completed","achieved":1,"unlocktime":1638316294,"name":"MORE","description":""},{"apiname":"achievement_2_completed","achieved":0,"unlocktime":0,"name":"MORE and MORE","description":""},{"apiname":"achievement_3_completed","achieved":0,"unlocktime":0,"name":"even MORE","description":""},{"apiname":"achievement_4_completed","achieved":0,"unlocktime":0,"name":"so much MORE","description":""},{"apiname":"achievement_5_completed","achieved":1,"unlocktime":100,"name":"there's still MORE","description":""},{"apiname":"achievement_6_completed","achieved":0,"unlocktime":0,"name":"MORE than ever","description":""},{"apiname":"achievement_7_completed","achieved":0,"unlocktime":0,"name":"MORE power","description":""},{"apiname":"achievement_8_completed","achieved":0,"unlocktime":0,"name":"MORE mysteries","description":""},{"apiname":"achievement_9_completed","achieved":1,"unlocktime":200,"name":"MORE story","description":""},{"apiname":"achievement_10_completed","achieved":0,"unlocktime":0,"name":"MORE slashing","description":""},{"apiname":"achievement_11_completed","achieved":0,"unlocktime":0,"name":"MORE shooting","description":""},{"apiname":"achievement_12_completed","achieved":0,"unlocktime":0,"name":"MORE punching","description":""},{"apiname":"achievement_13_completed","achieved":0,"unlocktime":0,"name":"less is MORE","description":""},{"apiname":"achievement_14_completed","achieved":0,"unlocktime":0,"name":"back for MORE","description":""}],"success":true}}
1 change: 1 addition & 0 deletions testdata/TestGetAchievementCount/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"playerstats":{"steamID":"76561198009303675","gameName":"SHENZHEN SOLITAIRE","achievements":[{"apiname":"SOLITAIRE_1","achieved":1,"unlocktime":1620259716,"name":"CLIMB THE MOUNTAIN","description":"Win a game of solitaire."},{"apiname":"SOLITAIRE_2","achieved":0,"unlocktime":0,"name":"MEET THE DRAGON","description":"Win 10 games of solitaire."},{"apiname":"SOLITAIRE_3","achieved":0,"unlocktime":0,"name":"BECOME IMMORTAL","description":"Win 100 games of solitaire."}],"success":true}}
1 change: 1 addition & 0 deletions testdata/TestGetAchievementCount/3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"playerstats":{"steamID":"76561198009303675","gameName":"Dota 2","success":true}}
1 change: 1 addition & 0 deletions testdata/TestGetAchievementCount/4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"playerstats":{"steamID":"76561198009303675","gameName":"SHENZHEN SOLITAIRE","achievements":[{"apiname":"SOLITAIRE_1","achieved":0,"unlocktime":0,"name":"CLIMB THE MOUNTAIN","description":"Win a game of solitaire."},{"apiname":"SOLITAIRE_2","achieved":0,"unlocktime":0,"name":"MEET THE DRAGON","description":"Win 10 games of solitaire."},{"apiname":"SOLITAIRE_3","achieved":0,"unlocktime":0,"name":"BECOME IMMORTAL","description":"Win 100 games of solitaire."}],"success":true}}
1 change: 1 addition & 0 deletions testdata/TestGetAchievementCount/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"playerstats":{"steamID":"76561198009303675","gameName":"SHENZHEN SOLITAIRE","achievements":[{"apiname":"SOLITAIRE_1","achieved":0,"unlocktime":100,"name":"CLIMB THE MOUNTAIN","description":"Win a game of solitaire."},{"apiname":"SOLITAIRE_2","achieved":0,"unlocktime":200,"name":"MEET THE DRAGON","description":"Win 10 games of solitaire."},{"apiname":"SOLITAIRE_3","achieved":0,"unlocktime":300,"name":"BECOME IMMORTAL","description":"Win 100 games of solitaire."}],"success":true}}

0 comments on commit 4d45e20

Please sign in to comment.