@name FailCake LearnALot [BOT]
@persist E:entity Enabled
@persist [BotQuestion BotAwnser]:array Chat:array
@persist BQ:string BA:string Tem:array GG:array Encoded:string SF
@persist Says:string Num
@trigger all
@model models/bull/gates/processor.mdl

if(first() || duped()){
function void loadDataBases( I:number ){
    
if(I == 0){
fileLoad(">e2shared/BotQuestion.txt") 
setName("FailCake LearnALot [BOT] [Checking Questions Database]")
timer("HoldTimeBeforeAfkFileLol",2000)
}elseif(I == 1){
setName("FailCake LearnALot [BOT] [Checking Awnser Database]")
fileLoad(">e2shared/BotAwnser.txt")
timer("HoldTimeBeforeAfkFileLol2",2000)
}

}

function void createDataBases( I:number ){  
    
if(I == 0){
fileWrite(">e2shared/BotQuestion.txt","")
setName("FailCake LearnALot [BOT] [Creating Q Database]")
timer("savesecond",3000)
}elseif(I == 1){
fileWrite(">e2shared/BotAwnser.txt","")
setName("FailCake LearnALot [BOT] [Creating A Database]")
loadDataBases(0)
}

            
}

}

if(clk("retrySaving")){
createDataBases(0)    
}

if(clk("savesecond")){
stoptimer("savesecond")
createDataBases(1)    
}



function void prepareDataBases(I:number){
    
Tem:clear()
  
if(I == 0){  
Tem = vonDecode(BQ) 

for(O = 1,Tem:count()){
BotQuestion:pushString(Tem[O,string])   
}

hint("* Preparing Q Database *",7)
setName("FailCake LearnALot [BOT] [Preparing Q Database]")
prepareDataBases(1)

}elseif(I == 1){
Tem = vonDecode(BA)

for(O = 1,Tem:count()){
BotAwnser:pushString(Tem[O,string])   
}

hint("* Preparing A Database *",7)
setName("FailCake LearnALot [BOT] [Preparing A Database]")
}


}



interval(100)

if(first() | duped()){
runOnChat(1)
runOnFile(1)
runOnLast(1)

SF = 0
E = entity()

E:setSkin(1) 

loadDataBases(0) 
Enabled = 0
Learn = 1
}

## Database Load

if(clk("HoldTimeBeforeAfkFileLol")){
stoptimer("HoldTimeBeforeAfkFileLol")

if(fileLoaded()){
BQ = fileRead()
loadDataBases(1)
}else{
hint("* Database Not Found! Creating! *",7)
setName("FailCake LearnALot [BOT] [Database Not Found!]")
createDataBases(0)
}

}

if(clk("HoldTimeBeforeAfkFileLol2")){
stoptimer("HoldTimeBeforeAfkFileLol2")

if(fileLoaded()){
BA = fileRead()
prepareDataBases(0)
Enabled = 1
hint("* Bot Ready! *",7)
setName("FailCake LearnALot [BOT] [Ready]")
}else{
hint("* Second Database Not Found! Creating! *",7)
createDataBases(1)
}

}




## Database Save

if(clk("retrySaving")){
stoptimer("retrySaving")
createDataBases(0)    
setName("FailCake LearnALot [BOT] [Retrying]")
}

if(clk("savesecond")){
stoptimer("savesecond")
setName("FailCake LearnALot [BOT] [Creating Awnser Database]")
createDataBases(1)    
}


function number checkData(S:string,I:number){ 
    
if(I == 0){    
for(K = 1,BotQuestion:count()){ 
if(S:lower() == BotQuestion[K,string]:lower()){
return 0   
}

if(K >= BotQuestion:count()){    
return 1    
}
    
} 


}elseif(I == 1){
for(K = 1,BotAwnser:count()){
    
if(BotAwnser[K,string]:lower() == S:lower()){
return 0    
}

if(K >= BotAwnser:count()){     
return 1    
}
    
} 

}

}








function void learnNewData(I:number,S:string){
GG:clear()
GG = S:explode(" ")

for(I2 = 1,GG:count()){
    
if(I == 0){   
if(checkData(GG[I2,string],0)){
BotQuestion:pushString(GG[I2,string])  
}

}elseif(I == 1){

if(checkData(GG[I2,string],1)){
BotAwnser:pushString(GG[I2,string])    
}

}

}
    
}



function void buildStuff(){
Num = randint(1,10)

if(randint(1,2) == 1){
    
for(D = 1,Num){
Says += BotQuestion[randint(1,BotQuestion:count()),string] + " "
}   

}else{

for(D = 1,Num){
Says += BotAwnser[randint(1,BotAwnser:count()),string]+ " "
}   

}

concmd("say [Garry] " + Says)
   
}


timer("autosay",10000)
if(clk("autosay")){
buildStuff()    
}


function void saveNewData(){

  
if(SF == 0){ 
Encoded = vonEncode(BotQuestion)
fileWrite(">e2shared/BotQuestion.txt",Encoded)
}elseif(SF == 1){
Encoded = vonEncode(BotAwnser)
fileWrite(">e2shared/BotAwnser.txt",Encoded)
}

if(SF == 1){
SF = 0    
}elseif(SF == 0){
SF = 1
}

setName("FailCake LearnALot [BOT]")
}



function void botChat(S:string){

if(S:right(1) == "?"){
learnNewData(0,S) 
}else{
learnNewData(1,S)  
}

 buildStuff() ## After Learning Print It!  
}




if(Enabled){
    
if(chatClk()){
Chat = lastSaid():explode(" ")

if(Chat[1,string]:left(1) == "-"){
hint("Bot Chatting",7)
botChat(lastSaid():replace("-",""))  
}

    
}
   
}

if(Enabled){
timer("Checksave",8000)
}

if(clk("Checksave")){
setName("FailCake LearnALot [BOT] [Updating Databases]")
E:soundPlay(1,1,"/buttons/button7.wav")
saveNewData()    
}

if(last()){
saveNewData()     
}
