<commit_before>/*eslint-env browser*/
/* global chrome */

var Bot = function ($) {
    var engine = {},
        b_console = { log : function (a) { } },
        bot_enable_debug = !('update_url' in chrome.runtime.getManifest());
    
    if (bot_enable_debug && console) {
        b_console =  console;
    }

    var g_backgroundEngineValid = true,
        g_backgroundEngine = null,
        g_analyzing = false,
        blob = null;

    function init (afterInit) {
        try {
            $.get("https://raw.githubusercontent.com/recoders/chessbot/master/scripts/garbochess-b.js", {},
                function (workerCode) {
                    blob = new Blob([workerCode], {type : 'javascript/worker'});
                    if (afterInit) {
                        b_console.log('Chess engine load correctly.');
                        afterInit();
                    }
            });
        } catch (error) {
            b_console.log('Chess engine not load correctly.');
            g_backgroundEngineValid = false;
        }
    }

    function EnsureAnalysisStopped() {
        if (g_analyzing && g_backgroundEngine !== null) {
            g_backgroundEngine.terminate();
            g_backgroundEngine = null;
        }
    }

    function MakeMove(move) {
        if (engine.moveFound !== null) {
            engine.moveFound(move);
        } else {
            console.error("Error move:" + move);
        }
    }

    function InitializeBackgroundEngine(success) {
        if (!blob || !g_backgroundEngineValid) {
            return false;
        }

        if (g_backgroundEngine === null) {
            // g_backgroundEngineValid = true;
                try {
                    g_backgroundEngine = new Worker(window.URL.createObjectURL(blob));
                    g_backgroundEngine.onmessage = function (e) {
                        if (e.data.match("^pv") == "pv") {
                            // Ready Move
                            var data_raw = e.data.replace('pv ', '');
                            var data = JSON.parse(data_raw);
                            b_console.log('Next moves: ' + data.humanMoves);
                            MakeMove(data);
                        } else if (e.data.match("^message") == "message") {
                            EnsureAnalysisStopped();
                        } else {
                            // I dont know what could be happened here:
                            // UIPlayMove(GetMoveFromString(e.data), null);
                        }
                    };
                    g_backgroundEngine.error = function (e) {
                        console.error("Error from background worker:" + e.message);
                    };
                    if (success) {
                        success();
                    }
                } catch (error) {
                    g_backgroundEngineValid = false;
                }
        }

        return g_backgroundEngineValid;
    }

    engine.makeMove = function (fen) {
        if (g_backgroundEngine) {
            g_backgroundEngine.postMessage("position " + fen);
            g_backgroundEngine.postMessage("analyze");

        } else {
            InitializeBackgroundEngine(function(){
                g_backgroundEngine.postMessage("position " + fen);
                g_backgroundEngine.postMessage("analyze");
            });
        }
    };

    // Live moves
    var movesMaded = 0;
    var getNextMove = function (movesArray) {
        if (movesArray.length > 0) {
            for(var i = 0; i < movesArray.length; i++) {
                if (i === movesMaded && movesArray[i].innerText !== '' && movesArray[i].innerText.indexOf('0') === -1) {
                    movesMaded++;
                    // b_console.log("Move: " + move);
                    return movesArray[i].innerText.replace('O-O+', 'O-O'); // Sometimes it was happened
                }
            }
        }<commit_msg>@ornicar, please dont do that!<commit_after>/*eslint-env browser*/
/* global chrome */

var Bot = function ($) {
    var engine = {},
        b_console = { log : function (a) { } },
        bot_enable_debug = !('update_url' in chrome.runtime.getManifest());
    
    if (bot_enable_debug && console) {
        b_console =  console;
    }

    var g_backgroundEngineValid = true,
        g_backgroundEngine = null,
        g_analyzing = false,
        blob = null;

    function init (afterInit) {
        try {
            $.get("https://raw.githubusercontent.com/recoders/chessbot/master/scripts/garbochess-b.js", {},
                function (workerCode) {
                    blob = new Blob([workerCode], {type : 'javascript/worker'});
                    if (afterInit) {
                        b_console.log('Chess engine load correctly.');
                        afterInit();
                    }
            });
        } catch (error) {
            b_console.log('Chess engine not load correctly.');
            g_backgroundEngineValid = false;
        }
    }

    function EnsureAnalysisStopped() {
        if (g_analyzing && g_backgroundEngine !== null) {
            g_backgroundEngine.terminate();
            g_backgroundEngine = null;
        }
    }

    function MakeMove(move) {
        if (engine.moveFound !== null) {
            engine.moveFound(move);
        } else {
            console.error("Error move:" + move);
        }
    }

    function InitializeBackgroundEngine(success) {
        if (!blob || !g_backgroundEngineValid) {
            return false;
        }

        if (g_backgroundEngine === null) {
            // g_backgroundEngineValid = true;
                try {
                    g_backgroundEngine = new Worker(window.URL.createObjectURL(blob));
                    g_backgroundEngine.onmessage = function (e) {
                        if (e.data.match("^pv") == "pv") {
                            // Ready Move
                            var data_raw = e.data.replace('pv ', '');
                            var data = JSON.parse(data_raw);
                            b_console.log('Next moves: ' + data.humanMoves);
                            MakeMove(data);
                        } else if (e.data.match("^message") == "message") {
                            EnsureAnalysisStopped();
                        } else {
                            // I dont know what could be happened here:
                            // UIPlayMove(GetMoveFromString(e.data), null);
                        }
                    };
                    g_backgroundEngine.error = function (e) {
                        console.error("Error from background worker:" + e.message);
                    };
                    if (success) {
                        success();
                    }
                } catch (error) {
                    g_backgroundEngineValid = false;
                }
        }

        return g_backgroundEngineValid;
    }

    engine.makeMove = function (fen) {
        if (g_backgroundEngine) {
            g_backgroundEngine.postMessage("position " + fen);
            g_backgroundEngine.postMessage("analyze");

        } else {
            InitializeBackgroundEngine(function(){
                g_backgroundEngine.postMessage("position " + fen);
                g_backgroundEngine.postMessage("analyze");
            });
        }
    };

    // Live moves
    var movesMaded = 0;
    var getNextMove = function (movesArray) {
        if (movesArray.length > 0) {
            for(var i = 0; i < movesArray.length; i++) {
                if (i === movesMaded && movesArray[i].innerText !== '' && movesArray[i].innerText.indexOf('0') === -1) {
                    movesMaded++;
                    // b_console.log("Move: " + move);
                    return movesArray[i].innerText.replace('O-O+', 'O-O').replace('х', 'x'); // Sometimes it was happened
                }
            }
        }
{'fraction_non_alphanumeric': 0.11639074939972198, 'fraction_numerical': 0.0012637432073802604, 'mean_word_length': 1.3421130511985795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1654725', 'n_tokens_mistral': 2062, 'n_tokens_neox': 1984, 'n_words': 389}