Skip to content

Commit

Permalink
complete rewrite of deck layout and build script
Browse files Browse the repository at this point in the history
  • Loading branch information
theclue committed Sep 26, 2017
1 parent 3312480 commit ed57b10
Show file tree
Hide file tree
Showing 20 changed files with 601 additions and 263 deletions.
56 changes: 51 additions & 5 deletions R/deck.parsing.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
deck.parsing <- function(deck, domain = NULL){

do.call(rbind,xml_find_all(woc.decks, "/deck/card") %>%
do.call(plyr::rbind.fill, xml_find_all(woc.decks, "/deck/card") %>%
lapply(function(x){

### CARD
Expand Down Expand Up @@ -81,17 +81,63 @@ deck.parsing <- function(deck, domain = NULL){
darkbond.type <- xml_attr(xml_find_first(x, ".//darkbond"), "type")
}

### RITUAL
### RITUALS
ritual <- ""
ritual.icons <- data.frame()
ritual.phases <- data.frame()

if(class((xml_find_first(x, ".//ritual"))) != "xml_missing"){

### RITUAL DESCRIPTION
t <- xml_find_first(x, ".//ritual")
ritual <- do.call(paste, lapply(xml_find_all(t, ".//phase"), function(phase){do.call(gettextf, c(list(xml_text(xml_find_first(phase, ".//label"))), lapply(xml_find_all(t, ".//arg"), function(arg){
ritual <- do.call(paste, lapply(xml_find_all(t, ".//phase"), function(phase){do.call(gettextf, c(list(xml_text(xml_find_first(phase, ".//label"))), lapply(xml_find_all(phase, ".//arg"), function(arg){
if(xml_has_attr(arg, "unit")) return(sprintf("%s %s", xml_text(arg), gettext(xml_attr(arg, "unit"), domain = domain)))
return(gettext(xml_text(arg), domain = domain))
}), domain = domain))}))

### RITUAL ICONS
ritual.icons <- do.call(rbind, lapply(xml_find_all(t, ".//phase"), function(phase){do.call(rbind, lapply(xml_find_all(phase, ".//arg"), function(arg){
if(xml_has_attr(arg, "unit")){

arg.c <- do.call(cbind, list(lapply(1:as.integer(xml_text(arg)), function(x, unit){unit}, unit = xml_attr(arg, "unit"))))
arg.df <- data.frame(phase = rep(paste("ritual", xml_attr(phase, "type"), sep="."), length(arg.c),), value = arg.c, stringsAsFactors = FALSE)
return(arg.df)
}
}))}))

ritual.icons <- ritual.icons %>% group_by(phase) %>% mutate(icon.id = paste(phase, row_number(), sep = "."))

ritual.icons <- as.data.frame(ritual.icons)
ritual.icons$phase <- NULL
ritual.icons <- ritual.icons %>% spread(icon.id, value)

### RITUAL PHASES
ritual.phases <- do.call(rbind, lapply(xml_find_all(t, ".//phase"), function(phase){do.call(rbind, lapply(xml_find_all(phase, ".//arg"), function(arg){
if(xml_has_attr(arg, "unit")){

arg.df <- data.frame(phase = paste("ritual", xml_attr(phase, "type"), sep="."), value = xml_text(arg), unit=xml_attr(arg, "unit"))
colnames
return(arg.df)
}
}))}))

ritual.phases <- ritual.phases %>% group_by(phase) %>% mutate(icon.id = paste(phase, row_number(), sep = "."))

ritual.phases <- as.data.frame(ritual.phases)
ritual.phases$phase <- NULL

ritual.phases.units <- ritual.phases %>% select(-value) %>% spread(icon.id, unit)
colnames(ritual.phases.units) <- paste(colnames(ritual.phases.units), "unit", sep=".")

ritual.phases.values <- ritual.phases %>% select(-unit) %>% spread(icon.id, value)
colnames(ritual.phases.values) <- paste(colnames(ritual.phases.values), "values", sep=".")

ritual.phases <- data.frame(ritual.phases.units, ritual.phases.values, stringsAsFactors = FALSE)

}

return(data.frame(card, card.id, type, family, title, caption, "description"=trimws(paste(combination, description, collapse=" ")), ritual.description = ritual, ritual.type, darkbond.type, knowledge.points, stringsAsFactors = FALSE))
if(nrow(ritual.icons)>0) return(data.frame(card, card.id, type, family, title, caption, "description"=trimws(paste(combination, description, collapse=" ")), ritual.description = ritual, ritual.icons, ritual.type, darkbond.type, knowledge.points, stringsAsFactors = FALSE))
else return(data.frame(card, card.id, type, family, title, caption, "description"=trimws(paste(combination, description, collapse=" ")), ritual.description = ritual, ritual.type, darkbond.type, knowledge.points, stringsAsFactors = FALSE))

})

Expand Down
107 changes: 64 additions & 43 deletions R/decks.preparation.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#!/usr/bin/env Rscript
execution.start.time <- Sys.time()
td <- NULL
if(nchar(Sys.getenv("TRAVIS_BUILD_DIR"))>0){
setwd(Sys.getenv("TRAVIS_BUILD_DIR"))
td <- bindtextdomain(domain = "woc", dirname = file.path(Sys.getenv("TRAVIS_BUILD_DIR"), "translations"))
if(nchar(Sys.getenv("BUILD_DIR"))>0){
setwd(Sys.getenv("BUILD_DIR"))
td <- bindtextdomain(domain = "woc", dirname = file.path(Sys.getenv("BUILD_DIR"), "translations"))
} else {
td <- bindtextdomain(domain = "woc", dirname = file.path(".", "translations"))
}
message(paste("The text domain was set to", td))

lang <- Sys.getenv("WOC_DECK_LOCALE")
if(nchar(lang)==0) lang <- "en"

message(paste("Rendering locale:", lang))

#######################
# Requirements & Setup
##
if (!require("pacman")) install.packages("pacman"); library(pacman)
invisible(p_load("dplyr", "xml2"))
if (!require("pacman")) install.packages("pacman"); invisible(library(pacman))
invisible(p_load("dplyr", "xml2", "tidyr"))

os <- Sys.info()["sysname"]

Expand All @@ -26,58 +31,74 @@ cards.meta <- read.csv(file="./data/cards.meta.csv", stringsAsFactors = FALSE, c

picture.placeholder <- "picture.placeholder.png"
ritual.placeholder <- "icon.blank.png"
darkbond.placeholder <- "icon.blank.png"

source("./R/deck.parsing.R")

# TODO Add better internationalization support
switch(lang,
en={
language="English"
charset="en_US.UTF-8"
},
it={
language="Italian"
charset="it_IT.UTF-8"
}
)

####################
# Data Preparation
##

if(os %in% c("Linux", "Darwin", "Solaris")) {
Sys.setlocale("LC_ALL", "en_US.UTF-8")
message(paste("Setting locale to", if(os %in% c("Linux", "Darwin", "Solaris")) {
Sys.setlocale("LC_ALL", charset)
} else {
Sys.setlocale("LC_ALL", "English")
}
Sys.setenv(LANG = "en_US.UTF-8")
players.deck.en <- deck.parsing(woc.decks, domain = "woc")
Sys.setlocale("LC_ALL", language)
}))
Sys.setenv(LANG = charset)

if(os %in% c("Linux", "Darwin", "Solaris")) {
Sys.setlocale("LC_ALL", "it_IT.UTF-8")
} else {
Sys.setlocale("LC_ALL", "Italian")
}
Sys.setenv(LANG = "it_IT.UTF-8")
players.deck.it <- deck.parsing(woc.decks, domain = "woc")
players.deck <- deck.parsing(woc.decks, domain = "woc")

####################
# Deck Flattening
###

# NOTE:
# In this card layout we put darkbond and ritual type at the same position
affected.columns.idx <- which(grepl("ritual\\.(study|transmutation|sacrifice).*", colnames(players.deck), perl = TRUE))
affected.columns.names <- colnames(players.deck)[affected.columns.idx]

standard.deck.en <- players.deck.en %>%
left_join(deck.families.meta) %>%
left_join(cards.meta) %>%
left_join(rituals.meta) %>%
left_join(darkbonds.meta) %>%
mutate(picture = ifelse(is.na(picture), picture.placeholder, picture)) %>%
mutate(ritual.icon = ifelse(is.na(ritual.icon), ifelse(is.na(darkbond.icon), ritual.placeholder, darkbond.icon), ritual.icon)) %>%
select(card, card.id, family, background, title, description, type, caption, knowledge.points, ritual.icon, picture, ritual.description )
write.csv(standard.deck.en, file = "./data/woc.deck.en.csv", row.names = FALSE, na = "")

message("English Deck Head:\n")
print(standard.deck.en[1:5,c(2,3,5,8)])

standard.deck.it <- players.deck.it %>%
left_join(deck.families.meta) %>%
left_join(cards.meta) %>%
left_join(rituals.meta) %>%
left_join(darkbonds.meta) %>%
ritual.sub <- as.data.frame(do.call(cbind, lapply(affected.columns.idx, function(i, deck){
d <- gsub("FW Imm", "icon.forbidden-wisdom-immediate.png", deck[,i])
d <- gsub("FW", "icon.forbidden-wisdom.png", d)
d <- gsub("DM", "icon.dark-master.png", d)
d <- gsub("EN", "icon.entity.png", d)
d <- gsub("AS", "icon.alien-science.png", d)
d <- gsub("Place", "icon.arcane-place.png", d)
d <- gsub("NULL", "icon.blank.png", d)
d <- gsub("Res", "icon.research.png", d)
d <- gsub("Obs", "icon.obsession.png", d)
return(d)
}, deck = players.deck)), stringsAsFactors = FALSE)
colnames(ritual.sub) <- affected.columns.names

standard.deck <- players.deck %>%
left_join(deck.families.meta, by = "family") %>%
left_join(cards.meta, by = "card.id") %>%
left_join(rituals.meta, by = "ritual.type") %>%
left_join(darkbonds.meta, by = "darkbond.type") %>%
mutate(family.icon = gsub("background.", "icon.", background)) %>%
mutate(picture = ifelse(is.na(picture), picture.placeholder, picture)) %>%
mutate(ritual.icon = ifelse(is.na(ritual.icon), ifelse(is.na(darkbond.icon), ritual.placeholder, darkbond.icon), ritual.icon)) %>%
select(card, card.id, family, background, title, description, type, caption, knowledge.points, ritual.icon, picture, ritual.description )
write.csv(standard.deck.it, file = "./data/woc.deck.it.csv", row.names = FALSE, na = "")
mutate(ritual.icon = ifelse(is.na(ritual.icon), ritual.placeholder, ritual.icon)) %>%
mutate(darkbond.icon = ifelse(is.na(darkbond.icon), darkbond.placeholder, darkbond.icon)) %>%
mutate("ritual.study.trans.slash?" = ifelse(nchar(ritual.type)>0, "Y", "")) %>%
mutate("ritual.trans.sacrifice.slash?" = ifelse(nchar(ritual.type)>0, "Y", "")) %>%
select(card, card.id, family, background, caption, family.icon, title, description, type, caption, knowledge.points, ritual.icon, darkbond.icon, picture, ends_with("?")) %>%
cbind(ritual.sub)

deck.file <- paste("./build/woc.deck", lang, "csv", sep=".")

write.csv(standard.deck, file = deck.file, row.names = FALSE, na = "")

message(paste("First 5 cards in ", deck.file, ":\n", sep = ""))

message("Italian Deck Head:\n")
print(standard.deck.it[1:5,c(2,3,5,8)])
print(standard.deck[1:5,c(2,3,5,7)])
21 changes: 9 additions & 12 deletions _scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ if [ -n "${TRAVIS+x}" ]; then
echo "** Executing in local environment; build dir set to $WOC_BUILD_DIR";
export BUILD_DIR=$WOC_BUILD_DIR
else
echo "** Executing in local environment; build dir set to current directory"
echo "** Executing in local environment; build dir set to `pwd`"
export BUILD_DIR=`pwd`
exit 1
fi
fi

Expand All @@ -27,25 +26,23 @@ mkdir $BUILD_DIR/pdf

cp $BUILD_DIR/*.png $BUILD_DIR/build/

Rscript --no-save --no-restore --verbose $BUILD_DIR/R/decks.preparation.R

### English
python $BUILD_DIR/countersheet.py -r 30 -n deck -d $BUILD_DIR/data/woc.deck.en.csv -p $BUILD_DIR/build $BUILD_DIR/woc.deck.svg > $BUILD_DIR/build/woc.deck.en.svg
export WOC_DECK_LOCALE=en
Rscript --no-save --no-restore $BUILD_DIR/R/decks.preparation.R

python $BUILD_DIR/countersheet.py -r 30 -n deck -d $BUILD_DIR/build/woc.deck.en.csv -p $BUILD_DIR/build $BUILD_DIR/woc.deck.svg > $BUILD_DIR/build/woc.deck.en.svg
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$BUILD_DIR/pdf/woc.deck.en.pdf $BUILD_DIR/build/*.pdf
rm $BUILD_DIR/build/*.pdf

### Italian
export WOC_DECK_LOCALE=it
Rscript --no-save --no-restore $BUILD_DIR/R/decks.preparation.R

# Workaround due to countersheetsextension not supporting UTF-8
#if [ "$(uname)" == "Darwin" ]; then
# sed -i '' 'y/āáǎàēéěèīíǐìōóǒòūúǔùǖǘǚǜĀÁǍÀĒÉĚÈĪÍǏÌŌÓǑÒŪÚǓÙǕǗǙǛ/aaaaeeeeiiiioooouuuuuuuuAAAAEEEEIIIIOOOOUUUUUUUU/' $BUILD_DIR/data/woc.deck.it.csv
#elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
# sed -i 'y/āáǎàēéěèīíǐìōóǒòūúǔùǖǘǚǜĀÁǍÀĒÉĚÈĪÍǏÌŌÓǑÒŪÚǓÙǕǗǙǛ/aaaaeeeeiiiioooouuuuuuuuAAAAEEEEIIIIOOOOUUUUUUUU/' $BUILD_DIR/data/woc.deck.it.csv
#fi
python $BUILD_DIR/countersheet.py -r 30 -n deck -d $BUILD_DIR/data/woc.deck.it.csv -p $BUILD_DIR/build $BUILD_DIR/woc.deck.svg > $BUILD_DIR/build/woc.deck.it.svg
python $BUILD_DIR/countersheet.py -r 30 -n deck -d $BUILD_DIR/build/woc.deck.it.csv -p $BUILD_DIR/build $BUILD_DIR/woc.deck.svg > $BUILD_DIR/build/woc.deck.it.svg
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$BUILD_DIR/pdf/woc.deck.it.pdf $BUILD_DIR/build/*.pdf
rm $BUILD_DIR/build/*.pdf


rm $BUILD_DIR/build/*.*

# Build rules PDF
Expand Down
3 changes: 1 addition & 2 deletions _scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ if [ -n "${TRAVIS+x}" ]; then
echo "** Executing in local environment; build dir set to $WOC_BUILD_DIR";
export BUILD_DIR=$WOC_BUILD_DIR
else
echo "** Executing in local environment; build dir set to current directory"
echo "** Executing in local environment; build dir set to `pwd`"
export BUILD_DIR=`pwd`
exit 1
fi
fi

Expand Down
Binary file modified background.alien-science.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified background.dark-master.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified background.entity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified background.forbidden-wisdom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ed57b10

Please sign in to comment.