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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <html>
  <head>
    <link href='https://fonts.googleapis.com/css?family=Press+Start+2P' rel='stylesheet' type='text/css'/>
    <script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="https://underscorejs.org/underscore-min.js"></script>
</head>
<body>
  <div class="menu">
    <a class="link_player_one player_one" href="#">player 1</a>
    <a class="link_player_two player_two" href="#">player 2</a>
    <a class="link_reset" href="#">reset</a>
  </div>
  <h1 class="battle_alert">Live battle!</h1>
  <hr />
<h2 id="vs"></h2>
  <hr />
  <h3 class="featureing_title"><span>Featureing:</span><p class='featureing'></p></h3>
  <p class='programming'></p>
  
<table cellpadding=5 >
  <thead>
    <tr>
      <th>Skill</th>
      <th class="player_one">Player one</th>
      <th class="player_two">Player two</th>
      <th>Winner</th>
    </tr>
  </thead>
  <tbody>    
  </tbody>
  <tfoot>
    <tr>
      <th colspan=4 class="" style="text-align:right;">Outcome: <span class="overall_winner"></span></th>
    </tr>
  </tfoot>
</table>
<p id="game_details">
  This page shows progress made by each user since the last page reset. <span id="page_load"></span>   
</p>
</body> 
</html>

              
            
!

CSS

              
                body {
  color:green;
  font-family: 'Press Start 2P', cursive;
  background-color:black;
}
#vs, .featureing_title, .outcome{
  text-align:center;
  width:100%;  
}

hr {
  color: green;
  background-color: green;
  height: 5px;
  border:0px;
  width:800px;
}

.battle_alert {
  text-align:center;
  width:100%;  
}
.featureing, .featureing_title {
  text-align:center;
  color:#c412c4;
}

.featureing {
  width:50%;  
}

table, .featureing {
  margin-left:auto;
  margin-right:auto; 
  
}

.player_one {
  color:yellow;
}
.player_two {
  color:red;
}

#game_details {
  font-size:10px;
    text-align:center;
  line-height:200%;
  width:40%;
  margin-left:auto;
  margin-right:auto;
  color:#7E7B7B;
}

.menu {
  text-align:right;
}
              
            
!

