-
Notifications
You must be signed in to change notification settings - Fork 0
/
getset.Rmd
1694 lines (1591 loc) · 50.7 KB
/
getset.Rmd
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
---
title: "R Notebook"
output: html_notebook
---
```{r, echo=FALSE}
library(odbc)
library(DBI)
library(data.table)
library(jsonlite)
library(tidyverse)
library(stringr)
library(tictoc) #benchmarking
```
```{r}
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 17 for SQL Server",
Server = "localhost\\DAWNFIRE",
Database = "deckdater",
Trusted_Connection = "Yes")
```
```{r}
tic("load in sets")
tic("local setup")
setwd("C:/Users/cyrus/GitHere/deckdater")
options(timeout = 6000)
filepath <- paste0(getwd(), "/", "upd8_sets")
filename <- paste0("sets_", Sys.Date(), ".json")
toc()
tic("download file")
download.file("https://api.scryfall.com/sets/", paste0(filepath, "/", filename))
upd8sets <- fromJSON(paste0(filepath, "/", filename))
toc()
tic("file as datatable")
news <- as.data.table(upd8sets)
toc()
toc()
```
```{r}
tic("clean up columns")
tic("drop importy blob")
rm(upd8sets)
toc()
tic("change column names")
colnames(news) <- sub("data.", "", colnames(news))
toc()
toc()
```
```{r}
tic("remove columns")
news <- subset(news, select = c(id, code, name, uri, scryfall_uri, released_at, set_type, card_count, digital, block_code, block))
toc()
```
```{r}
tic("remove sets with illegal set codes")
news <- news[nchar(news$code) == 3]
toc()
```
```{r}
news[set_type == "spellbook"]
```
```{r}
unique(news$set_type)
dropSets <- c("masters", "masterpiece", "from_the_vault", "spellbook", "arsenal", "duel_deck", "token", "treasure_chest", "memorabilia", "premium_deck")
```
```{r}
tic("drop sets without unique cards")
tic("establish")
dropSets <- c("masters", "masterpiece", "from_the_vault", "spellbook", "arsenal", "duel_deck", "token", "treasure_chest", "memorabilia", "premium_deck")
toc()
tic("drop")
news <- news[!(set_type %in% dropSets)]
rm(dropSets)
toc()
toc()
```
```{r}
#allowances for my booleans
yesseR <- function(vec) {
v <- as.data.table(as.character(vec))
vec[vec == "TRUE"] <- "Y"
vec[vec == "FALSE"] <- "N"
vec
}
tic("replace True/False with Y/N")
news$digital <- yesseR(news$digital)
toc()
```
```{r}
sapply(news, class)
setnames(news, c("id", "uri", "scryfall_uri"), c("set_id", "scryfall_api", "scryfall_uri"))
setnames(news, "code", "set_code")
setnames(news, "name", "set_name")
setnames(news, "block", "block_name")
```
```{sql, connection=con}
create database deckdater
```
```{sql, connection=con}
drop table SCRY_CANON_SETS
```
```{r}
quotate <- function(dt) {
hold <- function(y) {
if (!is.na(y)) {
z <- paste0('"', as.character(y), '"')
z
} else {
z <- ""
}
}
enclose <- function(x) {
a <- lapply(x, hold)
a
}
for (i in seq_len(ncol(dt))) {
v <- dt[i, ]
q <- lapply(v, enclose)
dt[i, ] <- q
}
}
```
```{r}
rm(pls)
```
```{r}
pls <- news$block_code
t <- function(x) {
y <- fifelse(is.na(x), as.character(NA), paste0('"', x, '"'))
}
pls2 <- sapply(pls, t)
z <- function(x) {
q <- sapply(x, t)
q
}
choop <- news
cheep <- as.data.table(z(choop))
```
```{sql, connection=con}
create table SCRY_CANON_SETS (
CanonSetRowAdd int Identity(1,1) primary key NOT NULL,
set_id varchar(40) NOT NULL,
set_code varchar(5) NULL,
set_name varchar(255) NULL,
scryfall_api varchar(500) NULL,
scryfall_uri varchar(500) NULL,
released_at varchar(25) NULL,
set_type varchar(25) NULL,
card_count varchar(10) NULL,
digital varchar(3) NULL,
block_code varchar(5) NULL,
block_name varchar(255) NULL)
```
## set def tables
```{sql, connection=con}
create table /* IF NOT EXISTS */ defSET_TYPE (
SetTypeID int Identity(1,1) primary key NOT NULL,
SetTypeName varchar(25) unique NOT NULL,
SetTypeDesc varchar(200) NULL)
create table /* IF NOT EXISTS */ defSET_STATUS (
SetStatusID int Identity(1,1) primary key NOT NULL,
SetStatusName varchar(25) unique NOT NULL,
SetStatusDesc varchar(200) NULL)
```
## block table
```{sql, connection=con}
create table /* IF NOT EXISTS */ tblBLOCK (
BlockID int Identity(1,1) primary key NOT NULL,
BlockCode char(3) unique NOT NULL,
BlockName varchar(100) unique NOT NULL,
BlockScryfallURI varchar(255) NULL,
BlockScryfallAPI varchar(255) NULL)
```
## set table
```{sql, connection=con}
create table /* IF NOT EXISTS */ tblSET (
SetID varchar(36) primary key NOT NULL,
SetCode char(3) NOT NULL,
SetName varchar(100) NOT NULL,
SetReleaseDate date,
SetScryfallURI varchar(255) NULL,
SetScryfallAPI varchar(255) NULL,
SetTypeID int FOREIGN KEY references defSET_TYPE NOT NULL,
BlockID int FOREIGN KEY references tblBLOCK NULL,
IsDigital char(1) NULL,
CONSTRAINT sets_in_one_place_at_once UNIQUE(SetCode, IsDigital))
```
## blocks
```{sql, connection=con}
create OR alter function lookup_BlockIDbyCode(@Code char(3))
returns INT
as BEGIN
declare @RET int
set @RET = (select BlockID from tblBLOCK where BlockCode = @Code)
return @RET
END
```
### block lex procs
```{sql, connection=con}
create OR alter proc LEX_newBlockID
@BlockCode char(3),
@BlockID int OUT
as BEGIN
insert into tblBLOCK(BlockCode)
VALUES (@BlockCode)
set @BlockID = dbo.lookup_BlockIDbyCode(@BlockCode)
END
```
```{sql, connection=con}
create OR alter proc LEX_addBlockName
@BlockID int,
@BlockName varchar(100)
as BEGIN
if @BlockName != (select BlockName from tblBLOCK where BlockID = @BlockID)
BEGIN
update tblBLOCK
set BlockName = @BlockName
where BlockID = @BlockID
END
END
```
### block dict proc
```{sql, connection=con}
create OR alter proc DICT_addNewBlock
@LetBlockCode char(3),
@LetBlockName varchar(100),
@VarBlockID int OUT
as BEGIN
exec dbo.LEX_newBlockID
@BlockCode = @LetBlockCode,
@BlockID = @VarBlockID OUT
exec dbo.LEX_addBlockName
@BlockID = @VarBlockID,
@BlockName = @LetBlockName
END
```
### wrangle blocks
```{sql, connection=con}
create OR alter proc FETCH_NewOrExistingBlock
@ConstBlockCode char(3),
@ConstBlockName varchar(200) NULL,
@IsBlockID int OUT
as BEGIN
set @IsBlockID = dbo.lookup_BlockIDbyCode(@ConstBlockCode)
if @IsBlockID IS NULL
BEGIN
exec dbo.DICT_addNewBlock
@LetBlockCode = @ConstBlockCode,
@LetBlockName = @ConstBlockName,
@VarBlockID = @IsBlockID OUT
END
if @ConstBlockName IS NOT NULL
BEGIN
exec dbo.LEX_addBlockName
@BlockID = @IsBlockID,
@BlockName = @ConstBlockName
END
set @IsBlockID = dbo.lookup_BlockIDbyCode(@ConstBlockCode)
END
```
## add the blocks
```{r}
blocks <- unique(subset(news, select = c(block_code, block_name)))
blocks <- blocks[!is.na(blocks$block_code)]
setnames(blocks, c("block_code", "block_name"), c("BlockCode", "BlockName"))
dbAppendTable(con, "tblBLOCK", blocks)
```
```{sql, connection=con}
select * from defSET_TYPE
```
## add types
```{sql, connection=con}
create OR alter proc LOAD_intoSetTypes
@IsSetTypeName varchar(25)
as BEGIN
declare @ignore int
if not exists (select SetTypeID from defSET_TYPE where SetTypeName = @IsSetTypeName)
BEGIN
exec dbo.AUTO_CheckIfNewSetType
@SetTypeName = @IsSetTypeName,
@GotSetTypeID = @ignore OUT
END
END
```
```{r}
types <- unique(news$set_type)
types <- types[!is.na(types)]
for (i in seq_len(length(types))) {
dbExecute(con, paste0("exec dbo.LOAD_intoSetTypes @IsSetTypeName = '", types[[i]], "'"))
}
rm(i)
```
```{sql, connection=con}
select * from defSET_TYPE
```
## lookups
### look up set type ID
```{sql, connection=con}
create OR alter function lookup_SetTypeIDbyName(@VarSetTypeName varchar)
RETURNS int
as BEGIN
declare @SetTypeID int
set @SetTypeID = (select SetTypeID
from defSET_TYPE
where SetTypeName = @VarSetTypeName)
RETURN @SetTypeID
END
```
## set types
### lex: add new set type
```{sql, connection=con}
create OR alter proc LEX_newSetTypeName
@SetTypeName varchar(25)
as BEGIN
insert into defSET_TYPE(SetTypeName)
VALUES(@SetTypeName)
END
```
### lex: add set description
```{sql, connection=con}
create OR alter proc LEX_addSetTypeDesc
@NewSetTypeDesc varchar(200),
@SetTypeID int
as BEGIN
update defSET_TYPE
set SetTypeDesc = @NewSetTypeDesc
where SetTypeID = @SetTypeID
END
```
### dict new set type
```{sql, connection=con}
create OR alter proc DICT_newSetType
@LetSetTypeName varchar(25),
@LetSetTypeDesc varchar(200) NULL,
@VarSetTypeID int OUT
as BEGIN
set @VarSetTypeID = dbo.lookup_SetTypeIDbyName(@LetSetTypeName)
if @VarSetTypeID is NULL
BEGIN
exec dbo.LEX_newSetTypeName
@SetTypeName = @LetSetTypeName
set @VarSetTypeID = dbo.lookup_SetTypeIDbyName(@LetSetTypeName)
END
if @LetSetTypeDesc IS NOT NULL
BEGIN
exec dbo.LEX_addSetTypeDesc
@SetTypeID = @VarSetTypeID,
@NewSetTypeDesc = @LetSetTypeDesc
END
END
```
### automated set type handling
```{sql, connection=con}
create OR alter proc AUTO_CheckIfNewSetType
@SetTypeName varchar(25),
@GotSetTypeID int OUT
as BEGIN
set @GotSetTypeID = dbo.lookup_setTypeIDbyName(@SetTypeName)
if @GotSetTypeID IS NULL
BEGIN
exec dbo.DICT_newSetType
@LetSetTypeName = @SetTypeName,
@LetSetTypeDesc = 'FLAG: This set type was added automatically. Have a human review when possible.',
@VarSetTypeID = @GotSetTypeID OUT
set @GotSetTypeID = dbo.lookup_setTypeIDbyName(@SetTypeName)
END
END
```
## update info about a set
```{sql, connection=con}
create OR alter proc UPD8_CanonicalSet
@WithSetID varchar(36),
@AsSetCode char(3) NULL,
@AsSetName varchar(100) NULL,
@AsSetReleaseDate date NULL,
@AsSetTypeID int NULL,
@AsSetScryfallURI varchar(255) NULL,
@AsSetScryfallAPI varchar(255) NULL,
@AsBlockID int NULL,
@AsSetIsDigital char(1) NULL
as BEGIN
if @AsSetCode IS NOT NULL
update tblSET
set SetCode = @AsSetCode
where SetID = @WithSetID
if @AsSetName IS NOT NULL
update tblSET
set SetName = @AsSetName
where SetID = @WithSetID
if @AsSetReleaseDate IS NOT NULL
update tblSET
set SetReleaseDate = @AsSetReleaseDate
where SetID = @WithSetID
if @AsSetTypeID IS NOT NULL
update tblSET
set SetTypeID = @AsSetTypeID
where SetID = @WithSetID
if @AsSetScryfallURI IS NOT NULL
update tblSET
set SetScryfallURI = @AsSetScryfallURI
where SetID = @WithSetID
if @AsSetScryfallAPI IS NOT NULL
update tblSET
set SetScryfallAPI = @AsSetScryfallAPI
where SetID = @WithSetID
if @AsBlockID IS NOT NULL
update tblSET
set BlockID = @AsBlockID
where SetID = @WithSetID
if @AsSetIsDigital IS NOT NULL
update tblSET
set IsDigital = @AsSetIsDigital
where SetID = @WithSetID
END
```
### add a new set
```{sql, connection=con}
create OR alter proc ADD_CanonicalSet
@WithSetID varchar(36),
@AsSetCode char(3),
@AsSetName varchar(100),
@AsSetReleaseDate date,
@AsSetTypeID int,
@AsSetScryfallURI varchar(255) NULL,
@AsSetScryfallAPI varchar(255) NULL,
@AsBlockID int NULL,
@AsSetIsDigital char(1)
as BEGIN
insert into tblSET(SetID, SetCode, SetName, SetReleaseDate, SetTypeID, SetScryfallURI, SetScryfallAPI, BlockID, IsDigital)
VALUES(@WithSetID, @AsSetCode, @AsSetName, @AsSetReleaseDate, @AsSetTypeID, @AsSetScryfallAPI, @AsSetScryfallURI, @AsBlockID, @AsSetIsDigital)
END
```
### sets loader
```{sql, connection=con}
create OR alter proc LOAD_intoSets
@ConstSetID varchar(36),
@ConstSetCode char(3),
@ConstSetName varchar(100),
@ConstSetReleaseDate date,
@ConstSetTypeID int,
@ConstSetScryfallURI varchar(255),
@ConstSetScryfallAPI varchar(255),
@ConstBlockID int,
@ConstSetIsDigital char(1)
as BEGIN
if exists (select SetID from tblSET where SetID = @ConstSetID)
BEGIN
exec dbo.UPD8_CanonicalSet
@WithSetID = @ConstSetID,
@AsSetCode = @ConstSetCode,
@AsSetName = @ConstSetName,
@AsSetReleaseDate = @ConstSetReleaseDate,
@AsSetTypeID = @ConstSetTypeID,
@AsBlockID = @ConstBlockID,
@AsSetScryfallURI = @ConstSetScryfallURI,
@AsSetScryfallAPI = @ConstSetScryfallAPI,
@AsSetIsDigital = @ConstSetIsDigital
END
ELSE
BEGIN
exec dbo.ADD_CanonicalSet
@WithSetID = @ConstSetID,
@AsSetCode = @ConstSetCode,
@AsSetName = @ConstSetName,
@AsSetReleaseDate = @ConstSetReleaseDate,
@AsSetTypeID = @ConstSetTypeID,
@AsBlockID = @ConstBlockID,
@AsSetScryfallURI = @ConstSetScryfallURI,
@AsSetScryfallAPI = @ConstSetScryfallAPI,
@AsSetIsDigital = @ConstSetIsDigital
END
END
```
## trigger
```{sql, connection=con}
create OR alter TRIGGER t_CanonizeSet on SCRY_CANON_SETS
AFTER insert
as BEGIN
set NOCOUNT ON
declare @SetID varchar(36),
@SetCode char(3),
@SetName varchar(100),
@SetReleaseDate varchar(20),
@SetScryfallURI varchar(255),
@SetScryfallAPI varchar(255),
@SetTypeName varchar(25),
@SetBlockCode char(3),
@SetBlockName varchar(100),
@SetIsDigital char(1),
@SetBlockID int,
@CanonPK int,
@SetTypeID int
set @CanonPK = (select TOP 1 CanonSetRowAdd from inserted order by CanonSetRowAdd)
select @SetID = replace(set_id, '"', ''),
@SetCode = Cast(replace(set_code, '"', '') as char(3)),
@SetName = replace(set_name, '"', ''),
@SetReleaseDate = Cast(replace(released_at, '"', '') as date),
@SetScryfallAPI = replace(scryfall_api, '"', ''),
@SetScryfallURI = replace(scryfall_uri, '"', ''),
@SetTypeName = replace(set_type, '"', ''),
@SetBlockCode = Cast(replace(block_code, '"', '') as char(3)),
@SetBlockName = replace(block_name, '"', ''),
@SetIsDigital = Cast(replace(digital, '"', '') as char(1))
from inserted where CanonSetRowAdd = @CanonPK
set @SetTypeID = (select SetTypeID from defSET_TYPE where SetTypeName = @SetTypeName)
if @SetBlockCode is NOT NULL
BEGIN
exec dbo.FETCH_NewOrExistingBlock
@ConstBlockCode = @SetBlockCode,
@ConstBlockName = @SetBlockName,
@IsBlockID = @SetBlockID OUT
END
exec dbo.LOAD_intoSets
@ConstSetID = @SetID,
@ConstSetCode = @SetCode,
@ConstSetName = @SetName,
@ConstSetReleaseDate = @SetReleaseDate,
@ConstSetTypeID = @SetTypeID,
@ConstSetScryfallURI = @SetScryfallURI,
@ConstSetScryfallAPI = @SetScryfallAPI,
@ConstBlockID = @SetBlockID,
@ConstSetIsDigital = @SetIsDigital
DELETE from SCRY_CANON_SETS where CanonSetRowAdd = @CanonPK
END
```
```{r}
dbAppendTable(con, "SCRY_CANON_SETS", cheep)
```
# download cards
```{r}
options(timeout = 6000000)
tic("ok cards now")
tic("get bulk info")
bulkindex <- as.data.table(fromJSON("https://api.scryfall.com/bulk-data"))
toc()
tic("get urls")
defaultURI <- bulkindex[bulkindex$data.type == "default_cards", data.download_uri]
allURI <- bulkindex[bulkindex$data.type == "all_cards", data.download_uri]
toc()
setwd("C:/Users/cyrus/GitHere/deckdater")
filepath <- paste0(getwd(), "/upd8_cards/")
defaultfile <- paste0("cards_default_", Sys.Date(), ".json")
allfile <- paste0("cards_all_", Sys.Date(), ".json")
tic("downloading default cards")
download.file(defaultURI, paste0(filepath, defaultfile))
toc()
tic("downloading all cards")
download.file(allURI, paste0(filepath, allfile))
toc()
toc()
```
```{r}
allfile <- file.choose()
```
```{r}
tic("loading cards from files")
tic("load in default cards")
tic("json")
cards <- fromJSON("C:/Users/cyrus/GitHere/deckdater/upd8_cards/cards_default_2022-07-20.json")
toc()
tic("table")
cardtable <- as.data.table(cards)
rm(cards)
toc()
toc()
tic("load in all cards")
tic("json")
bigblob <- fromJSON("C:/Users/cyrus/GitHere/deckdater/upd8_cards/cards_all_2022-07-20.json")
toc()
tic("table")
namesblob <- as.data.table(bigblob)
rm(bigblob)
toc()
toc()
```
```{r}
cardkeep <- cardtable[news, on = "set_id", nomatch = 0L]
```
```{r}
layouts <- unique(cardkeep$layout)
```
```{r}
keywords <- as.data.table(as.character(cardkeep$keywords))
keywords[keywords == "character(0)"] <- NA
keywords <- na.omit(keywords)
keywords <- separate_rows(keywords, everything(), sep = '", "')
setDT(keywords)
keywords[, V1 := str_replace(V1, '^c."', '')][, V1 := str_replace(V1, '".$', '')]
keywords <- unique(keywords)
```
```{r}
keywords[V1 %like% 'walk', type := 'landwalk'][V1 %like% '[C|c]ycling', type := 'cycling'][V1 %like% 'Hexproof', type := 'hexproof'][V1 %like% 'Partner', type := 'partner'][V1 %like% '[K|k]icker', type := 'kicker'][V1 %like% '[N|n]injutsu', type := 'ninjutsu'][V1 %like% 'Venture into', type := 'venture'][V1 %like% '[Day|Night]bound', type := 'woofs'][V1 %like% '[S|s]torm', type := 'storm'][V1 %like% '[A|a]genda', type := 'agenda'][V1 %like% 'strike', type := 'damaging'][V1 %like% 'Infect', type := 'toxicity'][V1 %like% 'Wither', type := 'toxicity']
```
```{r}
keyword_classes <- data.table(class = unique(na.omit(keywords$type)))
```
```{r}
unique(na.omit(keywords$type))
```
```{r}
keyword_classes[class == 'damaging', gloss := 'Changes combat damage allocation'][class == 'woofs', gloss := 'Werewolf-style transformation'][class == 'toxicity', gloss := 'Harmful counters'][class == 'agenda', gloss := 'Affects one or more secret cards before game starts']
```
```{sql, connection=con}
create table defKEYWORD_CLASS(
KeywordClassID int Identity(1,1) primary key NOT NULL,
KeywordClassName varchar(25) unique NOT NULL,
KeywordClassDesc varchar(500) NULL)
```
```{sql, connection=con}
create table refKEYWORD(
KeywordID int Identity(1,1) primary key NOT NULL,
KeywordName varchar(200) unique NOT NULL,
KeywordClassID int FOREIGN KEY references defKEYWORD_CLASS NULL,
KeywordDesc varchar(500) NULL)
```
```{sql, connection=con}
create OR alter proc LEX_addKeywordClassDesc
@ForClass int,
@NewGloss varchar(500)
as BEGIN
if @NewGloss != (select KeywordClassDesc from defKEYWORD_CLASS where KeywordClassID = @ForClass)
BEGIN
update defKEYWORD_CLASS
set KeywordClassDesc = @NewGloss
where KeywordClassID = @ForClass
END
END
```
```{sql, connection=con}
create OR alter proc LEX_newKeywordClass
@Class varchar(25)
as BEGIN
insert into defKEYWORD_CLASS(KeywordClassName)
VALUES(@Class)
END
```
```{sql, connection=con}
create OR alter function lookup_KeywordClassID(
@Class varchar(25))
RETURNS int
as BEGIN
declare @RET int
set @RET = (select KeywordClassID from defKEYWORD_CLASS where KeywordClassName = @Class)
return @RET
END
```
```{sql, connection=con}
create OR alter proc DICT_addKeywordClass
@KC varchar(25),
@KG varchar(500) NULL
as BEGIN
exec dbo.LEX_newKeywordClass
@Class = @KC
if @KG IS NOT NULL
BEGIN
declare @ID int
set @ID = dbo.lookup_KeywordClassID(@KC)
exec dbo.LEX_addKeywordClassDesc
@ForClass = @ID,
@NewGloss = @KG
END
END
```
```{sql, connection=con}
create OR alter proc LOAD_intoKeywordClass
@KeywordClassName varchar(25),
@KeywordClassDesc varchar(500) NULL
as BEGIN
if (dbo.lookup_KeywordClassID(@KeywordClassName)) IS NULL
BEGIN
exec dbo.DICT_addKeywordClass
@KC = @KeywordClassName,
@KG = @KeywordClassDesc
END
END
```
```{r}
setnames(keyword_classes, c("class", "gloss"), c("KeywordClassName", "KeywordClassDesc"))
dbAppendTable(con, "defKEYWORD_CLASS", keyword_classes)
```
```{sql, connection=con}
create OR alter function lookup_KeywordID(@KeywordName varchar)
returns INT
as BEGIN
declare @KeywordID int
set @KeywordID = (select KeywordID from refKEYWORD where KeywordName = @KeywordName)
return @KeywordID
END
```
```{sql, connection=con}
create OR alter proc LOAD_intoKeyword
@IsKeywordName varchar(200),
@IsKeywordClassName varchar(25) NULL
as BEGIN
if not exists (select KeywordID from refKEYWORD where KeywordName = @IsKeywordName)
BEGIN
insert into refKEYWORD(KeywordName)
VALUES(@IsKeywordName)
END
if @IsKeywordClassName IS NOT NULL
BEGIN
declare @ClassID int, @KeywordID int
set @ClassID = dbo.lookup_KeywordClassID(@IsKeywordClassName)
set @KeywordID = dbo.lookup_KeywordID(@IsKeywordName)
if @ClassID != (select KeywordClassID from refKEYWORD where KeywordID = @KeywordID)
update refKEYWORD
set KeywordClassID = @ClassID
where KeywordID = @KeywordID
END
END
```
```{r}
keywords[!is.na(type), type := paste0('"', type, '"')]
keywords[is.na(type), type := 'NULL'][type == '""', type := 'NULL']
```
```{r}
tic("keyword insertion?")
for (i in seq_len(nrow(keywords))) {
dbExecute(con, paste0('exec dbo.LOAD_intoKeyword @IsKeywordName = "', keywords[i, 1], '", @IsKeywordClassName = ', keywords[i, 2]))
}
rm(i)
toc()
```
```{r}
keywordsWithClass <- keywords[type != 'NULL']
```
```{r}
keywordsWithClass[, type := str_replace_all(type, '"', '')]
```
```{r}
dbGetQuery(con, "select KeywordClassID from defKEYWORD_CLASS where KeywordClassName = 'damaging'")[[1]]
```
```{r}
tic("keyword insertion x2?")
for (i in seq_len(nrow(keywordsWithClass))) {
class <- dbGetQuery(con, paste0("select KeywordClassID from defKEYWORD_CLASS where KeywordClassName = '", keywordsWithClass[i, 2], "'"))[[1]]
vals <- paste0("set KeywordClassID = ", class, "where KeywordName = '", keywordsWithClass[i, 1], "'")
dbExecute(con, paste0("update refKEYWORD ", vals))
}
rm(i)
toc()
```
```{sql, connection=con}
select * from refKEYWORD
```
```{r}
a <- unique(na.omit(cardkeep$power))
b <- unique(na.omit(cardkeep$toughness))
jeepers <- as.data.table(unique(c(a, b)))
#for creature feature values...
```
```{r}
dotypes <- as.data.table(unique(toupper(cardkeep$type_line)))
setDT(dotypes)
dotypes[, V1 := str_replace_all(V1, " // ", "_..._")]
dotypes <- separate_rows(dotypes, everything(), sep = "_..._")
dotypes <- separate(dotypes, V1, c("type", "subtype"), sep = ' — ', fill = 'right')
```
```{r}
nosplittypes <- as.data.table(unique(dotypes$type))
nosplittypes[V1 == "SCARIEST CREATURE YOU’LL EVER SEE", V1 := NA]
nosplittypes <- na.omit(nosplittypes)
nosplitsubtypes <- na.omit(as.data.table(unique(dotypes$subtype)))
nosplitsubtypes[V1 %like% ',', V1 := NA][V1 %like% 'AND/OR ', V1 := str_replace_all(V1, 'AND/OR ', '')][V1 %like% 'LADY OF PROPER ', V1 := NA][V1 == 'ELEMENTAL?', V1 := NA]
types <- unique(separate_rows(nosplittypes, everything(), sep = " "))
subtypes <- as.data.table(c(unlist(unique(separate_rows(nosplitsubtypes, everything(), sep = " "))), "LADY OF PROPER ETIQUETTE"))
setDT(types)
supertypes <- types[!nosplittypes, on = "V1"]
supertypes[V1 == "PLANESWALKER", V1 := NA]
supertypes <- na.omit(supertypes)
types <- types[!supertypes, on = "V1"]
```
```{sql, connection=con}
create table defTYPE (
TypeID int Identity(1,1) primary key NOT NULL,
TypeName varchar(25) unique NOT NULL)
create table defSUBTYPE (
SubtypeID int Identity(1,1) primary key NOT NULL,
SubtypeName varchar(25) unique NOT NULL)
create table defSUPERTYPE (
SupertypeID int Identity(1,1) primary key NOT NULL,
SupertypeName varchar(25) unique NOT NULL)
```
```{sql, connection=con}
delete from defTYPE
delete from defSUPERTYPE
delete from defSUBTYPE
```
```{r}
setnames(types, "TypeName")
setnames(supertypes, "SupertypeName")
setnames(subtypes, "SubtypeName")
types <- na.omit(types)
supertypes <- na.omit(supertypes)
subtypes <- na.omit(subtypes)
tic("adding type line content")
tic("types")
dbAppendTable(con, "defTYPE", types)
toc()
tic("subtypes")
dbAppendTable(con, "defSUBTYPE", subtypes)
toc()
tic("supertypes")
dbAppendTable(con, "defSUPERTYPE", supertypes)
toc()
toc()
```
```{sql, connection=con}
create OR alter function lookup_TypeID(@CardType varchar)
returns INT
as BEGIN
declare @TypeID int
set @TypeID = (select TypeID from defTYPE where TypeName = @CardType)
return @TypeID
END
```
```{sql, connection=con}
create OR alter function lookup_SubtypeID(@CardSubtype varchar)
returns INT
as BEGIN
declare @SubtypeID int
set @SubtypeID = (select SubtypeID from defSUBTYPE where SubtypeName = @CardSubtype)
return @SubtypeID
END
```
```{sql, connection=con}
create OR alter function lookup_SupertypeID(@CardSupertype varchar)
returns INT
as BEGIN
declare @SupertypeID int
set @SupertypeID = (select SupertypeID from defSUPERTYPE where SupertypeName = @CardSupertype)
return @SupertypeID
END
```
```{sql, connection=con}
create OR alter function check_ExistsOnTypeLine(@Maybe varchar)
returns INT
as BEGIN
declare @RET int
set @RET = dbo.lookup_SubtypeID(@Maybe)
if @RET IS NULL
BEGIN
set @RET = dbo.lookup_TypeID(@Maybe)
if @RET IS NULL
BEGIN
set @RET = dbo.lookup_SupertypeID(@Maybe)
if @RET IS NULL
set @RET = 0
END
END
return @RET
END
```
```{sql, connection=con}
create OR alter proc LOAD_intoTypes
@NewType varchar(25)
as BEGIN
if (dbo.check_ExistsOnTypeLine(@NewType) = 0)
BEGIN
insert into defTYPE(TypeName)
VALUES(@NewType)
END
END
```
```{sql, connection=con}
create OR alter proc LOAD_intoSubtypes
@NewSubtype varchar(25)
as BEGIN
if (dbo.check_ExistsOnTypeLine(@NewSubtype) = 0)
BEGIN
insert into defSUBTYPE(SubtypeName)
VALUES(@NewSubtype)
END
END
```
```{sql, connection=con}
create OR alter proc LOAD_intoSupertypes
@NewSupertype varchar(25)
as BEGIN
if (dbo.check_ExistsOnTypeLine(@NewSupertype) = 0)
BEGIN
insert into defSUPERTYPE(SupertypeName)
VALUES(@NewSupertype)
END
END
```
```{sql, connection=con}
create OR alter function check_SubtypeOverlap(@ID int)
returns INT
as BEGIN
declare @RET int, @name varchar
set @RET = 0
set @name = (select SubtypeName from defSUBTYPE where SubtypeID = @ID)
if exists (select TypeID from defTYPE where TypeName = @name) OR exists (select SupertypeID from defSUPERTYPE where SupertypeName = @name)
set @RET = 1
return @RET
end
```
```{sql, connection=con}
create OR alter function check_SupertypeOverlap(@ID int)
returns INT
as BEGIN
declare @RET int, @name varchar
set @RET = 0
set @name = (select SupertypeName from defSUPERTYPE where SupertypeID = @ID)
if exists (select TypeID from defTYPE where TypeName = @name) OR exists (select SubtypeID from defSUBTYPE where SubtypeName = @name)
set @RET = 1
return @RET
end
```
```{sql, connection=con}
create OR alter function check_TypeOverlap(@ID int)
returns INT
as BEGIN
declare @RET int, @name varchar
set @RET = 0
set @name = (select TypeName from defTYPE where TypeID = @ID)
if exists (select SubtypeID from defSUBTYPE where SubtypeName = @name) OR exists (select SupertypeID from defSUPERTYPE where SupertypeName = @name)
set @RET = 1
return @RET
end
```
```{sql, connection=con}
alter table defSUBTYPE
ADD CONSTRAINT ck_uniqueSubtypes
CHECK(dbo.check_SubtypeOverlap(SubtypeID) = 0)
```
```{sql, connection=con}
alter table defTYPE
ADD CONSTRAINT ck_uniqueTypes
CHECK(dbo.check_TypeOverlap(TypeID) = 0)
```
```{sql, connection=con}
alter table defSUPERTYPE
ADD CONSTRAINT ck_uniqueSupertypes
CHECK(dbo.check_SupertypeOverlap(SupertypeID) = 0)
```
```{sql, connection=con}
create table defCREATURE_FEATURE (
CreatureFeatureID int Identity(1,1) primary key NOT NULL,
CreatureFeatureName varchar(25) unique NOT NULL,
CreatureFeatureAbbrev char(1) unique NOT NULL,
CreatureFeatureDesc varchar(200) NULL)
```
```{sql, connection=con}
create table defCREATURE_FEATURE_VALUE (
CreatureFeatureValueID int Identity(1,1) primary key NOT NULL,
CreatureFeatureValueText varchar(10) NOT NULL,
CreatureFeatureValueDesc varchar(200) NULL)
```
```{sql, connection=con}
insert into defCREATURE_FEATURE(CreatureFeatureName, CreatureFeatureAbbrev)
VALUES('Power', 'P'), ('Toughness', 'T')
```
```{r}
length(unique(jeepers$CreatureFeatureValueText))
length(jeepers$CreatureFeatureValueText)
```
```{sql, connection=con}
delete from defCREATURE_FEATURE_VALUE
```
```{r}
jeepers[CreatureFeatureValueText == '∞', CreatureFeatureValueText := 'Infinity']
```