<commit_before>'use strict';

console.log(''); // new line
var vm = require('vm');
var fs = require('fs');
var zlib = require('zlib');
var http = require('http');
var WebSocketServer = require("ws").Server

/**
 * Declares the gameEngine variable for a later use.
 * This variable will be set to an instance of murmures.GameEngine after all classes are loaded.
 * @instance
 * @type {murmures.GameEngine}
 */
var gameEngine = {};

/**
 * The main namespace. All classes should be prefixed with it.
 * @namespace
 */
var murmures = {
    /**
     * Startup time.
     * @private
     */
    startTime: process.hrtime(),

    /**
     * Writes timestamped log to the console.
     * @public
     */
    serverLog: function (txt) {
        let diff = process.hrtime(this.startTime);
        let fdiff = diff[0] + diff[1] / 1e9;
        if (typeof txt !== 'undefined') {
            console.log(fdiff.toFixed(6) + ' - ' + txt);
        }
        return fdiff;
    },

    restartGame: function (targetLevel) {
        gameEngine.levels = [];
        gameEngine.activeLevel = 0;
        gameEngine.levelIds = fs.readdirSync('./data/staticlevels/', 'utf8');
        let loopCounter = 0;
        gameEngine.levelIds.forEach(function (levelName) {
            let level1Txt = fs.readFileSync('./data/staticlevels/' + levelName, 'utf8').toString().replace(/^\uFEFF/, '');
            let level1 = new murmures.Level();
            level1.build(JSON.parse(level1Txt));
            level1.id = levelName.replace('.json', '');
            gameEngine.levels.push(level1);
            if (typeof targetLevel !== 'undefined' && targetLevel && targetLevel === levelName) {
                gameEngine.activeLevel = loopCounter;
            }
            loopCounter++;
        }, this);
        gameEngine.level = gameEngine.levels[gameEngine.activeLevel];

        gameEngine.heros = [];
        let allHeroesKeys = [];
        for (let assetId in gameEngine.bodies) {
            let ref = gameEngine.bodies[assetId];
            if (murmures.C.LAYERS[ref.layerId][0] === 'Hero') allHeroesKeys.push(assetId);
        }

        let chosenHeroesKeys = [];
        let chosenHero;
        for (loopCounter = 0; loopCounter < 3; loopCounter++) {
            do {
                let rand = Math.floor(Math.random() * allHeroesKeys.length);
                chosenHero = allHeroesKeys[rand];
            } while (chosenHeroesKeys.indexOf(chosenHero) >= 0); // This loop prevents duplicate heroes
            chosenHeroesKeys.push(chosenHero);
            let hero1 = new murmures.Character();
            hero1.build(gameEngine.level.getStartingPoint(), chosenHero);
            hero1.typeCharacter = murmures.C.TYPE_CHARACTER_HERO;
            if (loopCounter === 0) {
                hero1.setVision();
                hero1.stateOrder = murmures.C.STATE_HERO_ORDER_INPROGRESS;
            }

            let chosenSkill= [];
            for(let loopSkill =0;loopSkill < 2;loopSkill ++){
              let rd=1;
              let nbSkill = 0;
              for(let itSkill in gameEngine.skills) nbSkill ++;
              do{
                rd = (Math.floor(Math.random() *nbSkill) + 1)
              }while (chosenSkill.indexOf(rd)>=0);
              chosenSkill.push(rd);
              if(loopSkill ==0){
                hero1.activeSkill = rd;
              }
              hero1.skills[rd] = gameEngine.skills[rd];
            }

            gameEngine.heros.push(hero1);
        }

        gameEngine.reportQueue = [];
        gameEngine.state = murmures.C.STATE_ENGINE_INIT;
    }
};

murmures.serverLog('Loading classes');

/**
 * Loads classes into the murmures namespace
 */
