-
Notifications
You must be signed in to change notification settings - Fork 0
/
bracket.js
77 lines (71 loc) · 2.67 KB
/
bracket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return "";
}
var modifyDOM = function (data) {
// console.log(data);
// modify bracket DOM
var bitstringIndex = 1;
var rndnum = 2;
data['matches'].forEach(function (rnd) {
var matchnum = 1; // matchnum % 2 == 1 for slot 1s, matchnum % 2 == 0 for slot 2s
rnd.forEach(function (match) {
match.forEach(function (team) {
var sel = "#" + matchnum + "r" + rndnum;
// console.log("Processing " + sel);
$(sel + " .name").html(team['name']);
$(sel + " .seed").html(team['seed']);
if (parseInt(data['bitstring'][bitstringIndex]) % 2 == (matchnum-1) % 2) {
// console.log(sel + " is a winner");
$(sel).addClass("winner");
}
matchnum += 1;
});
bitstringIndex += 1;
});
rndnum += 1;
});
var champion = data['matches'][data['matches'].length-1][0][parseInt(data['bitstring'][63])];
$("#champion .name").html(champion['name']);
$("#champion .seed").html(champion['seed']);
// HARDCODED: SUBJECT TO CHANGE!
$("#first-region").html("MIDWEST");
$("#second-region").html("WEST");
$("#third-region").html("SOUTH");
$("#fourth-region").html("EAST");
// modify debug DOM
$(".debug #time").html("This bracket was generated on " + data['timestamp'] + " UTC");
var score = data['score'];
var gamesCorrect = data['gamesCorrect'];
var gamesCorrectList = data['gamesCorrectList'];
if (data['isNew']) {
// $(".debug #link a").attr("href", "/cgi-bin/bracket.py?id=" + data['uniqueID']);
$(".debug #link").html("<a href='/bracket.html?id=" + data['uniqueID'] + "'>Permalink to generated bracket</a>");
$("#refreshmessage a").html("Your bracket would have scored " + score + " points and had " + gamesCorrect + " out of 63 games correct. (" + gamesCorrectList + ") Refresh this page for another bracket");
} else {
$(".debug #link").html("<a href='/bracket.html'>Generate another bracket</a>");
$("#refreshmessage a").html("Your bracket would have scored " + score + " points and had " + gamesCorrect + " out of 63 games correct. (" + gamesCorrectList + ") Refresh this page for another bracket");
}
// tweet button
$("iframe").attr("src", $("iframe").attr("src") + "%3Fid%3D" + data['uniqueID']);
};
$(document).ready(function () {
var URL = "http://bracketodds.cs.illinois.edu/cgi-bin/api.py";
var id = getQueryVariable("id");
if (id) {
URL += ("?id=" + id);
}
$.ajax({
url: URL,
dataType: "json",
success: function (data) {
modifyDOM(data);
}
});
});