Skip to content

Commit a759139

Browse files
committed
Colorred output, split utils, gofmt, and README.md improvements
1 parent 3c7983f commit a759139

File tree

7 files changed

+150
-189
lines changed

7 files changed

+150
-189
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
![Tons of essence](./assets/blueessence.png)
21

3-
# disenchant
4-
Disenchant all **owned** *champions* and *champion shards* with 1 click
52

6-
![Disenchant Champion shards](./assets/disenchant.png)
3+
# disenchant ![go19](https://img.shields.io/badge/go-1.19-blue) ![LoL client](https://img.shields.io/badge/LeagueClient-12.16.462.9630-gold) ![LoL Game](https://img.shields.io/badge/LeagueGame-12.16.4624391-purple)
4+
Disenchant all **owned** *champions* and *champion shards* with 1 click
75

6+
<d> | <d>
7+
:-------------------------:|:-------------------------:
8+
![Disenchant Champion shards](./assets/disenchant.png) | ![Tons of essence](./assets/blueessence.png)
89

910
## 🚀 Quickstart
1011

1112
- **Open** the LoL client
12-
- **Download** [disenchant.exe](https://github.com/onescriptkid/disenchant/releases/download/v0.0.3/disenchant.exe)
13+
- **Download** [disenchant.exe](https://github.com/onescriptkid/disenchant/releases/download/v0.0.4/disenchant.exe)
1314
- **Doubleclick** the exectuable - `disenchant.exe`
1415

1516
## 😢 Notable shortfalls
1617

17-
- #️⃣ only works with windows
18-
- ~~🐧not compatible with linux~~
19-
- ~~🍎 not compatiable with OSX~~
18+
- #️⃣ only built for windows
19+
- ~~🐧not yet compatible on linux~~
20+
- ~~🍎 not yet compatiable on OSX~~
2021

2122
## Riot League Client API
2223

23-
`disenchant.go` queries the riot league client [api](https://riot-api-libraries.readthedocs.io/en/latest/lcu.html) to gather loot and select champion shards to convert to blue essence
24+
`disenchant.go` queries the riot league client [api](https://riot-api-libraries.readthedocs.io/en/latest/lcu.html) for loot and selects champion shards to convert to blue essence
2425

2526
### Get Request - list of champion shards
27+
28+
> Replace `<password>` with the *4th* value from the `lockfile - "C:\\Riot Games\\League of Legends\\lockfile"` : `LeagueClient:13340:56695:yQ*****Ekw:https`
29+
2630
```bash
2731
curl --insecure --basic --user riot:<password> -H "Accept: application/json" -v https://localhost:65023/lo
2832
l-loot/v1/player-loot

disenchant.go

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
package main
22

33
import (
4-
"io/ioutil"
5-
"os"
6-
"fmt"
4+
"bytes"
5+
"crypto/tls"
6+
"encoding/base64"
7+
"encoding/json"
78
"errors"
9+
"fmt"
810
"github.com/onescriptkid/disenchant/utils"
11+
"io/ioutil"
12+
"log"
13+
"net/http"
14+
"os"
915
"path/filepath"
1016
"strings"
11-
"encoding/base64"
12-
"net/http"
13-
"time"
14-
"crypto/tls"
15-
"encoding/json"
1617
"sync"
17-
"bytes"
18-
"log"
19-
// "io"
18+
"time"
2019
)
2120

2221
type RiotLoot struct {
2322
DisenchantLootName string `json:"disenchantLootName"`
24-
ItemStatus string `json:"itemStatus"`
25-
ItemDesc string `json:"itemDesc"`
26-
LootName string `json:"lootName"`
27-
Count int `json:"count"`
28-
Type string `json:"type"`
23+
ItemStatus string `json:"itemStatus"`
24+
ItemDesc string `json:"itemDesc"`
25+
LootName string `json:"lootName"`
26+
Count int `json:"count"`
27+
Type string `json:"type"`
2928
}
3029

3130
func main() {
31+
// Fix colorred terminal output specific to windows cmd prompt
32+
utils.FixWindowsColors()
33+
34+
// Show Title and set trap to always prompt user before quiting. Prevents app from immediately closing on finish.
3235
utils.Title("Disenchanting blue essence ...")
3336
defer utils.OnFinish()
3437

@@ -37,7 +40,7 @@ func main() {
3740
if err != nil {
3841
utils.ErrorFatal(err)
3942
}
40-
43+
4144
// Build http client to interact with Riot Lol client api
4245
client, err := BuildHttpClient(port, token)
4346
if err != nil {
@@ -53,7 +56,7 @@ func main() {
5356
AreYouSure()
5457

5558
// Disenchant champion shards
56-
err = DisenchantChampionShards(client, port, token, champions)
59+
err = DisenchantChampionShards(client, port, token, champions)
5760
if err != nil {
5861
utils.ErrorFatal(err)
5962
}
@@ -62,9 +65,9 @@ func main() {
6265
}
6366

6467
// Get Port and Token from LoL Riot lockfile
65-
func getPortAndToken() ( port string, token string, err error ) {
68+
func getPortAndToken() (port string, token string, err error) {
6669
utils.Header("Searching for lockfile ...")
67-
70+
6871
// Retrieve standard set of lockfile paths
6972
paths, pathErr := utils.GetLockFilePaths()
7073
if err != nil {
@@ -86,14 +89,14 @@ func getPortAndToken() ( port string, token string, err error ) {
8689

8790
// If lockfile found, exit loop. Otherwise, keep searching.
8891
lockfilePath = abs
89-
_, err = os.Stat(lockfilePath);
92+
_, err = os.Stat(lockfilePath)
9093
if err == nil {
9194
msg := fmt.Sprintf(" Found %s. Parsing lockfile ...", lockfilePath)
9295
foundLockfile = true
9396
fmt.Println(msg)
9497
break
9598
} else if errors.Is(err, os.ErrNotExist) {
96-
msg := fmt.Sprintf(" Missing %s. Seaching other locations for lockfile", lockfilePath)
99+
msg := fmt.Sprintf(" Missing %s. Seaching other locations for lockfile ...", lockfilePath)
97100
utils.Warn(msg)
98101
} else {
99102
return
@@ -133,18 +136,18 @@ func getPortAndToken() ( port string, token string, err error ) {
133136
}
134137

135138
// Build http client to interact with Riot Lol client api
136-
func BuildHttpClient(port string, token string)(client http.Client, err error) {
139+
func BuildHttpClient(port string, token string) (client http.Client, err error) {
137140
utils.Header("Building http client ...")
138141

139142
// Instantiate http client
140-
tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true} }
143+
tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
141144
client = http.Client{Timeout: time.Duration(10) * time.Second, Transport: tr}
142145

143146
return
144147
}
145148

146149
// List all champion shards convertable to blue essence
147-
func ListChampionShards(client http.Client, port string, token string)(champions []RiotLoot, err error) {
150+
func ListChampionShards(client http.Client, port string, token string) (champions []RiotLoot, err error) {
148151
utils.Header("Searching for champions to disenchant ...")
149152
host := fmt.Sprintf("https://127.0.0.1:%s", port)
150153
auth := fmt.Sprintf("Basic %s", token)
@@ -187,12 +190,15 @@ func ListChampionShards(client http.Client, port string, token string)(champions
187190
json.NewDecoder(res.Body).Decode(&riotLoot)
188191

189192
// Iterate over all RiotLoot and only select champion shards that are owned for disenchanting
193+
total := 0
190194
for _, loot := range riotLoot {
191195
if loot.DisenchantLootName == "CURRENCY_champion" && loot.ItemStatus == "OWNED" {
192-
fmt.Printf(" Found %4v %s \n", loot.Count, loot.ItemDesc,)
196+
fmt.Printf(" Found %4v %s \n", loot.Count, loot.ItemDesc)
193197
champions = append(champions, loot)
198+
total += loot.Count
194199
}
195200
}
201+
fmt.Printf("Total %v champions to disenchant ... \n", total)
196202

197203
return
198204
}
@@ -202,20 +208,20 @@ func AreYouSure() {
202208
var input string
203209

204210
for input != "y" && input != "Y" {
205-
utils.Title("Are you sure? Press [y] to continue or [n] to quit...\n")
206-
fmt.Scan(&input)
211+
utils.Title("Are you sure? Press [y] to continue or [n] to quit\n")
212+
fmt.Scanln(&input)
207213

208214
// Quit if n or N
209-
if(input == "n" || input == "N") {
215+
if input == "n" || input == "N" {
210216
no := errors.New("Quitting ...")
211217
utils.ErrorFatal(no)
212218
}
213219
}
214220

215221
}
216222

217-
// Disenchant all champion shards found on the account
218-
func DisenchantChampionShards(client http.Client, port string, token string, champions []RiotLoot)( err error) {
223+
// Disenchant all champion shards found on the account
224+
func DisenchantChampionShards(client http.Client, port string, token string, champions []RiotLoot) (err error) {
219225
utils.Header("Disenchanting champions ...")
220226
host := fmt.Sprintf("https://127.0.0.1:%s", port)
221227
auth := fmt.Sprintf("Basic %s", token)
@@ -260,23 +266,22 @@ func DisenchantChampionShards(client http.Client, port string, token string, cha
260266
return
261267
}
262268

263-
// Print JSON response from disenchant post request
264-
// Uncomment to print result of disenchant post request json
269+
// Uncomment to print JSON response for disenchant POST request
265270
// b, err := io.ReadAll(res.Body)
266271
// if err != nil {
267272
// log.Fatalln(err)
268273
// }
269274
// fmt.Println(string(b))
270275
// fmt.Printf(" Url %s\n", url)
271276
// fmt.Printf(" Auth %s\n", auth)
277+
272278
wg.Done()
273279
}(client, port, token, champion)
274280
}
275-
281+
276282
// Wait for every thread to finish
277283
wg.Wait()
278284

279285
return
280286

281287
}
282-

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ require (
77
github.com/fatih/color v1.13.0 // indirect
88
github.com/mattn/go-colorable v0.1.9 // indirect
99
github.com/mattn/go-isatty v0.0.14 // indirect
10-
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
10+
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
1111
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK
1313
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1414
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
1515
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16+
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM=
17+
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

utils/helpers.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package utils
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"runtime"
7+
)
8+
9+
// Always catch on finish
10+
func OnFinish() {
11+
fmt.Print("Press [enter] to continue ...")
12+
fmt.Scanln()
13+
}
14+
15+
// Get LockFile Locations
16+
func GetLockFilePaths() (paths []string, err error) {
17+
osname := runtime.GOOS
18+
switch osname {
19+
case "windows":
20+
samePath := "./lockfile"
21+
lolPath := "C:\\Riot Games\\League of Legends\\lockfile"
22+
pbePath := "C:\\Riot Games\\League of Legends (PBE)\\lockfile"
23+
paths = []string{samePath, lolPath, pbePath}
24+
return
25+
case "darwin":
26+
err = errors.New("Unavailable on OSX")
27+
return
28+
case "linux":
29+
err = errors.New("Unavailable on linux")
30+
return
31+
default:
32+
fmt.Printf("%s.\n", osname)
33+
msg := fmt.Sprintf("Unavailabe on %s", osname)
34+
err = errors.New(msg)
35+
return
36+
}
37+
}

utils/text.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"github.com/fatih/color"
6+
"golang.org/x/sys/windows"
7+
"os"
8+
"runtime"
9+
)
10+
11+
func FixWindowsColors() {
12+
if runtime.GOOS == "windows" {
13+
stdout := windows.Handle(os.Stdout.Fd())
14+
var originalMode uint32
15+
16+
windows.GetConsoleMode(stdout, &originalMode)
17+
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
18+
}
19+
}
20+
21+
// Print error in red and then exit with exit status 1
22+
// Escape colors work differntly in Windows. Swapping to tabs/spaces.
23+
func ErrorFatal(err error) {
24+
red := color.New(color.FgRed).SprintFunc()
25+
fmt.Println(red(err))
26+
os.Exit(1)
27+
}
28+
29+
// Print log in yellow as warning
30+
func Warn(msg string) {
31+
yellow := color.New(color.FgYellow).SprintFunc()
32+
fmt.Println(yellow(msg))
33+
}
34+
35+
// Print success in green
36+
func Green(msg string) {
37+
green := color.New(color.Bold, color.FgGreen).SprintFunc()
38+
fmt.Println(green(msg))
39+
}
40+
41+
// Print header in bold font
42+
func Header(msg string) {
43+
bold := color.New(color.Bold).SprintFunc()
44+
fmt.Println(bold(msg))
45+
}
46+
47+
// Print Title in bold cyan font
48+
func Title(msg string) {
49+
boldCyan := color.New(color.Bold, color.FgCyan).SprintFunc()
50+
fmt.Println(boldCyan(msg))
51+
}
52+
53+
// Print in in bold cyan font
54+
func BoldCyan(msg string) {
55+
boldCyan := color.New(color.Bold, color.FgCyan).SprintFunc()
56+
fmt.Println(boldCyan(msg))
57+
}

0 commit comments

Comments
 (0)