@name recorder
@inputs 
@outputs [W A S D Shift Space]
@persist Data:array Rate Frame [Owner Vehicle]:entity
@trigger none

if (first())
{
    Rate = 60 # Polling rate in ms
    Owner = owner()
    
    function string getInput( Player:entity ) {
        local IW = Player:keyForward()
        local IA = Player:keyLeft()
        local IS = Player:keyBack()
        local ID = Player:keyRight()
        local IShift = Player:keySprint()
        local ISpace = Player:keyJump()

        return "" + IW + IA + IS + ID + IShift + ISpace
    }
    
    runOnChat(1)
}
else
{
    if (chatClk(Owner)) 
    {
        local LS = lastSaid():explode(" ")
        switch (LS[1, string]) 
        {
            case "!record",
                Data:clear()
                Vehicle = Owner:vehicle()
                
                hideChat(1)
                timer("Record", 15)
            break
            
            case "!play",
                if (Data:count()) 
                {
                    Frame = 0

                    hideChat(1)
                    stoptimer("Record")
                    timer("Play", 1000)
                }
            break  
        }
    }
    
    if (clk("Record")) 
    {
        if (Vehicle & Vehicle:driver()) 
        {
            timer("Record", Rate)
            
            #------------------
            #- Inputs
            local Driver = Vehicle:driver()
            local Input = getInput( Driver )
             
            #------------------
            #- Record
            Data:unshiftString( Input )
        }
        else {
   
        }
    }
    
    if (clk("Play")) 
    {
        if (Frame < Data:count()) 
        {
            timer("Play", Rate)
            
            Frame++

            local Input = Data[Frame, string]
            
            W = Input[1]:toNumber()
            A = Input[2]:toNumber()
            S = Input[3]:toNumber()
            D = Input[4]:toNumber()
            Shift = Input[5]:toNumber()
            Space = Input[6]:toNumber()
        }
    }
}
