-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
436 lines (417 loc) · 11.8 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/Valorith/EQRaidAssist/alias"
"github.com/Valorith/EQRaidAssist/config"
"github.com/Valorith/EQRaidAssist/mongodb"
"github.com/Valorith/EQRaidAssist/raid"
"github.com/Valorith/EQRaidAssist/scanner"
)
var (
commandsDisplayed bool = false
)
func main() {
defer close()
var err error
var count int //scanline arg return count
var userInput string
defer mongodb.DisconnectALL()
// Load the config file
err = config.ReadConfig()
if err != nil {
fmt.Printf("main: failed to read config: %s\n", err)
}
// Initialize database connections
mongodb.Init()
// Load the alias data from the database
err = alias.ActiveAliases.LoadFromDB()
if err != nil {
fmt.Printf("main: failed to load aliases: %s\n", err)
}
// Load the raid data from the database
err = raid.AllRaids.LoadFromDB()
if err != nil {
fmt.Printf("main: failed to load raids: %s\n", err)
}
for {
// If the character name is not set, request it
if !scanner.IsCharacterNameSet() {
fmt.Printf("Enter the character name that you want to monitor (first name only): ")
count, err = fmt.Scanln(&userInput)
if err != nil {
fmt.Println("invalid name input:", err)
continue
}
if count != 1 {
fmt.Println("incorrect number of arguments for character name")
continue
}
err = scanner.SetCharacterName(strings.Title(userInput))
if err != nil {
fmt.Println("invalid character name:", err)
continue
}
}
// If the server name is not set, attempt to infer it
if !scanner.IsServerNameSet() {
inferServerName()
}
// If the server name is not set (infer failed), request it
if !scanner.IsServerNameSet() {
fmt.Printf("Enter your server short name: ")
count, err = fmt.Scanln(&userInput)
if err != nil {
fmt.Println("invalid server input:", err)
continue
}
if count != 1 {
fmt.Println("incorrect number of arguments for server name")
continue
}
err = scanner.SetServerName(userInput)
if err != nil {
fmt.Println("invalid server name:", err)
continue
}
}
if !commandsDisplayed {
// Print the available commands to the user
printCommands()
}
var subCommand, value string
_, err = fmt.Scanln(&userInput, &subCommand, &value)
if err != nil {
if !(err.Error() == "unexpected newline") { // Filter out this specific known error condition that is not of concern
fmt.Printf("command error: %s %s %s: %s\n", userInput, subCommand, value, err)
}
}
// Retrieve commands from user
go getUserInput(userInput, subCommand, value)
}
}
// Print the available commands to the user
func printCommands() {
commandsDisplayed = true
fmt.Printf("Commands:\nStart scanning raid file: 'start'\nStop scanning raid file: 'stop'\nExit application: 'exit' or 'quit'\n")
fmt.Printf("Load the most recent saved raid: 'get lastraid'\n")
fmt.Printf("Show current raid participants: 'get raid'\n")
fmt.Printf("Reset all session data: 'set reset'\n")
fmt.Printf("Get app variables: 'get <identifier>'\nSet app variables: 'set <identifier>'\n")
fmt.Println("-----------------")
fmt.Println("Enter a command:")
}
// Attempt to infer what the server name is based on the character name and client files
func inferServerName() {
// Attempt to infer the server name based upon the provided char name
fmt.Println("Attempting to infer server name...")
possibleServerNames, err := config.GetPossibleServerNames(scanner.GetCharacterName())
if err != nil {
fmt.Println("main: failed to load possible server names:", err)
}
if len(possibleServerNames) > 1 {
var err error
var count int //scanline arg return count
var userInput string
index := 1
for _, serverName := range possibleServerNames {
fmt.Printf("%d) %s\n", index, serverName)
index++
}
// Set the server to the selected server
fmt.Printf("Enter your server selection (1 - %d):\n", len(possibleServerNames))
count, err = fmt.Scanln(&userInput)
if err != nil {
fmt.Println("invalid server input:", err)
return
}
selection, err := strconv.Atoi(userInput)
if err != nil {
fmt.Println("strconv.Atoi:", err)
return
}
if count != 1 {
fmt.Println("incorrect number of arguments for server name")
return
}
selectedServer := possibleServerNames[selection-1]
fmt.Println("Setting server to:", selectedServer)
err = scanner.SetServerName(selectedServer)
if err != nil {
fmt.Println("invalid server name:", err)
return
}
} else if len(possibleServerNames) == 1 {
err = scanner.SetServerName(possibleServerNames[0])
if err != nil {
fmt.Println("main: failed to set server name:", err)
}
fmt.Printf("Setting server to: %s (infered)\n", possibleServerNames[0])
} else {
fmt.Println("main: failed to infer server name")
}
}
func getUserInput(input, subcommand, value string) {
var err error
fmt.Println("-----------------")
// Primary Command Handler
switch input {
case "start":
if scanner.IsRunning() {
fmt.Printf("getUserInput: scanner is already running")
return
}
scanner.Start()
case "stop":
scanner.Stop()
case "exit":
fmt.Println("[Status] Exiting...")
config.SaveConfig()
os.Exit(0)
case "set":
switch subcommand {
case "server":
fmt.Println("Setting server name to:", value)
err = scanner.SetServerName(value)
if err != nil {
fmt.Printf("getUserInput: invalid server name: %s", err)
}
case "character":
fmt.Println("Setting character name to:", value)
err = scanner.SetCharacterName(value)
if err != nil {
fmt.Printf("getUserInput: invalid character name: %s->%s", value, err)
}
if scanner.IsRunning() {
scanner.Reboot()
}
case "token":
fmt.Println("Setting bot token to:", value)
err = config.SetBotToken(value)
if err != nil {
fmt.Printf("getUserInput: %s->%s\n", value, err)
}
case "prefix":
fmt.Println("Setting bot prefix to:", value)
err = config.SetBotPrefix(value)
if err != nil {
fmt.Printf("getUserInput: %s->%s\n", value, err)
}
case "channel":
fmt.Println("Setting loot channel to:", value)
err = config.SetLootChannel(value)
if err != nil {
fmt.Printf("getUserInput: %s->%s\n", value, err)
}
case "lootwebhook":
fmt.Println("Setting loot webhook url to:", value)
err = config.SetLootWebHookUrl(value)
if err != nil {
fmt.Printf("getUserInput: %s->%s\n", value, err)
}
case "attendwebhook":
fmt.Println("Setting attendance webhook url to:", value)
err = config.SetAttendWebHookUrl(value)
if err != nil {
fmt.Printf("getUserInput: %s->%s\n", value, err)
}
case "guild":
fmt.Println("Setting guild name to:", value)
err = config.SetGuildName(value)
if err != nil {
fmt.Printf("getUserInput: %s->%s\n", value, err)
}
case "guildalias":
// Get a new alias list from the detected guild roster dump
fmt.Println("Importing the guild list from file and generating the alias list...")
err := alias.GenerateAliasListFromGuildList()
if err != nil {
fmt.Printf("GenerageAliasListFromGuildList(): %s\n", err)
}
// Update database with new alias list
err = alias.ActiveAliases.UpdateDB()
if err != nil {
fmt.Printf("ActiveAliases.UpdateDB(): %s\n", err)
}
case "timer":
fmt.Println("Setting timer to:", value)
// convert string to int
intValue, err := strconv.Atoi(value)
if err != nil {
fmt.Printf("getUserInput: invalid timer value: %s\n", err)
}
scanner.SetRaidFrequency(intValue)
case "raid":
if value == "checkin" {
err := raid.ActiveRaid.CheckIn()
if err != nil {
fmt.Printf("ActiveRaid.CheckIn(): %s\n", err)
}
} else {
fmt.Printf("ActiveRaid.CheckIn(): %s\n", "invalid subcommand")
}
case "reboot":
if scanner.IsRunning() {
scanner.Reboot()
} else {
fmt.Println("The scanner is not running...")
}
case "dbusername":
fmt.Println("Setting database username to:", value)
// convert string to int
err := config.SetDBUsername(value)
if err != nil {
fmt.Printf("set dbusername error: : %s\n", err)
}
case "dbpassword":
fmt.Println("Setting database password to:", value)
// convert string to int
err := config.SetDBPassword(value)
if err != nil {
fmt.Printf("set dbpassword error: : %s\n", err)
}
case "reset":
/*
scanner.Stop()
core.ClearPlayers()
scanner.ResetData()
discordwh.ResetData()
raid.ResetData()
config.ResetData()
*/
fmt.Println("Reset Not yet implemented")
default:
fmt.Printf("invalid command(%s)\n", subcommand)
}
case "get":
switch subcommand {
case "server":
serverName := scanner.GetServerName()
fmt.Println("Server Name:", serverName)
case "loot":
if raid.Active {
raid.ActiveRaid.PrintLoot()
} else {
fmt.Println("No active raid")
}
case "character":
characterName := scanner.GetCharacterName()
fmt.Println("Character Name:", characterName)
case "token":
token, err := config.GetBotToken()
if err != nil {
fmt.Printf("GetBotToken(): %s\n", err)
}
fmt.Println("Bot Token:", token)
case "prefix":
prefix, err := config.GetBotPrefix()
if err != nil {
fmt.Printf("GetBotPrefix(): %s\n", err)
}
fmt.Println("Bot Prefix:", prefix)
case "channel":
channel, err := config.GetLootChannel()
if err != nil {
fmt.Printf("GetLootChannel(): %s\n", err)
}
fmt.Println("Bot Loot Channel:", channel)
case "lootwebhook":
webHookUrl, err := config.GetLootWebHookUrl()
if err != nil {
fmt.Printf("GetLootWebHookUrl(): %s\n", err)
}
fmt.Println("Loot Web Hook Url:", webHookUrl)
case "attendwebhook":
webHookUrl, err := config.GetAtendWebHookUrl()
if err != nil {
fmt.Printf("GetAtendWebHookUrl(): %s\n", err)
}
fmt.Println("Attendance Web Hook Url:", webHookUrl)
case "guild":
guildName, err := config.GetGuildName()
if err != nil {
fmt.Printf("GetGuildName(): %s\n", err)
}
fmt.Println("Guild Name:", guildName)
case "guildalias":
err := alias.ReadGuildMembers()
if err != nil {
fmt.Printf("ReadGuildMembers(): %s\n", err)
}
fmt.Println("Guild aliases loaded...")
case "timer":
timer := scanner.GetRaidTimer()
fmt.Println("Raid file scan timer:", timer)
case "raid":
switch value {
case "":
err := raid.ActiveRaid.PrintParticipation()
if err != nil {
fmt.Printf("ActiveRaid.PrintParticipation(): %s\n", err)
}
default:
fmt.Printf("raid: invalid subcommand --> %s\n", subcommand)
}
case "alias":
switch value {
case "":
err := alias.ActiveAliases.PrintAliases()
if err != nil {
fmt.Printf("ActiveAliases.PrintAliases(): %s\n", err)
}
default:
fmt.Printf("alias: invalid subcommand --> %s\n", subcommand)
}
case "displaylist":
switch value {
case "":
err := raid.PrintDisplayList()
if err != nil {
fmt.Printf("raid.PrintDisplayList(): %s\n", err)
}
default:
fmt.Printf("displaylist: invalid subcommand --> %s\n", subcommand)
}
case "lastraid":
err := raid.ActiveRaid.Load(value)
if err != nil {
fmt.Printf("ActiveRaid.Load(): %s\n", err)
}
case "commands":
printCommands()
case "quit":
fmt.Println("[Status] Exiting...")
config.SaveConfig()
os.Exit(0)
default:
fmt.Printf("invalid command(%s)\n", subcommand)
}
case "alias":
characterName := subcommand
handle := value
if characterName == "" || handle == "" {
fmt.Println("invalid command: Expected: alias <characterName> <handle>")
return
}
err := alias.AddAlias(characterName, handle)
if err != nil {
fmt.Printf("AddAlias(): %s\n", err)
}
case "ping":
fmt.Println("Pong")
default:
fmt.Printf("invalid command(%s)\n", input)
}
if err != nil {
fmt.Printf("failed command: %s %s %s: %s\n", input, subcommand, value, err)
}
}
func close() {
fmt.Println("Cleaning up before exit...")
if raid.Active {
raid.Active = false
}
config.SaveConfig()
}