<commit_before>var express = require('express'),
    bodyParser = require('body-parser'),
    app = express(),
    http = require('http'),
    port = 8000,
    _ = require('lodash');

var questionTime = 10; // 10 sec
var users = {};
var quizzStarted = false;
var timeoutQuizzStart = undefined;
var questionTimeout = undefined;
var currentQuestionId = -1;
var questions = [{    
    question: {
        intitule: "blablabla",
        wikipediaUrl: "https://fr.wikipedia.org/w/api.php?action=query&format=json&titles=Jules%20Verne&prop=extracts&explaintext=true&exintro=true",
        removeTerms: ["jules", "verne"],
        answers: [
            "<NAME>",
            "<NAME>",
            "<NAME>"
        ]
    },
    answer: {
        id: 0,
        link: 'https://www.google.fr/',
        streetView: 'http://streetviewing.fr/'
    }
}, {
    question: {
        intitule: "blobloblo",
        wikipediaUrl: "https://fr.wikipedia.org/w/api.php?action=query&format=json&titles=Marc%20Caro&prop=extracts&explaintext=true&exintro=true",
        removeTerms: ["marc", "caro"],
        answers: [
            "<NAME>",
            "<NAME>",
            "<NAME>"
        ]
    },
    answer: {
        id: 0,
        link: 'https://www.google.fr/',
        streetView: 'http://streetviewing.fr/'
    }
}];
var userAnswers = [];


// Express configuration
//
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// Start the HTTP server
//
var httpServer = http.Server(app);
httpServer.listen(port, function() {
    console.log('*** You can test by opening: "localhost:' + port + '" ***');
});

// Socket IO processing
//
var io = require('socket.io').listen(httpServer);

io.on('connection', function(socket) {

    // Events received by the server and the emission of events :
    //  - user connect (P1)
    //    >> quizz started (P1)
    //    >> quizz not started (P1)
    //    >> user list (P2)
    //  - user disconnect (P2)
    //    >> user list (P2)
    //  - user answer (P1)
    //
    // Other emitted events:
    //  - The quizz starts:
    //    >> quizz started
    //  - New question sent to players:
    //    >> new question
    //    >> question timeleft
    //    >> question answer
    //  - End of the game:
    //    >> end quizz

    socket.on('play quizz', function() {

        if (!quizzStarted) {
            if (!timeoutQuizzStart) {
                // Start a timer, after the end of it, the quizz will start
                setTimeout(startQuizz, 10000);  
                timeoutQuizzStart = { start: Date.now(), delay: 10000 };
            }

            console.log(' >> quizz not started');
            socket.emit('quizz not started', {
                msg: 'Le quizz va bienôt démarrer, merci de patienter',
                users: users,
                startIn:  getTimeLeft(timeoutQuizzStart) });
        }
    });

    socket.on('user connect', function(nickname) {
        console.log(' << new user : ' + nickname);
        socket.nickname = nickname;
        users[nickname] = { nickname : nickname, score : 0, socketId: socket.id };

        if (quizzStarted) {
            /*socket.emit('quizz started', { 
                msg: 'Un quizz est démarré, vous commencerez avec la prochaine question',
                users: users });*/
        }
        else {
            if (_.size(users) == 1) {
                // Start a timer, after the end of it, the quizz will start
                setTimeout(startQuizz, 10000);  
                timeoutQuizzStart = { start: Date.now(), delay: 10000 };
            }

            console.log(' >> quizz not started');
            socket.emit('quizz not started', {
                msg: 'Le quizz va bienôt démarrer, merci de patienter',
                users: users,
                startIn:  getTimeLeft(timeoutQuizzStart) });
        }
    });

    socket.on('user disconnect', function(nickname) {
        console.log(' << user disconnect : ' + nickname);
        delete users[nickname];

        console.log(' >> user list : ' + nickname);
        socket.broadcast.emit('user list', {
            users: users
        });

        if (_.size(users) == 0) {
            reinitQuizz();
        }
    });

    socket.on('user answer', function (userAnswer) {
        console.log(' << user answer : ' + userAnswer);
        // userAnswer.answerId, userAnswer.nickname
        if (questionTimeout) { // Answers are still accepted
            userAnswers.push(userAnswer);
        }
        else {
            console.log(' * too late for ' + userAnswer.nickname);
        }
    });

});

