Seeding

by anup1986

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="list">

</div>

JavaScript

function orderList(listToOrder){
	return _.chain(listToOrder).groupBy(function(x) { return x.id; }).sortBy(function(x){ return -x.length;}).flatten().value();
};
var knownBrackets = [2, 4, 8, 16, 32, 64, 128];
var seedings = {
  seeds2: [1, 2],
  seeds4: [1, 4, 2, 3],
  seeds8: [1, 8, 4, 5, 3, 6, 2, 7],
  seeds16: [1, 16, 8, 9, 4, 13, 5, 12, 2, 15, 7, 10, 3, 14, 6, 11],
  seeds32: [1, 32, 16, 17, 8, 25, 9, 24, 4, 29, 13, 20, 5, 28, 12, 21, 2, 31, 15, 18, 7, 26, 10, 23, 3, 30, 14, 19, 6, 27, 11, 22],
  seeds64: [1, 64, 32, 33, 16, 49, 17, 48, 8, 57, 25, 40, 9, 56, 24, 41, 4, 61, 29, 36, 13, 52, 20, 45, 5, 60, 28, 37, 12, 53, 21, 44, 2, 63, 31, 34, 15, 50, 18, 47, 7, 58, 26, 39, 10, 55, 23, 42, 3, 62, 30, 35, 14, 51, 19, 46, 6, 59, 27, 38, 11, 54, 22, 43],
  seeds128: [1, 128, 64, 65, 32, 97, 33, 96, 16, 113, 49, 80, 17, 112, 48, 81, 8, 121, 57, 72, 25, 104, 40, 89, 9, 120, 56, 73, 24, 105, 41, 88, 4, 125, 61, 68, 29, 100, 36, 93, 13, 116, 52, 77, 20, 109, 45, 84, 5, 124, 60, 69, 28, 101, 37, 92, 12, 117, 53, 76, 21, 108, 44, 85, 2, 127, 63, 66, 31, 98, 34, 95, 15, 114, 50, 79, 18, 111, 47, 82, 7, 122, 58, 71, 26, 103, 39, 90, 10, 119, 55, 74, 23, 106, 42, 87, 3, 126, 62, 67, 30, 99, 35, 94, 14, 115, 51, 78, 19, 110, 46, 83, 6, 123, 59, 70, 27, 102, 38, 91, 11, 118, 54, 75, 22, 107, 43, 86]};

function seedList(listOfCompetitors) {
  var shuffled = orderList(listOfCompetitors);
  var closest = _.find(knownBrackets, function(knownBracket) {
    return knownBracket >= shuffled.length;
  });

  //fillList(shuffled, closest);
  printList(shuffled);
  
  var seeds = seedings['seeds' + closest.toString()];
  _.each(shuffled, function(competitor, index) {
    _.extend(competitor, {
      position: seeds[index]
    });
  });

  return shuffled;
};

function fillList(listToFill, fillTo) {
  while (listToFill.length < fillTo) {
    listToFill.push({id: 0});
  }
};

function printList(listToPrint){
var div = $('#list');
	_.each(listToPrint, function(p){
  	div.append(p.id +...