Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jeffalo
GitHub Repository: jeffalo/kahoot-gui
Path: blob/master/yeeter.js
1754 views
1
var Kahoot = require("kahoot.js-updated");
2
const UserInterface = require("./UserInterface.js");
3
const Functions = require("./Functions.js");
4
var clients = [];
5
var currentBotCount = 0;
6
var gamePin = null;
7
var botName = "Something Broke";
8
var joinSpeed = "100";
9
var amountOfBotsToJoin = 100; //amount of bots that join (dont do more than 2000)
10
var currentStatus = "\n" + document.getElementById("status").innerHTML;
11
var toldToStop = false;
12
var selectedBotNamingOption = null;
13
var quizName = document.getElementById("quiz-name");
14
var questionAmount = document.getElementById("question-amount");
15
var currentQuestion = document.getElementById("current-question");
16
var useHydraMode = null;
17
var hydraAmount = 0;
18
19
function stop() {
20
// Stops the bot joining process
21
toldToStop = true;
22
Functions.status(
23
"bot spawner murdered; a grand total of " +
24
currentBotCount +
25
" bots joined, however a couple more might have slipped through."
26
);
27
}
28
29
async function leave() {
30
// Make all bots leave the game instance
31
for (client in clients) {
32
await clients[client].leave();
33
}
34
}
35
36
function scrollTop() {
37
// Scrolls to the top of the application
38
var x = document.getElementById("settings");
39
x.style.display = "none";
40
document.location.href = "#top";
41
}
42
43
function chooseBotNamingOption(name, currentBotCount, times) {
44
// See which input is checked and then choose the corresponding bot naming procedure
45
if (
46
selectedBotNamingOption == "prefix-number" ||
47
selectedBotNamingOption == null
48
) {
49
let bot = Functions.simpleBotName(name, currentBotCount);
50
return bot;
51
} else if (selectedBotNamingOption == "number-suffix") {
52
let bot = Functions.simpleBotName(currentBotCount, name);
53
return bot;
54
} else {
55
let bot = botNameFromList(currentBotCount - 1, times);
56
return bot;
57
}
58
}
59
60
function botNameFromList(index, times) {
61
// Take the value of the textarea, convert to array of string,
62
// and then return names in order
63
// Will loop over the name as many times as requested in the "number of bots" field
64
var botNameList = document.getElementById("bot-name-list").value;
65
botNameList = botNameList.split("\n");
66
index >= botNameList.length - 1
67
? (currentBotCount = 0)
68
: (currentBotCount = currentBotCount);
69
if (times > 1) {
70
return botNameList[index] + times;
71
} else {
72
return botNameList[index];
73
}
74
}
75
function timer(gamePin, name, numberOfBotsToJoin, joinSpeed) {
76
currentBotCount = 0;
77
// Added times currentBotCount to count how many times the program went over the specified list of names
78
var times = 0;
79
var timer = setInterval(function() {
80
currentBotCount == 0 ? (times += 1) : (times = times);
81
currentBotCount++;
82
// Do the check before the join function so that it stops at the right moment
83
if (selectedBotNamingOption == "name-list") {
84
if (times > numberOfBotsToJoin || toldToStop == true) {
85
clearInterval(timer);
86
return;
87
}
88
} else if (
89
currentBotCount - 1 >= numberOfBotsToJoin ||
90
toldToStop == true
91
) {
92
clearInterval(timer);
93
return;
94
}
95
let botName = chooseBotNamingOption(name, currentBotCount, times);
96
Functions.status("Bot (" + botName + ") recived instructions to join.");
97
joinKahoot(gamePin, botName);
98
}, joinSpeed); //joinSpeed goes here please don't do less than 75
99
}
100
101
function joinKahoot(gamePin, name) {
102
const client = new Kahoot();
103
client.setMaxListeners(Number.POSITIVE_INFINITY);
104
client.join(gamePin /* Or any other kahoot game gamePin */, name);
105
client.on("joined", () => {
106
//Functions.status("Bot ("+name+" "+currentBotCount+") joined.");
107
});
108
client.on("quizStart", Quiz => {
109
Functions.status("[" + name + "] Quiz has started!");
110
quizName.innerHTML = Quiz.name;
111
questionAmount.innerHTML = Quiz.questionCount + " questions";
112
currentQuestion.innerHTML = "no questions yet";
113
});
114
client.on("questionStart", question => {
115
var answ = Math.floor(Math.random() * Math.floor(4));
116
Functions.status("[" + name + "] answering opt" + answ);
117
question.answer(answ);
118
});
119
120
client.on("question", currentQuestion => {
121
currentQuestion.innerHTML = "question#" + currentQuestion.number;
122
});
123
client.on("questionEnd", question => {
124
Functions.status("[" + name + "] correct?=" + question.correct);
125
});
126
client.on("quizEnd", () => {
127
Functions.status("[" + name + "] quiz ended");
128
});
129
client.on("disconnect", () => {
130
Functions.status("[" + name + "] disconnected");
131
if (useHydraMode == true){
132
var hydraCounter = 0
133
while (hydraCounter < hydraAmount){
134
joinKahoot(gamePin, name+"'s revenge"+hydraCounter)
135
hydraCounter++
136
}
137
138
}
139
});
140
141
clients.push(client);
142
}
143
144
// Add event listeners for different buttons
145
document
146
.getElementById("randomize-btn")
147
.addEventListener("click", Functions.random);
148
document
149
.getElementById("input-names-btn")
150
.addEventListener("click", Functions.inputNames);
151
document.getElementById("save-form-btn").addEventListener("click", scrollTop);
152
document
153
.getElementById("open-settings-btn")
154
.addEventListener("click", UserInterface.openSettings);
155
document.getElementById("leave-btn").addEventListener("click", leave);
156
document.getElementById("stop-btn").addEventListener("click", stop);
157
document
158
.getElementById("open-nav-btn")
159
.addEventListener("click", UserInterface.openNav);
160
document
161
.getElementById("close-nav-btn")
162
.addEventListener("click", UserInterface.closeNav);
163
document
164
.getElementById("toggle-show-btn")
165
.addEventListener("click", UserInterface.toggleShow);
166
document.getElementById("todo-form").addEventListener("submit", evt => {
167
// prevent default refresh functionality of forms
168
evt.preventDefault();
169
170
// input on the form
171
botName = evt.target[0].value;
172
gamePin = evt.target[1].value;
173
amountOfBotsToJoin = evt.target[2].value;
174
joinSpeed = evt.target[3].value;
175
toldToStop = false;
176
timer(gamePin, botName, amountOfBotsToJoin, joinSpeed);
177
Functions.status("recived instructions for bots");
178
});
179
document.getElementById("settings-form").addEventListener("submit", evt => {
180
// Prevent default refresh functionality of forms
181
evt.preventDefault();
182
183
// If list naming option is selected disable name input
184
selectedBotNamingOption = document.querySelector(
185
"input[name=add-input]:checked"
186
).id;
187
if (evt.target[2].checked) {
188
UserInterface.disableNameInput(true);
189
} else {
190
UserInterface.disableNameInput(false);
191
}
192
193
console.log(evt.target[6].value)
194
if(evt.target[6].value == 0){
195
useHydraMode = false
196
} else{
197
useHydraMode = true
198
hydraAmount = evt.target[6].value
199
}
200
201
});
202
Functions.status("No bots currently; no kahoots yeeted.");
203
204