-
Notifications
You must be signed in to change notification settings - Fork 0
/
edhmanager.rmd
186 lines (165 loc) · 5.22 KB
/
edhmanager.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
---
title: "edhmanager"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Load libraries:
```{r}
library(scryr)
library(plyr)
library(cowplot)
library(magick)
library(ggplot2)
```
# Load deck:
```{r}
getCardInfo <- function(cardName) {
rawInfo <- scryr::scry_card_name(cardName[1])
if(!("toughness" %in% names(rawInfo))) {
rawInfo$power <- "-"
rawInfo$toughness <- "-"
}
if(!("flavor_text" %in% names(rawInfo))) {
rawInfo$flavor_text <- "-"
}
if(!("oracle_text" %in% names(rawInfo))) {
rawInfo$oracle_text <- "-"
}
if(length(rawInfo$prices[[1]]) == 0) {
rawInfo$prices <- c(0)
}
rawInfo$usd <- as.numeric(rawInfo$prices[[1]][1])
rawInfo$image <- as.character(rawInfo$image_uris[[1]][2])
cardInfo <- rawInfo[,c("name", "colors", "color_identity", "cmc", "oracle_text", "power", "toughness", "type_line", "keywords", "flavor_text", "usd", "rarity", "image")]
Sys.sleep(0.5)
return(cbind(status = cardName[2], cardInfo))
}
LoadDeck <- function(csvPath) {
deckRaw <- read.csv(file = csvPath, header = FALSE, sep = ";")
deckList <- apply(X = deckRaw, MARGIN = 1, FUN = getCardInfo)
deckTbl <- do.call(rbind, deckList)
deckDF <- as.data.frame(deckTbl)
return(deckDF)
}
deck <- LoadDeck("decks/OMW.csv")
```
# Modify deck:
```{r}
addCard <- function(newCard, deckDF) {
if(nrow(deckDF) < 100) {
cardInfo <- NULL
try(cardInfo <- getCardInfo(newCard))
if(is.null(cardInfo)) {
print("The card could not be found. No action was performed.")
return(deckDF)
}
deckDF <- rbind(deckDF, cardInfo)
print(paste0("Your deck is now ", as.character(100 - nrow(deckDF)), " cards from being full."))
return(deckDF)
} else {
print("Your deck is already full, please remove cards before you add more.")
return(deckDF)
}
}
dropCard <- function(oldCard, deckDF) {
oldCard <- tolower(oldCard)
availableCards <- tolower(deckDF$name)
if(oldCard %in% availableCards) {
deckDF <- deckDF[-c(match(oldCard, availableCards)),]
print(paste0("Your deck is now ", as.character(100 - nrow(deckDF)), " cards from being full."))
} else {
print("The card could not be found. No action was performed.")
}
return(deckDF)
}
deck <- addCard("forest", deck)
deck <- dropCard("forest", deck)
```
# Extract information:
```{r}
DeckStats <- function(deckDF) {
cat("Commander deck stats\n####################\n\n")
cat("Commander: ", deckDF[deck$status == "commander", "name"], "\n")
cat(paste0("Deck price: $", sum(deckDF$usd)), "\n\n")
cat("Mana:\n*****\n")
colorID <- unique(unlist(deckDF$color_identity, recursive = TRUE))
colorID <- colorID[!(colorID == "NULL")]
colorCheck <- function(x) {ifelse(x == "B", "black", ifelse(x == "G", "green", ifelse("R", "red", ifelse("U", "blue", ifelse("W", "white", "unknown")))))}
colorID <- lapply(colorID, colorCheck)
cat("- Color identity:", paste(colorID, collapse = ", "), "\n")
cat("- Converted mana costs:")
cmcs <- plyr::count(deckDF$cmc)
print.data.frame(unname(cmcs[order(cmcs$x, decreasing = TRUE),]), row.names = FALSE)
cat("\nCard types:\n***********")
cardTypes <- gsub(" —.+|Legendary |Basic | // .+", "", deckDF$type_line)
cardTypes <- plyr::count(unlist(cardTypes))
print.data.frame(unname(cardTypes[order(cardTypes$freq, decreasing = TRUE),]), row.names = FALSE)
cat("\nKeywords:\n*********")
keywords <- plyr::count(unlist(deckDF$keywords))
print.data.frame(unname(keywords[order(keywords$freq, decreasing = TRUE),]), row.names = FALSE)
}
DeckStats(deck)
```
# Interact with deck:
```{r fig.height=10, fig.width=15}
DrawHand <- function(deckDF) {
drawPool <- deckDF[-grepl("commander", deckDF$status),]
urlVector <- sample(x = drawPool$image, size = 7, replace = FALSE)
plotImage <- function(imageUrl) {
plottedImage <- cowplot::ggdraw() +
cowplot::draw_image(imageUrl)
return(plottedImage)
}
allCards <- lapply(urlVector, plotImage)
upperPlot <- cowplot::plot_grid(plotlist = allCards[1:4], nrow = 1)
lowerPlot <- cowplot::plot_grid(plotlist = allCards[5:7], nrow = 1)
cowplot::plot_grid(upperPlot, lowerPlot, nrow = 2) +
theme(plot.background = element_rect(fill = "black"))
}
DrawHand(deck)
```
# Test cards against deck:
```{r}
# Function testing a card against the deck
# - show which cards in the deck that interacts with the keywords of the card
# - show which cards (if any) that share a creature type with the card
```
# Write deck:
```{r}
writeDeck <- function(deckDF, csvPath) {
write.table(x = cbind(deckDF$name, deckDF$status), file = csvPath, sep = ";", col.names = FALSE, row.names = FALSE, quote = FALSE)
}
writeDeck(deck, "decks/OMW.csv")
saveRDS(deck, "deck.rds")
```
# Shiny app:
```{r}
library(shiny)
library(keys)
hotkeys <- c(
"up 1", # Load deck
"up 2", # Print deck stats
"up 3" # Draw hand
)
ui <- fluidPage(
useKeys(),
keysInput("keys", hotkeys)
)
server <- function(input, output, session) {
observeEvent(input$keys, {
if(input$keys == "up 1") {
deck <- readRDS("deck.rds")
print("Deck loaded.")
}
if(input$keys == "up 2" && exists("deck") == TRUE) {
DeckStats(deck)
}
if(input$keys == "up 3" && exists("deck") == TRUE) {
#DrawHand(deck)
}
})
}
shinyApp(ui, server)
```