-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamescript.js
115 lines (103 loc) · 3.68 KB
/
gamescript.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
/**
* @fileoverview client side JS for game
*/
var socket = io.connect();
const username = getUsername();
var image = document.getElementById("image");
var choiceA = document.getElementById("aValue");
var choiceB = document.getElementById("bValue");
var choiceC = document.getElementById("cValue");
var choiceD = document.getElementById("dValue");
var radios = document.getElementsByName("answer");
var radA = document.getElementById("A");
var radB = document.getElementById("B");
var radC = document.getElementById("C");
var radD = document.getElementById("D");
var feedback = document.getElementById("feedback");
var starImages = document.getElementById("starImageDisplay");
var questionCounter = document.getElementById("questionNumber");
var whyAmIEvenKeepingTrackOfThis = document.getElementById("submitOrNext");
let questionNumber = 0;
/**
* Parses cookies and stuff to figure out what the username is
* @returns username of the user
*/
function getUsername() {
let ret = undefined;
const allCookies = decodeURIComponent(document.cookie).split("; ");
for(let i = 0;i < allCookies.length;i++) {;
if (allCookies[i].split("=")[0] === "userData") {
let start = allCookies[i].split("=")[1].indexOf("\"username\":\"") + "\"username\":\"".length;
let end = allCookies[i].split("=")[1].indexOf(",") - 1;
ret = allCookies[i].split("=")[1].substring(start, end);
}
}
return ret;
}
/**
* Requests a single question
*/
function requestLevel() {
questionNumber++;
questionCounter.innerHTML = `Question number: ${questionNumber}`;
socket.emit("request question", username);
console.log("sent request");
}
/**
* Submits the current answer
*/
function submitQuestion() {
for(let i = 0;i < radios.length;i++) {
if (radios[i].checked) {
socket.emit("answer", {username: username, image: image.src, answer: radios[i].value});
}
}
}
/**
* Handles what happens when button is clicked
*/
function dealWithButton() {
if (whyAmIEvenKeepingTrackOfThis.innerHTML === "Submit") {
submitQuestion();
whyAmIEvenKeepingTrackOfThis.innerHTML = "Next";
} else if (whyAmIEvenKeepingTrackOfThis.innerHTML === "Next") {
requestLevel();
whyAmIEvenKeepingTrackOfThis.innerHTML = "Submit";
feedback.innerHTML = "";
} else if (whyAmIEvenKeepingTrackOfThis.innerHTML === "Done") {
window.location.href = "/dashboard";
}
}
socket.on("question", (data) => {
image.src = `Images/${data.image}`;
let questions = data.questions;
choiceA.innerHTML = questions[0];
radA.value = questions[0];
choiceB.innerHTML = questions[1];
radB.value = questions[1];
choiceC.innerHTML = questions[2];
radC.value = questions[2];
choiceD.innerHTML = questions[3];
radD.value = questions[3];
});
socket.on("results", (response) => {
feedback.innerHTML = response.feedback;
if (!response.correct) {
console.log(starImages.getAttribute("src"));
if (starImages.getAttribute("src") === "./Images/StarSystem/threestars.png") {
starImages.src = "./Images/StarSystem/twostars.png";
} else if (starImages.getAttribute("src") === "./Images/StarSystem/twostars.png") {
starImages.src = "./Images/StarSystem/onestar.png";
} else if (starImages.getAttribute("src") === "./Images/StarSystem/onestar.png") {
starImages.src = "./Images/StarSystem/zerostars.png";
}
}
});
socket.on("done", (passStatus) => {
if (passStatus) {
window.location.href = "/Congratulations.html";
} else {
window.location.href = "/Fail.html";
}
whyAmIEvenKeepingTrackOfThis.innerHTML = "Done";
});