//----------------------------------------------
// Quizz management functions
//----------------------------------------------
function reinitQuizz() {
    console.log(' * reinit quizz');

    quizzStarted = false;
    timeoutQuizzStart = undefined;
    questionTimeout = undefined;
    currentQuestionId = -1;
    userAnswers = [];

    _.forEach(users, function (user) {
        user.score = 0;
    });
}

function startQuizz() {
    console.log(' >> start quizz');
    quizzStarted = true;
    io.sockets.emit('quizz started', {
        msg: 'Le quizz démarre !'
    });

    setTimeout(sendQuestion, 5000);
    
}

function sendQuestion() {
    console.log(' >> new question');
    io.sockets.emit('new question', { 
        question: questions[++currentQuestionId].question,
        timeleft: questionTime
    });

    setTimeout(processQuestionTimeout, 1000);
    questionTimeout = { start: Date.now(), delay: questionTime * 1000 };
}

function processQuestionTimeout() {
    var timeleft = getTimeLeft(questionTimeout);
    console.log(' * question timeleft : ' + timeleft);
    if (timeleft <= 0) { // No more time
        questionTimeout = undefined;
        var winners = processUserAnswers();
        console.log(' >> question answer');
        io.sockets.emit('question answer', {
             answer: questions[currentQuestionId].answer,
             winners: winners
        });

        if (currentQuestionId+1 === questions.length) {
            console.log(' >> end quizz');
            io.sockets.emit('end quizz', users);
            reinitQuizz();
        }
        else {
            setTimeout(sendQuestion, 5000);
        }
    }
    else {
        var percent = 100 - (100 * (questionTime - timeleft) / questionTime); // Pfffffffffffffffff...
        console.log(' >> question timeleft');
        io.sockets.emit('question timeleft', { timeleft: timeleft, percent: percent , userAnswers: getUsersWithGoodAnswer() });
        setTimeout(processQuestionTimeout, 1000);

    }

}

