@name Universal Translator
@inputs
@outputs [Answer Question Sent TLang]:string Users:table CanRequest DataL
#@outputs Data:string 
@persist [Lang Lang1 Send Receive Pl]:array
@persist Users:table
@persist [TLang Filename OL]:string
@persist Plc Talk
@persist Words:array
@persist Answer:string
@trigger

#Universal Translator by Captain Maim STEAM_0:1:8017844

runOnHTTP(1)
runOnChat(1)
runOnFile(1)
Delay=1000 #1500 may be needed for some ULX servers
if(first()){
    OL="en" #owners langauge, set this to whatever you speak.
}

#common variables
O=owner()
Chat=lastSaid() #what did the last person say?
Spoke=lastSpoke() #who spoke last?
CTeam=O:lastSaidTeam() #used to see if the owner spoke in team chat
ChatT=O:lastSaid() #what did the owner say?


#load our settings file, this contains players we've tagged as foreign
#along with their foreign language
if(fileLoaded()&fileName()==Filename){
    Users=table()
    Users=glonDecodeTable(fileRead())   
}



#List of Chat strings to look for
Com="UVT" #command word
Team="team" #set your outbound team chat langauge
Brk="=" #Break Character, used for parsing strings
Set="set" #select a player's language (saves via steam ID)
Players="play" #list all players
Save="save"
Self="self" #set your own language
Languages="lang" #list all languages
Help="help"
All="all"

#checks for each word in the chat string list above
COM=ChatT:find(Com)
TEAM=ChatT:find(Team)
BRK=ChatT:find(Brk)
SET=ChatT:find(Set)
PLAYERS=ChatT:find(Players)
SAVE=ChatT:find(Save)    
SELF=ChatT:find(Self)
LANGUAGES=ChatT:find(Languages)
HELP=ChatT:find(Help)
ALL=ChatT:find(All)

#error
Error1="To use this type "+Com+" "+Set+Brk+"<player number on list>"+Brk+"<language number>"
HelpScreen="
Inorder to use the Universal Translator you must say '"+Com+"'
plus any of the following commands:
"+Team+" with '"+Brk+"' and the language number sets your outbound language when team chatting.
"+Team+" without '"+Brk+"' will display all languages with their numbers.
"+Players+" displays player numbers to use with the languages when setting a player to a certain language.
"+Languages+" lists all languages with their prefixes and index numbers.
"+Set+" is used to set incoming chat languages, example: "+Com+" "+Set+Brk+"<player index number>"+Brk+"<language index number>
"+Save+" writes the users who you say speak other languages to a file.
"+All+" Set all players but yourself to this language, Usage: "+All+Brk+"<language number>."



#Chat command tree, what do we do if someone talks?
if(chatClk()){ 
    if(COM){
        
        if(HELP){
            print(HelpScreen)
        }
        if(PLAYERS){
            Pl=players()
            print("To select someone from the list type "+Com+" "+Set+Brk+"#")
            for(C=1,Pl:count()){
                print(" "+C+" = "+Pl[C,entity]:name())   
            }
        }
        if(LANGUAGES){
            for(C=1,Lang:count()){
                print(" "+C+" = "+Lang1[C,string]+" ("+Lang[C,string]+")")
            }   
        }
        if(ALL){
          Pl=players()
            if(Pl:count()>1){
                Or=ChatT:explode(Brk) #orders
                if(Or:count()==2){
                    for(C=1,Pl:count()){
                        if(Pl[C,entity]!=O){
                            Users[Pl[C,entity]:steamID(),string]=Lang[Or[2,string]:toNumber(),string]
                            
                        }
                    }    
                    print("All players but yourself have been set to "+Lang1[Or[2,string]:toNumber(),string])
                }
            }
        }
        if(SET){

             if(Pl){
                 if(BRK){
                    Or=ChatT:explode(Brk) #orders
                    if(Or:count()==3){
                        Users[Pl[Or[2,string]:toNumber(),entity]:steamID(),string]=Lang[Or[3,string]:toNumber(),string]
                        print(Pl[Or[2,string]:toNumber(),entity]:name()+" has been set to "+Lang1[Or[3,string]:toNumber(),string])
                    }else{print(Error1)}
                }else{
                    print(Error1)
                }
            }else{print("Please use the '"+Players+"' before using this.")}
            
        }
        if(SAVE){
            fileWrite(Filename,glonEncode(Users))
            print("Translation users saved!")
        }
        if(TEAM){
            if(!BRK){
                for(C=1,Lang1:count()){
                    print(" "+C+" = "+Lang1[C,string]+" ("+Lang[C,string]+")")
                }    
            }else{
                Ex=Chat:explode(Brk)[2,string]:toNumber()
                TLang=Lang[Ex,string]
                print("Your Team chat language is set to: "+Lang1[Ex,string])
            }
        }
        
    }
    if(CTeam&!COM){
        Question=ChatT
        Translate=1
        Origin=OL
        Destination=TLang
        Talk=1
    }
    if(Users[Spoke:steamID(),string]){
        Translate=1
        Origin=Users[Spoke:steamID(),string]
        Destination=OL
        Question=Chat
        Talk=0
    }
}





