fkdata <- read.csv("fk_new.csv")

fkmatrices <- data.frame (
	experiment = "example",
	payoff_treatment = 1,
	role = 1,
	symmetric = 1,
	rows = 2,
	cols = 2
)
for(ii in 1:100){
	x <- matrix(c(0), 1, 1)
	colnames(x) <- paste("X", ii, sep="")
	fkmatrices <- cbind(fkmatrices, x)
}

fktreatments <<- data.frame (
	experiment = "example",
	treatment = 1,
	payoff_treatment = 1,
	matrix = 0,
	sequential = 0,
	supergame_ending = 1,
	observation_treatment = 0,
	communication_treatment = 0
)

fkmeta <- data.frame (
game = "prisoner's dilemma",
experiment = "pd",
description = "repeated repeated PD",
name = "name",
reference = "reference",
metastudy = "none",
location = "FR",
year = 1000,
original_units = 0,
currency = "unknown",
units = 1,
conversion = 1,
author_conversion = 1,
deflator = 100,
protocol = "random",
method = "normal",
matrix = 1,
paid_periods = -1,
missing_info = 0,
match_periods = -1,
roles = 2,
payoff_treatments = "various",
supergame_endings = "-1",
observation_treatments = "0 #none",
communication_treatments = "0 #none",
a1 = "1 for cooperate, 2 for defect",
a2 = "a2",
a3 = "a3",
a4 = "a4",
a5 = "a5",
a6 = "0",
session_time = -1,
instruction_time = -1,
session_games = -1, 
showup_fee = -1
)

fkpdata <- data.frame (
	experiment = "example",
	treatment = 1,
	session = 1,
	match_period = 1,
	match_group = 1,
	supergame_period = 1,
	player = 1,
	role = 1,
	a1 = 0,
	a2 = 0,
	a3 = 0,
	a4 = 0,
	a5 = 0,
	a6 = 0,
	obs = 0
)

get_experiment <- function(paper){
	experiment <- "pro_rus_sof"
	if(paper == "Honhon and Hyndman 2020") experiment <- "hon_hyn"
	if(paper ==  "Dal Bo and Frechette. 2019") experiment <- "dal_frec_19"      
	if(paper == "Arechar, Kouchaki, and Rand 2018") experiment <- "are_cou_ran"
	if(paper == "Ghidoni and Suetens 2021") experiment <- "ghi_sue"
	return(experiment)
}  

get_fk <- function(g, l){
	payoffs <- rep(0,4)
	payoffs[1] <- 1
	payoffs[2] <- -l
	payoffs[3] <- 1+g
	return(payoffs)
}

get_fk_matrices <- function(experiment, payoff_treatment, g, l){
	payoffs <- get_fk(g, l)
	row <- list(experiment, payoff_treatment, 1, 1, 2, 2)
	for(ii in 1:100){
		if(ii <= 4) row <- append(row, payoffs[ii])
		else row <- append(row, 0)
	}
	return(row)
}

do_fk_row <- function(row){
	paper <- row[["paper"]]
	experiment <- get_experiment(paper)
	#cooperate = 1, defect = -1
	c <- row[["c"]]
	opp_c <- row[["opp_c"]]
	action <- c
	if(action == -1) action <- 2
	oaction <- opp_c
	if(oaction == -1) oaction <- 2
	delta <- row[["delta"]]
	g <- row[["g"]]
	l <- row[["l"]]
	id <- row[["id"]]
	#id <- id/1000
	#id <- id - floor(id)
	#id <- 1000 * id
	inter <- row[["inter"]]
	round <- row[["round"]]
	session <- row[["session"]]
	gg <- floor(1000 * g)
	ll <- floor(1000 * g)
	treatment <- delta + (1000 * gg) + ll
	payoff_treatment <- treatment
	matrix_row <- get_fk_matrices(experiment, payoff_treatment, g, l)
	fkmatrices <<- rbind(fkmatrices, matrix_row)
	treatment_row <- list(experiment, treatment, payoff_treatment, 1, 0, delta, 0, 0)
	fktreatments <<- rbind(fktreatments, treatment_row)
	meta_template <- fkmeta[1,]
	meta_template["experiment"] <- experiment
	meta_template["name"] <- paper
	fkmeta <<- rbind(fkmeta, meta_template)
	match_group <- id + 8000
	#each match is listed twice for simplicity
	drow1 <- list(experiment, treatment, session, inter, match_group, round, id, 1, action, 0, 0, 0, 0, 0, 0)
	drow2 <- list(experiment, treatment, session, inter, match_group, round, match_group, 2, oaction, 0, 0, 0, 0, 0, 0)
	fkpdata <<- rbind(fkpdata, drow1, drow2)
}

for(ii in 1:nrow(fkdata)){
	do_fk_row(fkdata[ii,])
	fkmatrices <<- unique(fkmatrices)
	fkmeta <<- unique(fkmeta)
	fktreatments <<- unique(fktreatments)
}

fkmatrices <- fkmatrices[-1,]
fkmeta <- fkmeta[-1,]
fktreatments <- fktreatments[-1,]
fkpdata <- fkpdata[-1,]

write.csv(fkmatrices, "fkmatrices.csv", row.names = FALSE)
write.csv(fkmeta, "fkmeta.csv", row.names = FALSE)
write.csv(fktreatments, "fktreatments.csv", row.names = FALSE)
write.csv(fkpdata, "fkpdata.csv", row.names = FALSE)










