# import trusty tidyverse library
library(tidyverse)

# always make sure the data is in chronological order when using lag/lead functions that look at the last or next row
master <- master[order(master$id_master),] 

# these outcomes lead to no attack by 1st contact team, so if attack follows this code, it's an overpass
overpass <- c("Reception /", "Dig -", "Cover -", "Freeball /")

# setup attacking inputs as we did in previous code (Step 8)
master$input_step9 <- NA
master$input_step9 <- ifelse(master$skill=="Attack", lag(master$second_touch_result,1), master$input_step9)
master$input_step9 <- ifelse(master$skill=="Attack" & master$one_touch_ago %in% overpass, "overpass", master$input_step9)


# create output situation column
## if Block follows Attack, return whatever happens directly after the Block
### if Block does not follow Attack, return whatever happens after directly Attack
master$output_step9 <- NA
master$output_step9 <- ifelse(master$skill=="Attack" & lead(master$add_cover,1)=="Block", master$two_touch_future, master$output_step9)
master$output_step9 <- ifelse(master$skill=="Attack" & lead(master$add_cover,1)!="Block", master$one_touch_future, master$output_step9)

# if outcome is terminal, label appropriately
master$output_step9 <- ifelse(master$skill=="Attack" & master$skq=="Attack #", "Kill", master$output_step9)
master$output_step9 <- ifelse(master$skill=="Attack" & master$skq=="Attack =", "Attack Error", master$output_step9)
master$output_step9 <- ifelse(master$skill=="Attack" & master$skq=="Attack /", "Attack Blocked", master$output_step9)

# if outcome is within this list of logical options, keep it, otherwise toss it
master$output_step9 <- ifelse(master$output_step9 %in% c("Kill", "Attack Error", "Attack Blocked", "Dig +", "Dig #", "Dig -", "Cover #", "Cover +", "Cover -"), master$output_step9, NA)



# find out the Input Expected Value by using rally efficiency
input_eV <- aggregate(rally_eff ~ add_cover*input_step9, subset(master, add_cover=="Attack"), mean)
colnames(input_eV)[3] <- "input_eV"

# find out the Output Expected Value by using rally efficiency
output_eV <- aggregate(rally_eff ~ add_cover*output_step9, subset(master, add_cover=="Attack"), mean)
colnames(output_eV)[3] <- "output_eV"

# merge these together so each input also has each output so we can look at the combinations of the two
attack <- merge(input_eV, output_eV, by="add_cover")

# merge with main dataframe
master <- merge(master, attack, by=c("add_cover", "input_step9", "output_step9"), all.x = TRUE)
master <- master[order(master$id_master),]
master$eV_valueadd <- master$output_eV - master$input_eV

# create National Championship example data and export
a <- aggregate(output_eV ~ team*player_name, subset(master, skill=="Attack" & match_id=="7a7773f6bed6251f00b1ab12056fc2b6"), mean)
b <- aggregate(input_eV ~ team*player_name, subset(master, skill=="Attack" & match_id=="7a7773f6bed6251f00b1ab12056fc2b6"), mean)
c <- aggregate(count ~ team*player_name, subset(master, skill=="Attack" & match_id=="7a7773f6bed6251f00b1ab12056fc2b6"), sum)
d <- merge(a,b)
e <- merge(d,c)

e$eV_valueadd <- e$output_eV-e$input_eV
write.csv(e, "step9_valueadd.csv")