-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand_dungeon.go
703 lines (578 loc) · 17.8 KB
/
command_dungeon.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/google/uuid"
"github.com/olekukonko/tablewriter"
"github.com/spf13/viper"
)
// Dungeon CLI root command
type DungeonCommand struct {
}
func (c *DungeonCommand) Help() string {
helpText := `
Usage: evedbtool dungeon [options] ...
Manage EVEmu dungeons.
Subcommands:
list List all available dungeons in the database.
apply Apply all dungeons from the dungeon directory.
import Import a dungeon from file.
export Export a dungeon to file.
new Creates a new blank dungeon.
delete Delete a dungeon from the database.
add-room Add a new room to a dungeon.
remove-room Remove a room from a dungeon.
list-rooms Lists all rooms in the specified dungeon.
list-archetypes List available dungeon archetypes.
list-factions List available factions.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonCommand) Synopsis() string {
return "Manages EVEmu dungeons."
}
func (c *DungeonCommand) Run(args []string) int {
fmt.Println(c.Help())
return 0
}
// List dungeons
type DungeonListCommand struct {
}
func (c *DungeonListCommand) Help() string {
helpText := `
Usage: evedbtool dungeon list [options] ...
Lists all dungeons in the database.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonListCommand) Synopsis() string {
return "Lists all dungeons in the database."
}
func (c *DungeonListCommand) Run(args []string) int {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Dungeon ID", "Dungeon Name"})
table.SetColWidth(60)
listOutput := ListDungeons()
for _, entry := range listOutput {
table.Append([]string{
strconv.Itoa(entry.ID),
entry.Name,
})
}
table.Render()
return 0
}
// Export a dungeon to file
type DungeonExportCommand struct {
}
func (c *DungeonExportCommand) Help() string {
helpText := `
Usage: evedbtool dungeon export [options] ...
Exports a specific dungeon from the database to a file.
Options:
-dungeon ID of the dungeon to export.
-file Filename to write the dungeon data to.
-dryrun Don't write the output to file, just print it.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonExportCommand) Synopsis() string {
return "Exports a specific dungeon from the database to a file."
}
func (c *DungeonExportCommand) Run(args []string) int {
var dungeonID int
var outputFilename string
var dryrun bool
cmdFlags := flag.NewFlagSet("export", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.IntVar(&dungeonID, "dungeon", 0, "ID of the dungeon to export.")
cmdFlags.StringVar(&outputFilename, "file", "export.json", "Filename to write the dungeon data to.")
cmdFlags.BoolVar(&dryrun, "dryrun", false, "Don't write the output to file, just print it.")
if err := cmdFlags.Parse(args); err != nil {
log.Error("Error parsing arguments")
return 1
}
if dungeonID < 1 {
ui.Output(c.Help())
return 1
}
jsonOutput := ExportDungeon(dungeonID)
if dryrun {
fmt.Println(jsonOutput)
} else {
if _, err := os.Stat(outputFilename); err == nil {
log.Error("File already exists, not overwriting.")
return 1
} else {
if err := ioutil.WriteFile(outputFilename, []byte(jsonOutput), 0644); err != nil {
log.Error("Error writing file: ", err)
return 1
} else {
log.Info("Successfully exported dungeon to: ", outputFilename)
}
}
}
return 0
}
// Import a dungeon from file
type DungeonImportCommand struct {
}
func (c *DungeonImportCommand) Help() string {
helpText := `
Usage: evedbtool dungeon import <filename> [options] ...
Imports a dungeon from provided JSON file.
Options:
-file <filename> File to import.
-overwrite Overwrite existing dungeon if a match is found.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonImportCommand) Synopsis() string {
return "Imports a dungeon from provided JSON file."
}
func (c *DungeonImportCommand) Run(args []string) int {
var filename string
var overwrite bool
cmdFlags := flag.NewFlagSet("import", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.StringVar(&filename, "file", "", "File to import.")
cmdFlags.BoolVar(&overwrite, "overwrite", false, "Overwrite existing dungeon if a match is found.")
if err := cmdFlags.Parse(args); err != nil {
log.Error("Error parsing arguments")
return 1
}
if filename == "" {
ui.Output(c.Help())
return 1
}
if _, err := os.Stat(filename); err == nil {
if data, err := ioutil.ReadFile(filename); err != nil {
log.Error("Error reading file: ", err)
return 1
} else {
ImportDungeon(data, overwrite)
log.Info("Successfully imported dungeon!")
}
} else {
log.Error("File does not exist: ", filename)
return 1
}
return 0
}
// Apply all dungeons from dungeons directory
type DungeonApplyCommand struct {
}
func (c *DungeonApplyCommand) Help() string {
helpText := `
Usage: evedbtool dungeon apply [options] ...
Applies all dungeons from the dungeon directory to the database.
Options:
-overwrite Overwrite existing dungeon if a match is found.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonApplyCommand) Synopsis() string {
return "Applies all dungeons from the dungeon directory to the database."
}
func (c *DungeonApplyCommand) Run(args []string) int {
var overwrite bool
cmdFlags := flag.NewFlagSet("apply", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.BoolVar(&overwrite, "overwrite", false, "Overwrite existing dungeon if a match is found.")
items, _ := ioutil.ReadDir(viper.GetString("dungeon-dir"))
log.Info(fmt.Sprintf("Attempting to import %d dungeons...", len(items)))
successCount := 0
for _, item := range items {
if !item.IsDir() {
fullpath := filepath.Join(viper.GetString("dungeon-dir"), item.Name())
log.Trace("Import candidate: ", fullpath)
if _, err := os.Stat(fullpath); err == nil {
if data, err := ioutil.ReadFile(fullpath); err != nil {
log.Error("Error reading file: ", err)
return 1
} else {
ImportDungeon(data, overwrite)
successCount++
}
}
}
}
log.Info(fmt.Sprintf("Successfully imported %d dungeons!", successCount))
return 0
}
// Create a new blank dungeon
type DungeonNewCommand struct {
}
func (c *DungeonNewCommand) Help() string {
helpText := `
Usage: evedbtool dungeon new [options] ...
Creates a new blank dungeon in the database.
Options:
-name <string> Name of the new dungeon.
-status <1-3> Status (1=Release, 2=Testing, 3=Working Copy).
-faction <int> Faction ID (run 'evedbtool dungeon list-factions' to list available)
-archetype <int> Archetype ID (run 'evedbtool dungeon list-archetypes' to list available)
-dryrun Don't create dungeon, just print the JSON data.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonNewCommand) Synopsis() string {
return "Creates a new blank dungeon in the database."
}
func (c *DungeonNewCommand) Run(args []string) int {
var dungeon Dungeon
var err error
var dryrun bool
cmdFlags := flag.NewFlagSet("new", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.StringVar(&dungeon.DungeonName, "name", "", "Name of the new dungeon.")
cmdFlags.IntVar(&dungeon.Status, "status", 0, "Status (1=Release, 2=Testing, 3=Working Copy).")
cmdFlags.IntVar(&dungeon.FactionID, "faction", 0, "Faction ID (run 'evedbtool dungeon list-factions' to list available)")
cmdFlags.IntVar(&dungeon.ArchetypeID, "archetype", 0, "Archetype ID (run 'evedbtool dungeon list-archetypes' to list available)")
cmdFlags.BoolVar(&dryrun, "dryrun", false, "Don't create dungeon, just print the JSON data.")
if err := cmdFlags.Parse(args); err != nil {
log.Error("Error parsing arguments")
return 1
}
// Interactively ask for dungeon name
if dungeon.DungeonName == "" {
dungeon.DungeonName = StringPrompt("Dungeon Name: ")
}
// Interactively ask for dungeon status
if dungeon.Status == 0 {
dungeon.Status, err = strconv.Atoi(StringPrompt("Status: (1=Release, 2=Testing, 3=Working Copy) "))
if err != nil {
log.Error("Error parsing response: ", err)
return 1
}
}
// Interactively ask for dungeon faction
if dungeon.FactionID == 0 {
input := StringPrompt("Faction ID: (Type L to list all available) ")
if input == "L" {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Faction ID", "Faction Name"})
table.SetColWidth(60)
listOutput := ListFactions()
for _, entry := range listOutput {
table.Append([]string{
strconv.Itoa(entry.ID),
entry.Name,
})
}
table.Render()
input = StringPrompt("Faction ID: ")
}
dungeon.FactionID, err = strconv.Atoi(input)
if err != nil {
log.Error("Error parsing response: ", err)
return 1
}
}
// Interactively ask for dungeon archetype
if dungeon.ArchetypeID == 0 {
input := StringPrompt("Archetype ID: (Type L to list all available) ")
if input == "L" {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Archetype ID", "Archetype Name"})
table.SetColWidth(60)
listOutput := ListArchetypes()
for _, entry := range listOutput {
table.Append([]string{
strconv.Itoa(entry.ID),
entry.Name,
})
}
table.Render()
input = StringPrompt("Archetype ID: ")
}
dungeon.ArchetypeID, err = strconv.Atoi(input)
if err != nil {
log.Error("Error parsing response: ", err)
return 1
}
}
// Generate a uuid for this dungeon
dungeon.DungeonUUID = uuid.New().String()
// Create or print the dungeon
if data, err := json.Marshal(dungeon); err != nil {
log.Error("Error marshalling new dungeon: ", err)
} else {
if dryrun {
fmt.Println(string(data))
return 0
} else {
ImportDungeon(data, false)
}
}
return 0
}
// List all factions
type DungeonFactionListCommand struct {
}
func (c *DungeonFactionListCommand) Help() string {
helpText := `
Usage: evedbtool dungeon list-factions ...
Lists all faction IDs along with their names.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonFactionListCommand) Synopsis() string {
return "Lists all faction IDs along with their names."
}
func (c *DungeonFactionListCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("list-factions", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Faction ID", "Faction Name"})
table.SetColWidth(60)
listOutput := ListFactions()
for _, entry := range listOutput {
table.Append([]string{
strconv.Itoa(entry.ID),
entry.Name,
})
}
table.Render()
return 0
}
// List all Archetypes
type DungeonArchetypeListCommand struct {
}
func (c *DungeonArchetypeListCommand) Help() string {
helpText := `
Usage: evedbtool dungeon list-archetypes ...
Lists all Archetype IDs along with their names.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonArchetypeListCommand) Synopsis() string {
return "Lists all Archetype IDs along with their names."
}
func (c *DungeonArchetypeListCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("list-archetypes", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Archetype ID", "Archetype Name"})
table.SetColWidth(60)
listOutput := ListArchetypes()
for _, entry := range listOutput {
table.Append([]string{
strconv.Itoa(entry.ID),
entry.Name,
})
}
table.Render()
return 0
}
// Delete a dungeon from the database
type DungeonDeleteCommand struct {
}
func (c *DungeonDeleteCommand) Help() string {
helpText := `
Usage: evedbtool dungeon delete [options] ...
Deletes a dungeon from the database.
Options:
-dungeon <int> ID of the dungeon to delete.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonDeleteCommand) Synopsis() string {
return "Deletes a dungeon from the database."
}
func (c *DungeonDeleteCommand) Run(args []string) int {
var dungeonID int
cmdFlags := flag.NewFlagSet("delete", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.IntVar(&dungeonID, "dungeon", 0, "ID of the dungeon to delete.")
if err := cmdFlags.Parse(args); err != nil {
log.Error("Error parsing arguments")
return 1
}
if dungeonID == 0 {
ui.Output(c.Help())
return 1
}
if DeleteDungeon(dungeonID) == 0 {
log.Info("Successfully deleted the dungeon.")
}
return 0
}
// Adds a room to the specified dungeon in the database.
type DungeonAddRoomCommand struct {
}
func (c *DungeonAddRoomCommand) Help() string {
helpText := `
Usage: evedbtool dungeon add-room [options] ...
Adds a room to the specified dungeon in the database.
Options:
-dungeon <int> ID of the dungeon for which to add the room.
-name <string> Name of the room to add.
-dryrun Don't apply change, just print the JSON output.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonAddRoomCommand) Synopsis() string {
return "Adds a room to the specified dungeon in the database."
}
func (c *DungeonAddRoomCommand) Run(args []string) int {
var dungeon Dungeon
var room Room
var dryrun bool
var dungeonID int
cmdFlags := flag.NewFlagSet("new", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.IntVar(&dungeonID, "dungeon", 0, "ID of the dungeon for which to add the room.")
cmdFlags.StringVar(&room.RoomName, "name", "", "Name of the room to add.")
cmdFlags.BoolVar(&dryrun, "dryrun", false, "Don't apply change, just print the JSON output.")
if err := cmdFlags.Parse(args); err != nil {
ui.Output(c.Help())
return 1
}
if dungeonID == 0 {
ui.Output(c.Help())
return 1
}
// Get dungeon from database
if err := json.Unmarshal([]byte(ExportDungeon(dungeonID)), &dungeon); err != nil {
log.Error("Error unmarshalling")
return 1
}
// Interactively ask for room name
if room.RoomName == "" {
room.RoomName = StringPrompt("Room Name: ")
}
// Add room to dungeon
dungeon.Rooms = append(dungeon.Rooms, room)
// Create or print the dungeon
if data, err := json.Marshal(dungeon); err != nil {
log.Error("Error marshalling new dungeon: ", err)
} else {
if dryrun {
fmt.Println("Updated dungeon: ")
fmt.Println(string(data))
return 0
} else {
if DeleteDungeon(dungeonID) == 0 {
ImportDungeon(data, false)
} else {
log.Error("Error deleting existing dungeon, not importing updated dungeon.")
}
}
}
return 0
}
// Removes the specified room from the database.
type DungeonRemoveRoomCommand struct {
}
func (c *DungeonRemoveRoomCommand) Help() string {
helpText := `
Usage: evedbtool dungeon remove-room [options] ...
Removes the specified room from a dungeon in the database.
Options:
-dungeon <int> ID of the dungeon for which to remove the room.
-room <int> ID of the room to remove.
-dryrun Don't apply change, just print the JSON output.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonRemoveRoomCommand) Synopsis() string {
return "Removes the specified room from a dungeon in the database."
}
func (c *DungeonRemoveRoomCommand) Run(args []string) int {
var dungeonID int
var roomID int
var dryrun bool
cmdFlags := flag.NewFlagSet("remove-room", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.IntVar(&dungeonID, "dungeon", 0, "ID of the dungeon for which to remove the room.")
cmdFlags.IntVar(&roomID, "room", 99, "ID of the room to remove.")
cmdFlags.BoolVar(&dryrun, "dryrun", false, "Don't apply change, just print the JSON output.")
if err := cmdFlags.Parse(args); err != nil {
log.Error("Error parsing arguments")
return 1
}
if dungeonID < 1 {
ui.Output(c.Help())
return 1
}
if roomID == 99 {
ui.Output(c.Help())
return 1
}
var dungeon Dungeon
if err := json.Unmarshal([]byte(ExportDungeon(dungeonID)), &dungeon); err != nil {
log.Error("Error unmarshalling")
return 1
}
// Update slice
dungeon.Rooms = append(dungeon.Rooms[:roomID], dungeon.Rooms[roomID+1:]...)
// Create or print the dungeon
if data, err := json.Marshal(dungeon); err != nil {
log.Error("Error marshalling new dungeon: ", err)
} else {
if dryrun {
fmt.Println("Updated dungeon: ")
fmt.Println(string(data))
return 0
} else {
if DeleteDungeon(dungeonID) == 0 {
ImportDungeon(data, false)
} else {
log.Error("Error deleting existing dungeon, not importing updated dungeon.")
}
}
}
return 0
}
// Lists all rooms in the specified dungeon.
type DungeonRoomListCommand struct {
}
func (c *DungeonRoomListCommand) Help() string {
helpText := `
Usage: evedbtool dungeon list-rooms ...
Lists all rooms in the specified dungeon.
Options:
-dungeon <int> ID of the dungeon for which to add the room.
`
return strings.TrimSpace(helpText)
}
func (c *DungeonRoomListCommand) Synopsis() string {
return "Lists all rooms in the specified dungeon."
}
func (c *DungeonRoomListCommand) Run(args []string) int {
var dungeonID int
cmdFlags := flag.NewFlagSet("list-rooms", flag.ContinueOnError)
cmdFlags.Usage = func() { ui.Output(c.Help()) }
cmdFlags.IntVar(&dungeonID, "dungeon", 0, "ID of the dungeon to export.")
if err := cmdFlags.Parse(args); err != nil {
log.Error("Error parsing arguments")
return 1
}
if dungeonID < 1 {
ui.Output(c.Help())
return 1
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Room ID", "Room Name"})
table.SetColWidth(60)
listOutput := ListRooms(dungeonID)
if len(listOutput) == 0 {
log.Error("Either dungeon does not exist, or has no rooms.")
return 1
}
for _, entry := range listOutput {
table.Append([]string{
strconv.Itoa(entry.ID),
entry.Name,
})
}
table.Render()
return 0
}