hbtreatments <- read.csv("hbtreatments.csv")
hbmetadata <- read.csv("hbmetadata.csv")
hbmatrices <- read.csv("hbmatrices.csv")
hbdata <- read.csv("hbdata.csv")

econd <- read.csv("cond.03.csv")

fktreatments <- read.csv("fktreatments.csv")
fkmetadata <- read.csv("fkmeta.csv")
fkmatrices <- read.csv("fkmatrices.csv")
fkdata <- read.csv("fkpdata.csv")

hbtreatments <- rbind(hbtreatments, fktreatments)
hbmetadata <- rbind(hbmetadata, fkmetadata)
hbmatrices <- rbind(hbmatrices, fkmatrices)
hbdata <- rbind(hbdata, fkdata)

hbmetadata <- hbmetadata[which(hbmetadata$game == "prisoner's dilemma"),]
exps <- unique(hbmetadata$experiment)
hbtreatments <- hbtreatments[which(hbtreatments$experiment %in% exps),]
hbtreatments <- hbtreatments[which(hbtreatments$supergame_ending < 1),]
hbdata <- hbdata[which(hbdata$match_period >= sobs),]
#hbdata <- hbdata[which(hbdata$match_period <= 30),]

exps <- unique(hbtreatments$experiment)
hbmetadata <- hbmetadata[which(hbmetadata$experiment %in% exps),]

row_num <- rep(0,nrow(hbtreatments))
hbtreatments <- cbind(hbtreatments, row_num)
for(ii in 1:nrow(hbtreatments)) hbtreatments$row_num[ii] <- ii

matis <- read.csv("mats.csv")

get_matrix <- function(experiment, payoff_treatment){
	matrix_row <- hbmatrices[which(hbmatrices$experiment == experiment),]
	matrix_row <- matrix_row[which(matrix_row$payoff_treatment == payoff_treatment),]
	matrix <- rep(0,4)
	for(ii in 1:4) {
		matrix[ii] <- matrix_row[1, paste("X", ii, sep = "")]
	}
	bot <- matrix[4]
	for(ii in 1:4) matrix[ii] <- matrix[ii] - bot
	defl <- matrix[1]
	matrix <- matrix/defl
	matrix <- 100 * matrix
	return(matrix)
}

get_matrices <- function(){
	matrices <- matrix(0, nrow(hbtreatments), 4)
	for(ii in 1:nrow(hbtreatments)){
		matrices[ii,] <- get_matrix(hbtreatments$experiment[ii], hbtreatments$payoff_treatment[ii])
	}
	return(matrices)
}

get_deltas <- function(){
	deltas <- rep(0, nrow(hbtreatments))
	for(ii in 1:nrow(hbtreatments)){
		deltas[ii] <- hbtreatments$supergame_ending[ii]
	}
	return(deltas)
}

get_treat <- function(udata, ii){
	vdata <- hbtreatments[which(hbtreatments$treatment == udata$treatment[ii]),]
	vdata <- vdata[which(vdata$experiment == udata$experiment[ii]),]
	return(vdata$row_num[1])
}

matrices <- get_matrices()
deltas <- get_deltas()

make_report <- function(hh){
	zdata <- matis[which(matis$rep == hh),]
	ii <- get_treat(zdata, 1)
	experiment <- hbtreatments$experiment[ii]
	mat <- matrices[ii,]
	delta <- deltas[ii]
	emc <- econd[hh,]
	print(paste(experiment, delta))
	print(mat)
	getbest(mat, delta, emc)
	print(eels(mat,delta))
}

report_all <- function(){
	rest <- unique(matis$rep)
	nplay <- 0
	tses <- 0
	for(ii in 1:NROW(rest)) ewelf <- make_report(ii)
}

eels <- function(mat,delta){
	mat <- mat/100
	a <- 1 - (1/mat[2])
	b <- 1 - (mat[3]/mat[2])
	zz1 <- (2*(delta^2))-(4*delta)+1
	z1 <- 2 * a * zz1
	zz2 <- (delta - 1)^2
	z2 <- (b^2) * (zz2^2)
	z3 <- -2 * a * b * zz2
	z4 <- -2 * b * zz2
	first <- z1 + z2 + z3 + z4 + (a^2) + 1
	if(first < 0) return(-1)
	first <- sqrt(first)
	x1 <- b * (delta^2)
	x2 <- (2-(2*b))*delta
	second <- x1 + x2 + b - a - 1
	top <- first + second
	y1 <- 2 * b * (delta^2)
	yy2 <- (-2 * b) - (2 * a) + 2
	y2 <- yy2 * delta
	bot <- y1 + y2
	sig <- top/bot
	return(sig)
}




getbest <- function(mat, delta, emc){
	mat <- mat/100
	a <- 1 - (1/mat[2])
	b <- 1 - (mat[3]/mat[2])
	ee <- rep(0,4)
	te <- rep(0,4)
	sigDD <- (b * delta) - b + a - 1
	ee[4] <- 0
	ee[3] <- 1
	ee[2] <- ee[3]
	ee[1] <- b - a + 1
	sigDD <- sigDD / (b * delta)
	ee <- ee / (b * delta)
	tel <- (as.numeric(emc) - ee)/sigDD
	r <- mean(tel)
	sigDD <- sigDD * r
	if(sigDD < 0) sigDD <- 0
	sigCD <- ee[3] + sigDD
	sigCC <- ee[1] + sigDD	
	if(sigCC <= 1){
		print(paste(sigCC, sigCD, sigDD))
	}
}

report_all()

		
	