(function () {
    const ctx = {
        murmures: murmures,
        gameEngine: {}
    };
    vm.createContext(ctx);

    let constantsjs = fs.readFileSync('./src/js/core/constants.js', 'utf8').toString().replace(/^\uFEFF/, '');
    vm.runInContext(constantsjs, ctx, { filename: 'constants.js' });
    let gameEnginejs = fs.readFileSync('./src/js/core/gameengine.js', 'utf8').toString().replace(/^\uFEFF/, '');<commit_msg>fixing unwanted rollback<commit_after>'use strict';

console.log(''); // new line
var vm = require('vm');
var fs = require('fs');
var zlib = require('zlib');
var http = require('http');
var WebSocketServer = require("ws").Server

/**
 * Declares the gameEngine variable for a later use.
 * This variable will be set to an instance of murmures.GameEngine after all classes are loaded.
 * @instance
 * @type {murmures.GameEngine}
 */
var gameEngine = {};

/**
 * The main namespace. All classes should be prefixed with it.
 * @namespace
 */
var murmures = {
    /**
     * Startup time.
     * @private
     */
    startTime: process.hrtime(),

    /**
     * Writes timestamped log to the console.
     * @public
     */
    serverLog: function (txt) {
        let diff = process.hrtime(this.startTime);
        let fdiff = diff[0] + diff[1] / 1e9;
        if (typeof txt !== 'undefined') {
            console.log(fdiff.toFixed(6) + ' - ' + txt);
        }
        return fdiff;
    },

    restartGame: function (targetLevel) {
        gameEngine.levels = [];
        gameEngine.activeLevel = 0;
        gameEngine.levelIds = fs.readdirSync('./data/staticlevels/', 'utf8');
        let loopCounter = 0;
        gameEngine.levelIds.forEach(function (levelName) {
            let level1Txt = fs.readFileSync('./data/staticlevels/' + levelName, 'utf8').toString().replace(/^\uFEFF/, '');
            let level1 = new murmures.Level();
            level1.build(JSON.parse(level1Txt));
            level1.id = levelName.replace('.json', '');
            gameEngine.levels.push(level1);
            if (typeof targetLevel !== 'undefined' && targetLevel && targetLevel === levelName) {
                gameEngine.activeLevel = loopCounter;
            }
            loopCounter++;
        }, this);
        gameEngine.level = gameEngine.levels[gameEngine.activeLevel];

        gameEngine.heros = [];
        let allHeroesKeys = [];
        for (let assetId in gameEngine.bodies) {
            let ref = gameEngine.bodies[assetId];
            if (murmures.C.LAYERS[ref.layerId][0] === 'Hero') allHeroesKeys.push(assetId);
        }

        let chosenHeroesKeys = [];
        let chosenHero;
        for (loopCounter = 0; loopCounter < 3; loopCounter++) {
            do {
                let rand = Math.floor(Math.random() * allHeroesKeys.length);
                chosenHero = allHeroesKeys[rand];
            } while (chosenHeroesKeys.indexOf(chosenHero) >= 0); // This loop prevents duplicate heroes
            chosenHeroesKeys.push(chosenHero);
            let hero1 = new murmures.Character();
            hero1.build(gameEngine.level.getStartingPoint(), chosenHero);
            hero1.typeCharacter = murmures.C.TYPE_CHARACTER_HERO;
            if (loopCounter === 0) {
                hero1.setVision();
                hero1.stateOrder = murmures.C.STATE_HERO_ORDER_INPROGRESS;
            }

            let chosenSkill= [];
            let nbSkill = Object.keys(gameEngine.skills).length;
            //mainSkill is used to retrieve the min id of the skills of the hero in the aim to active the first skil on browser
            //The skills are shown on browser in Id Order 
            let mainSkill = -1;
            for(let loopSkill =0;loopSkill < 2;loopSkill ++){
              let rd=1;
              do{
                rd = (Math.floor(Math.random() *nbSkill) + 1)
              }while (chosenSkill.indexOf(rd)>=0);
              chosenSkill.push(rd);
              if (mainSkill == -1 || mainSkill > rd) mainSkill = rd;
              hero1.skills[rd] = gameEngine.skills[rd];
            }
            if (mainSkill != -1) hero1.activeSkill = mainSkill;

            gameEngine.heros.push(hero1);
        }

        gameEngine.reportQueue = [];
        gameEngine.state = murmures.C.STATE_ENGINE_INIT;
    }
};

murmures.serverLog('Loading classes');

/**
 * Loads classes into the murmures namespace
 */
(function () {
    const ctx = {
        murmures: murmures,
        gameEngine: {}
    };
    vm.createContext(ctx);

    let constantsjs = fs.readFileSync('./src/js/core/constants.js', 'utf8').toString().replace(/^\uFEFF/, '');
    vm.runInContext(constantsjs, ctx, { filename: 'constants.js' });
    let gameEnginejs = fs.readFileSync('./src/js/core/gameengine.js', 'utf8').toString().replace(/^\uFEFF/, '');
{'fraction_non_alphanumeric': 0.13642840180479696, 'fraction_numerical': 0.008905248159582047, 'mean_word_length': 2.149962602842184, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '1001253', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2518, 'n_words': 537}