From 67eaaddb44fcb72328047c9eb58e7f862b493e34 Mon Sep 17 00:00:00 2001 From: KasperRT Date: Thu, 9 Apr 2015 13:52:53 +0200 Subject: [PATCH] Backuppush --- js/admin.js | 4 + js/list.js | 12 +- js/search.js | 4 +- js/youtube.js | 319 +++++++++--------------------------------------- server/index.js | 204 ++++++++++++++++++++++++++----- 5 files changed, 247 insertions(+), 296 deletions(-) diff --git a/js/admin.js b/js/admin.js index 9f7d975b..97fc90ca 100755 --- a/js/admin.js +++ b/js/admin.js @@ -37,6 +37,10 @@ function submitAdmin(form) skipping = form.skip.value; shuffling = form.shuffle.value; + configs = [voting, addsongs, longsongs, frontpage, allvideos, removeplay, adminpass, skipping, shuffling]; + + socket.emit("config", configs); + confRes = $.ajax({ type: "POST", url: "php/change.php", diff --git a/js/list.js b/js/list.js index 34e62e0c..fc8d8543 100755 --- a/js/list.js +++ b/js/list.js @@ -35,13 +35,13 @@ function populate_list(msg) if(listeID.hasOwnProperty('startTime')) { console.log("startTime"); - if(listeID.hasOwnProperty("addsongs") && listeID.addsongs == "true") adminadd = 1; + if(listeID.hasOwnProperty("addsongs") && listeID.addsongs) adminadd = 1; else adminadd = 0; - if(listeID.hasOwnProperty("allvideos") && listeID.allvideos == "true") music = 1; + if(listeID.hasOwnProperty("allvideos") && listeID.allvideos) music = 1; else music = 0; - if(listeID.hasOwnProperty("longsongs") && listeID.longsongs == "true") longS = 1; + if(listeID.hasOwnProperty("longsongs") && listeID.longsongs) longS = 1; else longS = 0; - if(listeID.hasOwnProperty("vote") && listeID.vote == "true") adminvote = 1; + if(listeID.hasOwnProperty("vote") && listeID.vote) adminvote = 1; else adminvote = 0; if(listeID.hasOwnProperty("adminpass") && listeID.adminpass !== '') hasadmin = 1; else hasadmin = 0; @@ -157,7 +157,7 @@ function vote(id, vote){ function skip(){ socket.emit('skip', [chan, guid]); - +/* voteRes = ($.ajax({ type: "GET", url: "php/change.php", @@ -192,7 +192,7 @@ function skip(){ $("#pBar").removeClass("opacityFull"); $("#eBar").removeClass("opacityFull"); },1500); - +*/ } function show(){ diff --git a/js/search.js b/js/search.js index 50306956..dc77f0c6 100755 --- a/js/search.js +++ b/js/search.js @@ -186,6 +186,8 @@ function submitAndClose(id,title){ function submit(id,title,type){ + socket.emit("add", [id, title]); + /* serverAns = $.ajax({ type: "GET", url: "php/change.php", @@ -237,7 +239,7 @@ function submit(id,title,type){ $("#sBar").removeClass("opacityFull"); },1500); updateList(); - event.stopPropagation(); + event.stopPropagation();*/ } // if(reply=="added"){$("#search").removeClass('success'); $("#search").addClass('success');} diff --git a/js/youtube.js b/js/youtube.js index a3359e99..858cb84d 100755 --- a/js/youtube.js +++ b/js/youtube.js @@ -33,6 +33,27 @@ var adminpass = ""; var notified = false; var peis = false; var filesadded=""; +var chan = $("#chan").html(); +var player_ready = false; +var seekTo; +var arr = [] + +socket.on(chan.toLowerCase()+",np", function(obj) +{ + console.log("gotten shit"); + console.log(obj) + video_id = obj[0][0]["id"]; + conf = obj[1][0]; + time = obj[2]; + seekTo = time - conf["startTime"]; + getTitle(obj[0][0]["title"], 1); + if(player_ready) + { + ytplayer.loadVideoById(video_id); + ytplayer.playVideo(); + ytplayer.seekTo(seekTo); + } +}); $(document).ready(function() { @@ -49,33 +70,7 @@ $(document).ready(function() Notification.requestPermission(); } - updateList(); - wasPaused = false; - beginning = true; - diffVideo = false; - interval = false; - response = $.ajax({ type: "GET", - url: "php/change.php", - async: false - }).responseText; - //console.log(response); - response = $.parseJSON(response); - //console.log(response.nowPlaying.length); - conf = response.conf; - //console.log(conf); - try{ - for(var first in response.nowPlaying) break; - //console.log(first); - response = first; - }catch(err){ - response = "1"; - } - if(window.mobilecheck()){ - //syncInterval = setInterval(getTime, 50000); - //listInterval = setInterval(updateList, 50000); - mobileSync = setInterval(function(){getTime();updateList();}, 10000); - //listKillInterval = setInterval(ks, 50000); document.getElementById("search").blur(); readyLooks(); }else{ @@ -83,10 +78,6 @@ $(document).ready(function() tag.src = "https://www.youtube.com/iframe_api"; firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); - if(!syncInterval) - syncInterval = setInterval(getTime, 5000); - listInterval = setInterval(updateList, 10000); - //listKillInterval = setInterval(ks, 50000); if($("#chan").html().toLowerCase() == "jazz") { @@ -113,7 +104,7 @@ function onYouTubeIframeAPIReady() { ytplayer = new YT.Player('player', { height: window.height*0.75, width: window.width*0.6, - videoId: response, + videoId: video_id, playerVars: { rel:"0", wmode:"transparent", controls: "0" , iv_load_policy: "3", theme:"light", color:"white"}, events: { 'onReady': onPlayerReady, @@ -131,275 +122,87 @@ function onYouTubeIframeAPIReady() { } function onPlayerStateChange(newState) { - //console.log("new state: "+newState.data); - //console.log("beginning: "+beginning); - //ytplayer.seekTo(15); - if((newState.data === 0 && checkEnd()) || (newState.data == 1 && checkEnd())) + switch(newState.data) { - //console.log("nummer 1"); - startNextSong(); - ytplayer.pauseVideo(); - wasPaused = false; - }else if(newState.data == 1 && (wasPaused && !beginning)) - { - //console.log("unpaused"); - beginning = false; - wasPaused = false; - if(!syncInterval) - syncInterval = setInterval(getTime, 5000); - getTime(); - }else if(newState.data == 2) - { - //clearInterval(syncInterval); - interval = true; - wasPaused = true; - beginning = false; - } - if(newState.data == 1 || newState.data == 2) - { - activeButton = document.getElementById("playpause").className; - //console.log(activeButton); - if((newState.data == 2 && activeButton == "pause") || (newState.data == 1 && activeButton == "play")) - { + case -1: + console.log("not started"); + break; + case 0: + socket.emit("end", video_id); + console.log("ended"); + break; + case 1: + console.log("playing"); $("#playpause").toggleClass("play"); $("#playpause").toggleClass("pause"); - } - } - if(newState.data === 0) - { - quickFixCountdown = setTimeout(function(){ - //console.log("trying quickfix"); - if(ytplayer.getPlayerState() === 0){ - //console.log("quickfixPlay"); - startNextSong(); - wasPaused = false; - } - },5000); + break; + case 2: + console.log("paused"); + $("#playpause").toggleClass("play"); + $("#playpause").toggleClass("pause"); + break; + case 3: + console.log("buffering"); + break; } } function checkEnd() { - //console.log("sjekker om brukeren spolte"); - $.ajax({ - type: 'get', - url: 'php/timedifference.php', - data: "abcde", - async: false, - success: function(data) { - timeDifference = $.parseJSON(data); - } - }); - if(parseInt(timeDifference[0]) > ytplayer.getDuration()) - { - return true; - } - return false; + } function startNextSong() { - //console.log(getTime()); - if(checkEnd() && !changed) - { - setTimeout(function(){ - arr = $.ajax({ - type: "POST", - url: "php/change.php", - async: false, - data: "thisUrl="+response+"&act=save", - - success: function() { - //console.log("saved song-switch - "+response); - } - }).responseText; - arr = $.parseJSON(arr); - response = arr.id; - //console.log("next video: "+response); - getTitle(response); - if(!window.mobilecheck()) - { - ytplayer.loadVideoById(response); - notifyUser(response, arr.title); - } - beginning = true; - setBGimage(response); - - },2500); - updateList(); - changed = true; - - setTimeout(function() { - changed = false; - if(!syncInterval) - syncInterval = setInterval(getTime, 5000); - interval = true; - //console.log("starter intervallen. Interval: " + interval); - }, 2500); - } - } function getTime() { - //console.log("utenfor if test" + wasPaused); - if(!window.mobilecheck() && ytplayer.getCurrentTime() > 2 && ytplayer.getPlayerState() == 1) wasPaused = false; - if(!wasPaused) - { - //console.log("sjekker om brukeren spolte"); - $.ajax({ - type: 'get', - url: 'php/timedifference.php', - data: "abcde", - async: false, - success: function(data) { - timeDifference = $.parseJSON(data); - } - }); - //console.log("current song: "+response); - //console.log("song in database: "+timeDifference[1]); - if(!window.mobilecheck()){ //Added so the mobileversion will change banner - if(parseInt(timeDifference[2]) + 1> ytplayer.getCurrentTime() + parseInt(timeDifference[3]) && ytplayer.getPlayerState() === 0) - { - return true; - }else if(ytplayer.getCurrentTime() + parseInt(timeDifference[3]) > parseInt(timeDifference[2]) + 5 || (ytplayer.getCurrentTime() + parseInt(timeDifference[3]) < parseInt(timeDifference[2]) - 5 && ytplayer.getPlayerState() !== 0 && ytplayer.getPlayerState() != 3)) - { - if(parseInt(timeDifference[0]) > ytplayer.getDuration()) - { - //console.log("burde ikke søke, men hoppe til neste sang"); - } - ytplayer.seekTo(timeDifference[0]); - ytplayer.pauseVideo(); - ytplayer.playVideo(); - getTitle(); - return false; - } - } - //if(interval){syncInterval = setInterval(getTime, 5000);interval = false;} - - if(response != timeDifference[1]) - { - //clearInterval(syncInterval); - wasPaused = true; - //console.log("forskjellige videoer!!"); - if(!window.mobilecheck()) - { - ytplayer.pauseVideo(); - ytplayer.loadVideoById(timeDifference[1]); - notifyUser(timeDifference[1], timeDifference[4]); - wasPaused = false; - if(!syncInterval) - syncInterval = setInterval(getTime, 5000); - } - response = timeDifference[1]; - getTitle(); - setBGimage(timeDifference[1]); - updateList(); - /*setTimeout(function(){ - //console.log(response); - diffVideo = true; - beginning = true; - $.ajax({ - type: "POST", - url: "php/change.php", - async: false, - data: "thisUrl=123abcprompeprompe&act=save", - success: function(data) - { - response = timeDifference[1]; - getTitle(); - } - }); - wasPaused = false; - if(!syncInterval) - syncInterval = setInterval(getTime, 5000); - },2500);*/ - } - } } -function getTitle() +function getTitle(titt, viewers) { + /*var outPutWord = viewers[5].length > 1 ? "viewers" : "viewer";*/ + console.log(titt); + var title= titt.replace(/\\\'/g, "'").replace(/"/g,"'").replace(/&/g,"&"); + document.title = title + " • Zöff"; - $.ajax({ type: "GET", - url: "php/timedifference.php", - async: false, - success: function(data) { - viewers = $.parseJSON(data); - var outPutWord = viewers[5].length > 1 ? "viewers" : "viewer"; - var title= viewers[4].replace(/\\\'/g, "'").replace(/"/g,"'").replace(/&/g,"&"); - document.title = title + " • Zöff"; - if(!window.mobilecheck()) - document.getElementsByName('v')[0].placeholder = title + " • " + viewers[5].length + " " + outPutWord; - else - { - document.getElementById("mobileTitle").innerHTML = title; - document.getElementsByName('v')[0].placeholder = "Search • " + viewers[5].length + " " + outPutWord; - //document.getElementById("viewers").innerHTML = viewers[5].length + " " + outPutWord; - } - } - }); - + if(!window.mobilecheck()){ + console.log("ikke mobil"); + document.getElementsByName('v')[0].placeholder = title + " • 1 "; + }else + { + document.getElementById("mobileTitle").innerHTML = title; + document.getElementsByName('v')[0].placeholder = "Search • 1 " + outPutWord; + //document.getElementById("viewers").innerHTML = viewers[5].length + " " + outPutWord; + } } function errorHandler(newState) { - setTimeout(function(){ - arr = $.ajax({ - type: "POST", - url: "php/change.php", - async: false, - data: "thisUrl="+response+"&act=empty", - - success: function() { - //console.log("error! deleted video"); - } - }).responseText; - arr = $.parseJSON(arr); - response = arr.id; - if(!window.mobilecheck()) - { - ytplayer.loadVideoById(response); - notifyUser(response); - } - setBGimage(response); - },2500); -/* - setTimeout(function(){ - response = $.ajax({ type: "GET", - url: "change.php", - async: false - }).responseText; - var url = $.parseJSON(response); - response = url[0][0]; - - ytplayer.loadVideoById(response); - },2500);*/ + socket.emit("end", video_id); } function onPlayerReady(event) { - //ytplayer = document.getElementById("myytplayer"); - // ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); - //ytplayer.addEventListener("onError", "errorHandler"); + player_ready = true; getTime(); if(!window.mobilecheck()) { $("#player").css("opacity", "1"); $("#controls").css("opacity", "1"); $(".playlist").css("opacity", "1"); - //$("#player").fadeIn(); + ytplayer.loadVideoById(video_id); ytplayer.playVideo(); + ytplayer.seekTo(seekTo); } readyLooks(); initSlider(); - //durationFixer = setInterval(durationSetter, 1000); } function readyLooks() { - getTitle(); - setBGimage(response); + setBGimage(video_id); } function setBGimage(id){ diff --git a/server/index.js b/server/index.js index 8570cbdc..efca5889 100755 --- a/server/index.js +++ b/server/index.js @@ -6,7 +6,7 @@ var io = require('socket.io')(server); //db var mongojs = require('mongojs'); -var db = mongojs.connect('mydb', ['kasper']); +var db = mongojs.connect('mydb'); var port = 3000; var lists = []; @@ -20,7 +20,7 @@ io.on('connection', function(socket){ var coll; var guid; - + socket.on('list', function(list) { list = list.split(','); @@ -28,31 +28,124 @@ io.on('connection', function(socket){ guid = list[1]; console.log("user connected to list: " + list[0]); - if(lists[list[0]] == undefined) + if(lists[coll] == undefined) { - lists[list[0]] = []; - lists[list[0]].push(socket); - }else lists[list[0]].push(socket); - console.log(lists[list[0]].length); + lists[coll] = []; + lists[coll].push(guid); + }else lists[coll].push(guid); - db.collection(coll).find({views:{$exists : true}}, function(err, docs){ - if(!contains(docs[0]["views"], guid)) + io.sockets.emit(coll+",viewers", lists[coll].length); + + db.getCollectionNames(function(err, docs){ + if(contains(docs, coll)) { - db.collection(coll).update({views:{$exists : true}}, {$push:{views:guid}}, function(err, docs){ - db.collection(list[0]).find().sort({added:-1}, function(err, docs) { - console.log(docs); - socket.emit(list[1], docs); - }); - }); + db.collection(coll).find().sort({votes:-1}, function(err, docs) { + console.log(docs); + socket.emit(coll, docs); + db.collection(coll).find({now_playing:true}, function(err, np){ + console.log("sending now_playing to " + coll+",np"); + db.collection(coll).find({views:{$exists:true}}, function(err, conf){ + var d = new Date(); + var time = Math.floor(d.getTime() / 1000); + toSend = [np,conf,time]; + console.log(toSend); + socket.emit(coll+",np", toSend); + }); + }); + }); }else { - db.collection(list[0]).find().sort({added:-1}, function(err, docs) { - console.log(docs); - socket.emit(list[1], docs); - }); + db.createCollection(coll, function(err, docs){ + var d = new Date(); + var time = Math.floor(d.getTime() / 1000); + db.collection(coll).insert({"added":time,"guids":[],"id":"30H2Z8Lr-4c","now_playing":true,"title":"Empty Playlist","votes":0}, function(err, docs){ + db.collection(coll).insert({"addsongs":false, "adminpass":"", "allvideos":true, "frontpage":true, "longsongs":true, "removeplay": false, "shuffle": false, "skip": true, "skips": [], "startTime":time, "views": [], "vote": false}, function(err, docs) + { + socket.emit(coll, docs); + }) + }); + }) } - }); - + }); + }); + + socket.on('end', function(arg) + { + db.collection(coll).find({now_playing:true}, function(err, docs){ + if(docs[0]["id"] == arg){ + db.collection(coll).update({now_playing:true}, + {$set:{ + now_playing:false, + votes:0, + guids:[] + }}, function(err, docs) + { + db.collection(coll).findAndModify({ + query: {now_playing:false, id: {$ne: arg}}, + sort: {votes:-1}, + update: + {$set:{ + now_playing:true, + votes:0, + guids:[] + }} + }, function(err, docs) + { + var d = new Date(); + var time = Math.floor(d.getTime() / 1000); + db.collection(coll).update({views:{$exists:true}}, + {$set:{startTime:time}}, function(err, docs){ + db.collection(coll).find().sort({votes:-1}, function(err, docs) + { + io.sockets.emit(coll, docs); + db.collection(coll).find({now_playing:true}, function(err, np){ + console.log("sending now_playing to " + coll+",np"); + db.collection(coll).find({views:{$exists:true}}, function(err, conf){ + var d = new Date(); + var time = Math.floor(d.getTime() / 1000); + toSend = [np,conf,time]; + console.log(toSend); + io.sockets.emit(coll+",np", toSend); + }); + }); + }); + }); + }); + }); + } + }) + }); + + socket.on('add', function(arr) + { + console.log("add songs"); + var id = arr[0]; + var title = arr[1]; + db.collection(coll).find({id:id}, function(err, docs){ + if(docs.length < 1) + { + var d = new Date(); + var time = Math.floor(d.getTime() / 1000); + var guids = [guid]; + var votes = 1; + db.collection(coll).find({id:"30H2Z8Lr-4c"}, function(err, docs){ + if(docs.length == 0){ + db.collection(coll).insert({"added":time,"guids":guids,"id":id,"now_playing":false,"title":title,"votes":votes}, function(err, docs){ + db.collection(coll).find().sort({votes:-1}, function(err, docs){ + io.sockets.emit(coll, docs); + }); + }); + }else{ + db.collection(coll).update({id:"30H2Z8Lr-4c"}, + {"added":time,"guids":guids,"id":id,"now_playing":false,"title":title,"votes":votes}, function(err, docs){ + db.collection(coll).find().sort({votes:-1}, function(err, docs){ + io.sockets.emit(coll, docs); + }); + }); + } + }); + } + }); }); socket.on('vote', function(msg) @@ -61,8 +154,9 @@ io.on('connection', function(socket){ var id = msg[1]; guid = msg[3]; + db.collection(coll).find({id:id}, function(err, docs){ - if(contains(docs[0]["guids"], guid)) + if(!contains(docs[0]["guids"], guid)) { db.collection(coll).update({id:id}, {$inc:{votes:1}}, function(err, docs) { @@ -80,10 +174,11 @@ io.on('connection', function(socket){ db.collection(coll).find().sort({votes:-1}, function(err, docs) { console.log(docs); - for(x in lists[coll]) + io.sockets.emit(coll, docs); + /*for(x in lists[coll]) { lists[coll][x].emit(coll, docs); - } + }*/ }); }); } @@ -91,17 +186,65 @@ io.on('connection', function(socket){ }); + socket.on('pos', function() + { + + }); + socket.on('skip', function(list) { console.log("skip on list: " + list); var coll = list[0].toLowerCase(); - db.collection(coll).find({skip: "true"}, function(err, docs){ + db.collection(coll).find({skip: true}, function(err, docs){ if(docs.length == 1) { - if(docs[0]["views"].length/2 <= docs[0]["skips"]+1) + console.log(lists[coll]); + if(lists[coll].length/2 <= docs[0]["skips"]+1) { - //aggregationfunction to update now playing and the next boolean values - //Also flush skips array so its length = 0 + db.collection(coll).update({now_playing:true}, + {$set:{ + now_playing:false, + votes:0, + guids:[] + }}, function(err, docs) + { + db.collection(coll).findAndModify({ + query: {now_playing:false}, + sort: {votes:-1}, + update: + {$set:{ + now_playing:true, + votes:0, + guids:[] + }} + }, function(err, docs) + { + var d = new Date(); + var time = Math.floor(d.getTime() / 1000); + db.collection(coll).update({views:{$exists:true}}, + {$set:{startTime:time}}, function(err, docs){ + db.collection(coll).find().sort({votes:-1}, function(err, docs) + { + io.sockets.emit(coll, docs); + db.collection(coll).find({now_playing:true}, function(err, np){ + console.log("sending now_playing to " + coll+",np"); + db.collection(coll).find({views:{$exists:true}}, function(err, conf){ + var d = new Date(); + var time = Math.floor(d.getTime() / 1000); + toSend = [np,conf,time]; + console.log(toSend); + io.sockets.emit(coll+",np", toSend); + }); + }); + }); + }); + }); + }); + }else{ + db.collection(coll).update({views:{$exists:true}}, {$push:{guids:guid}}, function(err, coll){ + //reply with skips or something + console.log("skipped without effect"); + }); } } }); @@ -111,15 +254,14 @@ io.on('connection', function(socket){ { try { - var index = lists[coll].indexOf(socket); + var index = lists[coll].indexOf(guid); lists.splice(index, 1); + io.sockets.emit(coll+",viewers", lists[coll].length); }catch(err){} /*db.collection(coll).update({guids: guid},{$pull: {guids: guid}}, {multi: true}, function(err, docs) {}); db.collection(coll).update({skips: guid},{$pull: {skips: guid}}, {multi: true}, function(err, docs) {});*/ - db.collection(coll).update({views: guid},{$pull: {views: guid}}, {multi: true}, function(err, docs) - {}); }); });