Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//cdn.pubnub.com/pubnub.min.js"></script>

<div class="container-fluid">
  <div class="row">
    <div class="col-md-12">
      <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
          <div class="navbar-header">
            <a class="navbar-brand">MemeWarz!</a>
          </div>
          <div class="navbar-form navbar-left">
            <button type="submit" class="btn btn-default" id="find-match">Find Match</button>
          </div>
          <ul class="nav navbar-nav navbar-right">
            <li>
              <a href="#">
                You are <strong id="whoami"></strong> 
                <span id="my_skill" class="badge"></span>
              </a>
            </li>
          </ul>
        </div>
      </nav>
    </div>
  </div>
  <div class="row">
    <div class="col-md-6">
      <div class="panel panel-default">
        <div class="panel-heading">Online Users</div>
        <div class="list-group" id="online-users"></div>
      </div>
    </div>
    <div class="col-md-6">
      <div class="panel panel-default">
        <div class="panel-heading">Lobby Chat</div>
        <ul class="list-group" id="chat-output"></ul>
        <div class="panel-body">
          <form id="chat">
            <div class="input-group">
              <input type="text" class="form-control" id="chat-input" />
              <span class="input-group-btn">
                <button type="submit" class="btn btn-default">Send Message</button>
              </span>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
              
            
!

CSS

              
                body {
  padding: 10px;
}
#chat-output, .private-chat-output {
  height: 200px;
  overflow-y: scroll;
}
#online-users .list-group-item {
  cursor: pointer;
  user-select: none;
}
#online-users .list-group-item:hover {
  text-decoration: underline;
}
.modal .panel {
  margin-bottom: 0px;
}
.modal .modal-footer {
  margin-top: 0px;
}
              
            
!

JS

              
                var pubnub = null;
var me = null;
var Users = null;

///21
var channel = 'memewarz-lobby-demo-24';

var $online_users = $('#online-users');
var $input = $('#chat-input');
var $output = $('#chat-output');

var randomName = function() {

  var animals = ['pigeon', 'seagull', 'bat', 'owl', 'sparrows', 'robin', 'bluebird', 'cardinal', 'hawk', 'fish', 'shrimp', 'frog', 'whale', 'shark', 'eel', 'seal', 'lobster', 'octopus', 'mole', 'shrew', 'rabbit', 'chipmunk', 'armadillo', 'dog', 'cat', 'lynx', 'mouse', 'lion', 'moose', 'horse', 'deer', 'raccoon', 'zebra', 'goat', 'cow', 'pig', 'tiger', 'wolf', 'pony', 'antelope', 'buffalo', 'camel', 'donkey', 'elk', 'fox', 'monkey', 'gazelle', 'impala', 'jaguar', 'leopard', 'lemur', 'yak', 'elephant', 'giraffe', 'hippopotamus', 'rhinoceros', 'grizzlybear'];

  var colors = ['silver', 'gray', 'black', 'red', 'maroon', 'olive', 'lime', 'green', 'teal', 'blue', 'navy', 'fuchsia', 'purple'];

  return colors[Math.floor(Math.random() * colors.length)] + '_' + animals[Math.floor(Math.random() * animals.length)];

};

var randomSkill = function() {
  return Math.floor(Math.random() * 3) + 1;
};

var User_factory = function() {

  var user_list = {};
  var self = this;

  self.remove = function(uuid) {
    delete user_list[uuid];
  };

  self.get = function(uuid) {
    if(user_list.hasOwnProperty(uuid)) {
      return user_list[uuid];
    } else {
      console.error('Trying to retrieve user that is not present.');
    }
  };

  self.set = function(uuid, data) {
    if(!user_list.hasOwnProperty(uuid)) {
      user_list[uuid] = new User(uuid, data);
    }
    return user_list[uuid];
  };
  
  self.all = function() {
    return user_list;
  }

};
  
var User = function(uuid, state) {

  var self = this;

  self.uuid = uuid || randomName();
  self.state = state || {skill: randomSkill()};

  var $tpl = $('\
    <li id="' + self.uuid + '" class="list-group-item"> \
    <span class="badge">' + self.state.skill + '</span> \
    ' + self.uuid + ' \
    </li>');

  self.chat = function(text, $target) {

    var $line = $('<li class="list-group-item"><strong>' + self.uuid + ':</strong> </span>');
    var $message = $('<span class="text" />').text(text).html();

    $line.append($message);
    $target.append($line);

    $target.scrollTop($target[0].scrollHeight);

  };

  self.leave = function() {
    $tpl.remove();
  };

  self.init = function() {  

    $tpl.click(function() {
      me.private_chat(self);
      return false;
    });
    
    $('#online-users').append($tpl);

  };

  return self;

};

var Client = function() {
  
  var self = new User(randomName());
  
  self.on_request = function(caller) {

    var response = confirm(caller.uuid + ' is challenging you to a match! Press OK to accept or Cancel to deny.');

    pubnub.publish({
      channel: channel,
      message: {
        type: 'challenge',
        payload: {
          action: 'response',
          accepted: response,
          uuid: self.uuid,
          target: caller.uuid
        }
      },
      callback: function(){
        alert('Your response has been sent.');
      }
    });
  };
  
  self.on_response = function(caller, accepted) {
    if(accepted) {
      alert(caller.uuid + ' has accepted your challenge!');
    } else {
      alert(caller.uuid + ' has rejected your challenge!');
    }
  };
  
  self.private_chat = function(target) {
    
    // open a modal for ourselves
 	new PrivateChat(target);

    // tell the other user to open the modal too
    pubnub.publish({
      channel: channel,
      message: {
        type: 'new-private-chat',
        payload: {
          uuid: self.uuid,
          target: target.uuid
        }
      }
    });
    
  };
  
  Users.set(self.uuid, self.state);
  
  return self;
  
};

