-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisenchant.go
287 lines (240 loc) · 7.66 KB
/
disenchant.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/onescriptkid/disenchant/utils"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type RiotLoot struct {
DisenchantLootName string `json:"disenchantLootName"`
ItemStatus string `json:"itemStatus"`
ItemDesc string `json:"itemDesc"`
LootName string `json:"lootName"`
Count int `json:"count"`
Type string `json:"type"`
}
func main() {
// Fix colorred terminal output specific to windows cmd prompt
utils.FixWindowsColors()
// Show Title and set trap to always prompt user before quiting. Prevents app from immediately closing on finish.
utils.Title("Disenchanting blue essence ...")
defer utils.OnFinish()
// Get Port and Token from LoL Riot lockfile
port, token, err := getPortAndToken()
if err != nil {
utils.ErrorFatal(err)
}
// Build http client to interact with Riot Lol client api
client, err := BuildHttpClient(port, token)
if err != nil {
utils.ErrorFatal(err)
}
// List all champion shards convertable to blue essence
champions, err := ListChampionShards(client, port, token)
if err != nil {
utils.ErrorFatal(err)
}
// Prompt player before disenchanting all of their champion shards - Are you sure?
AreYouSure()
// Disenchant champion shards
err = DisenchantChampionShards(client, port, token, champions)
if err != nil {
utils.ErrorFatal(err)
}
utils.Green("\nDisenchanting champions succeeded!")
}
// Get Port and Token from LoL Riot lockfile
func getPortAndToken() (port string, token string, err error) {
utils.Header("Searching for lockfile ...")
// Retrieve standard set of lockfile paths
paths, pathErr := utils.GetLockFilePaths()
if err != nil {
err = pathErr
return
}
var lockfilePath string
foundLockfile := false
// Search multiple dirs until lockfile is found for the lockfile from the Riot LoL directory on the host machine
for _, path := range paths {
// Convert to absolute dir to surface dir to end user
abs, absErr := filepath.Abs(path)
if absErr != nil {
err = absErr
return
}
// If lockfile found, exit loop. Otherwise, keep searching.
lockfilePath = abs
_, err = os.Stat(lockfilePath)
if err == nil {
msg := fmt.Sprintf(" Found %s. Parsing lockfile ...", lockfilePath)
foundLockfile = true
fmt.Println(msg)
break
} else if errors.Is(err, os.ErrNotExist) {
msg := fmt.Sprintf(" Missing %s. Seaching other locations for lockfile ...", lockfilePath)
utils.Warn(msg)
} else {
return
}
}
// If lockfile missing, error out
if !foundLockfile {
err = errors.New("Unable to find lockfile. Is your LoL client running?")
return
}
// Read in lockfile
content, readErr := ioutil.ReadFile(lockfilePath)
if readErr != nil {
err = readErr
return
}
contentString := string(content)
// Split lockfile content and parse port/token - LeagueClient:22232:56025:XXXXXXXXXXXXXX:https
chunks := strings.Split(contentString, ":")
password := chunks[3]
port = chunks[2]
pre64token := fmt.Sprintf("riot:%s", password)
token = base64.StdEncoding.EncodeToString([]byte(pre64token))
// Debug print statements
// fmt.Printf(" chunks: %s $\n", chunks)
// fmt.Printf(" password: %s $\n", password)
// fmt.Printf(" pre64token: %s $\n", pre64token)
// fmt.Printf(" port: %s $\n", port)
// fmt.Printf(" token: %s $\n", token)
fmt.Printf(" Port|Token: %s|%s\n", port, token)
return
}
// Build http client to interact with Riot Lol client api
func BuildHttpClient(port string, token string) (client http.Client, err error) {
utils.Header("Building http client ...")
// Instantiate http client
tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
client = http.Client{Timeout: time.Duration(10) * time.Second, Transport: tr}
return
}
// List all champion shards convertable to blue essence
func ListChampionShards(client http.Client, port string, token string) (champions []RiotLoot, err error) {
utils.Header("Searching for champions to disenchant ...")
host := fmt.Sprintf("https://127.0.0.1:%s", port)
auth := fmt.Sprintf("Basic %s", token)
url := fmt.Sprintf("%s/lol-loot/v1/player-loot", host)
// Instantiate http get request
req, httperr := http.NewRequest("GET", url, nil)
if httperr != nil {
err = httperr
return
}
// Set headers on RiotLoot get request
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", auth)
// Execute RiotLoot get request
res, geterr := client.Do(req)
if geterr != nil {
err = geterr
return
}
// Check status code
if res.StatusCode != http.StatusOK {
msg := fmt.Sprintf("Request to %s failed with status %v %v", url, res.StatusCode, http.StatusText(res.StatusCode))
err = errors.New(msg)
return
}
// Uncomment to print all of riot loot
// b, err := io.ReadAll(res.Body)
// if err != nil {
// log.Fatalln(err)
// }
// fmt.Println(string(b))
// Unmarshal RiotLoot get request into json
var riotLoot []RiotLoot
json.NewDecoder(res.Body).Decode(&riotLoot)
// Iterate over all RiotLoot and only select champion shards that are owned for disenchanting
total := 0
for _, loot := range riotLoot {
if loot.DisenchantLootName == "CURRENCY_champion" && loot.ItemStatus == "OWNED" {
fmt.Printf(" Found %4v %s \n", loot.Count, loot.ItemDesc)
champions = append(champions, loot)
total += loot.Count
}
}
fmt.Printf("Total %v champions to disenchant ... \n", total)
return
}
// Prompt player before disenchanting all of their champion shards - Are you sure?
func AreYouSure() {
var input string
for input != "y" && input != "Y" {
utils.Title("Are you sure? Press [y] to continue or [n] to quit\n")
fmt.Scanln(&input)
// Quit if n or N
if input == "n" || input == "N" {
no := errors.New("Quitting ...")
utils.ErrorFatal(no)
}
}
}
// Disenchant all champion shards found on the account
func DisenchantChampionShards(client http.Client, port string, token string, champions []RiotLoot) (err error) {
utils.Header("Disenchanting champions ...")
host := fmt.Sprintf("https://127.0.0.1:%s", port)
auth := fmt.Sprintf("Basic %s", token)
// For each champion, create a thread
wg := sync.WaitGroup{}
for _, champion := range champions {
wg.Add(1)
go func(client http.Client, port string, token string, champion RiotLoot) {
fmt.Printf(" Disenchanting %v %s\n", champion.Count, champion.ItemDesc)
url := fmt.Sprintf("%s/lol-loot/v1/recipes/%s_disenchant/craft?repeat=%v", host, champion.Type, champion.Count)
// Build post json body
var jsonStr = []byte(fmt.Sprintf(`["%s"]`, champion.LootName))
jsonBytes := bytes.NewBuffer(jsonStr)
// Instantiate http get request
req, httperr := http.NewRequest("POST", url, jsonBytes)
if httperr != nil {
err = httperr
log.Fatalln(err)
return
}
// Set headers on RiotLoot get request
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", auth)
// Execute RiotLoot get request
res, geterr := client.Do(req)
if geterr != nil {
err = geterr
log.Fatalln(err)
return
}
// Check status code
if res.StatusCode != http.StatusOK {
msg := fmt.Sprintf("Request to %s failed with status %v %v", url, res.StatusCode, http.StatusText(res.StatusCode))
err = errors.New(msg)
log.Fatalln(err)
return
}
// Uncomment to print JSON response for disenchant POST request
// b, err := io.ReadAll(res.Body)
// if err != nil {
// log.Fatalln(err)
// }
// fmt.Println(string(b))
// fmt.Printf(" Url %s\n", url)
// fmt.Printf(" Auth %s\n", auth)
wg.Done()
}(client, port, token, champion)
}
// Wait for every thread to finish
wg.Wait()
return
}