<commit_before>/**
 * Created by Lukas on 30-6-2017.
 */
angular.module('CWEE.services.server', [])

.service('ServerInteractService', ['$rootScope', function($rootScope)
{
    var socket = io.connect("localhost:3000",{transports:['websocket']});
    this.on = function(eventName, callback)
    {
        socket.on(eventName, function()
        {
            var args = arguments;
            $rootScope.$apply(function()
            {
                callback.apply(socket, args);
            });
        });
    };

    this.emit = function(eventName, data, callback)
    {
        socket.emit(eventName, data, function()
        {
            var args = arguments;
            $rootScope.$apply(function()
            {
                if(callback)
                {
                    callback.apply(socket, args);
                }
            });
        });
    };
}])

.service('GameService', ['ServerInteractService', 'UserService', function(ServerInteractService, UserService)
{
    this.connect = function(data, callback)
    {
        ServerInteractService.emit('CONNECT', data);
        ServerInteractService.on('READY', function(data)
        {
            console.log('READY: ' + data);
            UserService.setCurrentUser(data);
            callback();
        });
    };
}])

.service('UserService', [function()
{
    var currentUser = null;

    this.setCurrentUser = function(user)
    {
        currentUser = user;
    };

    this.getCurrentUser = function()
    {
        return currentUser;
    };
}]);<commit_msg>*fix previous commit<commit_after>/**
 * Created by Lukas on 30-6-2017.
 */
angular.module('CWEE.services.server', [])

.service('ServerInteractService', ['$rootScope', function($rootScope)
{
    var socket = io.connect("localhost:3000",{transports:['websocket']});
    this.on = function(eventName, callback)
    {
        socket.on(eventName, function()
        {
            var args = arguments;
            $rootScope.$apply(function()
            {
                callback.apply(socket, args);
            });
        });
    };

    this.emit = function(eventName, data, callback)
    {
        socket.emit(eventName, data, function()
        {
            var args = arguments;
            $rootScope.$apply(function()
            {
                if(callback)
                {
                    callback.apply(socket, args);
                }
            });
        });
    };
}])

.service('GameService', ['$location', 'ServerInteractService', 'UserService', function($location, ServerInteractService, UserService)
{
    var self = this;
    console.log(self);
    UserService.lookForUser();
    if(UserService.getCurrentUser())
    {
        connect(UserService.getCurrentUser(), null);
        $location.path('/game');
    }

    function connect(data, callback)
    {
        ServerInteractService.emit('CONNECT', data);
        ServerInteractService.on('READY', function(data)
        {
            UserService.setCurrentUser(data);
            if(callback)
                callback();
        });
    }

    function disconnect(data, callback)
    {
        ServerInteractService.emit('DISCONNECT', data);
        ServerInteractService.on('READY', function(data)
        {
            UserService.disconnectUser();
            if(callback)
                callback();
        });
    }

    return {
        connect: connect,
        disconnect: disconnect
    };

    //this.connect = function(data, callback)
    //{
    //    ServerInteractService.emit('RECONNECT')
    //}
}])

.service('UserService', ['localStorageService', function(localStorageService)
{
    var currentUser = null;

    this.setCurrentUser = function(user)
    {
        currentUser = user;
        localStorageService.set('clientId', user.clientId);
        localStorageService.set('username', user.username);
    };

    this.getCurrentUser = function()
    {
        return currentUser;
    };

    this.disconnectUser = function()
    {
        currentUser = null;
        localStorageService.remove('clientId');
        localStorageService.remove('username');
    }

    this.lookForUser = function()
    {
        console.log('looking for user');
        var user = {};
        var id = localStorageService.get('clientId');
        var name = localStorageService.get('username');
        console.log();
        if(id || name)
        {
            user.clientId = id;
            user.username = name;
            currentUser = user;
        }
    };
}]);
{'fraction_non_alphanumeric': 0.13739439751000446, 'fraction_numerical': 0.004891062694530902, 'mean_word_length': 1.9755291005291005, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6502373', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1223, 'n_words': 198}