<commit_before>'use strict';

var io = require('socket.io');
var moment = require('moment');
var util = require('util');

var models = require('./models');
var enet = require('./eventnet');

var SwiftCODESockets = function() {
    var self = this;

    self.listen = function(server) {
        self.io = server;
        self.io.set('log level', 1);

        self.setupListeners();
    };

    /**
     * Setup socket listeners
     */
    self.setupListeners = function() {
        var lobbyCount = 0;

        var lobbySockets = self.io.of('/lobby')
        .on('connection', function(socket) {
            socket.on('games:fetch', function(data) {
                models.Game.find({ isViewable: true }, null, { sort: { isJoinable: -1 } }, function(err, docs) {
                    socket.emit('games:fetch:res', docs);
                });
            });

            socket.on('games:join', function(data) {
                models.User.findById(data.player, function(err, user) {
                    if (err) {
                        util.log(err);
                        util.log('games:join error'); return;
                    }
                    if (user) {
                        user.prepareIngameAction('join', {
                            game: data.game
                        }, function(err) {
                            if (err) {
                                util.log(err);
                            }
                            socket.emit('games:join:res', { success: !err });
                        });
                    }
                });
            });

            socket.on('games:createnew', function(data) {
                models.User.findById(data.player, function(err, user) {
                    if (err) {
                        util.log(err);
                        util.log('games:join error'); return;
                    }
                    if (user) {
                        user.prepareIngameAction('createnew', {
                            lang: data.key,
                            isSinglePlayer: data.gameType === 'single'
                        }, function(err) {
                            if (err) {
                                util.log(err);
                            }
                            socket.emit('games:createnew:res', { success: !err });
                        });
                    }
                });
            });

            socket.on('disconnect', function() {
                lobbyCount--;
                lobbySockets.emit('lobbycount', { count: lobbyCount });
            });

            lobbyCount++;
            lobbySockets.emit('lobbycount', { count: lobbyCount });
        });

        var gameSockets = self.io.of('/game')
        .on('connection', function(socket) {
            socket.on('ingame:ready', function(data) {
                socket.set('player', data.player, function() {
                    models.User.findById(data.player, function(err, user) {
                        if (err) {
                            util.log(err);
                            util.log('ingame:ready error');
                            return socket.emit('ingame:ready:res', {
                                success: false,
                                err: err
                            });;
                        }
                        if (user) {
                            user.performIngameAction(function(err, success, game) {
                                if (!success) {
                                    return socket.emit('ingame:ready:res', {
                                        success: false,
                                        err: err
                                    });
                                }

                                socket.set('game', game.id);
                                models.Exercise.findById(game.exercise, 'code projectName typeableCode typeables', function(err, exercise) {
                                    if (exercise) {
                                        // Join a room and broadcast join
                                        socket.join('game-' + game.id);

                                        socket.emit('ingame:ready:res', {
                                            success: true,
                                            game: game,
                                            timeLeft: game.starting ? moment().diff(game.startTime) : undefined,
                                            exercise: exercise,
                                            nonTypeables: models.NON_TYPEABLE_CLASSES
                                        });
                                    } else {
                                        util.log('exercise not found');
                                        socket.emit('ingame:ready:res', {<commit_msg>Remove extraneous semicolon<commit_after>'use strict';

var io = require('socket.io');
var moment = require('moment');
var util = require('util');

var models = require('./models');
var enet = require('./eventnet');

var SwiftCODESockets = function() {
    var self = this;

    self.listen = function(server) {
        self.io = server;
        self.io.set('log level', 1);

        self.setupListeners();
    };

    /**
     * Setup socket listeners
     */
    self.setupListeners = function() {
        var lobbyCount = 0;

        var lobbySockets = self.io.of('/lobby')
        .on('connection', function(socket) {
            socket.on('games:fetch', function(data) {
                models.Game.find({ isViewable: true }, null, { sort: { isJoinable: -1 } }, function(err, docs) {
                    socket.emit('games:fetch:res', docs);
                });
            });

            socket.on('games:join', function(data) {
                models.User.findById(data.player, function(err, user) {
                    if (err) {
                        util.log(err);
                        util.log('games:join error'); return;
                    }
                    if (user) {
                        user.prepareIngameAction('join', {
                            game: data.game
                        }, function(err) {
                            if (err) {
                                util.log(err);
                            }
                            socket.emit('games:join:res', { success: !err });
                        });
                    }
                });
            });

            socket.on('games:createnew', function(data) {
                models.User.findById(data.player, function(err, user) {
                    if (err) {
                        util.log(err);
                        util.log('games:join error'); return;
                    }
                    if (user) {
                        user.prepareIngameAction('createnew', {
                            lang: data.key,
                            isSinglePlayer: data.gameType === 'single'
                        }, function(err) {
                            if (err) {
                                util.log(err);
                            }
                            socket.emit('games:createnew:res', { success: !err });
                        });
                    }
                });
            });

            socket.on('disconnect', function() {
                lobbyCount--;
                lobbySockets.emit('lobbycount', { count: lobbyCount });
            });

            lobbyCount++;
            lobbySockets.emit('lobbycount', { count: lobbyCount });
        });

        var gameSockets = self.io.of('/game')
        .on('connection', function(socket) {
            socket.on('ingame:ready', function(data) {
                socket.set('player', data.player, function() {
                    models.User.findById(data.player, function(err, user) {
                        if (err) {
                            util.log(err);
                            util.log('ingame:ready error');
                            return socket.emit('ingame:ready:res', {
                                success: false,
                                err: err
                            });
                        }
                        if (user) {
                            user.performIngameAction(function(err, success, game) {
                                if (!success) {
                                    return socket.emit('ingame:ready:res', {
                                        success: false,
                                        err: err
                                    });
                                }

                                socket.set('game', game.id);
                                models.Exercise.findById(game.exercise, 'code projectName typeableCode typeables', function(err, exercise) {
                                    if (exercise) {
                                        // Join a room and broadcast join
                                        socket.join('game-' + game.id);

                                        socket.emit('ingame:ready:res', {
                                            success: true,
                                            game: game,
                                            timeLeft: game.starting ? moment().diff(game.startTime) : undefined,
                                            exercise: exercise,
                                            nonTypeables: models.NON_TYPEABLE_CLASSES
                                        });
                                    } else {
                                        util.log('exercise not found');
                                        socket.emit('ingame:ready:res', {
{'fraction_non_alphanumeric': 0.10985510225053952, 'fraction_numerical': 0.0006165861679169664, 'mean_word_length': 0.8704593503747837, 'pattern_counts': {'":': 0, '<': 3, '<?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': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6460706', 'n_tokens_mistral': 2181, 'n_tokens_neox': 2044, 'n_words': 382}