summaryrefslogtreecommitdiff
path: root/modules/poke/poke.js
blob: af0285d811743bc8ef22c82c8bed2c6448ca2beb (plain)
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
var cmn=require("../$common.js"),
    fs=require("fs"),
    Naampje=require("naampje").name;

var conns=[];

var uniqid=(function(){
	var id=0;
	return function(){return id++;};
})();

module.exports=function(app,io,moddir){
	var ioNsp=io.of("/poke");
	app.get("/poke",function(req,res){
		res.sendFile(moddir+"/poke.html");
	});
	ioNsp.on("connection",function(socket){
		var id=uniqid();
		var nick=Naampje();
		var status="";
		socket.emit("reset",null);
		socket.emit("self",{id:id,nick:nick});
		conns.forEach(function(c){
			socket.emit("connection add",{id:c.id,nick:c.nick});
			socket.emit("status",{id:c.id,status:c.status});
		});
		ioNsp.emit("connection add",{id:id,nick:nick});
		conns.push({id:id,nick:nick,status:"",socket:socket});
		socket.on("disconnect",function(){
			for(var i=0;i<conns.length;i++)if(conns[i].id==id)break;
			if(i!=conns.length)conns.splice(i,1);
			ioNsp.emit("connection remove",id);
		});
		socket.on("status",function(newst){
			status=newst.toString();
			for(var i=0;i<conns.length;i++)if(conns[i].id==id)break;
			if(i==conns.length)return; //?
			conns[i].status=status;
			ioNsp.emit("status",{id:id,status:status});
		});
		socket.on("poke",function(target){
			for(var i=0;i<conns.length;i++)if(conns[i].id==target)break;
			if(i==conns.length)return; //hah.
			conns[i].socket.emit("poke",id);
		});
	});
};