-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsteam.go
94 lines (86 loc) · 3.19 KB
/
steam.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
"time"
)
const achievementAPIURL = "https://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=250900&key=%s&steamid=%d"
const userIDAPIURL = "http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=%s&vanityurl=%s"
// SteamAchievementResponse represents the JSON response provided by the Steam Web API
// when one prompts for the achievements belonging to a given user.
type SteamAchievementResponse struct {
Playerstats struct {
SteamID string `json:"steamID"`
GameName string `json:"gameName"`
Achievements []struct {
Name string `json:"name"`
Achieved int `json:"achieved"`
} `json:"achievements"`
Stats []struct {
Name string `json:"name"`
Value int `json:"value"`
} `json:"stats"`
} `json:"playerstats"`
}
// readSteamStats calls the Steam Web API to obtain information about the achievements belonging
// to a given user.
func readSteamStats(steamID int) (steamAchievementResponse SteamAchievementResponse, err error) {
client := &http.Client{Timeout: 10 * time.Second}
response, err := client.Get(fmt.Sprintf(achievementAPIURL, config.SteamAPIKey, steamID))
if err != nil {
return
}
defer response.Body.Close()
err = json.NewDecoder(response.Body).Decode(&steamAchievementResponse)
return
}
// unearnedAchievements collects the list of achievements yet to be earned by the user with
// a given Steam ID.
func unearnedAchievements(steamID int) (unearnedAchievements []Achievement, err error) {
steamAchievementResponse, err := readSteamStats(steamID)
if err != nil {
return
}
var earnedAchievements []int
// The achievements as obtained from Steam are not sorted, so we do that first
// to make them easier to manipulate.
for _, achievement := range steamAchievementResponse.Playerstats.Achievements {
achievementID, _ := strconv.Atoi(achievement.Name)
earnedAchievements = append(earnedAchievements, achievementID)
}
sort.Ints(earnedAchievements)
earnedAchievements = append(earnedAchievements, 404)
// Find the complement of the slice
previousAchievement := 0
for _, thisAchievement := range earnedAchievements {
for i := previousAchievement + 1; i < thisAchievement; i++ {
achievement := getAchievementByID(i)
unearnedAchievements = append(unearnedAchievements, achievement)
}
previousAchievement = thisAchievement
}
return
}
// SteamPlayerIDResponse represents the JSON response provided by the Steam Web API
// when one prompts for the integral Steam ID belonging to a user with a given username.
type SteamPlayerIDResponse struct {
Response struct {
Steamid string `json:"steamid"`
Success int `json:"success"`
} `json:"response"`
}
// getUserID calls the Steam Web API to determine the Steam ID of a user with a given username.
func getUserID(username string) (userID int, err error) {
client := &http.Client{Timeout: 10 * time.Second}
response, err := client.Get(fmt.Sprintf(userIDAPIURL, config.SteamAPIKey, username))
if err != nil {
return
}
defer response.Body.Close()
steamResponse := SteamPlayerIDResponse{}
err = json.NewDecoder(response.Body).Decode(&steamResponse)
return strconv.Atoi(steamResponse.Response.Steamid)
}