$(function() {
  getGameStatus();

  $('body').click(function() {
    $('#user_input').focus();
  });

  $('#command-form').submit(sendCommand);
});

function removeCursor() {
  $('.teletype-cursor').remove();
}
function updateOutput(info, callback) {
  removeCursor();
  var data = info.split('\n').map(function(el) {
    return el + "\\n";
  });
  data.push("\\nWhat do you want to do?");
  var dom = $('<p>');
  $('#output').append(dom);
  console.log(dom);
  dom.teletype({
      text: data,
      typeDelay: 10,
      backDelay: 10,
      delay: 10,
      preserve: true,
      cursor: '▋',
      loop: 1,
      humanise: false,
      callbackFinished: function (teletype) {
        console.log("FINISHED");
      }
  });
}

function sendCommand(event) {
  event.preventDefault();
  var input = $('#user_input');
  var command = input.val();
  console.log("Sending command " + command);

  $.ajax({
    type: 'POST',
    url: '/send_command',
    data: { command: input.val(), },
    success: function(data, textStatus, jqXHR) {
      console.log("RESPONSE:", data);
      getGameStatus();
    }
  });
  input.val('');
}

function getGameStatus() {
  $.get("/status")
    .success(function(data) {
      console.log("Status requested successfully...");
      console.log(data);
      try {
        data = JSON.parse(data);
      } catch (e) {
        alert("ERROR while parsing status");
        console.error(e);
      }
      $('#player_name').text(data.player + "@oasis $>");
      updateOutput(data.info, requestInput);
    });
}

function requestInput(teletype) {
  console.log("AGAIN");
}

@@ -235,18 +235,8 @@
 info
-, callback
 ) %7B%0A
@@ -370,16 +370,17 @@
 t to do?
+ 
 %22);%0A  va
@@ -429,28 +429,8 @@
 m);%0A
-  console.log(dom);%0A
   do
@@ -684,21 +684,402 @@
 %0A      %7D
-%0A  %7D)
+,%0A      callbackNext: function(current, teletype) %7B%0A        var el = $('#output');%0A        el.scrollTop(el.height()); // Auto scroll to the bottom line%0A      %7D%0A  %7D);%0A%7D%0A%0Afunction validCommand(command) %7B%0A    var VALID_COMMANDS = %5B%22north%22, %22south%22, %22east%22, %22west%22, %22up%22, %22down%22,%0A      %22magic%22, %22run%22, %22fight%22, %22tally%22, %22consume%22, %22pick up%22%5D;%0A    return VALID_COMMANDS.indexOf(command) %3E= 0
 ;%0A%7D%0A%0Afun
@@ -1192,16 +1192,40 @@
 .val();%0A
+  if (!command) return;%0A
   consol
@@ -1260,16 +1260,54 @@
 ommand);
+%0A  if (!validCommand(command)) return;
 %0A%0A  $.aj
@@ -1923,91 +1923,16 @@
 info
-, requestInput);%0A    %7D);%0A%7D%0A%0Afunction requestInput(teletype) %7B%0A  console.log(%22AGAIN%22
+);%0A    %7D
 );%0A%7D

Check valid commmands in client-side