From efc74f972ab1200a52841f17cfaa6499d9bf2359 Mon Sep 17 00:00:00 2001
From: tomsmeding <hallo@tomsmeding.nl>
Date: Sun, 13 Dec 2015 22:18:22 +0100
Subject: DONGDONGDONGDONGDONGDONG

---
 dong.mp3   | Bin 0 -> 6657 bytes
 dong.ogg   | Bin 0 -> 7570 bytes
 index.html |  37 ++++++++++++++++++++++++++++---------
 index.js   |  25 +++++++++++++++----------
 4 files changed, 43 insertions(+), 19 deletions(-)
 create mode 100644 dong.mp3
 create mode 100644 dong.ogg

diff --git a/dong.mp3 b/dong.mp3
new file mode 100644
index 0000000..b82b085
Binary files /dev/null and b/dong.mp3 differ
diff --git a/dong.ogg b/dong.ogg
new file mode 100644
index 0000000..d15d02b
Binary files /dev/null and b/dong.ogg differ
diff --git a/index.html b/index.html
index 22bc9c2..852ff97 100644
--- a/index.html
+++ b/index.html
@@ -12,7 +12,7 @@ var COLOURS=["#00F","#F00","#0CC"];
 
 var socket=io(),me=[-1,""],userlist=[],rooms={},visibleroomuidiv=null;
 socket.on("disconnect",function(){
-	location.href=location.href;
+	location.reload();
 });
 socket.on("message",function(msg){
 	alert("Message:\n\n"+msg);
@@ -184,6 +184,8 @@ function Room(_roomid){
 	this.uidiv.appendChild(this.statusdiv);
 
 	document.body.appendChild(this.uidiv);
+
+	setTimeout(this.becomeVisible.bind(this),1);
 }
 Room.prototype.becomeVisible=function(){
 	if(visibleroomuidiv)visibleroomuidiv.classList.add("invisible");
@@ -204,7 +206,7 @@ Room.prototype.destroy=function(){
 	this.uidiv.parentElement.removeChild(this.uidiv);
 	this.listdiv.parentElement.removeChild(this.listdiv);
 };
-Room.prototype.chat=function(by,msg){
+Room.prototype.chat=function(by,msg,isme){
 	var tr,td,div;
 	tr=document.createElement("tr");
 	td=document.createElement("td");
@@ -215,6 +217,7 @@ Room.prototype.chat=function(by,msg){
 	tr.appendChild(td);
 	this.uidiv.getElementsByClassName("chatlogtbody")[0].appendChild(tr);
 	tr.scrollIntoView();
+	if(!isme)sounddong();
 };
 Room.prototype.creategame=function(){
 	this.newgamebtn.classList.add("invisible");
@@ -228,14 +231,13 @@ Room.prototype.myturn=function(ot){
 	this.onturn=ot;
 	this.statusdiv.innerHTML="Your turn!";
 };
-Room.prototype.place=function(pos,pidx){
+Room.prototype.place=function(pos,pidx,isme){
 	//console.log("place: onturn "+this.onturn+" -> "+pidx);
 	this.onturn=pidx;
 	this.canplace=false;
 	this.lastplacepos=pos;
 	queueapplymove(this.id,this.ctx,this.cellsz,pos,pidx,this.lastplacepos);
 	queueapplymove(this.id,function(){
-		//WINCHECK?
 		//console.log("place qam_func: onturn "+this.onturn+" -> "+(this.onturn+1)%this.userlist.length);
 		if(!this.canplace){
 			this.onturn=(this.onturn+1)%this.userlist.length;
@@ -243,6 +245,7 @@ Room.prototype.place=function(pos,pidx){
 		}
 		drawboard(this.ctx,this.cellsz,this.bd,this.onturn,this.lastplacepos);
 	}.bind(this));
+	if(!isme)sounddong();
 };
 Room.prototype.gotwin=function(pidx){
 	queueapplymove(this.id,function(){
@@ -255,7 +258,10 @@ Room.prototype.gotwin=function(pidx){
 };
 
 function roominvite(roomid,by){
-	if(!confirm("User '"+by[1]+"' sent you an invite to join their room. Accept?"))return;
+	if(!confirm("User '"+by[1]+"' sent you an invite to join their room. Accept?")){
+		socket.emit("inviteaccept",roomid,true);
+		return;
+	}
 	socket.emit("inviteaccept",roomid);
 }
 
@@ -264,8 +270,8 @@ function roomdestroy(roomid){
 	delete rooms[roomid];
 }
 
-function roomchat(roomid,by,msg){
-	rooms[roomid].chat(by,msg);
+function roomchat(roomid,by,msg,isme){
+	rooms[roomid].chat(by,msg,isme);
 }
 
 function roomcreategame(roomid){
@@ -276,8 +282,8 @@ function roomgameturn(roomid,ot){
 	rooms[roomid].myturn(ot);
 }
 
-function roomgameplace(roomid,pos,pidx){
-	rooms[roomid].place(pos,pidx);
+function roomgameplace(roomid,pos,pidx,isme){
+	rooms[roomid].place(pos,pidx,isme);
 }
 
 function roomgamewin(roomid,pidx){
@@ -455,6 +461,14 @@ function queueapplymove(id,ctx,cellsz,idx,c,lastplacepos){
 		else applymove(ctx,cellsz,idx,c,lastplacepos,id);
 	}
 }
+
+
+var sounddong__audioelem=null;
+function sounddong(){
+	if(sounddong__audioelem==null)sounddong__audioelem=document.getElementById("dongaudioelem");
+	sounddong__audioelem.currentTime=0;
+	sounddong__audioelem.play();
+}
 </script>
 <style>
 body{
@@ -617,5 +631,10 @@ div#roomuisplash{
 	</div>
 	<div id="roomlistcontainer"></div>
 </div>
+
+<audio id="dongaudioelem" preload="auto">
+	<source src="dong.ogg">
+	<source src="dong.mp3">
+</audio>
 </body>
 </html>
diff --git a/index.js b/index.js
index f4f55b2..33b1ffe 100755
--- a/index.js
+++ b/index.js
@@ -22,7 +22,7 @@ app.get("/",function(req,res){
 	res.sendFile(__dirname+"/index.html");
 });
 
-["index.html","common.js"].forEach(function(fname){
+["index.html","common.js","dong.ogg","dong.mp3"].forEach(function(fname){
 	app.get("/"+fname,function(req,res){
 		res.sendFile(__dirname+"/"+fname);
 	});
@@ -99,12 +99,14 @@ io.on("connection",function(conn){
 		if(idx==-1)return; //WAT
 		obj.pending.forEach(function(pendid){
 			io.to(pendid).emit("roomdestroy",pendid);
-			rooms[pendid].ids.forEach(function(id){
-				var o=connlist[findid(id)];
-				o.conn.leave(pendid);
-				o.rooms.splice(o.rooms.indexOf(pendid),1);
-			});
-			delete rooms[pendid];
+			if(rooms[pendid]){
+				rooms[pendid].ids.forEach(function(id){
+					var o=connlist[findid(id)];
+					o.conn.leave(pendid);
+					o.rooms.splice(o.rooms.indexOf(pendid),1);
+				});
+				delete rooms[pendid];
+			}
 		});
 		obj.rooms.forEach(function(roomid){
 			io.to(roomid).emit("roomdestroy",roomid);
@@ -157,9 +159,10 @@ io.on("connection",function(conn){
 			connlist[idx].pending.push(roomid);
 		});
 	});
-	conn.on("inviteaccept",function(roomid){
+	conn.on("inviteaccept",function(roomid,wellactuallydecline){
 		var idx=obj.pending.indexOf(roomid);
 		if(idx!=-1)obj.pending.splice(idx,1);
+		if(wellactuallydecline)return;
 		if(idx==-1||rooms[roomid]==undefined){
 			conn.emit("message","Cannot accept non-existent invitation");
 			return;
@@ -200,7 +203,8 @@ io.on("connection",function(conn){
 	});
 	conn.on("roomchat",function(roomid,message){
 		if(!guardroomid(roomid))return;
-		io.to(roomid).emit("roomchat",roomid,[obj.id,obj.name],message);
+		conn.broadcast.to(roomid).emit("roomchat",roomid,[obj.id,obj.name],message,false); //send to everyone in room EXCEPT this client
+		conn.emit("roomchat",roomid,[obj.id,obj.name],message,true);
 	});
 	conn.on("roomcreategame",function(roomid){
 		if(!guardroomid(roomid))return;
@@ -250,7 +254,8 @@ io.on("connection",function(conn){
 			conn.emit("message","Invalid move!");
 			return;
 		}
-		io.to(roomid).emit("roomgameplace",roomid,pos,playeridx);
+		conn.broadcast.to(roomid).emit("roomgameplace",roomid,pos,playeridx,false);
+		conn.emit("roomgameplace",roomid,pos,playeridx,true);
 		var win=rooms[roomid].game.checkwin();
 		if(win!=-1){
 			io.to(roomid).emit("roomgamewin",roomid,playeridx);
-- 
cgit v1.2.3-70-g09d2