-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift.go
176 lines (144 loc) · 4.26 KB
/
shift.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
167
168
169
170
171
172
173
174
175
176
package bl3auto
import (
"errors"
"strings"
"time"
)
type ShiftConfig struct {
CodeListUrl string `json:"codeListUrl"`
CodeInfoUrl string `json:"codeInfoUrl"`
UserInfoUrl string `json:"userInfoUrl"`
GameCodename string `json:"gameCodename"`
AllowInactive bool
}
type ShiftCodeMap map[string][]string
func (codeMap ShiftCodeMap) Contains(code, platform string) bool {
platforms, found := codeMap[code]
if !found {
return false
}
for _, p := range platforms {
if p == platform {
return true
}
}
return false
}
type shiftCode struct {
Game string `json:"offer_title"`
Platform string `json:"offer_service"`
Active bool `json:"is_active"`
}
type shiftCodeFromList struct {
Code string `json:"code"`
Platform string `json:"platform"`
}
func (client *Bl3Client) GetCodePlatforms(code string) ([]string, bool) {
platforms := make([]string, 0)
res, err := client.Get(client.Config.Shift.CodeInfoUrl + code + "/info")
if err != nil {
return platforms, false
}
json, err := res.BodyAsJson()
if err != nil {
return platforms, false
}
codes := make([]shiftCode, 0)
json.From("entitlement_offer_codes").Select("offer_service", "is_active", "offer_title").Out(&codes)
for _, code := range codes {
//if (code.Active || client.Config.Shift.AllowInactive) && code.Game == client.Config.Shift.GameCodename {
platforms = append(platforms, code.Platform)
//}
}
if len(platforms) == 0 {
return platforms, false
}
return platforms, true
}
func (client *Bl3Client) RedeemShiftCode(code, platform string) error {
response, err := client.Post(client.Config.Shift.CodeInfoUrl+code+"/redeem/"+platform, "", nil)
if err != nil {
return errors.New("failed to initialize code redemption")
}
type redemptionJob struct {
JobId string `json:"job_id"`
Wait int `json:"max_wait_milliseconds"`
}
resJson, err := response.BodyAsJson()
if err != nil {
return errors.New("bad code init response")
}
redemptionInfo := redemptionJob{}
resJson.Out(&redemptionInfo)
if redemptionInfo.JobId == "" {
redemptionError := ""
resJson.Reset().From("error.code").Out(&redemptionError)
if redemptionError != "" {
return errors.New(strings.ToLower(strings.Join(strings.Split(redemptionError, "_"), " ")) + ". Try again later.")
}
return errors.New("failed to schedule code redemption")
}
// not sure if this is necessary
time.Sleep(time.Duration(redemptionInfo.Wait) * time.Millisecond)
redeemResponse, err := client.Get(client.Config.Shift.CodeInfoUrl + code + "/job/" + redemptionInfo.JobId)
if err != nil {
return errors.New("failed to initialize code redemption")
}
resJson, err = redeemResponse.BodyAsJson()
if err != nil {
return errors.New("bad code redemption response")
}
success := false
resJson.From("success").Out(&success)
errs := make([]string, 0)
resJson.Reset().From("errors").Out(&errs)
if len(errs) > 0 {
return errors.New(strings.ToLower(strings.Join(strings.Split(errs[0], "_"), " ")) + ".")
}
if !success {
return errors.New("failed to redeem SHiFT code")
}
resJson.Out(&redemptionInfo)
return nil
}
func (client *Bl3Client) GetShiftPlatforms() (StringSet, error) {
platforms := StringSet{}
response, err := client.Post(client.Config.Shift.UserInfoUrl, "", nil)
if err != nil {
return platforms, errors.New("failed to get available platforms list")
}
resJson, err := response.BodyAsJson()
if err != nil {
return platforms, err
}
platformList := make([]string, 0)
resJson.From("platforms").Out(&platformList)
for _, platform := range platformList {
platforms.Add(platform)
}
return platforms, nil
}
func (client *Bl3Client) GetFullShiftCodeList() (ShiftCodeMap, error) {
codeMap := ShiftCodeMap{}
httpClient, err := NewHttpClient()
if err != nil {
return codeMap, err
}
res, err := httpClient.Get(client.Config.Shift.CodeListUrl)
if err != nil {
return codeMap, errors.New("failed to get SHiFT code list")
}
json, err := res.BodyAsJson()
if err != nil {
return codeMap, errors.New("failed to get SHiFT code list body as JSON")
}
codes := make([]shiftCodeFromList, 0)
json.From("[0].codes").Select("code", "platform").Out(&codes)
for _, code := range codes {
platforms, valid := client.GetCodePlatforms(code.Code)
if valid {
codeMap[code.Code] = platforms
}
}
return codeMap, nil
}