-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacters.go
166 lines (150 loc) · 4.79 KB
/
characters.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
type memberChars struct {
Response struct {
Characters struct {
Data map[int64]character `json:"data"`
Privacy int `json:"privacy"`
} `json:"characters"`
ItemComponents struct {
} `json:"itemComponents"`
} `json:"Response"`
ErrorCode int `json:"ErrorCode"`
ThrottleSeconds int `json:"ThrottleSeconds"`
ErrorStatus string `json:"ErrorStatus"`
Message string `json:"Message"`
MessageData struct {
} `json:"MessageData"`
}
type character struct {
MembershipID string `json:"membershipId"`
MembershipType int `json:"membershipType"`
CharacterID string `json:"characterId"`
DateLastPlayed time.Time `json:"dateLastPlayed"`
MinutesPlayedThisSession string `json:"minutesPlayedThisSession"`
MinutesPlayedTotal string `json:"minutesPlayedTotal"`
Light int `json:"light"`
Stats map[int64]int `json:"stats"`
RaceHash int64 `json:"raceHash"`
GenderHash int64 `json:"genderHash"`
ClassHash int64 `json:"classHash"`
RaceType int `json:"raceType"`
ClassType int `json:"classType"`
GenderType int `json:"genderType"`
EmblemPath string `json:"emblemPath"`
EmblemBackgroundPath string `json:"emblemBackgroundPath"`
EmblemHash int64 `json:"emblemHash"`
EmblemColor struct {
Red int `json:"red"`
Green int `json:"green"`
Blue int `json:"blue"`
Alpha int `json:"alpha"`
} `json:"emblemColor"`
LevelProgression struct {
ProgressionHash int `json:"progressionHash"`
DailyProgress int `json:"dailyProgress"`
DailyLimit int `json:"dailyLimit"`
WeeklyProgress int `json:"weeklyProgress"`
WeeklyLimit int `json:"weeklyLimit"`
CurrentProgress int `json:"currentProgress"`
Level int `json:"level"`
LevelCap int `json:"levelCap"`
StepIndex int `json:"stepIndex"`
ProgressToNextLevel int `json:"progressToNextLevel"`
NextLevelAt int `json:"nextLevelAt"`
} `json:"levelProgression"`
BaseCharacterLevel int `json:"baseCharacterLevel"`
PercentToNextLevel float32 `json:"percentToNextLevel"`
}
type Character struct {
MembershipID string `json:"MembershipID" bson:"MembershipID"`
CharacterID string `json:"CharacterID" bson:"CharacterID"`
Race int `json:"Race" bson:"Race"`
Gender int `json:"Gender" bson:"Gender"`
Class int `json:"Class" bson:"Class"`
LastRetrievedActivity string `json:"LastRetrievedActivity" bson:"LastRetrievedActivity"`
LastRetrievedDate time.Time `json:"LastRetrievedDate" bson:"LastRetrievedDate"`
DateLastPlayed time.Time `json:"DateLastPlayed" bson:"DateLastPlayed"`
Enabled bool `json:"Enabled" bson:"Enabled"`
}
func GetCharacters(memberID string) []Character {
url := fmt.Sprintf("https://bungie.net/Platform/Destiny2/%s/Profile/%s?components=200", config.MembershipType, url.QueryEscape(memberID))
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("x-api-key", config.APIKey)
if err != nil {
log.Fatal("NewRequest: ", err)
return nil
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Body: ", err)
return nil
}
// Fill the record with the data from the JSON
var record memberChars
if err := json.Unmarshal([]byte(string(body)), &record); err != nil {
log.Printf("Unmarshal error: %v", err)
}
characters := []Character{}
for _, character := range record.Response.Characters.Data {
//fmt.Printf("%s\r\n", member.DestinyUserInfo.DisplayName)
characters = append(
characters,
Character{
MembershipID: memberID,
CharacterID: character.CharacterID,
Race: character.RaceType,
Gender: character.GenderType,
Class: character.ClassType,
DateLastPlayed: character.DateLastPlayed,
},
)
}
return characters
}
func Race(raceID int) string {
switch raceID {
case 0:
return "Human"
case 1:
return "Awoken"
case 2:
return "Exo"
}
return "Unknown Race"
}
func Gender(genderID int) string {
switch genderID {
case 0:
return "Male"
case 1:
return "Female"
}
return "Genderless"
}
func Class(classID int) string {
switch classID {
case 0:
return "Titan"
case 1:
return "Hunter"
case 2:
return "Warlock"
}
return "Unknown Class"
}