-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js.save
392 lines (358 loc) · 11.9 KB
/
index.js.save
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//IP AND PORT
var ip = "teewurst24.party";
var portt = "80";
//API KEYS
var apiKey_random = "tpacken von .../perl-openssl-defaults_3_armhf.deb ...
Entpacken von perl-openssl-defaults:armhf (3) ...
Vormals nicht ausgewähltes Paket libnet-ssleay-perl wird gewählt.
Vorbereitung zum Entpacken von .../libnet-ssleay-perl_1.80-1_armhf.deb ...
Entpacken von libnet-ssleay-perl (1.80-1) ...
Vormals nicht ausgewähltes Paket libio-socket-ssl-perl wird gewählt.
Vorbereitung zum Entpacken von .../libio-socket-ssl-perl_2.044-1_all.deb ...
Entpacken von libio-socket-ssl-perl (2.044-1) ...
Vormals nicht ausgewähltes Paket libnet-libidn-perl wird gewählt.
Vorbereitung zum Entpacken von .../libnet-libidn-perl_0.12.ds-2+b3_armhf.deb ...
Entpacken von libnet-libidn-perl (0.12.ds-2+b3) ...
Vormals nicht ausgewähltes Paket ddclient wird gewählt.
Vorbereitung zum Entpacken von .../ddclient_3.8.3-1.1_all.deb ...
Entpacken von ddclient (3.8.3-1.1) ...
libnet-libidn-perl (0.12.ds-2+b3) wird eingerichtet ...
perl-openssl-defaults:armhf (3) wird eingerichtet ...
ddclient (3.8.3-1.1) wird eingerichtet ...
Trigger für systemd (232-25+deb9u1) werden verarbeitet ...
Trigger für man-db (2.7.6."; //https://randomyoutube.net/api
var apiKey_google = ""; //https://developers.google.com/youtube/v3/getting-started
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || portt;
var express = require('express');
var path = require('path');
var wm = new WeakMap();
var wm_names = new WeakMap();
var wm_points = new WeakMap();
var video_clicks = -1;
var gameOver = 1; //Zu Beginn ist die 0.te Runde vorbei
var multipl = 1;
var startingConntectedPlayers = 0;
var wm_startingPlayer = new WeakMap();
var wm_fish = new WeakMap();
var haken = " <span style=\"color:#0f0;font-size:30px;\">✓</span>";
var kreuz = " <span style=\"color:#f00;font-size:30px;\">✗</span>";
var afk_symbol = " <span style=\"color:#A28823;font-size:30px;\">✈</span>";;
var wm_afk = new WeakMap();
var skipCount = 0;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/ytclicks', function(req, res){
res.sendFile(__dirname + '/ytclicks.html');
});
app.get('/ytclicks/mobile',function(req,res){
res.sendFile(path.join(__dirname +'/ytclicks/mobile.html'));
});
app.get('/montagsmaler', function(req, res){
res.sendFile(__dirname + '/montagsmaler.html');
});
app.use(express.static(path.join(__dirname, 'public')));
var favicon = require('serve-favicon');
app.use(favicon(__dirname + '/public/images/teewurst_icon.ico'));
io.on('connection', function(socket){
//INIT IF CONNECTED
socket.emit('setIPPORT', ip, portt);
socket.on('gameConnection', function(){
wm_points.set(socket, 0);
wm_afk.set(socket, false);
io.emit('chat message', "A new player has connected.");
refreshPlayerlist();
if(gameOver == 0) { //Spiel läuft
socket.emit('deactNextB'); //nextButton für den neuen Spieler deaktivieren
}
});
//ALL FUNCTIONS
socket.on('startLocal', function(){
//falls Spieler momentan tippen muss
if(gameOver ==0 && wm_startingPlayer.has(socket)) {
startingConntectedPlayers--;
if(startingConntectedPlayers == 0) {
gameOver = 1;
io.emit('actNextB'); //ak nextButton
multipl = 1;
}
}
//lösche Spieler aus allen Listen
wm.delete(socket);
wm_names.delete(socket);
wm_points.delete(socket);
wm_startingPlayer.delete(socket);
wm_fish.delete(socket);
refreshPlayerlist();
});
socket.on('stopLocal', function(){
//füge Host wieder hinzu
wm_points.set(socket, 0);
refreshPlayerlist();
if(gameOver == 0) { //Spiel läuft
socket.emit('deactNextB'); //nextButton für den "neuen" Spieler deaktivieren
}
});
socket.on('change name', function(name){
var oldName = wm_names.get(socket)
wm_names.set(socket, name);
io.emit('chat message', "\"" + oldName + "\" changed his name to \"" + name + "\".");
refreshPlayerlist();
//console.log(name)
});
socket.on('fish', function(){
io.emit('chat message', wm_names.get(socket) + " used the FISHCARD!");
wm_fish.set(socket, 1); //vermerken wer fish setzt
});
socket.on('nextVideo', function(){
if(gameOver == 0) return; //nextVideo nur möglich wenn Runde vorbei
gameOver = 0;
io.emit('deactNextB'); //deak nextButton
console.log("nextVideo");
skipCount = 0;
//getStartingPlayerCount
startingConntectedPlayers = 0;
for (var i in io.sockets.connected) {
s = io.sockets.connected[i];
if(!(wm_points.has(s))) continue; //localhost überspringe
if(wm_afk.get(s)) continue; //afk-player nicht mitzählen
startingConntectedPlayers++;
wm_startingPlayer.set(s, 1); //vermerken wer mitspielt
}
//delete tipps
for (var i in io.sockets.connected) {
s = io.sockets.connected[i];
wm.delete(s);
wm_fish.delete(s);
}
refreshPlayerlist();
//getRandomVID...
var vid = "test";
var request = require('request');
var random_api_url = "http://randomyoutube.net/api/getvid?api_token=" + apiKey_random;
console.log(random_api_url);
request(random_api_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var importedJSON = JSON.parse(body);
vid = importedJSON.vid;
if (typeof vid === "undefined") {
console.log("undefined VID");
io.emit('actNextB'); //ak nextButton
gameOver = 1; //damit Button wieder gedrückt werden kann
return;
}
console.log(vid);
io.emit('nv', vid);
//getClicks
var google_api_url = "https://www.googleapis.com/youtube/v3/videos?id=" + vid + "%20&part=snippet%2CcontentDetails%2Cstatistics%20&key=" + apiKey_google;
console.log(google_api_url);
request(google_api_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var importedJSON = JSON.parse(body);
console.log(JSON.stringify(importedJSON));
if (typeof JSON.stringify(importedJSON.items[0]) === "undefined") {
console.log("undefined clicks");
notAvailableVideo();
return;
}
if(typeof JSON.stringify(importedJSON.items[0]["contentDetails"]["regionRestriction"]) === "undefined") {}
else{
//check if video is not visible in Germany
console.log("regionRestriction exists");
var x = importedJSON.items[0]["contentDetails"]["regionRestriction"];
if(!(typeof JSON.stringify(x["allowed"]) === "undefined")) {
console.log("allowed array exist")
if(!contains(x["allowed"], "DE")) {
console.log("DE not in allowed");
notAvailableVideo();
return;
}
}
if(!(typeof JSON.stringify(x["blocked"]) === "undefined")) {
console.log("blocked array exist")
if(contains(x["blocked"], "DE")) {
console.log("DE in blocked");
notAvailableVideo();
return;
}
}
}
var clicks = JSON.stringify(importedJSON.items[0]["statistics"]["viewCount"]);
clicks = clicks.substring(1, clicks.length-1); //delete ""
console.log("clicks: " + clicks);
video_clicks = clicks;
}
else {
console.log("error?!?!?!");
}
});
}
else {
console.log("error?!?!?!");
}
});
});
socket.on('chat message', function(msg){ //Nur zum Tippen der Clicks
if(video_clicks == -1) return; //erstes Video
if(gameOver == 1) return;
if(!(wm_startingPlayer.has(socket))) return //neuer Spieler darf nicht voten
wm.set(socket, msg);
var tipps = 0
for (var i in io.sockets.connected) {
var s = io.sockets.connected[i];
if(wm.has(s)) {
console.log("value: " + wm.get(s))
tipps++;
}
}
if(wm_afk.get(socket)) {
wm_startingPlayer.set(socket,1);
startingConntectedPlayers++;
wm_afk.set(socket,false);
}
console.log("startingConntectedPlayers: " + startingConntectedPlayers);
console.log("tipps: " + tipps);
refreshPlayerlist();
if(startingConntectedPlayers == tipps) {
//Game over, show results
showResults();
}
});
socket.on('disconnect', function() {
io.emit('chat message', wm_names.get(socket) + " disconnected.");
if(wm_startingPlayer.has(socket)) { //wenn dieser Player mitgespielt hat
startingConntectedPlayers--; //need one less
}
if(startingConntectedPlayers == 0) { //letzter aktiver Spieler verlässt
gameOver = 1;
io.emit('actNextB'); //ak nextButton
multipl = 1;
} else {
var tipps = 0
for (var i in io.sockets.connected) {
var s = io.sockets.connected[i];
if(wm.has(s)) {
console.log("value: " + wm.get(s))
tipps++;
}
}
if(startingConntectedPlayers == tipps) {
//Game over, show results
if(gameOver == 0) {
showResults();
}
}
}
refreshPlayerlist();
});
socket.on('skip', function() {
skipCount++;
io.emit('chat message', wm_names.get(socket) + " voted to skip (" + skipCount + "/" + startingConntectedPlayers + ").");
if(skipCount > (startingConntectedPlayers/2)) {
//setze alle, die nicht gevoted haben auf den AFK status
for (var i in io.sockets.connected) {
s = io.sockets.connected[i];
if(!(wm_points.has(s))) continue; //überspringe Host
if(wm_startingPlayer.has(s)) {
if(!(wm.has(s))) {
wm_afk.set(s, true);
}
}
}
showResults();
}
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
function showResults() {
//Game over, show results
gameOver = 1;
io.emit('actNextB'); //ak nextButton
io.emit('clickAmount', video_clicks);
var win_s = []; //Liste von Gewinnern
var diff = 9007199254740992;
var voteList = "";
for (var i in io.sockets.connected) { //player with lowest diff
s = io.sockets.connected[i];
if(!(wm_points.has(s))) continue; //überspringe Host
console.log(video_clicks);
if(Math.abs(wm.get(s) - video_clicks) < diff) {
win_s = []; //array leeren, gibt nur einen Besten(Fall zwei zuvor haben sie selbe nicht minDiff-Wertung)
win_s[0] = s;
diff = Math.abs(wm.get(s) - video_clicks);
} else if(Math.abs(wm.get(s) - video_clicks) == diff) {
win_s.push(s);
}
}
//add points
var value = wm.get(win_s[0]);
var addPoints = 0;
if(value == video_clicks && video_clicks != 0) {
addPoints = video_clicks;
} else {
addPoints = 1;
}
addPoints = addPoints * multipl;
multipl = 1; //nur für diese Runde
for (var i = 0; i < win_s.length; i++) {
var p_addPoints = addPoints;
var win = win_s[i];
if(wm_fish.has(win)) p_addPoints = p_addPoints * 2; //fishcard aktiviert
var newScore = parseInt(p_addPoints) + parseInt(wm_points.get(win));
wm_points.set(win, newScore);
//send winning message
var name = wm_names.get(win);
var message = name + " won with an estimation of " + wm.get(win_s[i]) + " clicks and get " + p_addPoints + " Points.";
io.emit('chat message', message);
}
//show all scores and guesses
var scoresText = "<tr><th></th><th>Guess</th><th>Score</th></tr>";
for (var i in io.sockets.connected) { //player with lowest diff
s = io.sockets.connected[i];
if(!(wm_points.has(s))) continue; //überspringe Host
var name = wm_names.get(s);
var value = wm_points.get(s);
var guess = wm.get(s);
scoresText += "<tr><td>" + name + "</td><td>" + guess + "</td><td>" + value + "</td></tr>";
}
io.emit('score', scoresText);
}
function refreshPlayerlist() {
var pl = "";
for (var i in io.sockets.connected) {
s = io.sockets.connected[i];
if(!(wm_points.has(s))) continue; //überspringe Host
var symbol = "";
if(wm_startingPlayer.has(s)) {
if(wm_afk.get(s)) {
symbol = afk_symbol;
} else if(wm.has(s)) {
symbol = haken;
} else {
symbol = kreuz;
}
}
pl = pl + "<li>" + wm_names.get(s) + symbol + "</li>";
}
io.emit('playerlist', pl);
}
function contains(arr, element) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === element) {
return true;
}
}
return false;
}
function notAvailableVideo() {
gameOver = 1;
multipl = multipl*2; //MULTIPLIKATOR
io.emit('chat message', "MULTIPLIER!");
io.emit('actNextB'); //ak nextButton
return;
}