var PrivateChat = function(user) {
  
  var self = this;
  
  var $tpl = $('\
    <div class="modal fade"> \
      <div class="modal-dialog"> \
        <div class="modal-content"> \
          <div class="modal-header"> \
            <button type="button" class="close" data-dismiss="modal">&times;</button> \
            <h4 class="modal-title">' + user.uuid + ' <span class="badge">' + user.state.skill + '</span></h4> \
          </div> \
          <div class="modal-body"> \
            <div class="panel panel-default"> \
              <div class="panel-heading">Private Chat</div> \
              <ul class="list-group private-chat-output"></ul> \
              <div class="panel-body"> \
                <form class="private-chat"> \
                  <div class="input-group"> \
                    <input type="text" class="form-control private-chat-input" /> \
                    <span class="input-group-btn"> \
                      <button type="submit" class="btn btn-default">Send Message</button> \
                    </span> \
                  </div> \
                </form> \
              </div> \
            </div> \
          </div> \
          <div class="modal-footer"> \
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> \
            <button type="button" class="challenge-user btn btn-primary">Challenge</button> \
          </div> \
        </div> \
      </div> \
    </div>');
  
  var $chat_form = $tpl.find('.private-chat');
  var $output = $tpl.find('.private-chat-output');
  var $input = $tpl.find('.private-chat-input');
  var $challenge = $tpl.find('.challenge-user');
  
  private_channel = channel + ':' + [me.uuid, user.uuid].sort().join(':');
  
  self.hide = function(callback) {
    $tpl.on('shown.bs.modal', callback);
    $tpl.modal('hide');
  };
  
  self.show = function(callback) {
    $tpl.on('hidden.bs.modal', callback);
    $tpl.modal('show');
  };
  
  var init = function() {
  	      
    pubnub.grant({
      channel: private_channel,
      auth_key: me.uuid,
      read: true,
      write: true,
      ttl: 0,
      callback: function() {
        
        $('body').append($tpl);

        pubnub.subscribe({
          channel: private_channel,
          message: function(data) {   
            Users.get(data.payload.uuid).chat(data.payload.text, $output);

          }
        });

        $chat_form.submit(function() {
          pubnub.publish({
            channel: private_channel,
            message: {
              type: 'private-chat',
              payload: {
                text: $input.val(),
                uuid: me.uuid
              }
            }
          });

          $input.val('');

          return false;

        });

        $challenge.click(function(){

          pubnub.publish({
            channel: channel,
            message: {
              type: 'challenge',
              payload: {
                action: 'request',
                uuid: me.uuid,
                target: user.uuid
              }
            }
          });

          alert('Challenging ' + user.uuid + '...');

        });

        self.show();
    
      }
    });
    
  };
  
  init();
  
  return self;
  
};

var App = function() {
  
  Users = new User_factory();
  me = new Client();

  pubnub = PUBNUB.init({
    publish_key: 'pub-c-c89a5671-7237-44db-8885-6d1d53e26090',
    subscribe_key: 'sub-c-d227fd0a-0865-11e4-8e62-02ee2ddab7fe',
    secret_key: 'sec-c-NjcyM2EwM2UtNjEzOC00ZTc1LWJmZGYtYzE1N2NkOTEyZThl',
    uuid: me.uuid,
    auth_key: me.uuid,
    ssl: true
  });
  
  pubnub.grant({
    channel: channel,
    auth_key: me.uuid,
    read: true,
    write: true,
    ttl: 0,
    callback: function() {
      
      pubnub.grant({
        channel: channel + '-pnpres',
        auth_key: me.uuid,
        read: true,
        write: true,
        ttl: 0,
        callback: function() {

          pubnub.subscribe({
            channel: channel,
            state: me.state,
            message: function(data) {

              if(data.type == 'chat') {    
                Users.get(data.payload.uuid).chat(data.payload.text, $output);
              }

              if(data.payload.target == me.uuid) {    

                if(data.type == 'new-private-chat') {

                  new PrivateChat(Users.get(data.payload.uuid));

                } 

                if(data.type == 'challenge') {

                  var challenger = Users.get(data.payload.uuid);

                  if(data.payload.action == 'request') {
                    me.on_request(challenger);
                  }

                  if(data.payload.action == 'response') {
                    me.on_response(challenger, data.payload.accepted);
                  }

                }

              }

            }, 
            presence: function(data) {

              console.log(data);

              if(data.action == "join") {
                Users.set(data.uuid, data.state).init();
              }

              if(data.action == "leave" || data.action == "timeout") {
                Users.remove(data.uuid);
              }

            }
          });

          $('#find-match').click(function(){

            var matches = [];
            var users = Users.all();

            for(var uuid in users) {
              if(uuid !== me.uuid && users[uuid].state.skill == me.state.skill) {
                matches.push(users[uuid]);
              }
            }

            if(!matches.length) {
              alert('Nobody is online with the same skill level.');
            } else {
              var opponent = matches[Math.floor(Math.random() * matches.length)];
              alert('Opponent Found: ' + opponent.uuid + ' with skill level ' + opponent.state.skill);
            }

          });

          $('#chat').submit(function() {

            pubnub.publish({
              channel: channel,
              message: {
                type: 'chat',
                payload: {
                  text: $input.val(),
                  uuid: me.uuid
                }
              }
            });

            $input.val('');

            return false;

          });

          $('#whoami').text(me.uuid);
          $('#my_skill').text(me.state.skill);

        }
      });
    }
  });
}

App();
              
            
!
999px

Console