<commit_before>'use strict';
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var port = process.env.PORT || 5000;
var room = process.env.room || 'portal';

var routes = require('./routes/index');
var app = express();
var remote = require('./remote/control.js');
// var fs = require('fs');
// This is useful just in development env.
// var keys = {
//   key: fs.readFileSync('ssl/key.pem'),
//   cert: fs.readFileSync('ssl/cert.pem')
// };
var http = require('http').Server(app);
var io = require('socket.io')(http);

var current = 0;

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'images/favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

io.on('connection', function(socket) {
    socket.on('room', function(room) {
        if (current < 2) {
            socket.join(room);
            current = io.nsps['/'].adapter.rooms[room].length;

            io.sockets.in(room).emit('join', current);

            socket.on('message', function(desc) {
                io.sockets.in(room).emit('message', desc);
            });

            socket.on('talk', function(desc) {
                io.sockets.in(room).emit('talk', desc);
            });

            socket.on('quiet', function(desc) {
                io.sockets.in(room).emit('quiet', desc);
            });

            socket.on('refresh', function() {
                io.sockets.in(room).emit('refresh');
            });

        } else {
            socket.emit('redirect', current);
            return;
        }

    });

    socket.on('disconnect', function() {
        if(io.nsps['/'].adapter.rooms[room]){
            current = io.nsps['/'].adapter.rooms[room].length;  
        }
        if(current<2){
            io.sockets.in(room).emit('refresh');
        }
        socket.leave(socket.room);
    });

});

app.get('/r/:room', function(req, res) {
    var room = req.param('room');
    res.render('portal', {title: 'Test', room: room});
});

app.post('/off', function(req, res) {
    var message = remote.getActionMessage(req.body.token, '/off');
    if(message.status === 200) {
        io.sockets.in(room).emit('quiet');
    }
    res.json(message);
});

app.post('/on', function(req, res) {
    var message = remote.getActionMessage(req.body.token, '/on');
    if(message.status === 200) {
        io.sockets.in(room).emit('talk');
    }
    res.json(message);
});

http.listen(port);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;<commit_msg>Add function getCurrent()<commit_after>'use strict';
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var port = process.env.PORT || 5000;
var room = process.env.room || 'portal';

var routes = require('./routes/index');
var app = express();
var remote = require('./remote/control.js');
// var fs = require('fs');
// This is useful just in development env.
// var keys = {
//   key: fs.readFileSync('ssl/key.pem'),
//   cert: fs.readFileSync('ssl/cert.pem')
// };
var http = require('http').Server(app);
var io = require('socket.io')(http);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'images/favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

io.on('connection', function(socket) {
    socket.on('room', function(room) {
        var current = getCurrent(room);
        if (current < 2) {
            socket.join(room);
            current = getCurrent(room);

            io.sockets.in(room).emit('join', current);

            socket.on('message', function(desc) {
                io.sockets.in(room).emit('message', desc);
            });

            socket.on('talk', function(desc) {
                io.sockets.in(room).emit('talk', desc);
            });

            socket.on('quiet', function(desc) {
                io.sockets.in(room).emit('quiet', desc);
            });

            socket.on('refresh', function() {
                io.sockets.in(room).emit('refresh');
            });

        } else {
            socket.emit('redirect', current);
            return;
        }

    });

    socket.on('disconnect', function() {
        var current = getCurrent(room);
        if(io.nsps['/'].adapter.rooms[room]) {
            current = io.nsps['/'].adapter.rooms[room].length;
        }
        if(current <2) {
            io.sockets.in(room).emit('refresh');
        }
        socket.leave(socket.room);
    });

});

function getCurrent(room) {
    var roomVar = io.nsps['/'].adapter.rooms[room];
    return (roomVar === undefined)? 0:roomVar.length;
}

app.get('/r/:room', function(req, res) {
    var room = req.param('room');
    res.render('portal', {title: 'Test', room: room});
});

app.post('/off', function(req, res) {
    var message = remote.getActionMessage(req.body.token, '/off');
    if(message.status === 200) {
        io.sockets.in(room).emit('quiet');
    }
    res.json(message);
});

app.post('/on', function(req, res) {
    var message = remote.getActionMessage(req.body.token, '/on');
    if(message.status === 200) {
        io.sockets.in(room).emit('talk');
    }
    res.json(message);
});

http.listen(port);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
{'fraction_non_alphanumeric': 0.19738706123091454, 'fraction_numerical': 0.005981426097906501, 'mean_word_length': 2.8439201451905625, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5050208', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1986, 'n_words': 380}