<commit_before>/* global require, socket, process */
/* jshint strict: false, devel: true */

/**
 * Obacht Game Node.js Multiplayer Server
 *
 * @author <NAME>
 */

//////////////////////////////
// Modules and Variables    //
//////////////////////////////

var gameServer = {
    maxPlayers: 2,
    maxPin: 9999,
    port: (process.argv[2] ? process.argv[2] : 8080) // Use Console Argument if there
};

var io = require('socket.io').listen(gameServer.port);


//////////////////////////////
// Configuration            //
//////////////////////////////

io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging


//////////////////////////////
// Comminication            //
//////////////////////////////

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

    /**
     * New Player connects to Server
     * Sends ID back so that the client knows it's successful connected
     */
    socket.emit('connected', {
        pid: socket.id
    });
    console.log('+++ NEW REMOTE CONNECTION');


    /**
     * Connection to Server fails
     */
    socket.on('connect_failed', function(){
        socket.emit('connected', {
            pid: socket.id,
            error: 'Connection to Server failed!'
        });
        console.log('!!! REMOTE CONNECTION FAILED');
    });


    /**
     * New Room Request
     * Draws new private PIN (closed Room) and sends it back to Client
     */
    socket.on('new_room', function(room) {
        var pin = getNewPin();
        var room_detail =  {
            pin: pin,
            closed: room.closed,
            players: [],  // No Players yet
            theme: room.theme,
            options: room.options
        };

        socket.emit('room_detail', room_detail);
        console.log('--> Client requested new Room PIN #' + pin);
    });

    /**
     * Join Room Request
     */
    socket.on('join_room', function(roomDetail) {

        // Leave old Room if connected to one
        if (socket.pin) {
            console.log('--> Client leaves Room #' + socket.pin);
            socket.leave(socket.pin);
        }

        // Bind PIN to Client, use private PIN if room is closed
        if (roomDetail.closed) {
            // Private Room which cannot be joined with Random Games
            socket.pin = 'P-' + roomDetail.pin;
        } else {
            socket.pin = roomDetail.pin;
        }

        // Create Return Object
        var room_detail =  {
            pin: roomDetail.pin,
            closed: roomDetail.closed,
            players: io.sockets.clients,
            theme: roomDetail.theme,
            options: roomDetail.options
        };

        if (io.sockets.clients(roomDetail.pin).length < gameServer.maxPlayers) {
            // Room available: 0 or 1 Player
            socket.join(socket.pin);
            socket.broadcast.to(socket.pin).emit('room_detail', room_detail);
            console.log('--> Client joined Room #' + socket.pin);
        } else {
            // Room is full: >= 2 Player
            room_detail.error = 'Client tryed to join full Room #';
            socket.emit('room_detail', room_detail);
            console.log('--> Client tryed to join full Room #' + socket.pin);
        }

    });


    /**
     * Find Match
     * Looks for Player waiting for another Player
     * If none available, return 0 -> Client will create a new Game
     */
    socket.on('find_match', function() {

        console.log('--> Client request new Match');

        // If still in Room, leave it
        if (socket.pin) {
            socket.leave(socket.pin);
        }

        var pin = findMatch();

        // Create Return Object
        var room_detail =  {
            pin: pin
        };

        socket.emit('room_detail', room_detail);

    });


    /**
     * Leave Room currently connected
     */
    socket.on('leave_room', function() {
        if (socket.room) {
            console.log('--> Client leaves Room #' + socket.pin);
            socket.leave(socket.room);
        }
    });


    /**
     * Broadcast to other Players in Room Request
     */
    socket.on('player_action', function(data) {

        console.log('<-> Player Action "' + data.type + '" in Room #' + socket.room);

        // Broadcast to all Players in the room
        socket.broadcast.to(socket.room).emit('hurdle', data);
//        io.sockets.in(socket.room).emit('hurdle', data);
    });


    /**
     * Get all open Rooms Request
     */
    socket.on('get_rooms', function() {
        socket.emit('get_rooms', io.sockets.manager.rooms);
        console.log('<-- Sent current rooms Information');
    });


    /**
     * Client disconnect Event
     */
    socket.on('disconnect', function() {
        // Rooms are automatically leaved and pruned
        console.log('--- DISCONNECT FROM REMOTE');
    });<commit_msg>Bugfixes to the GameServer Minor Refactoring<commit_after>/* global require, socket, process */
/* jshint strict: false, devel: true */

/**
 * Obacht Game Node.js Multiplayer Server
 *
 * @author <NAME>
 */

