summaryrefslogtreecommitdiffstats
path: root/index.js
blob: 23849b6e9c9ee278ee3d325837071541fd38d969 (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
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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = (process.env.PORT || 5000);
var LOGBROADCAST = true;
//get port from commandline parameteres
if (process.argv.length== 3){
  port = process.argv[2];
}

var que = [];
var pws = {};

app.get('/', function(req, res){
  res.sendFile(__dirname + '/f/templates/index.html');
});
app.get('/adm', function(req, res){
  res.sendFile(__dirname + '/f/templates/adm.html');
});
app.get('/log', function(req, res){
  res.sendFile(__dirname + '/f/templates/log.html');
});


//route all paths beginning with /f/ to real files 
app.get('/f/*', function(req, res){
  res.sendFile(__dirname + req.path);
});


io.on('connection', function(socket){
  var uid = Date.now();
  var pw =  Math.random()*10000000 + Math.random()*10000000 -1;
  pws[uid] = pw;


  socket.emit("userinfo",{"uid":uid,"pw":pw});
  socket.emit("quedata",que);
  socket.on("logsubscribe",function(data){
    log("User "+data.uid+ " has subscribed in logging");
    socket.join("log");
  });  
   
  socket.on("queadd",function(data){
		log("User " +data.name+ " on row " +data.row+" added to queue");
    data.qid= Date.now();

  	que.push(data);
		io.emit("quedata",que);
  });
	socket.on("quepopfirst",function(data){
  	    var deleted = que.shift();
        log("admin deleted "+ deleted.name +" from queue");
		io.emit("quedata",que);

  });
  socket.on("quedelete",function(data){
    for (var i=0;i<que.length;i++){
      if (que[i].qid == data.qid){
        if (data.pw != pws[que[i].uid]){
          socket.emit("err",{"msg":"you're not allowed to remove this entry"});
          break;
        }
        log(que[i].name+" deleted himself from queue");
        que.splice(i,1);
        io.emit("quedata",que);
        break;
      }
    }
  });

});

function getTimeString()
{
    return Date().toLocaleString();
}

function log(msg){
    var logmsg=getTimeString()+": "+msg;
    console.log(logmsg);
    if(LOGBROADCAST){
      io.to("log").emit("logmessage",logmsg);
    }
}
http.listen(port, function(){
  log('listening on port '+port);
});