@name Fail-Success
@inputs 
@outputs 
@persist Num1 Num2 Msg:string Won:array Interval
@trigger 

interval(10)
runOnChat(1)

#ware/exp_loop_1.wav
#ware/exp_loop_2.wav
#ware/game_prologue.mp3
#ware/game_epilogue.mp3
#ware/countdown_dos_sec1.mp3
#ware/countdown_dos_sec2.mp3
#ware/countdown_dos_sec3.mp3
#ware/countdown_dos_sec4.mp3
#ware/countdown_dos_sec5.mp3

#ware/local_exo_target_hit.wav
#ware/local_exo_won1.wav
#ware/local_exo_won2.wav
#ware/local_exo_won3.wav
#ware/local_lose2.wav
#ware/local_lose3.wav
#ware/local_lose4.wav
#ware/other_exo_won1.wav
#ware/other_lose1.wav

#ware/everyone_lose2.wav
#ware/everyone_won3.wav
#ware/exp_game_lose_1.mp3
#ware/exp_game_lose_2.mp3
#ware/exp_game_lose_3.mp3

#ware/exp_game_new_1.mp3
#ware/exp_game_new_2.mp3
#ware/exp_game_new_3.mp3
#ware/exp_game_new_4.mp3
#ware/exp_game_new_5.mp3
#ware/exp_game_transit_1.mp3
#ware/exp_game_transit_2.mp3
#ware/exp_game_win_1.mp3
#ware/exp_game_win_2.mp3
#ware/exp_game_win_3.mp3

#if(first()){timer("00",1000)}

#if(clk("00")){soundPlay(1,0,"ware/countdown_dos_sec5.mp3"),timer("01",1000)}
#if(clk("01")){soundPlay(2,0,"ware/countdown_dos_sec4.mp3"),timer("02",1000)}
#if(clk("02")){soundPlay(3,0,"ware/countdown_dos_sec3.mp3"),timer("03",1000)}
#if(clk("03")){soundPlay(4,0,"ware/countdown_dos_sec2.mp3"),timer("04",1000)}
#if(clk("04")){soundPlay(5,0,"ware/countdown_dos_sec1.mp3"),timer("05",1000)}
#if(clk("05")){soundPlay(6,0,"ware/local_exo_won1.wav"),timer("06",1000)}
#if(clk("06")){soundPlay(7,0,"ware/game_epilogue.mp3"),timer("07",22000)}
#if(clk("07")){soundPlay(8,0,"ware/exp_loop_1.wav"),timer("08",1000)}


#entity():soundPlay("ware1",0,"")
#timer("ware3",22000)

#if(clk("ware3"))
#{
#    soundStop("ware1")
#    entity():soundPlay("ware2",0,"ware/exp_loop_1.wav")
#}

if(first())
{
    Num1 = floor(random(16,64))
    Num2 = floor(random(16,64))
    Msg = Num1+"+"+Num2+"=?"
    Interval = randint(1000,2000)
    concmd("say "+Msg)
    findByClass("player")
    for(I=1,numPlayers())
    {
        holoCreate(I)
        holoPos(I,findResult(I):pos())
        holoEntity(I):soundPlay(I,0,"ware/exp_game_new_"+randint(1,5)+".mp3")
        holoDelete(I)
    }
    soundPlay(0,0,"ware/exp_game_new_"+randint(1,5)+".mp3")
    timer("round5",2000)
}

if(lastSaidWhen() == curtime() & lastSaid() != Msg)
{
    if(Won[lastSpoke():id(),number])
    {
    }
    else
    {
        if(lastSaid():find(toString(Num1 + Num2)))
        {
            holoCreate(0)
            holoPos(0,lastSpoke():pos())
            holoEntity(0):soundPlay(1,0,"ware/local_exo_won"+randint(1,3)+".wav")
            holoDelete(0)
            Won[lastSpoke():id(),number] = 1
        }
        else
        {
            holoCreate(0)
            holoPos(0,lastSpoke():pos())
            holoEntity(0):soundPlay(12,0,"ware/local_lose"+randint(2,4)+".wav")
            holoDelete(0)
        } 
    }
}


if(clk("round5")){soundPlay(5,0,"ware/countdown_dos_sec5.mp3"),timer("round4",Interval)}
if(clk("round4")){soundPlay(4,0,"ware/countdown_dos_sec4.mp3"),timer("round3",Interval)}
if(clk("round3")){soundPlay(3,0,"ware/countdown_dos_sec3.mp3"),timer("round2",Interval)}
if(clk("round2")){soundPlay(2,0,"ware/countdown_dos_sec2.mp3"),timer("round1",Interval)}
if(clk("round1")){soundPlay(1,0,"ware/countdown_dos_sec1.mp3"),timer("roundResults",Interval)}

if(clk("roundResults"))
{
    findByClass("player")
    for(I=1,numPlayers())
    {
        if(!Won:concat(" "):find(""+findResult(I):id()))
        {
            holoCreate(0)
            holoPos(0,findResult(I):pos())
            holoEntity(0):soundPlay(123,0,"ware/exp_game_lose_"+randint(1,3)+".mp3")
            holoDelete(0)
        }
        else
        {
            holoCreate(0)
            holoPos(0,findResult(I):pos())
            holoEntity(0):soundPlay(123,0,"ware/exp_game_win_"+randint(1,3)+".mp3")
            holoDelete(0)
        }
    }
    
    timer("roundEnd",3000)
}

if(clk("roundEnd"))
{
    
    
    Won = array()
    Num1 = floor(random(16,64))
    Num2 = floor(random(16,64))
    Msg = Num1 + "+" + Num2+"=?"
    Interval = randint(1000,2000)
    concmd("say " + Msg)
    findByClass("player")
    for(I=1,numPlayers())
    {
        holoCreate(I)
        holoPos(I,findResult(I):pos())
        holoEntity(I):soundPlay(I,0,"ware/exp_game_new_"+floor(random(1,5))+".mp3")
        holoDelete(I)
    }
    soundPlay(0,0,"ware/exp_game_new_"+floor(random(1,5))+".mp3")
    timer("round5",2000)
}
