######################################################################################################################
x<-"Description of the data and the goal: Dataset used was Minecraft research data. Goal was to plot users based on deaths in game. Color corresponds to number of enemies killed.
"
######################################################################################################################
library(igraph)
library(networkD3)
## Warning: package 'networkD3' was built under R version 3.1.3
#dataframe col1=source(aka unique id). col2=target to link source to. col3=color atttribute. col4=vertex labels, col5=line weights
#d3 color group order: blue, orange, green, red
forceNetwork_from_dataframe<-function(dtm2){
set.seed(42)
g<-graph.data.frame(dtm2[,1:ncol(dtm2)], directed=TRUE) #directed=TRUE because one unique_id (aka source) only points to one target
V(g)$type <- V(g)$name %in% dtm2[,1] #purpose is to give true and false for node and community?????

i<-table(V(g)$type)[2]#captures number of unique actors in dtm2.... is 15
j<-table(V(g)$type)[1]#gets number of communities in dtm2


V(g)$name<-1:length(V(g)) #numbers each node as unique number. is source for links
V(g)$label<-paste("name:",V(g)$name,"_dmgDealt:",dtm2[,4],sep="")
V(g)$label[i+1:j]<-"community" #label communities as community. syntax when end num in : less then beginning do repeat for end number times
#V(g)$label<-V(g)$name

links<-as.data.frame(get.edgelist(g))
links$V1<-as.numeric(as.character(links$V1))
#links

links$V2<-as.numeric(as.character(links$V2))
#str(links)

colnames(links)<-c("source","target")
#links
links<-(links-1)#can show all links (start count from 0)
links$value <- dtm2[,5]
#links


#add communities to color list.
groupings<-c(dtm2[,3],rep(999,j))#j is number of communities

#nodes <- data.frame(name=V(g)$label, group=c(rep(1,i),rep(2,(j))))#change group numbers####CLASS CODE
nodes <- data.frame(name=V(g)$label, group=groupings)
nodes #shows actors and group

nothingbutnet<-forceNetwork(Links = links, Nodes = nodes,
             Source = "source", Target = "target",
             NodeID = "name", Value = "value",
             Group = "group", opacity = 0.8, colourScale = "d3.scale.category10()")

return(nothingbutnet)
}
#########################################################################Calling function
csv<-read.csv("C:\\Users\\King\\Desktop\\campbell\\minecraftdatapuller\\Analysis\\minecraft_with_0_and_MI.csv")
dtm2<-data.frame(csv$stat.damageDealt,csv$stat.deaths,csv$stat.mob)#damage delt on deaths, mob kills (is color)
#write.csv(dtm2,"output_df_for_jason.csv",row.names=F)
#adding UNIQUE names column
dtm2$names<-1:length(dtm2[,1])
dtm2$names<-paste("uniqueID_",dtm2$names,sep="")

dtm2$colors <- ifelse(is.na(dtm2[,3]), 999,
                 ifelse(as.numeric(dtm2[,3]) < 10, 1,
                        ifelse(as.numeric(dtm2[,3]) >= 30, 3, 2)))
#reorder columns so unique id is next to kills. uniqueID will be source and target will be kills 
dtm2<-dtm2[,c(4,2,5,1)]
#add lineweights to dataframe
dtm2$lineWeights<-3

res<-forceNetwork_from_dataframe(dtm2)
#htmltools::html_print(res, viewer = utils::browseURL) #display forceNetwork in browser
res

name:1_dmgDealt:1170name:2_dmgDealt:840name:3_dmgDealt:2595name:4_dmgDealt:5410name:5_dmgDealt:2720name:6_dmgDealt:1720name:7_dmgDealt:180name:8_dmgDealt:1990name:9_dmgDealt:3445name:10_dmgDealt:2250name:11_dmgDealt:1965name:12_dmgDealt:3345name:13_dmgDealt:4480name:14_dmgDealt:2225name:15_dmgDealt:3870name:16_dmgDealt:2460name:17_dmgDealt:1705name:18_dmgDealt:2480name:19_dmgDealt:3495name:20_dmgDealt:1870name:21_dmgDealt:4620name:22_dmgDealt:240name:23_dmgDealt:1305name:24_dmgDealt:0name:25_dmgDealt:0name:26_dmgDealt:6585name:27_dmgDealt:6490name:28_dmgDealt:1840name:29_dmgDealt:3340name:30_dmgDealt:5970name:31_dmgDealt:3450name:32_dmgDealt:3005name:33_dmgDealt:2550name:34_dmgDealt:130name:35_dmgDealt:0name:36_dmgDealt:580name:37_dmgDealt:1395name:38_dmgDealt:3595name:39_dmgDealt:455name:40_dmgDealt:7385name:41_dmgDealt:3170name:42_dmgDealt:580name:43_dmgDealt:6890name:44_dmgDealt:295name:45_dmgDealt:730name:46_dmgDealt:1005name:47_dmgDealt:605name:48_dmgDealt:0name:49_dmgDealt:5775name:50_dmgDealt:1580name:51_dmgDealt:0name:52_dmgDealt:645name:53_dmgDealt:3510name:54_dmgDealt:1620name:55_dmgDealt:4870name:56_dmgDealt:3855name:57_dmgDealt:210name:58_dmgDealt:700name:59_dmgDealt:2460name:60_dmgDealt:6840name:61_dmgDealt:3490name:62_dmgDealt:1910name:63_dmgDealt:4475name:64_dmgDealt:9665name:65_dmgDealt:1060name:66_dmgDealt:4735name:67_dmgDealt:4325name:68_dmgDealt:4580name:69_dmgDealt:2950name:70_dmgDealt:2805name:71_dmgDealt:2435name:72_dmgDealt:1080name:73_dmgDealt:1330name:74_dmgDealt:7575name:75_dmgDealt:1675name:76_dmgDealt:4560name:77_dmgDealt:2385name:78_dmgDealt:4215name:79_dmgDealt:1705name:80_dmgDealt:5420name:81_dmgDealt:1950name:82_dmgDealt:3120name:83_dmgDealt:10350name:84_dmgDealt:4910name:85_dmgDealt:630name:86_dmgDealt:590name:87_dmgDealt:5575name:88_dmgDealt:8575name:89_dmgDealt:1525name:90_dmgDealt:5415name:91_dmgDealt:4020name:92_dmgDealt:785name:93_dmgDealt:0name:94_dmgDealt:1615name:95_dmgDealt:7770name:96_dmgDealt:8065name:97_dmgDealt:5225name:98_dmgDealt:4015