-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
155 lines (150 loc) · 4.67 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const Kahoot = require("kahoot.js-updated");
var express = require('express');
var flatted = require('flatted');
var fs = require('fs')
var mime = require('mime-types')
var games = [];
var logging = Boolean(Number(process.env.LOGGING)) // set to false to disable logs
var app = express()
var port = process.env.PORT || 5500
if (logging) {
console.log(process.env.PORT)
}
//kahoot function. Handles all gameplay
function joingame(pin, bot_name, bot_count) {
var gameid = games.length;
games.push({
pin: pin,
name: bot_name,
bot_count: bot_count,
bots: [],
deadbots: []
});
for (var i = 0; i < bot_count; i++) {
games[gameid].bots.push(new Kahoot)
games[gameid].bots[i].join(pin, bot_name + String(i)).catch(error => {
if (logging) {
console.log("join failed " + error.description + " " + error.status)
}
});
games[gameid].bots[i].on("Joined", () => {
if (logging) {
console.log("successfully joined game")
}
});
games[gameid].bots[i].on("QuestionStart", (question) => {
question.answer(
Math.floor(
(Math.random() * question.quizQuestionAnswers[question.questionIndex]) + 0));
});
games[gameid].bots[i].on("Disconnect", (reason) => {
if (logging) {
console.log("disconnected because " + reason)
}
if (games[gameid] != null) {
games[gameid].deadbots.push(i)
if (games[gameid].deadbots.length > 8) {
for (ii in games[gameid].bots) {
games[gameid].bots[ii].leave();
}
if (logging) {
console.log("leaving game")
}
games.splice(gameid, 1)
}
}
});
}
}
//express stuff
app.get("/endpoint/:pin/:amount/:name", function(request, response) {
if (logging) {
console.log("PIN: " + request.params.pin)
console.log("amount: " + request.params.amount)
console.log("bot name: " + request.params.name)
}
if (Number(request.params.amount) <= 150) {
joingame(request.params.pin, request.params.name, Number(request.params.amount))
response.writeHead(200, {
"content-type": "application/json",
'cache-control': 'no-cache',
'access-control-allow-origin': '*',
'connection': 'keep-alive'
});
response.write(`
{
"success":true
}
`)
response.end()
} else {
response.writeHead(200, {
"content-type": "application/json",
'cache-control': 'no-cache',
'access-control-allow-origin': '*',
'connection': 'keep-alive'
});
response.write(`
{
"success":false
}
`)
response.end()
}
});
//list all games
app.get("/gameslist", function(request, response) {
response.writeHead(200, {
"content-type": "application/json",
'cache-control': 'no-cache',
'access-control-allow-origin': '*',
'connection': 'keep-alive'
});
response.write(flatted.stringify(games))
response.end()
});
app.get("/bot_count", function(request, response) {
response.writeHead(200, {
"content-type": "application/json",
'cache-control': 'no-cache',
'access-control-allow-origin': '*',
'connection': 'keep-alive'
});
var res = {
game_count: games.length
}
response.write(JSON.stringify(res))
response.end()
})
app.get("/:page", function(request, response) {
if (logging) {
console.log("request: " + request.params.page)
}
if (fs.existsSync(request.params.page)) {
response.writeHead(200, {
"content-type": mime.lookup(request.params.page),
'cache-control': 'no-cache',
'access-control-allow-origin': '*',
'connection': 'keep-alive'
});
fs.createReadStream(request.params.page).pipe(response);
} else {
response.writeHead(404, {
"content-type": "text/html",
'cache-control': 'no-cache',
'access-control-allow-origin': '*',
'connection': 'keep-alive'
});
fs.createReadStream("404.html").pipe(response);
}
})
app.get("/", function(request, response) {
response.writeHead(200, {
"content-type": "text/html",
'cache-control': 'no-cache',
'access-control-allow-origin': '*',
'connection': 'keep-alive'
})
fs.createReadStream("index.html").pipe(response);
})
app.listen(port);