-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
1854 lines (1754 loc) · 58.2 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
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bufio"
"context"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
)
type Deck struct {
Name string
Colors string
Date_Entered time.Time
Favorite int
Max_Streak int
Cur_Streak int
Num_Cards int
Num_Lands int
Num_Creat int
Num_Spells int
Num_Enchant int
Num_Art int
Disable int
}
type Game struct {
Results int
Cause string
Deck string
Opponent string
Level string
CurrentStreak int
MaxStreak int
GameType string
}
type Cards struct {
Cards []struct {
Artist string `json:"artist"`
Availability []string `json:"availability"`
BorderColor string `json:"borderColor"`
ColorIdentity []string `json:"colorIdentity"`
Colors []string `json:"colors"`
ConvertedMana float64 `json:"convertedManaCost"`
FaceConvertedMana float64 `json:"faceConvertedManaCost"`
FaceManaValue float64 `json:"faceManaValue"`
Rank int `json:"edhrecRank"`
Finishes []string `json:"finishes"`
ForeignData []interface{} `json:"foreignData"`
FrameVersion string `json:"frameVersion"`
Foil bool `json:"hasFoil"`
NonFoil bool `json:"hasNonFoil"`
Identifiers struct {
McmID string `json:"mcmId"`
JSONID string `json:"mtgjsonV4Id"`
MultiverseID string `json:"multiverseId"`
ScryfallID string `json:"scryfallId"`
ScryFallPictureID string `json:"scryfallIllustrationId"`
ScryfallOracleID string `json:"scryfallOracleId"`
ProductID string `json:"tcgplayerProductId"`
} `json:"identifiers"`
Reprint bool `json:"isReprint"`
Keywords []string `json:"keywords"`
Layout string `json:"layout"`
Legalities struct {
Commander string `json:"commander"`
Duel string `json:"duel"`
Legacy string `json:"legacy"`
Oldschool string `json:"oldschool"`
Penny string `json:"penny"`
Premodern string `json:"premodern"`
Vintage string `json:"vintage"`
} `json:"legalities"`
ManaCost string `json:"manaCost"`
ManaValue float64 `json:"manaValue"`
Name string `json:"name"`
Number string `json:"number"`
OriginalText string `json:"originalText"`
OriginalType string `json:"originalType"`
Printings []string `json:"printings"`
PurchaseUrls struct {
Tcgplayer string `json:"tcgplayer"`
} `json:"purchaseUrls"`
Rarity string `json:"rarity"`
Rulings []struct {
Date string `json:"date"`
Text string `json:"text"`
} `json:"rulings"`
SetCode string `json:"setCode"`
Side string `json:"side"`
Subtypes []string `json:"subtypes"`
Supertypes []string `json:"supertypes"`
Text string `json:"text"`
Type string `json:"type"`
Types []string `json:"types"`
UUID string `json:"uuid"`
} `json:"cards"`
}
func main() {
menu()
}
func menu() {
//main menu
fmt.Println("mtga Stats")
m := make(map[string]string)
reader := bufio.NewReader(os.Stdin)
// Set key/value pairs using typical `name[key] = val`
m["k1"] = fmt.Sprintf("%-25s", "New Deck")
m["k2"] = fmt.Sprintf("%0s", "New Game")
m["k3"] = fmt.Sprintf("%-25s", "Deck Rankings")
m["k4"] = fmt.Sprintf("%0s", "View Game Count")
m["k5"] = fmt.Sprintf("%-25s", "View Decks")
m["k6"] = fmt.Sprintf("%0s", "Top Ten Decks")
m["k7"] = fmt.Sprintf("%-25s", "Deck Details")
m["k8"] = fmt.Sprintf("%0s", "Win Percent")
m["k9"] = fmt.Sprintf("%-25s", "Analysis")
m["k10"] = fmt.Sprintf("%0s", "Favorites")
m["k11"] = fmt.Sprintf("%-25s", "Import Set Data")
m["k12"] = fmt.Sprintf("%0s", "Quit")
// print menu options
fmt.Println(" 1:", m["k1"]+" 2:", m["k2"])
fmt.Println(" 3:", m["k3"]+" 4:", m["k4"])
fmt.Println(" 5:", m["k5"]+" 6:", m["k6"])
fmt.Println(" 7:", m["k7"]+" 8:", m["k8"])
fmt.Println(" 9:", m["k9"]+"10:", m["k10"])
fmt.Println("11:", m["k11"]+"12:", m["k12"])
in := bufio.NewScanner(os.Stdin)
in.Scan()
choice, _ := strconv.Atoi(in.Text())
switch choice {
case 1:
fmt.Println("Enter or Import Deck: ")
edchoice, _ := reader.ReadString('\n')
//edchoice = strings.TrimSuffix(strings.TrimSuffix(edchoice, "\n"), "\r")
edchoice = strings.TrimSpace(edchoice)
//validate user input
edchoice = validateuserinput(edchoice, "choice")
fmt.Println("Deck Name: ")
name, _ := reader.ReadString('\n')
//name = strings.TrimSuffix(strings.TrimSuffix(name, "\n"), "\r")
name = strings.TrimSpace(name)
fmt.Println("Deck Name: " + name)
fmt.Println("Multi-Colored Deck(y/n)")
multi, _ := reader.ReadString('\n')
multi = strings.TrimSuffix(strings.TrimSuffix(multi, "\n"), "\r")
//validate user input
multi = validateuserinput(multi, "confirm")
var color string
if multi == "y" {
fmt.Println("How many colors?")
num_col, _ := reader.ReadString('\n')
//num_col = strings.TrimSuffix(strings.TrimSuffix(num_col, "\r"), "\n")
num_col = strings.TrimSpace(num_col)
fmt.Println("You deck has " + num_col + " colors")
fmt.Println("what is your first color?(Black|White|Blue|Red|Green)")
cols, _ := reader.ReadString('\n')
//cols = strings.TrimSuffix(strings.TrimSuffix(cols, "\r"), "\n")
cols = strings.TrimSpace(cols)
//validate user input
cols = validateuserinput(cols, "colors")
count := 1
snum, _ := strconv.Atoi(num_col)
for count != snum {
count++
fmt.Println("Next Color(Black|White|Blue|Red|Green): ")
ncol, _ := reader.ReadString('\n')
//ncol = strings.TrimSuffix(strings.TrimSuffix(ncol, "\r"), "\n")
ncol = strings.TrimSpace(ncol)
validateuserinput(ncol, "colors")
cols = cols + "," + ncol
}
color = cols
} else if multi == "n" {
fmt.Println("What color is your deck?(Black|White|Blue|Red|Green)")
color, _ = reader.ReadString('\n')
//color = strings.TrimSuffix(strings.TrimSuffix(color, "\r"), "\n")
color = strings.TrimSpace(color)
validateuserinput(color, "colors")
}
fmt.Println("Favorite(y/n): ")
favorite, _ := reader.ReadString('\n')
//favorite = strings.TrimSuffix(strings.TrimSuffix(favorite, "\r"), "\n")
favorite = strings.TrimSpace(favorite)
//validate user input
validateuserinput(favorite, "confirm")
fmt.Println("Your new deck " + name + " is a favorite: " + favorite)
//convert string to int equivilant
favorite_bin := new(int)
if favorite == "y" {
*favorite_bin = 0
} else {
*favorite_bin = 1
}
if edchoice == "import" || edchoice == "Import" {
fmt.Println("Sync to Existing Deck(y/n)")
syncd, _ := reader.ReadString('\n')
//syncd = strings.TrimSuffix(strings.TrimSuffix(syncd, "\r"), "\n")
syncd = strings.TrimSpace(syncd)
fmt.Println("Import Option")
d := Deck{
Name: name,
Colors: color,
Date_Entered: time.Now(),
Favorite: int(*favorite_bin),
}
importdeck(d, syncd)
} else if edchoice == "enter" || edchoice == "Enter" {
fmt.Println("Total Number of cards: ")
numcards, _ := reader.ReadString('\n')
//numcards = strings.TrimSuffix(strings.TrimSuffix(numcards, "\r"), "\n")
numcards = strings.TrimSpace(numcards)
icards := new(int)
*icards, _ = strconv.Atoi(numcards)
fmt.Print("Total number of cards: " + numcards + "\n")
fmt.Println("Total number of instant/sorcery: ")
numspells, _ := reader.ReadString('\n')
//numspells = strings.TrimSuffix(strings.TrimSuffix(numspells, "\r"), "\n")
numspells = strings.TrimSpace(numspells)
ispells := new(int)
*ispells, _ = strconv.Atoi(numspells)
fmt.Print("Total number of instant/sorcery: " + numspells + "\n")
fmt.Println("Total number of creatures: ")
numcreatures, _ := reader.ReadString('\n')
//numcreatures = strings.TrimSuffix(strings.TrimSuffix(numcreatures, "\r"), "\n")
numcreatures = strings.TrimSpace(numcreatures)
icreatures := new(int)
*icreatures, _ = strconv.Atoi(numcreatures)
fmt.Print("Total number of creatures: " + numcreatures + "\n")
fmt.Println("Total number of lands: ")
numlands, _ := reader.ReadString('\n')
//numlands = strings.TrimSuffix(strings.TrimSuffix(numlands, "\r"), "\n")
numlands = strings.TrimSpace(numlands)
ilands := new(int)
*ilands, _ = strconv.Atoi((numlands))
fmt.Print("Total number of lands: " + numlands + "\n")
fmt.Println("Total number of enchantments: ")
numench, _ := reader.ReadString('\n')
//numlands = strings.TrimSuffix(strings.TrimSuffix(numlands, "\r"), "\n")
numench = strings.TrimSpace(numench)
iench := new(int)
*iench, _ = strconv.Atoi((numench))
fmt.Print("Total number of lands: " + numench + "\n")
fmt.Println("Total number of lands: ")
numarts, _ := reader.ReadString('\n')
//numlands = strings.TrimSuffix(strings.TrimSuffix(numlands, "\r"), "\n")
numarts = strings.TrimSpace(numarts)
iarts := new(int)
*iarts, _ = strconv.Atoi((numarts))
fmt.Print("Total number of lands: " + numarts + "\n")
//enter into database
//newdeck(name, color, favorite)
d := Deck{
Name: name,
Colors: color,
Date_Entered: time.Now(),
Favorite: int(*favorite_bin),
Num_Cards: int(*icards),
Num_Lands: int(*ilands),
Num_Spells: int(*ispells),
Num_Creat: int(*icreatures),
Num_Enchant: int(*iench),
Num_Art: int(*iarts),
}
err := newdeck(d)
if err != nil {
log.Printf("Insert deck failed with error %s", err)
return
}
}
case 2:
fmt.Println("Results(won/lost): ")
results, _ := reader.ReadString('\n')
//results = strings.TrimSuffix(strings.TrimSuffix(results, "\r"), "\n")
results = strings.TrimSpace(results)
//validate results input
results = validateuserinput(results, "results")
fmt.Print("You " + results + " this game.\n")
fmt.Println("Why do you think you " + results + " this game?")
cause, _ := reader.ReadString('\n')
//cause = strings.TrimSuffix(strings.TrimSuffix(cause, "\r"), "\n")
cause = strings.TrimSpace(cause)
fmt.Println("Cause: " + cause)
fmt.Println("Deck Name:")
deck, _ := reader.ReadString('\n')
//deck = strings.TrimSuffix(strings.TrimSuffix(deck, "\r"), "\n")
deck = strings.TrimSpace(deck)
//validate deck name
deck = validatedeck(deck, "n", "")
fmt.Println("You " + results + " your game using " + deck)
fmt.Println("Opponent Name:?")
opp, _ := reader.ReadString('\n')
//opp = strings.TrimSuffix(strings.TrimSuffix(opp, "\r"), "\n")
opp = strings.TrimSpace(opp)
fmt.Println("Opponent Name: " + opp)
fmt.Println("Game Level:(Bronze, Silver, Gold, Platinum, Diamond, and Mythic)")
lev, _ := reader.ReadString('\n')
//lev = strings.TrimSuffix(strings.TrimSuffix(lev, "\r"), "\n")
lev = strings.TrimSpace(lev)
//validate level input
lev = validateuserinput(lev, "level")
fmt.Println("Game Tier:(1-4)")
tier, _ := reader.ReadString('\n')
//tier = strings.TrimSuffix(strings.TrimSuffix(tier, "\r"), "\n")
tier = strings.TrimSpace(tier)
//validate tier input
tier = validateuserinput(tier, "tier")
fmt.Println("Game Level: " + lev + " Tier: " + tier)
cmblvl := lev + "-" + tier
//set game type
fmt.Println("Game Type:")
gametype := gametype()
fmt.Println("Game Type: " + gametype)
//convert string to int
results_bin := new(int)
if results == "won" {
*results_bin = 0
} else {
*results_bin = 1
}
//enter into database
g := Game{
Results: int(*results_bin),
Cause: cause,
Deck: deck,
Opponent: opp,
Level: cmblvl,
GameType: gametype,
}
err := newgame(g)
if err != nil {
log.Printf("Insert game failed with error %s", err)
return
}
case 3:
fmt.Println("Would you like to narrow your search?(y/n)")
deckchoice, _ := reader.ReadString('\n')
//deckchoice = strings.TrimSuffix(strings.TrimSuffix(deckchoice, "\r"), "\n")
deckchoice = strings.TrimSpace(deckchoice)
validateuserinput(deckchoice, "confirm")
rkchoice := historic()
if deckchoice == "y" {
fmt.Println("Deck Name: ")
deckname, _ := reader.ReadString('\n')
deckname = strings.TrimSpace(deckname)
drank(deckname, rkchoice)
} else {
drank("n", rkchoice)
}
case 4:
m := make(map[string]string)
reader := bufio.NewReader(os.Stdin)
// Set key/value pairs using typical `name[key] = val`
m["k1"] = fmt.Sprintf("%-25s", "All Decks")
m["k2"] = fmt.Sprintf("%0s", "Specify Deck")
m["k3"] = fmt.Sprintf("%-25s", "Most Games")
m["k4"] = fmt.Sprintf("%0s", "Least Games")
// print menu options
fmt.Println(" 1:", m["k1"]+" 2:", m["k2"])
fmt.Println(" 3:", m["k3"]+" 4:", m["k4"])
in := bufio.NewScanner(os.Stdin)
in.Scan()
choice, _ := strconv.Atoi(in.Text())
//include deleted decks
ctchoice := historic()
switch choice {
case 1:
gamecount("all", ctchoice)
case 2:
fmt.Println("Deck Name: ")
deckname, _ := reader.ReadString('\n')
//deckname = strings.TrimSuffix(strings.TrimSuffix(deckname, "\r"), "\n")
deckname = strings.TrimSpace(deckname)
gamecount(deckname, ctchoice)
case 3:
gamecount("high", ctchoice)
case 4:
gamecount("low", ctchoice)
}
case 5:
vchoice := historic()
viewdecks("n", 0, vchoice)
case 6:
topten()
case 7:
fmt.Println("View/Edit/Delete Deck: ")
edchoice, _ := reader.ReadString('\n')
edchoice = strings.TrimSuffix(strings.TrimSuffix(edchoice, "\r"), "\n")
edchoice = strings.TrimSpace(edchoice)
//validate user input
edchoice = validateuserinput(edchoice, "edit")
fmt.Println("Deck:")
deck, _ := reader.ReadString('\n')
//deck = strings.TrimSuffix(strings.TrimSuffix(deck, "\r"), "\n")
deck = strings.TrimSpace(deck)
if edchoice == "delete" || edchoice == "Delete" {
fmt.Println("Delete Deck: " + deck)
viewdecks(deck, 1, "n")
fmt.Println("Confirm(y/n): ")
confirm, _ := reader.ReadString('\n')
//confirm = strings.TrimSuffix(strings.TrimSuffix(confirm, "\r"), "\n")
confirm = strings.TrimSpace(confirm)
//validate confirmation entry
confirm = validateuserinput(confirm, "confirm")
if confirm == "y" || confirm == "Y" {
fmt.Println("Confirm Delete")
deletedeck(deck)
} else if confirm == "n" || confirm == "N" {
menu()
}
} else if edchoice == "edit" || edchoice == "Edit" {
editdeck(deck)
} else if edchoice == "view" || edchoice == "View" {
vchoice := historic()
viewdecks(deck, 0, vchoice)
}
case 8:
m := make(map[string]string)
reader := bufio.NewReader(os.Stdin)
// Set key/value pairs using typical `name[key] = val`
m["k1"] = fmt.Sprintf("%-25s", "All Decks")
m["k2"] = fmt.Sprintf("%0s", "Specify Deck")
m["k3"] = fmt.Sprintf("%-25s", "Highest Percentage")
m["k4"] = fmt.Sprintf("%0s", "Lowest Percentage")
// print menu options
fmt.Println(" 1:", m["k1"]+" 2:", m["k2"])
fmt.Println(" 3:", m["k3"]+" 4:", m["k4"])
in := bufio.NewScanner(os.Stdin)
in.Scan()
choice, _ := strconv.Atoi(in.Text())
switch choice {
case 1:
pctchoice := historic()
pctvals("all", pctchoice)
case 2:
fmt.Println("Deck Name: ")
deckname, _ := reader.ReadString('\n')
//deckname = strings.TrimSuffix(strings.TrimSuffix(deckname, "\r"), "\n")
deckname = strings.TrimSpace(deckname)
pctchoice := historic()
pctvals(deckname, pctchoice)
case 3:
pctchoice := historic()
pctvals("high", pctchoice)
case 4:
pctchoice := historic()
pctvals("low", pctchoice)
}
case 9:
anal_menu()
case 10:
favmenu()
case 11:
importset()
case 12:
os.Exit(0)
default:
//Reset Menu for invalid in put
fmt.Println("Invalid Selection.")
fmt.Println("")
menu()
}
}
func favmenu() {
fmt.Println("Favorites")
m := make(map[string]string)
// Set key/value pairs using typical `name[key] = val`
m["k1"] = fmt.Sprintf("%-25s", "List Favorites")
m["k2"] = fmt.Sprintf("%0s", "Reset Favorites")
m["k3"] = fmt.Sprintf("%-25s", "Assign Top Ten to Favorites")
m["k10"] = fmt.Sprintf("%-24s", "Return to Main Menu")
m["k11"] = fmt.Sprintf("%0s", "Quit")
fmt.Println(" 1:", m["k1"])
fmt.Println(" 2:", m["k2"])
fmt.Println(" 3:", m["k3"])
fmt.Println("10:", m["k10"])
fmt.Println("11:", m["k11"])
in := bufio.NewScanner(os.Stdin)
in.Scan()
choice, _ := strconv.Atoi(in.Text())
switch choice {
case 1:
favs("list", "")
case 2:
favs("reset", "")
case 3:
favs("assign", "")
case 10:
main()
case 11:
os.Exit(0)
}
}
func gametype() (typereturn string) {
m := make(map[string]string)
// Set key/value pairs using typical `name[key] = val`
m["k1"] = fmt.Sprintf("%-30s", "Play")
m["k2"] = fmt.Sprintf("%0s", "Brawl")
m["k3"] = fmt.Sprintf("%-30s", "Standard Ranked")
m["k4"] = fmt.Sprintf("%0s", "Traditional Standard Play")
m["k5"] = fmt.Sprintf("%-30s", "Traditional Standard Ranked")
m["k6"] = fmt.Sprintf("%0s", "Traditional Historic Ranked")
m["k7"] = fmt.Sprintf("%-30s", "Historic Ranked")
m["k8"] = fmt.Sprintf("%0s", "Historic Brawl")
m["k9"] = fmt.Sprintf("%-30s", "Bot")
m["k10"] = fmt.Sprintf("%0s", "Event")
fmt.Println("1:", m["k1"], " 2:", m["k2"])
fmt.Println("3:", m["k3"], " 4:", m["k4"])
fmt.Println("5:", m["k5"], " 6:", m["k6"])
fmt.Println("7:", m["k7"], " 8:", m["k8"])
fmt.Println("9:", m["k9"], "10:", m["k10"])
in := bufio.NewScanner(os.Stdin)
in.Scan()
choice, _ := strconv.Atoi(in.Text())
switch choice {
case 1:
typereturn = "Play"
case 2:
typereturn = "Brawl"
case 3:
typereturn = "Standard Ranked"
case 4:
typereturn = "Traditional Standard Play"
case 5:
typereturn = "Traditional Standard Ranked"
case 6:
typereturn = "Traditional Historic Ranked"
case 7:
typereturn = "Historic Ranked"
case 8:
typereturn = "Historic Brawl"
case 9:
typereturn = "Bot"
case 10:
fmt.Println("Event Name:")
in.Scan()
typereturn = in.Text()
typereturn = strings.TrimSpace(typereturn)
default:
println("Invalid Entry")
gametype()
}
return typereturn
}
func opendb() *sql.DB {
db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/mtga?parseTime=true")
// if there is an error opening the connection, handle it
if err != nil {
panic(err.Error())
}
return db
}
func newdeck(d Deck) error {
// Open up our database connection.
db := opendb()
// defer the close till after the main function has finished
// executing
defer db.Close()
d.Name = strings.TrimSpace(d.Name)
// perform a db.Query insert
query := "INSERT INTO mtga.decks(name, colors, favorite, numcards, numlands, numspells, numcreatures, numenchant, numartifacts) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?)"
ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelfunc()
stmt, err := db.PrepareContext(ctx, query)
if err != nil {
log.Printf("Error %s when preparing SQL statement", err)
panic(err.Error())
}
defer stmt.Close()
res, err := stmt.ExecContext(ctx, d.Name, d.Colors, d.Favorite, d.Num_Cards, d.Num_Lands, d.Num_Spells, d.Num_Creat, d.Num_Enchant, d.Num_Art)
if err != nil {
log.Printf("Error %s when inserting row into deck table", err)
panic(err.Error())
}
rows, err := res.RowsAffected()
if err != nil {
log.Printf("Error %s when finding rows affected", err)
panic(err.Error())
}
log.Printf("%d deck created ", rows)
fmt.Println("")
menu()
return nil
}
func newgame(g Game) error {
// Open up our database connection.
db := opendb()
// defer the close till after the main function has finished
// executing
defer db.Close()
// perform a db.Query insert
query := "INSERT INTO mtga.games(results, cause, deck, opponent, level, game_type) VALUES (?,?,?,?,?,?)"
ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelfunc()
stmt, err := db.PrepareContext(ctx, query)
if err != nil {
log.Printf("Error %s when preparing SQL statement", err)
panic(err.Error())
}
defer stmt.Close()
res, err := stmt.ExecContext(ctx, g.Results, g.Cause, g.Deck, g.Opponent, g.Level, g.GameType)
if err != nil {
log.Printf("Error %s when inserting row into deck table", err)
panic(err.Error())
}
rows, err := res.RowsAffected()
if err != nil {
log.Printf("Error %s when finding rows affected", err)
panic(err.Error())
}
log.Printf("%d row added ", rows)
//determin max and current streak
streaks(g.Deck)
fmt.Println("")
menu()
return nil
}
func drank(DeckName string, h string) error {
// Open up our database connection.
db := opendb()
// defer the close till after the main function has finished
// executing
defer db.Close()
var (
deckname string
wins int
loses int
ranking float32
rkquery string
rkquery_all string
)
if h == "y" {
rkquery = "SELECT deck, ranking, wins, loses FROM mtga.rankings WHERE deck=?"
rkquery_all = "SELECT deck, ranking, wins, loses FROM mtga.rankings"
} else {
rkquery = "SELECT deck, ranking, wins, loses FROM mtga.rankings WHERE deleted IS NULL AND deck=?"
rkquery_all = "SELECT deck, ranking, wins, loses FROM mtga.rankings WHERE deleted IS NULL"
}
if DeckName != "n" {
DeckName = strings.TrimSuffix(strings.TrimSuffix(DeckName, "\r"), "\n")
DeckName = validatedeck(DeckName, h, "")
// Execute the query
results := db.QueryRow(rkquery, DeckName)
err := results.Scan(&deckname, &ranking, &wins, &loses)
if err != nil {
if strings.Contains(err.Error(), "no rows in result set") {
fmt.Println("No Games Recored for this Deck")
fmt.Println("")
menu()
} else {
panic(err.Error())
}
}
deckname = fmt.Sprintf("%-25s", deckname)
frank := fmt.Sprintf("%f", ranking)
frank = frank[2:6]
frank = fmt.Sprintf("%-20s", "Ranking: "+frank)
fwins := fmt.Sprintf("%-10s", "Wins: "+strconv.Itoa(wins))
floses := fmt.Sprintf("%-5s", "Loses: "+strconv.Itoa(loses))
finalrecord := fmt.Sprint(frank + deckname + fwins + floses)
log.Println(finalrecord)
fmt.Println("")
} else {
results, err := db.Query(rkquery_all)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
for results.Next() {
//var records Records
// for each row, scan the result into our deck composite object
err = results.Scan(&deckname, &ranking, &wins, &loses)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// and then print out the tag's Name attribute
log.SetFlags(0)
deckname = fmt.Sprintf("%-25s", deckname)
frank := fmt.Sprintf("%f", ranking)
frank = frank[2:6]
frank = fmt.Sprintf("%-20s", "Ranking: "+frank)
fwins := fmt.Sprintf("%-10s", "Wins: "+strconv.Itoa(wins))
floses := fmt.Sprintf("%-5s", "Loses: "+strconv.Itoa(loses))
finalrecord := fmt.Sprint(frank + deckname + fwins + floses)
log.Println(finalrecord)
}
}
fmt.Println("")
menu()
return nil
}
func gamecount(d string, h string) {
db := opendb()
// executing
defer db.Close()
var (
deckname string
count int
ctquery string
rsquery string
)
switch d {
case "all":
if h == "y" {
ctquery = "SELECT deck, results AS Count FROM mtga.game_count ORDER BY results DESC"
} else {
ctquery = "SELECT deck, results AS Count FROM mtga.game_count WHERE deleted IS NULL ORDER BY results DESC"
}
results, err := db.Query(ctquery)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
for results.Next() {
err = results.Scan(&deckname, &count)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// and then print out the tag's Name attribute
log.SetFlags(0)
fdeck := fmt.Sprintf("%-30s", deckname)
finalcount := fmt.Sprint(fdeck + " Game Count: " + strconv.Itoa(count))
log.Println(finalcount)
}
fmt.Println("")
menu()
case "high":
if h == "y" {
ctquery = "SELECT MAX(results) FROM mtga.game_count"
} else {
ctquery = "SELECT MAX(results) FROM mtga.game_count WHERE deleted IS NULL"
}
hresult := db.QueryRow(ctquery)
err := hresult.Scan(&count)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
results, err := db.Query("SELECT deck, results AS Count FROM mtga.game_count WHERE results =?", count)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
for results.Next() {
err = results.Scan(&deckname, &count)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// and then print out the tag's Name attribute
log.SetFlags(0)
fdeck := fmt.Sprintf("%-30s", deckname)
finalcount := fmt.Sprint(fdeck + " Game Count: " + strconv.Itoa(count))
log.Println(finalcount)
}
fmt.Println("")
menu()
case "low":
if h == "y" {
ctquery = "SELECT MIN(results) FROM mtga.game_count"
rsquery = "SELECT deck, results AS Count FROM mtga.game_count WHERE results =?"
} else {
ctquery = "SELECT MIN(results) FROM mtga.game_count WHERE deleted IS NULL"
rsquery = "SELECT deck, results AS Count FROM mtga.game_count WHERE results =? AND deleted IS NULL"
}
lresult := db.QueryRow(ctquery)
err := lresult.Scan(&count)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
results, err := db.Query(rsquery, count)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
for results.Next() {
err = results.Scan(&deckname, &count)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// and then print out the tag's Name attribute
log.SetFlags(0)
fdeck := fmt.Sprintf("%-30s", deckname)
finalcount := fmt.Sprint(fdeck + " Game Count: " + strconv.Itoa(count))
log.Println(finalcount)
}
fmt.Println("")
menu()
default:
d = validatedeck(d, h, "")
results := db.QueryRow("SELECT deck, results AS Count FROM mtga.game_count WHERE deck=?", d)
err := results.Scan(&deckname, &count)
if err != nil {
//panic(err.Error())
println("No Games Recorded for this deck.")
}
finalcount := fmt.Sprint(deckname + " Game Count: " + strconv.Itoa(count))
log.Println(finalcount)
fmt.Println("")
menu()
}
}
func viewdecks(DeckName string, edit int, h string) (ret string) {
// Open up our database connection.
db := opendb()
defer db.Close()
var vquery string
var vquery_all string
if h == "y" {
vquery = "SELECT name, colors, date_entered, favorite, max_streak, cur_streak, numcards, numlands, numspells, numcreatures, numenchant, numartifacts FROM mtga.decks_all WHERE name=?"
vquery_all = "SELECT name, colors, date_entered, favorite, max_streak FROM mtga.decks_all ORDER BY name"
} else {
vquery = "SELECT name, colors, date_entered, favorite, max_streak, cur_streak, numcards, numlands, numspells, numcreatures, numenchant, numartifacts FROM mtga.decks WHERE name=?"
vquery_all = "SELECT name, colors, date_entered, favorite, max_streak FROM mtga.decks ORDER BY favorite"
}
if DeckName != "n" {
var d Deck
DeckName = strings.TrimSuffix(strings.TrimSuffix(DeckName, "\r"), "\n")
//validate deck name
DeckName = validatedeck(DeckName, h, "")
results := db.QueryRow(vquery, DeckName)
err := results.Scan(&d.Name, &d.Colors, &d.Date_Entered, &d.Favorite, &d.Max_Streak, &d.Cur_Streak,
&d.Num_Cards, &d.Num_Lands, &d.Num_Spells, &d.Num_Creat, &d.Num_Enchant, &d.Num_Art)
if err != nil {
panic(err.Error())
}
m := make(map[string]string)
// Set key/value pairs using typical `name[key] = val`
m["k1"] = fmt.Sprintf("%-30s", d.Name)
m["k2"] = fmt.Sprintf("%-20s", d.Colors)
m["k3"] = fmt.Sprintf("%-25s", d.Date_Entered.Format("01-02-2006"))
m["k4"] = fmt.Sprintf("%-15s", strconv.Itoa(d.Favorite))
m["k5"] = fmt.Sprintf("%-24s", strconv.Itoa(d.Max_Streak))
m["k6"] = fmt.Sprintf("%-11s", strconv.Itoa(d.Cur_Streak))
m["k7"] = fmt.Sprintf("%-23s", strconv.Itoa(d.Num_Cards))
m["k8"] = fmt.Sprintf("%-14s", strconv.Itoa(d.Num_Lands))
m["k9"] = fmt.Sprintf("%-35s", strconv.Itoa(d.Num_Spells))
m["k10"] = fmt.Sprintf("%-7s", strconv.Itoa(d.Num_Enchant))
m["k11"] = fmt.Sprintf("%-23s", strconv.Itoa(d.Num_Art))
m["k12"] = fmt.Sprintf("%-19s", strconv.Itoa(d.Num_Creat))
ffav := d.Favorite
var sfav string
if ffav == 0 {
sfav = "Yes"
} else {
sfav = "No"
}
// print deck details
fmt.Println("Name:", m["k1"]+"Color:", m["k2"]+"Date Entered:", m["k3"]+"Favorite:", sfav)
fmt.Println("Total Cards:", m["k7"]+"Total Lands:", m["k8"]+"Total Instant/Sorcery:", m["k9"])
fmt.Println("Total Creatures:", m["k12"]+"Total Enchantments:", m["k10"]+"Total Artifacts:", m["k11"])
fmt.Println("Max Streak:", m["k5"]+"Current Streak:", m["k6"])
ret = d.Name
} else {
results, err := db.Query(vquery_all)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
var count int
for results.Next() {
var deck Deck
count++
// for each row, scan the result into our deck composite object
err = results.Scan(&deck.Name, &deck.Colors, &deck.Date_Entered, &deck.Favorite, &deck.Max_Streak)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// and then print out the tag's Name attribute
log.SetFlags(0)
mstreak := deck.Max_Streak
var fav string
if deck.Favorite == 0 {
fav = fmt.Sprintf("%-4s", "Yes")
} else {
fav = fmt.Sprintf("%-4s", "No")
}
//format strings to be more readable
fcount := fmt.Sprintf("%2s: ", strconv.Itoa(count))
deck.Name = fmt.Sprintf("%-25s", deck.Name)
deck.Colors = fmt.Sprintf("%-15s", deck.Colors)
fdate := fmt.Sprintf("%-15s", deck.Date_Entered.Format("2006-01-02"))
fmstreak := fmt.Sprintf("%-4s", strconv.Itoa(mstreak))
finalrecord := fmt.Sprint(fcount + deck.Name + " Colors: " + deck.Colors + " Date Entered: " + fdate +
" Favorite: " + fav + " Max Streak: " + fmstreak)
log.Println(finalrecord)
}
}
if edit == 0 {
fmt.Println("")
menu()
}
return
}
func topten() {
// Open up our database connection.
db := opendb()
defer db.Close()
results, err := db.Query("SELECT deck, ranking, wins, loses FROM mtga.topten")
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
var num int
for results.Next() {
var (
name string
wins int
loses int
ranking float64
)
num++
// for each row, scan the result into our deck composite object
err = results.Scan(&name, &ranking, &wins, &loses)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
frank := fmt.Sprintf("%f", ranking)
frank = frank[2:6]
if ranking == 1 {
frank = "100"
}
log.SetFlags(0)
//format strings to be more readable
name = fmt.Sprintf("%-25s", name)
frank = fmt.Sprintf("%-10s", frank)
fwins := fmt.Sprintf("%-5s", strconv.Itoa(wins))
floses := fmt.Sprintf("%-5s", strconv.Itoa(loses))
fnum := fmt.Sprintf("%1s", strconv.Itoa(num))
if num != 10 {
fnum = fmt.Sprintf("%2s", strconv.Itoa(num))
}
finalrecord := fmt.Sprint(fnum + ": " + name + "Ranking: " + frank + " Wins: " + fwins + " Loses: " + floses)
log.Println(finalrecord)
}
fmt.Println("")
menu()
}
func editdeck(d string) {
// Open up our database connection.
db := opendb()
defer db.Close()
//show current deck attributes
d = viewdecks(d, 1, "n")
//create new deck structure variable
var deck Deck
d = strings.TrimSuffix(strings.TrimSuffix(d, "\r"), "\n")
results := db.QueryRow("SELECT name, colors, date_entered, favorite, max_streak, cur_streak, numcards, numlands, numspells, numcreatures, numenchant, numartifacts, disable FROM mtga.decks WHERE name=?", d)
err := results.Scan(&deck.Name, &deck.Colors, &deck.Date_Entered, &deck.Favorite, &deck.Max_Streak, &deck.Cur_Streak,
&deck.Num_Cards, &deck.Num_Lands, &deck.Num_Spells, &deck.Num_Creat, &deck.Num_Enchant, &deck.Num_Art, &deck.Disable)
if err != nil {
panic(err.Error())
}