<commit_before>'use strict';

var ocargo = ocargo || {};

ocargo.Level = function(map, van, ui) {
    this.levelId = null;
    this.map = map;
    this.van = van;
    this.ui = ui;
    this.correct = 0;
    this.attemptData = {};
    this.blockLimit = null;
};

ocargo.Level.prototype.play = function(program) {

    this.attemptData = {};
    var commandStack = [];

    if (ocargo.level.blockLimit && 
            ocargo.blocklyControl.getBlocksCount() > ocargo.level.blockLimit) {
        enableDirectControl();
        startPopup("Oh no!", "", "You used too many blocks!");
        sendAttempt(0);
        return;
    }

    ocargo.level.attemptData.level = ocargo.level.levelId.toString(); 

    for (var i = 0; i < program.stack.length; i++) {
        ocargo.level.recogniseStack(program.stack[i], commandStack);
    }

    this.attemptData.commandStack = JSON.stringify(commandStack);
    program.startBlock.select();

    var stepFunction = stepper(this);

    program.stepCallback = stepFunction;
    this.program = program;
    setTimeout(stepFunction, 500);
};

ocargo.Level.prototype.recogniseStack = function(stack, returnStack) {
    if(stack) {
        for (var i = 0; i < stack.length; i++) {
            var command = recogniseCommand(stack[i], returnStack);
            returnStack.push(command);
        } 
    }
    
    function recogniseCommand(command, returnStack) {
        var parsedCommand = {};
        
        if (command instanceof ForwardCommand) {
            parsedCommand.command = 'Forward';
        } else if (command instanceof TurnLeftCommand) {
            parsedCommand.command = 'Left';
        } else if (command instanceof TurnRightCommand) {
            parsedCommand.command = 'Right';
        } else if (command instanceof TurnAroundCommand) {
            parsedCommand.command = 'TurnAround';
       
        } else if (command instanceof While) {
            var block = [];
            ocargo.level.recogniseStack(command.body, block);
            parsedCommand.command = 'While';
            parsedCommand.condition = command.condition.toString();
            parsedCommand.block = block;
       
        } else if (command instanceof If) {
            var commands = command.conditionalCommandSets[0];
            var ifBlock = [];
            parsedCommand.condition = commands.condition.toString();
            ocargo.level.recogniseStack(commands.commands, ifBlock);

            if (command.elseCommands) {
                commands = command.elseCommands;
                var elseBlock = [];
                ocargo.level.recogniseStack(commands, elseBlock);
                parsedCommand.elseBlock = elseBlock;
            }
            parsedCommand.command = 'If';
            parsedCommand.ifBlock = ifBlock;
        }
        return parsedCommand;
    }
};

ocargo.Level.prototype.step = function() {
    if (this.program.canStep()) {
        this.program.step(this);

    } else {
    	if (this.van.currentNode === this.map.destination && !this.program.isTerminated) {
            this.win();
        }
    }
};

ocargo.Level.prototype.win = function() {
    console.debug('You win!');
    sendAttempt(100);
    ocargo.level.successful = false;
    ocargo.sound.win();
    var message = '';
    var subtitle = '';
    enableDirectControl();

    if (ocargo.level.levelId < LEVEL_COUNT) {
        message = '<button onclick="window.location.href=' + "'/game/" + (ocargo.level.levelId + 1) + 
                  "'" + '"">Next lesson</button>';
    } else {
        subtitle = "Congratulations, that's all we've got for you now! ";
        message = "Why not try to create your own road? <br><br> " +
                  '<button onclick="window.location.href=' + "'/game/level_editor'" +
                  '"">Create your own map!</button> </center>' +
                  '<button onclick="window.location.href=' + "'/home/'" + '"">Home</button>';
    }
    startPopup("You win!", subtitle, message);
};

ocargo.Level.prototype.fail = function(msg) {
    var title = 'Oh dear! :(';
    ocargo.level.successful = false;
    console.debug(title);
    enableDirectControl();
    ocargo.sound.failure();
    startPopup(title, '', msg);
    sentAttempt(0);
};

function stepper(level) {
    return function() {
        try {
            if (level.program.canStep()) {
                level.correct = level.correct + 1;
                level.program.step(level);
            } else {
                if (level.van.currentNode === level.map.destination && !level.program.isTerminated) {
                    level.win();
                } else {
                    level.fail("You ran out of instructions!");
                    level.program.terminate();
                }
            }
        } catch (error) {
            level.program.terminate();
        }
    };
}

