-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
269 lines (227 loc) · 6.35 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/goamaan/valocli/internal/core"
"github.com/goamaan/valocli/internal/player"
"github.com/goamaan/valocli/internal/store"
)
type AuthConfiguration struct {
Username string `json:"username"`
Password string `json:"password"`
Region string `json:"region"`
}
const (
ConfigFileDirectory = ".valocli"
ConfigFilePath = "valocli_config.json"
AuthSaveDataFilePath = "valocli_auth_save.json"
)
func main() {
client := core.New(nil)
config, saveData := readFromConfig()
client.Region = config.Region
if saveData != nil {
client.AuthData = saveData
// ensure that saved auth data actually works
err := client.SetUserId()
if err != nil {
fmt.Printf("Got error: %s. Previous tokens have expired. Logging in again...\n", err)
} else {
saveAuthSaveData(getSaveDataPath(), client.AuthData)
cliLoop(client)
return
}
}
err := client.Authorize(config.Username, config.Password)
if err != nil {
if err == core.ErrorRiotMultifactor {
fmt.Println("Seems like you have Multi factor set up. Enter the code sent to your email: ")
var multifactorCode string
fmt.Scan(&multifactorCode)
err = client.MultiFactorAuth(multifactorCode)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
saveAuthSaveData(getSaveDataPath(), client.AuthData)
cliLoop(client)
return
}
func cliLoop(c *core.Client) {
var response string
for {
fmt.Println("what do you want to do - enter the corresponding number")
fmt.Println("Check Store - 1")
fmt.Println("Check Wallet - 2")
fmt.Println("Check MMR (Rank Data) - 3")
fmt.Println("Quit - 0")
fmt.Scan(&response)
if response == "1" {
err := store.GetStorefront(c)
if err != nil {
log.Fatalf("error getting store: %s", err)
break
}
} else if response == "2" {
err := store.GetWallet(c)
if err != nil {
log.Fatalf("error getting wallet: %s", err)
break
}
} else if response == "3" {
err := player.GetPlayerMMR(c)
if err != nil {
log.Fatalf("error getting player mmr: %s", err)
break
}
} else if response == "0" {
break
}
}
}
func readFromConfig() (AuthConfiguration, *core.AuthSaveData) {
var config AuthConfiguration
configPath := getConfigPath()
fmt.Println("VALORANT helper:")
fmt.Println()
if _, err := os.Stat(configPath); os.IsNotExist(err) {
// Configuration file doesn't exist, prompt for username and password
userAuthInput(&config)
userRegionInput(&config)
saveConfiguration(configPath, config)
return config, nil
}
// Configuration file exists, read file
config = loadConfiguration(configPath)
fmt.Printf("Use previously saved username (%s) and password?: Y/n - ", config.Username)
var usePrevious string
fmt.Scan(&usePrevious)
if usePrevious == "n" || usePrevious == "N" {
userAuthInput(&config)
userRegionInput(&config)
saveConfiguration(configPath, config)
return config, nil
}
// using previous username, password, so try saved auth data
saveData := readFromSaveData()
return config, saveData
}
func userAuthInput(config *AuthConfiguration) {
fmt.Println("Please enter your username:")
fmt.Scan(&config.Username)
fmt.Println("Please enter your password:")
fmt.Scan(&config.Password)
}
func userRegionInput(config *AuthConfiguration) {
fmt.Println("What region was your account made in? Enter the corresponding keyword")
fmt.Println("North America, Brazil, Latin America - na")
fmt.Println("Europe - eu")
fmt.Println("Asia Pacific - ap")
fmt.Println("Korea - kr")
var response string
fmt.Scan(&response)
if response != "na" && response != "eu" && response != "ap" && response != "kr" {
response = "na"
}
config.Region = response
}
func readFromSaveData() *core.AuthSaveData {
var saveData *core.AuthSaveData
saveDataPath := getSaveDataPath()
if _, err := os.Stat(saveDataPath); os.IsNotExist(err) {
return nil
}
// Save data file exists, read file
saveData = loadAuthSaveData(saveDataPath)
oneHourAgo := time.Now().Add(-time.Hour)
// Compare SavedAt time with one hour ago
if saveData.SavedAt.Before(oneHourAgo) {
// discard old save data if it's been more than an hour
fmt.Println("Last login was more than an hour ago, trying to login again...")
return nil
}
fmt.Println("Attempting to use previous login tokens...")
return saveData
}
func saveConfiguration(path string, config AuthConfiguration) {
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
fmt.Println("Error marshaling configuration:", err)
return
}
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(getConfigDirectory(), 0700)
}
err = os.WriteFile(path, data, 0644)
if err != nil {
fmt.Println("Error writing configuration file:", err)
}
}
func loadConfiguration(path string) AuthConfiguration {
data, err := os.ReadFile(path)
if err != nil {
fmt.Println("Error reading configuration file:", err)
return AuthConfiguration{}
}
var config AuthConfiguration
err = json.Unmarshal(data, &config)
if err != nil {
fmt.Println("Error unmarshaling configuration:", err)
return AuthConfiguration{}
}
return config
}
func getConfigPath() string {
configDir := getConfigDirectory()
configPath := filepath.Join(configDir, ConfigFilePath)
return configPath
}
func getSaveDataPath() string {
configDir := getConfigDirectory()
saveDataPath := filepath.Join(configDir, AuthSaveDataFilePath)
return saveDataPath
}
func getConfigDirectory() string {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println("Error getting user home directory:", err)
return ""
}
configDir := filepath.Join(homeDir, ConfigFileDirectory)
return configDir
}
func saveAuthSaveData(path string, authData *core.AuthSaveData) {
data, err := json.MarshalIndent(authData, "", " ")
if err != nil {
fmt.Println("Error marshaling auth save data:", err)
return
}
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(getConfigDirectory(), 0700)
}
err = os.WriteFile(path, data, 0644)
if err != nil {
fmt.Println("Error writing auth save data:", err)
}
}
func loadAuthSaveData(path string) *core.AuthSaveData {
data, err := os.ReadFile(path)
if err != nil {
fmt.Println("Error reading auth save data:", err)
return nil
}
var config *core.AuthSaveData
err = json.Unmarshal(data, &config)
if err != nil {
fmt.Println("Error unmarshaling auth save data:", err)
return nil
}
return config
}