'use strict';

var prefix = [ "Simon says type the word... ", "Type the word... " ];
var commands = [ "YES", "NO", "GO", "STOP" ];

// Returns a random element from an array
var randElem = function(array) {
  var index = Math.floor(Math.random() * array.length);
  return array[index];
};

module.exports = function simonSays(controller) {
  controller.hears('simonsays', ['direct_mention'], function(bot, message) {

    var simonAsk = function(response, convo) {
      var simonSays = [randElem(prefix), randElem(commands)];
      var simonString = simonSays[0] + simonSays[1];
      convo.ask(simonString, function(response, convo) {
        if (simonSays[0] && response.text === simonSays[1]) {
          convo.say('Good job!');
          simonAsk(response, convo);
        } else {
          convo.say('Game Over!');
        }
        convo.next();
      });
    }

    bot.startConversation(message, simonAsk);
  });
}

@@ -23,17 +23,17 @@
 fix = %5B 
-%22
+'
 Simon sa
@@ -52,16 +52,14 @@
 word
-... %22, %22
+: ', '
 Type
@@ -71,13 +71,11 @@
 word
-... %22
+: '
  %5D;%0A
@@ -95,33 +95,64 @@
 = %5B 
-%22YES%22, %22NO%22, %22GO%22, %22
+'YES', 'NO', 'GO', '
 STOP
-%22
+', 'UP', 'DOWN', 'LEFT', 'RIGHT'
  %5D;%0A
@@ -437,17 +437,356 @@
 sage) %7B%0A
+    
 %0A
+    // Slack API call to retrieve player's name%0A    bot.api.users.info(%7B user: message.user %7D, function(err, res) %7B%0A      var name = res.ok ? ' ' + res.user.profile.first_name : '';%0A%0A      // Initial reply to indicate start of game%0A      bot.reply(message, 'Hey' + name + '! Let%5C's play Simon Says!');%0A%0A      // Main asking function%0A  
     var 
@@ -826,24 +826,26 @@
 vo) %7B%0A      
+  
 var simonSay
@@ -890,24 +890,26 @@
 ds)%5D;%0A      
+  
 var simonStr
@@ -943,16 +943,50 @@
 ays%5B1%5D;%0A
+        var responded = false;%0A%0A  
       co
@@ -1046,24 +1046,121 @@
     
-if (simonSays%5B0%5D
+  responded = true; // Stops setTimeout callback if player responds in time%0A%0A          if (simonString%5B0%5D === 'S'
  && 
@@ -1189,24 +1189,26 @@
 nSays%5B1%5D) %7B%0A
+  
           co
@@ -1224,21 +1224,35 @@
 Good job
+' + name + '
 !');%0A
+  
         
@@ -1292,44 +1292,196 @@
     
+  
 %7D else 
-%7B%0A          convo.say('Game O
+if (simonString%5B0%5D === 'T') %7B%0A            convo.say('I didn%5C't say Simon says! Game over' + name + '!');%0A          %7D else %7B%0A            convo.say('Wrong answer' + name + ', game o
 ver!
@@ -1492,18 +1492,23 @@
         
-%7D%0A
+  %7D%0A%0A  
         
@@ -1531,19 +1531,541 @@
     
+  
 %7D);%0A
-    %7D%0A%0A
+%0A        // If no one responds within time limit, either recurse into simonAsk again%0A        // or end the conversation%0A        setTimeout(function() %7B%0A          if (!responded) %7B%0A            if (simonString%5B0%5D === 'T') %7B%0A              convo.say('Paying attention, I see. Good job!');%0A              simonAsk(response, convo);%0A            %7D else %7B%0A              convo.say('Too slow! Game over' + name + '!');%0A            %7D%0A%0A            convo.next();%0A          %7D%0A        %7D, 4000);%0A      %7D%0A%0A      // Initiate conversation chain%0A  
     
@@ -2106,16 +2106,24 @@
 onAsk);%0A
+    %7D);%0A
   %7D);%0A%7D%0A

Modify simonSays to be time-sensitive and respond correctly