function sendAttempt(score) {<commit_msg>Fixed typo in level.js<commit_after>'use strict';

var ocargo = ocargo || {};

ocargo.Level = function(map, van, ui) {
    this.levelId = null;
    this.map = map;
    this.van = van;
    this.ui = ui;
    this.correct = 0;
    this.attemptData = {};
    this.blockLimit = null;
};

ocargo.Level.prototype.play = function(program) {

    this.attemptData = {};
    var commandStack = [];

    if (ocargo.level.blockLimit && 
            ocargo.blocklyControl.getBlocksCount() > ocargo.level.blockLimit) {
        enableDirectControl();
        startPopup("Oh no!", "", "You used too many blocks!");
        sendAttempt(0);
        return;
    }

    ocargo.level.attemptData.level = ocargo.level.levelId.toString(); 

    for (var i = 0; i < program.stack.length; i++) {
        ocargo.level.recogniseStack(program.stack[i], commandStack);
    }

    this.attemptData.commandStack = JSON.stringify(commandStack);
    program.startBlock.select();

    var stepFunction = stepper(this);

    program.stepCallback = stepFunction;
    this.program = program;
    setTimeout(stepFunction, 500);
};

ocargo.Level.prototype.recogniseStack = function(stack, returnStack) {
    if(stack) {
        for (var i = 0; i < stack.length; i++) {
            var command = recogniseCommand(stack[i], returnStack);
            returnStack.push(command);
        } 
    }
    
    function recogniseCommand(command, returnStack) {
        var parsedCommand = {};
        
        if (command instanceof ForwardCommand) {
            parsedCommand.command = 'Forward';
        } else if (command instanceof TurnLeftCommand) {
            parsedCommand.command = 'Left';
        } else if (command instanceof TurnRightCommand) {
            parsedCommand.command = 'Right';
        } else if (command instanceof TurnAroundCommand) {
            parsedCommand.command = 'TurnAround';
       
        } else if (command instanceof While) {
            var block = [];
            ocargo.level.recogniseStack(command.body, block);
            parsedCommand.command = 'While';
            parsedCommand.condition = command.condition.toString();
            parsedCommand.block = block;
       
        } else if (command instanceof If) {
            var commands = command.conditionalCommandSets[0];
            var ifBlock = [];
            parsedCommand.condition = commands.condition.toString();
            ocargo.level.recogniseStack(commands.commands, ifBlock);

            if (command.elseCommands) {
                commands = command.elseCommands;
                var elseBlock = [];
                ocargo.level.recogniseStack(commands, elseBlock);
                parsedCommand.elseBlock = elseBlock;
            }
            parsedCommand.command = 'If';
            parsedCommand.ifBlock = ifBlock;
        }
        return parsedCommand;
    }
};

ocargo.Level.prototype.step = function() {
    if (this.program.canStep()) {
        this.program.step(this);

    } else {
    	if (this.van.currentNode === this.map.destination && !this.program.isTerminated) {
            this.win();
        }
    }
};

ocargo.Level.prototype.win = function() {
    console.debug('You win!');
    sendAttempt(100);
    ocargo.level.successful = false;
    ocargo.sound.win();
    var message = '';
    var subtitle = '';
    enableDirectControl();

    if (ocargo.level.levelId < LEVEL_COUNT) {
        message = '<button onclick="window.location.href=' + "'/game/" + (ocargo.level.levelId + 1) + 
                  "'" + '"">Next lesson</button>';
    } else {
        subtitle = "Congratulations, that's all we've got for you now! ";
        message = "Why not try to create your own road? <br><br> " +
                  '<button onclick="window.location.href=' + "'/game/level_editor'" +
                  '"">Create your own map!</button> </center>' +
                  '<button onclick="window.location.href=' + "'/home/'" + '"">Home</button>';
    }
    startPopup("You win!", subtitle, message);
};

ocargo.Level.prototype.fail = function(msg) {
    var title = 'Oh dear! :(';
    ocargo.level.successful = false;
    console.debug(title);
    enableDirectControl();
    ocargo.sound.failure();
    startPopup(title, '', msg);
    sendAttempt(0);
};

function stepper(level) {
    return function() {
        try {
            if (level.program.canStep()) {
                level.correct = level.correct + 1;
                level.program.step(level);
            } else {
                if (level.van.currentNode === level.map.destination && !level.program.isTerminated) {
                    level.win();
                } else {
                    level.fail("You ran out of instructions!");
                    level.program.terminate();
                }
            }
        } catch (error) {
            level.program.terminate();
        }
    };
}

function sendAttempt(score) {
{'fraction_non_alphanumeric': 0.13302516692347202, 'fraction_numerical': 0.002876219825372368, 'mean_word_length': 2.09570747217806, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 23, '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': '788475', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2698, 'n_words': 545}