<commit_before>'use strict';
angular.module('wegas.models.sessions', [])
.service('SessionsModel', function ($http, $q, $interval, Auth) {
    /* Namespace for model accessibility. */
    var model = this,
    /* Caches for data */
    managedSessions = null,
    playedSessions = null;
    /* Tools to prevent asynchronous error. */
    managedSessionsLoading = false,
    waitManagedSessions = null,
    
    /* Return a session in a sessions cache. */
    findSession = function(sessions, id){
        return _.find(sessions, function (s) { return s.id == id; });
    },
    /* Format players in a list, individualy or grouped by team. */ 
    formatPlayers = function(data){
        data.forEach(function(session){
            if(!session.properties.freeForAll){
                var teams = session.teams;
                teams.forEach(function(team){
                    if(team["@class"] == "DebugTeam" || team.players.length < 1){
                        session.teams = _.without(session.teams, _.findWhere(session.teams, {id: team.id}));
                    }
                });
            }else{
                var teams = session.teams,
                    players = [];
                teams.forEach(function(team){
                    if(team["@class"] != "DebugTeam"){
                        team.players.forEach(function(player){
                            players.push(player);
                        });
                    }
                });
                session.players = players;
                delete session.teams;
            }
        });
        return data;
    },

    /* Call the REST service for getting managed sessions. */
    cacheManagedSessions = function () {
        var deferred = $q.defer();
        console.log("Load HTTP");
        $http.get(ServiceURL + "rest/GameModel/Game?view=EditorExtended").success(function(data){
            data = formatPlayers(data);
            managedSessions = data;
            console.log("Data loadded");
            deferred.resolve(managedSessions);
        }).error(function(data){
            managedSessions = [];
            deferred.resolve(managedSessions);
        });
        return deferred.promise;
    },

    cacheCoTrainersSession = function () {
        var deferred = $q.defer();
        console.log("Load HTTP");
        $http.get(ServiceURL + "rest/GameModel/Game?view=EditorExtended").success(function(data){
            data = formatPlayers(data);
            managedSessions = data;
            console.log("Data loadded");
            deferred.resolve(managedSessions);
        }).error(function(data){
            managedSessions = [];
            deferred.resolve(managedSessions);
        });
        return deferred.promise;
    },

    /* Stop waiting for simultanate sessions asking. */
    stopWaiting = function(waitFunction){
        $interval.cancel(waitFunction);
    },

    /* Do wait during data loading. */ 
    waitForManagedSessions = function(){
        var deferred = $q.defer();
        waitManagedSessions = $interval(function() {
            if(!managedSessionsLoading){
                stopWaiting(waitManagedSessions);
                deferred.resolve(true)
            }   
        }, 500);
        return deferred.promise;
    };

    /* Ask for all managed sessions. */
    model.getManagedSessions = function(){
        var deferred = $q.defer();
        if(managedSessionsLoading){
            waitForManagedSessions.then(function(){
                deferred.resolve(managedSessions);
            });
        }else{
            if(managedSessions != null){
                deferred.resolve(managedSessions);
            }else{
                managedSessionsLoading = true;
                cacheManagedSessions().then(function(){
                    deferred.resolve(managedSessions);
                    managedSessionsLoading = false;
                });
            }
        }
        return deferred.promise;
    };

    /* Ask for one managed session. */
    model.getManagedSession = function(id){
        var deferred = $q.defer(),
            session;
        if(managedSessionsLoading){
            waitForManagedSessions().then(function(){
                session = findSession(managedSessions, id);
                deferred.resolve(session);
            });
        }else{
            if(managedSessions == null) {
                model.getManagedSessions().then(function(){
                    session = findSession(managedSessions, id);
                    deferred.resolve(managedSessions);
                });
            }else{
                session = findSession(managedSessions, id);
                deferred.resolve(session);
            }
        }
        return deferred.promise;
    };

    /* Create a new session. */
    model.createManagedSession = function(sessionName, scenarioId){
        var deferred = $q.defer();<commit_msg>Session Model - Add Get Trainers<commit_after>'use strict';
angular.module('wegas.models.sessions', [])
.service('SessionsModel', function ($http, $q, $interval, Auth) {
    /* Namespace for model accessibility. */
    var model = this,
    /* Caches for data */
    managedSessions = null,
    playedSessions = null;
    /* Tools to prevent asynchronous error. */
    managedSessionsLoading = false,
    waitManagedSessions = null,
    
    /* Return a session in a sessions cache. */
    findSession = function(sessions, id){
        return _.find(sessions, function (s) { return s.id == id; });
    },

    /* Format players in a list, individualy or grouped by team. */ 
    formatPlayers = function(data){
        data.forEach(function(session){
            if(!session.properties.freeForAll){
                var teams = session.teams;
                teams.forEach(function(team){
                    if(team["@class"] == "DebugTeam" || team.players.length < 1){
                        session.teams = _.without(session.teams, _.findWhere(session.teams, {id: team.id}));
                    }
                });
            }else{
                var teams = session.teams,
                    players = [];
                teams.forEach(function(team){
                    if(team["@class"] != "DebugTeam"){
                        team.players.forEach(function(player){
                            players.push(player);
                        });
                    }
                });
                session.players = players;
                delete session.teams;
            }
        });
        return data;
    },

    /* Call the REST service for getting managed sessions. */
    cacheManagedSessions = function () {
        var deferred = $q.defer();
        $http.get(ServiceURL + "rest/GameModel/Game?view=EditorExtended").success(function(data){
            data = formatPlayers(data);
            managedSessions = data;
            deferred.resolve(managedSessions);
        }).error(function(data){
            managedSessions = [];
            deferred.resolve(managedSessions);
        });
        return deferred.promise;
    },
    
    /* Get all co-trainers for a managed session. */
    cacheTrainersForSession = function (id) {
        var deferred = $q.defer();
        session = findSession(managedSessions, id);
        if(session){
            if(session.trainers){
                deferred.resolve(session.trainers);
            }else{
                $http.get(ServiceURL + "rest/Extended/User/FindAccountPermissionByInstance/g" + session.id).success(function(data){
                    managedSessions[_.indexOf(managedSessions, session)].trainers = data;
                    deferred.resolve(data);
                }).error(function(data){
                    deferred.resolve(false);
                });
            }
        }else{
            deferred.resolve(false);
        }
        return deferred.promise;
    },

    /* Add trainers to a session in cache. */
    addTrainersToSession = function(id){
        var deferred = $q.defer(),
            session = findSession(managedSessions, id);
        if(session){
            if(session.trainers){
                deferred.resolve(session);
            }else{
                cacheTrainersForSession(session.id).then(function(data){
                    session = findSession(managedSessions, id);
                    deferred.resolve(session);
                });
            }
        }else{
            deferred.resolve(false);
        }
        return deferred.promise;
    },

    /* Stop waiting for simultanate sessions asking. */
    stopWaiting = function(waitFunction){
        $interval.cancel(waitFunction);
    },

    /* Do wait during data loading. */ 
    waitForManagedSessions = function(){
        var deferred = $q.defer();
        waitManagedSessions = $interval(function() {
            if(!managedSessionsLoading){
                stopWaiting(waitManagedSessions);
                deferred.resolve(true)
            }   
        }, 500);
        return deferred.promise;
    };

    /* Ask for all managed sessions. */
    model.getManagedSessions = function(){
        var deferred = $q.defer();
        if(managedSessionsLoading){
            waitForManagedSessions.then(function(){
                deferred.resolve(managedSessions);
            });
        }else{
            if(managedSessions != null){
                deferred.resolve(managedSessions);
            }else{
                managedSessionsLoading = true;
                cacheManagedSessions().then(function(){
                    deferred.resolve(managedSessions);
                    managedSessionsLoading = false;
                });
            }
        }
        return deferred.promise;
    };

    /* Ask for one managed session. */
    model.getManagedSession = function(id){
        var deferred = $q.defer();
        if(managedSessionsLoading){
            waitForManagedSessions().then(function(){
                addTrainersToSession(id).then(function(data){
                    deferred.resolve(data);
                });
            });
        }else{
            if(managedSessions == null) {
                model.getManagedSessions().then(function(){
                    addTrainersToSession(id).then(function(data){
                        deferred.resolve(data);
                    });
                });
            }else{
                addTrainersToSession(id).then(function(data){
                    deferred.resolve(data);
                });
            }
        }
        return deferred.promise;
    };

    /* Create a new session. */
    model.createManagedSession = function(sessionName, scenarioId){
        var deferred = $q.defer();
{'fraction_non_alphanumeric': 0.11533052039381153, 'fraction_numerical': 0.0007501172058134083, 'mean_word_length': 1.652573986570505, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7236349', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2609, 'n_words': 506}