singleElim

By: a guest on Dec 20th, 2012  |  syntax: JavaScript  |  size: 1.84 KB  |  views: 176  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. var seeds = [
2.       1,2,3,4,5,6,7,8
3.     ],
4.     num_rounds = Math.log(seeds.length)/Math.log(2);
5.
6. // 2-dimensional array
7. // Each subarray holds the seeds active in that round in order of matches played
8. // Example:
9. // seeds in first match of 2nd round are: seed_round[1][0] & seed_round[1][1]
10. var seed_rounds = [];
11.
12. // Create empty arrays inside seed_round
13. for(var i = 0; i < num_rounds; i++) {
14.   seed_rounds[i] = [];
15. }
16.
17. // Assuming no upsets
18. // Final is seed 1 and seed 2
19. seed_rounds[num_rounds] = [ seeds[0], seeds[1] ];
20.
21. // For each round in the bracket
22. for(var r = num_rounds; r > 0; r--) {
23.   var round = seed_rounds[r];
24.   var feed_round = seed_rounds[r - 1];
25.
26.   // For each seed in the round, work out who they defeated in previous round, storing result
27.   for(var m = 0; m < round.length; m++) {
28.     var num_teams_in_round = round.length * 2;
29.     // Feeder match seed A = current match seed "m"
30.     feed_round[m * 2] = round[m];
31.
32.     // Feeder match seed B = # teams in feeder round plus 1 minus current match seed "m"
33.     feed_round[(m * 2) + 1] = num_teams_in_round + 1 - round[m];
34.   }
35. }
36.
37. var firstHalf = [];
38. var secondHalf = [];
39.
40. for(var i = 0; i < (seeds.length / 2); i++) {
41.   firstHalf[i] = seed_rounds[1][i];
42. }
43.
44. for(var i = 0; i < num_rounds; i++) {
45.   secondHalf[i] = seed_rounds[1].slice((seeds.length / 2), (seeds.length));
46. }
47.
48. secondHalf[1].reverse();
49.
50. var newSeeds = firstHalf.concat(secondHalf[1]);
51.
52. var singleElim8Data = {
53.         teams : [              // Matchups
54.                 [ newSeeds[0],  newSeeds[1]  ],
55.     [ newSeeds[2],  newSeeds[3]  ],
56.     [ newSeeds[4],  newSeeds[5]  ],
57.     [ newSeeds[6],  newSeeds[7]  ]
58.   ],
59.   results : [[
60.       [ [1, 0], [1, 0], [0, 1], [0,1] ],
61.       [ [1, 0], [0, 1] ],
62.       [ [1, 0], [0, 1] ]
63.     ]]
64. }
65.
66. \$(function() {
67.         \$('#singleElim8').bracket({
68.                 init: singleElim8Data
69.         })
70. })
clone this paste RAW Paste Data
Top