<commit_before>/*
* Module : UTIL
*
* Hosts most of the common utility functions used by Battletanks
*/
var UTIL = (function () {
    var my = {};
    
    my.toTitleCase = function (str) {
        return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    };
    
    /*
    * Private Method: _idInArray
    *
    * Checks if id is found in array
    *
    * Parameters:
    *   array - the array to be checked
    *   id    - value to compare to
    *
    * Returns:
    *   a boolean true if id is found in array, else a boolean false
    */
    var _idInArray = function (array, id) {
        // Check if id exists in array.
        for (var i = 0; i < array.length; i++) {
            if (array[i].id == id) {
                return true;
            }
        }
        
        return false;
    };
    
    /*
    * Public Method: getMousePos
    *
    * Retreives the current mouse coordinates within an element
    *
    * Parameters:
    *   element - the element
    *   evt     - the event
    *
    * Returns:
    *   an object with the following properties: mX, mY (which are the mouse coordinates)
    */
    my.getMousePos = function (element, evt) {
        var rect = element.getBoundingClientRect();
        return {
            mX: evt.clientX - rect.left,
            mY: (evt.clientY - rect.bottom) * (-1) // multiply by -1 since the plane is inversed vertically
        };
    };
    
    /*
    * Public Method: genArrayId
    *
    * Generates a unique id for an array element
    *
    * Parameters:
    *   array - the array
    *
    * Returns:
    *   a unique alphanumeric string
    */
    my.genArrayId = function (array) {
        var id = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        
        do {
            for( var i=0; i < 5; i++ )
                id += possible.charAt(Math.floor(Math.random() * possible.length));
        } while (_idInArray(array, id));
        
        return id;
    };
    
    /*
    * Public Method: writeToLog
    *
    * Appends a string/message to the combat log, increments logNum (used for auto-scrolling)
    *
    * Parameters:
    *   message - the string to append
    */
    my.writeToLog = function (message) {
        if (!LOG_ENABLED) { return; }
        cLog.innerHTML = cLog.innerHTML + message + '<br>';
        cLog.scrollTop = document.getElementById('log-' + logNum).offsetTop;
        logNum++;
    };

    my.stopMusic = function (music) {
        music.pause();
        music.currentTime = 0;
    };
    
    my.pauseMusic = function (music) {
        music.pause();
    };
    
    my.playMusic = function (music) {
        if (music.readyState === 4) {
            music.play();
        }
    };
    
    my.getBotReference = function (id) {
        /* Return the reference to the bot matching the id from bots */
        for (var i = 0; i < bots.length; i++) {
            if (bots[i][0].config.id === id) {
                return bots[i];
            }
        }
    };
    
    my.levelCleared = function () {
        /* Check if all enemy tanks have been destroyed. */
        if (!GLOBALS.flags.initSpawn) {
            // if initial spawn hasn't started...
            return false;
        }
        
        for (var i = 0; i < bots.length; i++) {
            if (bots[i][0].config.active) {
                return false;
            }
        }
        
        return true;
    };
    
    my.getHighScores = function () {
        $.ajax({  
            type: "POST",  
            url: "/models/get_high_scores.php",  
            data: data,  
            success: function(response) {  
                var pr = JSON.parse(response);
                var count = 1;
                var spinner = document.getElementById('hof-spinner');
                var table = document.getElementById('hof-table').getElementsByTagName('tbody')[0];
                
                spinner.show();
                table.innerHTML = '';
                for (var i = 0; i < pr.length; i++) {
                    var row = document.createElement('table');
                    row.class = 'hof-row';
                    row.innerHTML = '<tr><td>'+count+'</td><td>'+pr.player+'</td><td>'+pr.map+'</td><td>'+pr.wave+'</td><td>'+pr.score+'</td></tr>';
                    table.appendChild(row);
                    count++;
                }
                spinner.hide();
            }
        }); 
    };
    
    my.post = function (url, data, callbackSuccess, callbackFailed) {
        callbackSuccess = function () {} || callbackSuccess;
        callbackFailed = function () {} || callbackFailed;
    
        /* POST request as JSON */
        var req = new XMLHttpRequest();
        
        req.onreadystatechange = function () {
            if (req.readyState === 4) {<commit_msg>fixed hs ajax function<commit_after>/*
* Module : UTIL
*
* Hosts most of the common utility functions used by Battletanks
*/
var UTIL = (function () {
    var my = {};
    
    my.toTitleCase = function (str) {
        return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    };
    
    /*
    * Private Method: _idInArray
    *
    * Checks if id is found in array
    *
    * Parameters:
    *   array - the array to be checked
    *   id    - value to compare to
    *
    * Returns:
    *   a boolean true if id is found in array, else a boolean false
    */
    var _idInArray = function (array, id) {
        // Check if id exists in array.
        for (var i = 0; i < array.length; i++) {
            if (array[i].id == id) {
                return true;
            }
        }
        
        return false;
    };
    
    /*
    * Public Method: getMousePos
    *
    * Retreives the current mouse coordinates within an element
    *
    * Parameters:
    *   element - the element
    *   evt     - the event
    *
    * Returns:
    *   an object with the following properties: mX, mY (which are the mouse coordinates)
    */
    my.getMousePos = function (element, evt) {
        var rect = element.getBoundingClientRect();
        return {
            mX: evt.clientX - rect.left,
            mY: (evt.clientY - rect.bottom) * (-1) // multiply by -1 since the plane is inversed vertically
        };
    };
    
    /*
    * Public Method: genArrayId
    *
    * Generates a unique id for an array element
    *
    * Parameters:
    *   array - the array
    *
    * Returns:
    *   a unique alphanumeric string
    */
    my.genArrayId = function (array) {
        var id = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        
        do {
            for( var i=0; i < 5; i++ )
                id += possible.charAt(Math.floor(Math.random() * possible.length));
        } while (_idInArray(array, id));
        
        return id;
    };
    
    /*
    * Public Method: writeToLog
    *
    * Appends a string/message to the combat log, increments logNum (used for auto-scrolling)
    *
    * Parameters:
    *   message - the string to append
    */
    my.writeToLog = function (message) {
        if (!LOG_ENABLED) { return; }
        cLog.innerHTML = cLog.innerHTML + message + '<br>';
        cLog.scrollTop = document.getElementById('log-' + logNum).offsetTop;
        logNum++;
    };

    my.stopMusic = function (music) {
        music.pause();
        music.currentTime = 0;
    };
    
    my.pauseMusic = function (music) {
        music.pause();
    };
    
    my.playMusic = function (music) {
        if (music.readyState === 4) {
            music.play();
        }
    };
    
    my.getBotReference = function (id) {
        /* Return the reference to the bot matching the id from bots */
        for (var i = 0; i < bots.length; i++) {
            if (bots[i][0].config.id === id) {
                return bots[i];
            }
        }
    };
    
    my.levelCleared = function () {
        /* Check if all enemy tanks have been destroyed. */
        if (!GLOBALS.flags.initSpawn) {
            // if initial spawn hasn't started...
            return false;
        }
        
        for (var i = 0; i < bots.length; i++) {
            if (bots[i][0].config.active) {
                return false;
            }
        }
        
        return true;
    };
    
    my.getHighScores = function () {
        $.ajax({  
            type: "POST",  
            url: "/models/get_high_scores.php",  
            data: {ajax: true},  
            success: function(response) {  
                var pr = JSON.parse(response);
                var count = 1;
                var spinner = document.getElementById('hof-spinner');
                var table = document.getElementById('hof-table').getElementsByTagName('tbody')[0];
                
                spinner.show();
                table.innerHTML = '';
                for (var i = 0; i < pr.length; i++) {
                    var row = document.createElement('table');
                    row.class = 'hof-row';
                    row.innerHTML = '<tr><td>'+count+'</td><td>'+pr.player+'</td><td>'+pr.map+'</td><td>'+pr.wave+'</td><td>'+pr.score+'</td></tr>';
                    table.appendChild(row);
                    count++;
                }
                spinner.hide();
            }
        }); 
    };
    
    my.post = function (url, data, callbackSuccess, callbackFailed) {
        callbackSuccess = function () {} || callbackSuccess;
        callbackFailed = function () {} || callbackFailed;
    
        /* POST request as JSON */
        var req = new XMLHttpRequest();
        
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
{'fraction_non_alphanumeric': 0.12743995912110373, 'fraction_numerical': 0.005518650996423097, 'mean_word_length': 1.531953428201811, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 29, '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': '179623', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2862, 'n_words': 730}