#  gSIMON GAMEh – an example of a RAGI call handler in action.

 See http://ragi.sf.net for an audio sample of this app running

#

#  RAGI -- Ruby Asterisk Gateway Interface

#  See http://ragi.sf.net for more information about RAGI

#

#  Sponsored by www.snapvine.com

#

#  ClasssimonCallHandler

#  Description:  gSimon Gameh - A simple memory game using RAGI

#

 

require 'ragi/callHandler'

 

module SimonCallHandler

  APP_NAME = 'simonCallHandler'

 

  class CallHandler < RAGI::CallHandler

       @theconnection = nil

 

    # When the RAGI server connects from Asterisk over AGI to our app, it calls this method.   

    def processCall(connection)

        # Store a class level handle to the connection.

        # Connection is the object that encapsulates the functions we can apply against a call

         @theconnection = connection 

      

        # Implement our simple simon game logic    

        @theconnection.answer(#the first thing to do is to answer the call

        @theconnection.wait(1)   #give it 1 seconds to make sure the connection is established

                  

        playAgain = true

        while (playAgain)

          instructions()               # tell the caller how to play the game

          score = playGame()           # start the game and play it, returning the score when done

          announceScore(score)         # tell them how they did

          playAgain = askPlayAgain()   # ask if they want to play again

        end

        sayGoodbye()

        @theconnection.hangUp(#end the call

    end

 

    def instructions

        @theconnection.playSound("simon-welcome")

    end

   

    def playGame 

     

      intArray = [rand(10)]

      score = 0

     

      while true

        # test the user's memory on the current int array

        result = memoryTest(intArray)

       

        # Let them continue if they got it right, otherwise bail out.

        if (result == false)

          return score

        else

          score = score + 1

        end

        

        # grow the array by one digit  

        nextDigit = rand(10) # choose a number between 0 and 9 inclusive

        intArray = intArray + [nextDigit]

       

        @theconnection.wait(1)

      end

    end

   

   def memoryTest(intArray)

      # say each number in the array

      index = 0

      intArray.length.times do

        @theconnection.playSound("simon-#{intArray[index]}"# there are sounds like simon-6.gsm

        index = index + 1

      end

    

      # listen for user key presses (number of presses should be == intArray.length)

      # Note:  give about 1 second per key press (intArray.length * 1500)

      resultStr = @theconnection.getData("simon-beep", (intArray.length * 1500), intArray.length)

     

      # Check if what they entered matches the int array

      if (resultStr == intArray.join(""))  # note:  join converts from array to string

        return true  

      else

        @theconnection.playSound("simon-gameover")

        return false

      end 

   end

  

   def announceScore(score)

     if (score <= 9)

       @theconnection.playSound("simon-score")  # gyour score was...h

       @theconnection.playSound("simon-#{score.to_s}")  # e.g. gfiveh

     end

     if (score < 5)

       @theconnection.playSound("simon-low")     # gyou did not do so wellh

     elsif (score < 10)

       @theconnection.playSound("simon-medium")  # gyou did OKh

     else

       @theconnection.playSound("simon-high")    # gyou did GREAT!h

     end

   end

 

   def askPlayAgain()

     resultStr = @theconnection.getData("simon-again", 3000, 1)

     if (resultStr.length >= 1)

       return true

     else

       return false

     end

   end

  

   def sayGoodbye()

     @theconnection.playSound("simon-goodbye")

   end

  end

 

end