//////////////////////////////
// Modules and Variables    //
//////////////////////////////

var gameServer = {
    maxPlayers: 2,
    maxPin: 9999,
    port: (process.argv[2] ? process.argv[2] : 8080) // Use Console Argument if there
};

var io = require('socket.io').listen(gameServer.port);


//////////////////////////////
// Configuration            //
//////////////////////////////

io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging


//////////////////////////////
// Comminication            //
//////////////////////////////

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

    /**
     * New Player connects to Server
     * Sends ID back so that the client knows it's successful connected
     */
    socket.emit('connected', {
        pid: socket.id
    });
    console.log('+++ NEW REMOTE CONNECTION');


    /**
     * Connection to Server fails
     */
    socket.on('connect_failed', function(){
        socket.emit('connected', {
            pid: socket.id,
            error: 'Connection to Server failed!'
        });
        console.log('!!! REMOTE CONNECTION FAILED');
    });


    /**
     * New Room Request
     * Draws new private PIN (closed Room) and sends it back to Client
     */
    socket.on('new_room', function(room) {
        var pin = getNewPin();
        var room_detail =  {
            pin: pin,
            closed: room.closed,
            players: [],  // No Players yet
            theme: room.theme,
            options: room.options
        };

        socket.emit('room_detail', room_detail);
        console.log('--> Client requested new Room PIN #' + pin);
    });

    /**
     * Join Room Request
     */
    socket.on('join_room', function(newRoom) {

        // Leave old Room if connected to one
        if (socket.pin) {
            console.log('--> Client leaves Room #' + socket.pin);
            socket.leave(socket.pin);
        }

        // Bind PIN to Client, use private PIN if room is closed
        if (newRoom.closed === true) {
            // Private Room which cannot be joined with Random Games
            socket.pin = 'P-' + newRoom.pin;
        } else {
            socket.pin = newRoom.pin;
        }

        // Create Return Object
        var room_detail =  {
            pin: newRoom.pin,
            closed: newRoom.closed,
            players: io.sockets.clients,
            theme: newRoom.theme,
            options: newRoom.options
        };

        if (io.sockets.clients(newRoom.pin).length < gameServer.maxPlayers) {
            // Room available: 0 or 1 Player
            socket.join(socket.pin);
            socket.broadcast.to(socket.pin).emit('room_detail', room_detail);
            console.log('--> Client joined Room #' + socket.pin);
        } else {
            // Room is full: >= 2 Player
            room_detail.error = 'Client tryed to join full Room #';
            socket.emit('room_detail', room_detail);
            console.log('--> Client tryed to join full Room #' + socket.pin);
        }

    });


    /**
     * Find Match
     * Looks for Player waiting for another Player
     * If none available, return 0 -> Client will create a new Game
     */
    socket.on('find_match', function() {

        console.log('--> Client request new Match');

        // If still in Room, leave it
        if (socket.pin) {
            socket.leave(socket.pin);
        }

        var pin = findMatch();
        var players = [];
        if (io.sockets.manager.rooms.hasOwnProperty('/' + pin)) {
            players = io.sockets.manager.rooms['/' + pin];
        }

        // Create Return Object
        var room_detail =  {
            pin: pin,
            players: players,
            closed: false
        };

        socket.emit('room_detail', room_detail);

    });


    /**
     * Leave Room currently connected
     * (Debugging Function)
     */
    socket.on('leave_room', function() {
        if (socket.pin) {
            console.log('--> Client leaves Room #' + socket.pin);
            socket.leave(socket.pin);
        }
    });


    /**
     * Broadcast to other Players in Room Request
     */
    socket.on('player_action', function(data) {
        console.log('<-> Player Action "' + data.type + '" in Room #' + socket.pin);
        socket.broadcast.to(socket.pin).emit('hurdle', data);
    });


    /**
     * Get all open Rooms Request
     * (Debugging Function)
     */
    socket.on('get_rooms', function() {
        socket.emit('get_rooms', io.sockets.manager.rooms);
        console.log('<-- Sent current rooms Information');
    });


    /**
     * Client disconnect Event
     */
    socket.on('disconnect', function() {
        // Rooms are automatically leaved and pruned
        console.log('--- DISCONNECT FROM REMOTE');
    });
{'fraction_non_alphanumeric': 0.1633215269578906, 'fraction_numerical': 0.003148366784730421, 'mean_word_length': 1.676408636124276, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '736328', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2715, 'n_words': 765}