JS

              
                $(document).ready(function(){
  
  function chkEmpty(item) {
    console.dir(item);
    if(item == null || item == false || item == undefined){
      console.log('empty');
      
      return true;
    }
    console.log('full');
    return false;
  }
  
  function getJsonStore(key) {
    var item = localStorage.getItem(key); 
    if(!chkEmpty(item)) {  
      //console.dir(item);
      item = JSON.parse(item);
    } 
    return item;
  }
  
    var player_one_username = localStorage.getItem('p1_name'),
    player_two_username = localStorage.getItem('p2_name'),
    player_one, 
    player_two, 
    player_one_init       = getJsonStore('player_one_init'), //these are set once at page load to see progess
    player_one_init_langs = getJsonStore('player_one_init_langs'),
    player_two_init       = getJsonStore('player_two_init'),
    player_two_init_langs = getJsonStore('player_two_init_langs'),
    page_loaded = localStorage.getItem('since');
    
    if(chkEmpty(player_one_username)) {
      player_one_username = "paul";
      localStorage.setItem('p1_name', player_one_username);
    }

    if(chkEmpty(player_two_username)) {
      player_two_username = "michaeljcalkins";
      localStorage.setItem('p2_name', player_two_username);
    }
  
    if(chkEmpty(page_loaded)) {
      page_loaded = new Date();
    }
    
  function reset() {
    page_loaded = new Date();
    
    localStorage.clear();
    localStorage.setItem('since', page_loaded);
    
    player_one_init = false; //these are set once at page load to see progess
    player_one_init_langs = false;
    player_two_init = false;
    player_two_init_langs = false;
    loadUserData();
  }
  
  $('.menu').on('click', '.link_player_one', function(e) {
    e.preventDefault();
    player_one_username = window.prompt('Enter a Codeivate username (see http://www.codeivate.com/users/leaderboard)', player_one_username);
    localStorage.setItem('p1_name', player_one_username);
    reset();
  });
  
  $('.menu').on('click', '.link_player_two', function(e) {
    e.preventDefault();
    player_two_username = window.prompt('Enter a Codeivate username (see http://www.codeivate.com/users/leaderboard)', player_two_username);
    localStorage.setItem('p2_name', player_two_username);
    reset();
  });
  
 $('.menu').on('click', '.link_reset', function(e) {
    e.preventDefault();    
    reset();
  });

  
 function getLanguageDiff(current, init) {
  var pdiff = {};
    _.each(current, function(value, key, list) {
      if( init[key] == undefined ) {
        pdiff[key] = value;
      } else {
         pdiff[key] = value - init[key]; 
      }
      if( pdiff[key] == 0) {
        delete  pdiff[key]; 
      }
    });
    return pdiff;
  }
  
  function battle() {
    var row, winner, item, items,i, win_count = 0, languages_used, p1_diff, p2_diff;

    p1_diff = getLanguageDiff(player_one.used_langs, player_one_init_langs);
    p2_diff = getLanguageDiff(player_two.used_langs, player_two_init_langs);
    
    
    var all_langs_used = _.keys(p1_diff).concat(_.keys(p2_diff));
    languages_used = _.uniq(all_langs_used);
    
      
    

    $('#page_load').html(page_loaded); 

    $('#vs').html("");
    $('#vs').append( $('<span>').html(player_one.name).addClass('player_one'));
    $('#vs').append(" vs ");
    $('#vs').append( $('<span>').html(player_two.name).addClass('player_two'));
       
    $('tbody').empty();

    
    if(_.size(languages_used) == 0) {
       languages_used.push("waiting for coders.."); 
      $(".featureing_title span").hide();
    } else {
      $(".featureing_title span").show(); 
    }
    console.dir(languages_used);
    $('.featureing').html(languages_used.join());
 
    
    
    items = {
      'languages_used' : {type:'calculated', name:'languages used'},
          'language_points':{name:'language points', type:'int'},    
          'level':{name:'level', type:'int'},
          'focus_level':{name:'focus level', type:'int'},
          'focus_points':{name:'focus points', type:'int'},
          //'languages':{name:'languages', type:'int'},
          'total_days_coded':{name:'total days coded', type:'int'},
          'total_flow_states':{name:'total flow states', type:'int'},
          'time_spent':{name:'time spent', type:'time'},
          'programming_now':{name:'programming now', type:'bool'},
          'streaking_now':{name:'streaking now', type:'bool'},
          'current_language':{name:"current language", type:'string'}
    };


    
    for(item in items) {
      var col1 = "", col2 = "";
      winner_class = "";
      row =  $('<tr>'); 
      row.append($('<th>').html(items[item].name));
      
      if(items[item].type === 'calculated') {
        
        col1 = _.size(p1_diff); 
        col2 = _.size(p2_diff);
        
        if( col1 > col2) {
          winner = 'player one'; 
          winner_class = "player_one";
          win_count --;          
        } else if(col1 < col2) {
          winner = 'player two';
          winner_class = "player_two";
          win_count ++;          
        } else if (col1 == 0 && col2 == 0) {
          winner = '-';
          winner_class = "";
        } else {
          winner_class = "";
          winner = 'draw';
        }
        
         
      }  else if (items[item].type === 'int') {

        //console.log("PRE:P1 - "+ player_one[item] + ": P2 - " + player_two[item], item);

        player_one[item] = parseFloat(player_one[item]) - parseFloat(player_one_init[item]);
        player_two[item] = parseFloat(player_two[item]) - parseFloat(player_two_init[item]);
        
        //console.log("POST:P1 - "+ player_one[item] + ": P2 - " + player_two[item], item);

        if( player_one[item] > player_two[item]) {
          winner = 'player one';
          winner_class = "player_one";
          win_count --;
          
        } else if(player_two[item] > player_one[item]) {
          winner = 'player two';
          winner_class = "player_two";
          win_count ++;          
        } else if (player_one[item] == 0 && player_two[item] == 0) {
          winner = '-';
          winner_class = "";
        } else {
          winner_class = "";
          winner = 'draw';
        }
      
        col1 = Math.floor(player_one[item]) + " (" + Math.floor((player_one[item] - Math.floor(player_one[item])) * 100) + "%)" ;
        col2 = Math.floor(player_two[item]) + " (" + Math.floor((player_two[item] - Math.floor(player_two[item])) * 100) + "%)" ;

      } else if (items[item].type === 'time') {

        winner = "&nbsp;";
        
        col1 = Math.floor(player_one[item]/60/60) + "h " + Math.floor(player_one[item]/60%60) + "m";
        col2 = Math.floor(player_two[item]/60/60) + "h "+ Math.floor(player_two[item]/60%60) + "m";

      } else {
        winner = "&nbsp;"; // td's can't be empty
        col1 = player_one[item];
        col2 = player_two[item];
      }

      row.append($('<td>').addClass('player_one').html(col1));
      row.append($('<td>').addClass('player_two').html(col2));
      row.append($('<td>').removeClass().addClass(winner_class).html(winner));
      $('tbody').append((row) );
    }
  
    if(win_count > 0) {
      overall_winner = "player two FTW!";
      win_class = "player_two";
    } else if(win_count < 0) {
      overall_winner = "player one FTW!";
      win_class = "player_one";
    } else {
      win_class= "";
      overall_winner = "draw!";
    }
    $('.overall_winner').html(overall_winner).removeClass('player_one').removeClass('player_two').addClass(win_class);

  }
 
  function loadUserData() {
      console.log('loading user data..');
      $.getJSON('http://codeivate.com/users/'+player_one_username+'.json?callback=?',
        function data(player_one_data) {
          
           player_one_data['language_points'] = _.reduce(player_one_data.languages, function(memo, details, language){return memo + details.points; }, 0);    
            player_one_data['used_langs'] = {};
             _.each(player_one_data.languages, function(value, key, list) {
                player_one_data['used_langs'][key] = value.points;    
              });
      

             player_one = player_one_data;

          if(chkEmpty(player_one_init)) {   
                      
            player_one_init_langs = _.clone(player_one_data['used_langs']);
            player_one_init = _.clone(player_one_data); //this is only a shallow copy..

            localStorage.setItem('player_one_init', JSON.stringify(player_one_init));
            localStorage.setItem('player_one_init_langs', JSON.stringify(player_one_init_langs));
          }

          $.getJSON('http://codeivate.com/users/'+player_two_username+'.json?callback=?',
            function data(player_two_data) {
              player_two_data['language_points'] = _.reduce(player_two_data.languages, function(memo, details, language){ return memo + details.points;}, 0);    
              
              player_two_data['used_langs'] = {};
              _.each(player_two_data.languages, function(value, key, list) {
                player_two_data['used_langs'][key] = value.points;    
              }); 
                            
              player_two = player_two_data;
             
              if(chkEmpty(player_two_init)) { 
                player_two_init_langs = _.clone(player_two_data['used_langs']);
                player_two_init = _.clone(player_two_data); //this is only a shallow clone..
       

                localStorage.setItem('player_two_init', JSON.stringify(player_two_init));
                localStorage.setItem('player_two_init_langs', JSON.stringify(player_two_init_langs));
              }              
              battle();
            }
          );
      });
  }
  
  loadUserData();
  setInterval( loadUserData, 30000);

});
              
            
!
999px

Console