function getUsersWithGoodAnswer() {
    console.log(' * getUsersWithGoodAnswer')<commit_msg>update tests<commit_after>var express = require('express'),
    bodyParser = require('body-parser'),
    app = express(),
    http = require('http'),
    port = 8000,
    _ = require('lodash');

var questionTime = 10; // 10 sec
var users = {};
var quizzStarted = false;
var timeoutQuizzStart = undefined;
var questionTimeout = undefined;
var currentQuestionId = -1;
var questions = [{    
    question: {
        intitule: "blablabla",
        wikipediaUrl: "https://fr.wikipedia.org/w/api.php?action=query&format=json&titles=Jules%20Verne&prop=extracts&explaintext=true&exintro=true",
        removeTerms: ["jules", "verne"],
        answers: [
            "<NAME>",
            "<NAME>",
            "<NAME>"
        ]
    },
    answer: {
        id: 0,
        link: 'https://www.google.fr/',
        streetView: 'http://streetviewing.fr/'
    }
}, {
    question: {
        intitule: "blobloblo",
        wikipediaUrl: "https://fr.wikipedia.org/w/api.php?action=query&format=json&titles=Marc%20Caro&prop=extracts&explaintext=true&exintro=true",
        removeTerms: ["marc", "caro"],
        answers: [
            "<NAME>",
            "<NAME>",
            "<NAME>"
        ]
    },
    answer: {
        id: 0,
        link: 'https://www.google.fr/',
        streetView: 'http://streetviewing.fr/'
    }
}];
var userAnswers = [];


// Express configuration
//
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// Start the HTTP server
//
var httpServer = http.Server(app);
httpServer.listen(port, function() {
    console.log('*** You can test by opening: "localhost:' + port + '" ***');
});

// Socket IO processing
//
var io = require('socket.io').listen(httpServer);

io.on('connection', function(socket) {

    // Events received by the server and the emission of events :
    //  - user connect (P1)
    //    >> quizz started (P1)
    //    >> quizz not started (P1)
    //    >> user list (P2)
    //  - user disconnect (P2)
    //    >> user list (P2)
    //  - user answer (P1)
    //
    // Other emitted events:
    //  - The quizz starts:
    //    >> quizz started
    //  - New question sent to players:
    //    >> new question
    //    >> question timeleft
    //    >> question answer
    //  - End of the game:
    //    >> end quizz

    socket.on('play quizz', function() {

        if (!quizzStarted) {
            if (!timeoutQuizzStart) {
                // Start a timer, after the end of it, the quizz will start
                setTimeout(startQuizz, 10000);  
                timeoutQuizzStart = { start: Date.now(), delay: 10000 };
            }

            console.log(' >> quizz not started');
            socket.emit('quizz not started', {
                msg: 'Le quizz va bienôt démarrer, merci de patienter',
                users: users,
                startIn:  getTimeLeft(timeoutQuizzStart) });
        }
    });

    socket.on('user connect', function(nickname) {
        console.log(' << new user : ' + nickname);
        socket.nickname = nickname;
        users[nickname] = { nickname : nickname, score : 0, socketId: socket.id };

        if (quizzStarted) {
            /*socket.emit('quizz started', { 
                msg: 'Un quizz est démarré, vous commencerez avec la prochaine question',
                users: users });*/
        }
        else {
            if (_.size(users) == 1) {
                // Start a timer, after the end of it, the quizz will start
                setTimeout(startQuizz, 10000);  
                timeoutQuizzStart = { start: Date.now(), delay: 10000 };
            }

            console.log(' >> quizz not started');
            socket.emit('quizz not started', {
                msg: 'Le quizz va bienôt démarrer, merci de patienter',
                users: users,
                startIn:  getTimeLeft(timeoutQuizzStart) });
        }
    });

    socket.on('user disconnect', function(nickname) {
        console.log(' << user disconnect : ' + nickname);
        delete users[nickname];

        console.log(' >> user list : ' + nickname);
        socket.broadcast.emit('user list', {
            users: users
        });

        if (_.size(users) == 0) {
            reinitQuizz();
        }
    });

    socket.on('user answer', function (userAnswer) {
        console.log(' << user answer : ' + userAnswer);
        // userAnswer.answerId, userAnswer.nickname
        if (questionTimeout) { // Answers are still accepted
            userAnswers.push(userAnswer);
        }
        else {
            console.log(' * too late for ' + userAnswer.nickname);
        }
    });

});

//----------------------------------------------
// Quizz management functions
//----------------------------------------------
function reinitQuizz() {
    console.log(' * reinit quizz');

    quizzStarted = false;
    timeoutQuizzStart = undefined;
    questionTimeout = undefined;
    currentQuestionId = -1;
    userAnswers = [];

    _.forEach(users, function (user) {
        user.score = 0;
    });
}

function startQuizz() {
    console.log(' >> start quizz');
    quizzStarted = true;
    io.sockets.emit('quizz started', {
        msg: 'Le quizz démarre !'
    });

    setTimeout(sendQuestion, 5000);
    
}

function sendQuestion() {
    console.log(' >> new question');
    io.sockets.emit('new question', { 
        question: questions[++currentQuestionId].question,
        timeleft: questionTime
    });

    setTimeout(processQuestionTimeout, 1000);
    questionTimeout = { start: Date.now(), delay: questionTime * 1000 };
}

function processQuestionTimeout() {
    var timeleft = getTimeLeft(questionTimeout);
    console.log(' * question timeleft : ' + timeleft);
    if (timeleft <= 0) { // No more time
        questionTimeout = undefined;
        var winners = processUserAnswers();
        console.log(' >> question answer');
        io.sockets.emit('question answer', {
             answer: questions[currentQuestionId].answer,
             winners: winners
        });

        if (currentQuestionId+1 === questions.length) {
            console.log(' >> end quizz');
            setTimeout(function () {
                io.sockets.emit('end quizz', users);
                reinitQuizz();
            }, 5000);
        }
        else {
            setTimeout(sendQuestion, 5000);
        }
    }
    else {
        var percent = 100 - (100 * (questionTime - timeleft) / questionTime); // Pfffffffffffffffff...
        console.log(' >> question timeleft');
        io.sockets.emit('question timeleft', { timeleft: timeleft, percent: percent , userAnswers: getUsersWithGoodAnswer() });
        setTimeout(processQuestionTimeout, 1000);

    }

}

function getUsersWithGoodAnswer() {
    console.log(' * getUsersWithGoodAnswer')
{'fraction_non_alphanumeric': 0.15436835891381345, 'fraction_numerical': 0.011363636363636364, 'mean_word_length': 1.9806465801627446, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 83, 'https://': 8, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '3986994', 'n_tokens_mistral': 4100, 'n_tokens_neox': 3856, 'n_words': 908}