Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
132 lines (124 loc) · 5.32 KB
/
Copy pathindex.html
File metadata and controls
132 lines (124 loc) · 5.32 KB
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
129
130
131
132
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NHIE</title>
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet"> </head>
<body>
<h1 id="title"></h1>
<div class="wrapper">
<div class="reg" id="reg">
<select style="margin-bottom: 10px;max-width: 202px;" id="languages" onchange="switchLang(this.value)">
<option style="display:none;" selected>Select language</option>
</select>
<form id="usersRegForm">
<div id="userInputs">
<div id="userInput">
<input type="text" class="usernameInput" name="username" placeholder="Player Name" pattern="[a-zA-Z]+" min=3 required>
<button type="button" class="removebtn" onclick="this.parentNode.remove();" style="display:none;">❌</button>
</div>
</div>
</form>
<div style="margin-top: 16px">
<button class="purple-button" onclick="addUserFields();"> + </button>
<button class="purple-button" onclick="startGame();"> Start the Game </button>
</div>
</div>
<div style="display:none;" class="questions" id="questions">
<p id="question"></p>
<div class="players">
<div class="player">
<input style="display:none" id="chk1" type="checkbox">
<label for="chk1"> <span class="text">GENERIC</span> </label>
</div>
</div>
<button class="purple-button" onclick="showNextQuestion();">Next Question</button>
<hr>
<button class="purple-button" onclick="if (confirm('Are you sure you want to finish the game?')) return finish();">Finish the Game</button>
</div>
<div style="display: none;" class="result" id="results"> </div>
<script src="main.js"></script>
<script>
document.getElementById("title").innerText = `${nhie[language]}...`;
for(var i = 0; i < languages.length; i++) {
document.getElementById("languages").insertAdjacentHTML('beforeend', '<option value="'+languages[i]+'">'+languages[i]+'</option>');
}
function addUserFields() {
var userInputs = document.getElementById("userInputs");
var clonedNode = userInputs.lastElementChild.cloneNode(true);
clonedNode.firstElementChild.value = "";
clonedNode.lastElementChild.style.removeProperty('display');
userInputs.appendChild(clonedNode);
}
function startGame() {
if(document.getElementById("usersRegForm").reportValidity()) {
let inputs = document.getElementsByClassName("usernameInput");
let userarray = [];
for(var i = 0; i < inputs.length; i++) {
userarray.push(inputs[i].value)
}
addPlayers(userarray);
generatePlayerButtons(userarray);
showNextQuestion();
document.getElementById("reg").style.display = "none";
document.getElementById("questions").style.removeProperty('display');
window.onbeforeunload = function() { return "Reloading the page results in losing progress."; }
}
}
function showNextQuestion() {
saveScoresOfUsers();
var questionEl = document.getElementById("question");
var randQuestion = pickQuestion();
if(randQuestion[0] == -1) return finish();
questionEl.innerText = randQuestion[1];
questionEl.setAttribute("qid", randQuestion[0]);
}
function generatePlayerButtons(userarray) {
var completedUsers = [];
var playersWrapper = document.getElementsByClassName("players")[0];
for(var i = 0; i < userarray.length; i++) {
var playerButton = playersWrapper.cloneNode(true);
playerButton.style.removeProperty('display');
playerButton.firstElementChild.firstElementChild.classList.add("playerbox");
if(!completedUsers.includes(userarray[i])) {
playerButton.firstElementChild.firstElementChild.id = userarray[i];
playerButton.firstElementChild.lastElementChild.setAttribute('for', userarray[i]);
playerButton.firstElementChild.lastElementChild.firstElementChild.innerText = userarray[i];
playersWrapper.appendChild(playerButton.firstElementChild);
completedUsers.push(userarray[i]);
}
}
playersWrapper.firstElementChild.remove();
}
function saveScoresOfUsers() {
var userCheckboxes = document.getElementsByClassName("playerbox");
var quesiton = document.getElementById("question").getAttribute("qid");
var checkedUsers = [];
for(var i = 0; i < userCheckboxes.length; i++) {
if(userCheckboxes[i].checked == true) {
checkedUsers.push(userCheckboxes[i].id);
userCheckboxes[i].checked = false;
}
}
addScore(checkedUsers, question);
}
function finish() {
saveScoresOfUsers();
var resultsWrapper = document.getElementById("results");
var scores = genScoreboard();
for(var i = 0; i < scores.length; i++) {
resultsWrapper.insertAdjacentHTML('beforeend', '<p>' + (i + 1) + '. <b>' + scores[i][0] + '</b> ' + scores[i][1].questions.length + '</p>');
}
resultsWrapper.insertAdjacentHTML('beforeend', '<button class="purple-button" onclick="location.reload();">Start a new game</button>');
document.getElementById("questions").style.display = "none";
resultsWrapper.style.removeProperty('display');
window.onbeforeunload = null;
}
</script>
</body>
</html>