if(first()){
    
    Filename="UniversalTranslator.txt"
    fileLoad(Filename)
    #translation prefixes
    Lang=array() Lang1=array()
    Lang:pushString("af") Lang1:pushString("Afrikaans")
    Lang:pushString("sq") Lang1:pushString("Albanian")
    Lang:pushString("ar") Lang1:pushString("Arabic")
    Lang:pushString("be") Lang1:pushString("Belarusian")
    Lang:pushString("bg") Lang1:pushString("Bulgarian")
    Lang:pushString("ca") Lang1:pushString("Catalan")
    Lang:pushString("zh-CN") Lang1:pushString("Chinese (Simplified)")
    Lang:pushString("zh-TW") Lang1:pushString("Chinese (Traditional)")
    Lang:pushString("hr") Lang1:pushString("Croatian")
    Lang:pushString("cs") Lang1:pushString("Czech")
    Lang:pushString("da") Lang1:pushString("Danish")
    Lang:pushString("nl") Lang1:pushString("Dutch")
    Lang:pushString("en") Lang1:pushString("English")
    Lang:pushString("et") Lang1:pushString("Estonian")
    Lang:pushString("tl") Lang1:pushString("Filipino")
    Lang:pushString("fi") Lang1:pushString("Finnish")
    Lang:pushString("fr") Lang1:pushString("French")
    Lang:pushString("gl") Lang1:pushString("Galician")
    Lang:pushString("de") Lang1:pushString("German")
    Lang:pushString("el") Lang1:pushString("Greek")
    Lang:pushString("iw") Lang1:pushString("Hebrew")
    Lang:pushString("hi") Lang1:pushString("Hindi")
    Lang:pushString("hu") Lang1:pushString("Hungarian")
    Lang:pushString("is") Lang1:pushString("Icelandic")
    Lang:pushString("id") Lang1:pushString("Indonesian")
    Lang:pushString("ga") Lang1:pushString("Irish")
    Lang:pushString("it") Lang1:pushString("Italian")
    Lang:pushString("ja") Lang1:pushString("Japanese")
    Lang:pushString("ko") Lang1:pushString("Korean")
    Lang:pushString("lv") Lang1:pushString("Latvian")
    Lang:pushString("lt") Lang1:pushString("Lithuanian")
    Lang:pushString("mk") Lang1:pushString("Macedonian")
    Lang:pushString("ms") Lang1:pushString("Malay")
    Lang:pushString("mt") Lang1:pushString("Maltese")
    Lang:pushString("no") Lang1:pushString("Norwegian")
    Lang:pushString("fa") Lang1:pushString("Persian")
    Lang:pushString("pl") Lang1:pushString("Polish")
    Lang:pushString("pt") Lang1:pushString("Portuguese")
    Lang:pushString("ro") Lang1:pushString("Romanian")
    Lang:pushString("ru") Lang1:pushString("Russian")
    Lang:pushString("sr") Lang1:pushString("Servian")
    Lang:pushString("sk") Lang1:pushString("Slovak")
    Lang:pushString("sl") Lang1:pushString("Slovenian")
    Lang:pushString("es") Lang1:pushString("Spanish")
    Lang:pushString("sw") Lang1:pushString("Swahili")
    Lang:pushString("sv") Lang1:pushString("Swedish")
    Lang:pushString("th") Lang1:pushString("Thai")
    Lang:pushString("tr") Lang1:pushString("Turkish")
    Lang:pushString("uk") Lang1:pushString("Ukrainian")
    Lang:pushString("vi") Lang1:pushString("Vietnamese")
    Lang:pushString("cy") Lang1:pushString("Welsh")
    Lang:pushString("yi") Lang1:pushString("Yiddish")
    
    PLL=table() #Player Language List
    #fill in the blanks and submit the form
    
    
}
#http://Send.dictionary.com/translation/I+need+to+test?src=en&dst=es


#test


if(Translate){
#    if(TLang=="es"){
#        #Spanish Only exception >:(
#        Send=array()
#        Send[1,string]="http://spanish.dictionary.com/translation/"
#        Send[2,string]=""
#        Send[3,string]="?src="
#        Send[4,string]=""
#        Send[5,string]="&dst="
#        Send[6,string]=""   
#        
#        #how to find the translated data.
#        Receive=array()
#        Receive[1,string]="readonly=\"readonly\">" #Tag immediatey before translation
#        Receive[2,string]="</textarea>" #tag immediately after
#
#    }else{
        Send=array()
        Send[1,string]="http://translate.reference.com/translate?query="
        Send[2,string]="" #what do you want to say?
        Send[3,string]="&src="
        Send[4,string]="" #what language is it in?
        Send[5,string]="&dst=" 
        Send[6,string]="" #what language do you want it in?
        #Send[7,string]="&v=1.0"
        
        Receive=array()
        Receive[1,string]="<div class=\"translateTxt\" >
                        
                            
                                
                                    "
        Receive[2,string]="</div>"
    #}

    if(Talk){hideChat(1)}
    Send1=Send:clone()
    Send1[2,string]=httpUrlEncode(Question)
    Send1[4,string]=Origin
    Send1[6,string]=Destination
    Sent=Send1:concat()
    #print(Sent)
    httpRequest(Sent)
    Translate=0
}else{hideChat(0)}
if(httpClk()){
    Answer=httpUrlDecode(httpData():explode(Receive[1,string])[2,string]:explode(Receive[2,string])[1,string]):replace("&#39;","'")
    Data=httpData()    
    DataL=Data:length()
}
if(changed(Answer)){
    if(Talk){timer("talk",Delay)}elseif(Question!=Answer){print(Answer)}    
}
if(clk("talk")){
    if(Talk){concmd("say "+Answer) Talk=0}
}
CanRequest=httpCanRequest()
