-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
129 lines (109 loc) · 3.61 KB
/
script.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var blockBuzz = true,
answerValue = 0,
questionsJson,
introClick = 1;
var onKeyPress = function(e) {
if(e.keyCode > 48 && e.keyCode < 52) {
triggerPlayer(e.keyCode - 48);
}
};
var onDocumentClick = function(e) {
if(introClick == 1) {
try {
switchOnLED(null);
} catch(e) {}
Array.prototype.forEach.call(document.querySelectorAll("#players input"), function(input) {
input.readOnly = true;
});
}
if(introClick < 7) {
var elem = document.getElementById("column" + introClick).children[0];
if(elem.classList.contains("highlight")) {
var x = elem.offsetLeft + elem.offsetWidth / 2;
var y = elem.offsetTop - elem.offsetHeight / 2;
elem.children[1].style.transformOrigin = x + "px " + y + "px 0px";
elem.classList.remove('highlight');
elem.classList.add('visible');
introClick++;
}
else {
document.getElementById("column" + introClick).children[0].classList.add('highlight');
}
}
if(introClick == 7) {
setTimeout(function() {
document.getElementById("board").classList.add("start");
}, 1500);
}
};
var onPlayerClick = function(e) {
if(e.currentTarget.classList.contains("buzzed")) {
e.currentTarget.children[1].textContent = parseInt(e.currentTarget.children[1].textContent, 10) + answerValue;
e.currentTarget.classList.remove("buzzed");
answerValue = 0;
switchOnLED(null);
}
else {
try {
document.querySelector(".buzzed").classList.remove("buzzed");
blockBuzz = false;
switchOnLED(null);
}
catch(e) {}
}
};
var onCardClick = function(e) {
if(e.currentTarget.classList.contains("open")) {
e.currentTarget.classList.remove("open")
e.currentTarget.classList.add("show");
}
else if(e.currentTarget.classList.contains("show")) {
e.currentTarget.classList.remove("show")
e.currentTarget.classList.add("done");
}
else if(introClick >= 7) {
var x = e.currentTarget.offsetLeft + e.currentTarget.offsetWidth / 2;
var y = e.currentTarget.offsetTop + e.currentTarget.offsetHeight / 2;
e.currentTarget.children[2].style.transformOrigin = x + "px " + y + "px 0px";
e.currentTarget.classList.add("open");
blockBuzz = false;
answerValue = parseInt(e.currentTarget.children[0].textContent, 10);
try {
switchOnLED(null);
}
catch(e) {}
try {
document.querySelector(".buzzed").classList.remove("buzzed");
}
catch(e) {}
}
};
var triggerPlayer = function(player_number) { // player_numbers: 1, 2, 3
if(blockBuzz) {
return;
}
blockBuzz = true;
document.getElementById('bsound').play();
document.getElementById("player" + player_number).classList.add("buzzed");
switchOnLED(player_number-1);
}
document.onkeypress = onKeyPress;
document.getElementById("board").onclick = onDocumentClick;
Array.prototype.forEach.call(document.getElementById("players").childNodes, function(player) {
player.onclick = onPlayerClick;
});
Array.prototype.forEach.call(document.querySelectorAll(".card"), function(card) {
card.onclick = onCardClick;
});
loadQuestions();
// Ajax
function loadQuestions() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
questionsJson = xhttp.responseText;
}
}
xhttp.open("GET", "data.json", true);
xhttp.send();
}