<commit_before>'use strict';

var ocargo = ocargo || {};

var BACKGROUND_COLOR = '#a8d44a';
var SELECTED_COLOR = '#70961f';
var SUGGESTED_COLOR = '#95b650';
var BORDER = '#bce369';

var BUSH_URL = '/static/game/image/bush.svg';
var TREE1_URL = '/static/game/image/tree1.svg';
var TREE2_URL = '/static/game/image/tree2.svg';
var HOUSE_URL = '/static/game/image/house1_noGreen.svg';
var CFC_URL = '/static/game/image/OcadoCFC_no_road.svg';


ocargo.LevelEditor = function() {
    this.nodes = [];
    this.start = null;
    this.end = null;
    this.currentStrike = [];
    this.decor = [];
    this.grid = this.initialiseVisited();

    // Is the start, end or delete mode on?
    this.startFlag = false;
    this.endFlag = false;
    this.deleteFlag = false;

    // type: Node
    this.pathStart = null;
    this.destination = null;
};

ocargo.LevelEditor.prototype.initialiseVisited = function() {
    var visited = new Array(10);
    for (var i = 0; i < 10; i++) {
        visited[i] = new Array(8);
    }
    return visited;
};

ocargo.LevelEditor.prototype.createGrid = function(paper) {
    for (var i = 0; i < GRID_WIDTH; i++) {
        for (var j = 0; j < GRID_HEIGHT; j++) {
            var x = i * GRID_SPACE_SIZE;
            var y = j * GRID_SPACE_SIZE;
            var segment = paper.rect(x, y, GRID_SPACE_SIZE, GRID_SPACE_SIZE);
            segment.attr({stroke: BORDER, fill: BACKGROUND_COLOR, "fill-opacity": 0});

            segment.node.onmousedown = function() {
                var this_rect = segment;
                return handleMouseDown(this_rect, segment);
            } ();

            segment.node.onmouseover = function() {
                var this_rect = segment;
                return handleMouseOver(this_rect, segment);
            } ();

            segment.node.onmouseup = function() {
                var this_rect = segment;
                return handleMouseUp(this_rect, segment);
            } ();

            segment.node.ontouchstart = function() {
                var this_rect = segment;
                return handleMouseDown(this_rect, segment);
            } ();

            segment.node.ontouchmove = function() {
                var this_rect = segment;
                return handleMouseOver(this_rect, segment);
            } ();

            segment.node.ontouchend = function() {
                var this_rect = segment;
                return handleMouseUp(this_rect, segment);
            } ();

            this.grid[i][j] = segment;
        }
    }
};

function handleMouseDown(this_rect, segment) {
    return function () {
        var getBBox = this_rect.getBBox();
        var coord = new ocargo.Coordinate(getBBox.x / 100, getBBox.y / 100);
        var transCoord = ocargo.levelEditor.translate(coord);
        var isPresent = ocargo.levelEditor.findNodeByCoordinate(ocargo.levelEditor.nodes, transCoord);

        if (ocargo.levelEditor.startFlag && isPresent > -1) {
            if (ocargo.levelEditor.pathStart) {
                var prevStart = ocargo.levelEditor.translate(
                    ocargo.levelEditor.pathStart.coordinate);
                ocargo.levelEditor.mark(prevStart, BACKGROUND_COLOR, 0, true);
            }

            ocargo.levelEditor.mark(coord, 'red', 0.7, true);
            var newStartIndex = ocargo.levelEditor.findNodeByCoordinate(
                ocargo.levelEditor.nodes, transCoord);

            // Putting the new start in the front of the nodes list.
            var temp = ocargo.levelEditor.nodes[newStartIndex];
            ocargo.levelEditor.nodes[newStartIndex] = ocargo.levelEditor.nodes[0];
            ocargo.levelEditor.nodes[0] = temp;
            ocargo.levelEditor.pathStart = ocargo.levelEditor.nodes[0];

        } else if (ocargo.levelEditor.endFlag && isPresent > -1) {
             if (ocargo.levelEditor.destination) {
                var prevEnd = ocargo.levelEditor.translate(
                    ocargo.levelEditor.destination.coordinate);
                ocargo.levelEditor.mark(prevEnd, BACKGROUND_COLOR, 0, true);
            }
            ocargo.levelEditor.mark(coord, 'blue', 0.7, true);
            var newEnd = ocargo.levelEditor.findNodeByCoordinate(ocargo.levelEditor.nodes, transCoord);
            ocargo.levelEditor.destination = ocargo.levelEditor.nodes[newEnd];

        } else if (ocargo.levelEditor.deleteFlag ||
            !(ocargo.levelEditor.endFlag || ocargo.levelEditor.startFlag)) {
            ocargo.levelEditor.start = coord;
            ocargo.levelEditor.mark(coord, SELECTED_COLOR, 0.7, true);
        }
    }
}

function handleMouseOver(this_rect, segment) {
    return function() {
        var startOrEnd = ocargo.levelEditor.endFlag || ocargo.levelEditor.startFlag
        if (ocargo.levelEditor.start !== null && !startOrEnd) {
            var getBBox = this_rect.getBBox();
            var coord = new ocargo.Coordinate(getBBox.x / 100, getBBox.y / 100);
            ocargo.levelEditor.recalculatePredictedRoad(coord);
        }
    }
}

function handleMouseUp(this_rect, segment) {
    return function() {
        var startOrEnd = ocargo.levelEditor.endFlag || ocargo.levelEditor.startFlag
        if (!startOrEnd) {
            ocargo.levelEditor.end = segment;
            var getBBox = this_rect.getBBox();
            var coord = new ocargo.Coordinate(getBBox.x / 100, getBBox.y / 100);
            if (ocargo.levelEditor.deleteFlag) {
                ocargo.levelEditor.finaliseDelete(coord);
            } else {
                ocargo.levelEditor.finaliseMove(coord);
            }
            paper.clear();
            ocargo.levelEditor.start = null;
            createRoad(ocargo.levelEditor.nodes);
            ocargo.levelEditor.createGrid(paper)
            ocargo.levelEditor.drawDecor();
            if (ocargo.levelEditor.pathStart !== null) {
                coord = ocargo.levelEditor.translate(ocargo.levelEditor.pathStart.coordinate);
                ocargo.levelEditor.mark(coord, 'red', 0.7, true);
            }
            if (ocargo.levelEditor.destination !== null) {
                coord = ocargo.levelEditor.translate(ocargo.levelEditor.destination.coordinate);
                ocargo.levelEditor.mark(coord, 'blue', 0.7, true);
            }
        }
    }
}

ocargo.LevelEditor.prototype.drawDecor = function() {
    for (var i = 0; i < ocargo.levelEditor.decor.length; i++) {
        var obj = ocargo.levelEditor.decor[i];
        var coord = obj['coordinate'];
        var img = paper.image(obj['url'], coord.x, PAPER_HEIGHT - coord.y - DECOR_SIZE, 
            DECOR_SIZE, DECOR_SIZE);
        img.draggableDecor(coord.x, PAPER_HEIGHT - coord.y - DECOR_SIZE);
    }
}

ocargo.LevelEditor.prototype.finaliseDelete = function(coord) {
    var x, y;
    if (this.start.x <= coord.x) {
        for (x = this.start.x + 1; x <= coord.x; x++) {
            deleteNode(x, this.start.y);
        }
    } else {
        for (x = this.start.x - 1; x >= coord.x; x--) {
            deleteNode(x, this.start.y);
        }
    }
    if (this.start.y <= coord.y) {
        for (y = this.start.y + 1; y <= coord.y; y++) {
            deleteNode(coord.x, y);
        }
    } else {
        for (y = this.start.y - 1; y >= coord.y; y--) {
            deleteNode(coord.x, y);
        }
    }

    function deleteNode(x, y) {
        var coord = ocargo.levelEditor.translate(new ocargo.Coordinate(x, y));
        var nodeIndex = ocargo.levelEditor.findNodeByCoordinate(ocargo.levelEditor.nodes, coord);
        if (nodeIndex > -1) {
            var node = ocargo.levelEditor.nodes[nodeIndex];
            // Remove all the references to the node we're removing.
            for (var i = 0; i < node.connectedNodes.length; i++) {
                node.removeDoublyConnectedNode(node.connectedNodes[i]);
            }
            var index = ocargo.levelEditor.nodes.indexOf(node);
            ocargo.levelEditor.nodes.splice(index, 1);
        }
    }
};

ocargo.LevelEditor.prototype.finaliseMove = function(coord) {
    var current;
    var prev;

    for (var i = 0; i < ocargo.levelEditor.currentStrike.length; i++) {
        current = ocargo.levelEditor.currentStrike[i];
        var index = this.findNodeByCoordinate(this.nodes, current.coordinate);

        if (index > -1) {
            var existing = ocargo.levelEditor.nodes[index];
            var list = []
            for (var j = 0; j < current.connectedNodes.length; j++) {<commit_msg>Fixed some of level editor deletion issues<commit_after>'use strict';

var ocargo = ocargo || {};

var BACKGROUND_COLOR = '#a8d44a';
var SELECTED_COLOR = '#70961f';
var SUGGESTED_COLOR = '#95b650';
var BORDER = '#bce369';

var BUSH_URL = '/static/game/image/bush.svg';
var TREE1_URL = '/static/game/image/tree1.svg';
var TREE2_URL = '/static/game/image/tree2.svg';
var HOUSE_URL = '/static/game/image/house1_noGreen.svg';
var CFC_URL = '/static/game/image/OcadoCFC_no_road.svg';


ocargo.LevelEditor = function() {
    this.nodes = [];
    this.start = null;
    this.end = null;
    this.currentStrike = [];
    this.decor = [];
    this.grid = this.initialiseVisited();

    // Is the start, end or delete mode on?
    this.startFlag = false;
    this.endFlag = false;
    this.deleteFlag = false;

    // type: Node
    this.pathStart = null;
    this.destination = null;
};

ocargo.LevelEditor.prototype.initialiseVisited = function() {
    var visited = new Array(10);
    for (var i = 0; i < 10; i++) {
        visited[i] = new Array(8);
    }
    return visited;
};

ocargo.LevelEditor.prototype.createGrid = function(paper) {
    for (var i = 0; i < GRID_WIDTH; i++) {
        for (var j = 0; j < GRID_HEIGHT; j++) {
            var x = i * GRID_SPACE_SIZE;
            var y = j * GRID_SPACE_SIZE;
            var segment = paper.rect(x, y, GRID_SPACE_SIZE, GRID_SPACE_SIZE);
            segment.attr({stroke: BORDER, fill: BACKGROUND_COLOR, "fill-opacity": 0});

            segment.node.onmousedown = function() {
                var this_rect = segment;
                return handleMouseDown(this_rect, segment);
            } ();

            segment.node.onmouseover = function() {
                var this_rect = segment;
                return handleMouseOver(this_rect, segment);
            } ();

            segment.node.onmouseup = function() {
                var this_rect = segment;
                return handleMouseUp(this_rect, segment);
            } ();

            segment.node.ontouchstart = function() {
                var this_rect = segment;
                return handleMouseDown(this_rect, segment);
            } ();

            segment.node.ontouchmove = function() {
                var this_rect = segment;
                return handleMouseOver(this_rect, segment);
            } ();

            segment.node.ontouchend = function() {
                var this_rect = segment;
                return handleMouseUp(this_rect, segment);
            } ();

            this.grid[i][j] = segment;
        }
    }
};

function handleMouseDown(this_rect, segment) {
    return function () {
        var getBBox = this_rect.getBBox();
        var coord = new ocargo.Coordinate(getBBox.x / 100, getBBox.y / 100);
        var transCoord = ocargo.levelEditor.translate(coord);
        var isPresent = ocargo.levelEditor.findNodeByCoordinate(ocargo.levelEditor.nodes, transCoord);

        if (ocargo.levelEditor.startFlag && isPresent > -1) {
            if (ocargo.levelEditor.pathStart) {
                var prevStart = ocargo.levelEditor.translate(
                    ocargo.levelEditor.pathStart.coordinate);
                ocargo.levelEditor.mark(prevStart, BACKGROUND_COLOR, 0, true);
            }

            ocargo.levelEditor.mark(coord, 'red', 0.7, true);
            var newStartIndex = ocargo.levelEditor.findNodeByCoordinate(
                ocargo.levelEditor.nodes, transCoord);

            // Putting the new start in the front of the nodes list.
            var temp = ocargo.levelEditor.nodes[newStartIndex];
            ocargo.levelEditor.nodes[newStartIndex] = ocargo.levelEditor.nodes[0];
            ocargo.levelEditor.nodes[0] = temp;
            ocargo.levelEditor.pathStart = ocargo.levelEditor.nodes[0];

        } else if (ocargo.levelEditor.endFlag && isPresent > -1) {
             if (ocargo.levelEditor.destination) {
                var prevEnd = ocargo.levelEditor.translate(
                    ocargo.levelEditor.destination.coordinate);
                ocargo.levelEditor.mark(prevEnd, BACKGROUND_COLOR, 0, true);
            }
            ocargo.levelEditor.mark(coord, 'blue', 0.7, true);
            var newEnd = ocargo.levelEditor.findNodeByCoordinate(ocargo.levelEditor.nodes, transCoord);
            ocargo.levelEditor.destination = ocargo.levelEditor.nodes[newEnd];

        } else if (ocargo.levelEditor.deleteFlag ||
            !(ocargo.levelEditor.endFlag || ocargo.levelEditor.startFlag)) {
            ocargo.levelEditor.start = coord;
            ocargo.levelEditor.mark(coord, SELECTED_COLOR, 0.7, true);
        }
    }
}

function handleMouseOver(this_rect, segment) {
    return function() {
        var startOrEnd = ocargo.levelEditor.endFlag || ocargo.levelEditor.startFlag
        if (ocargo.levelEditor.start !== null && !startOrEnd) {
            var getBBox = this_rect.getBBox();
            var coord = new ocargo.Coordinate(getBBox.x / 100, getBBox.y / 100);
            ocargo.levelEditor.recalculatePredictedRoad(coord);
        }
    }
}

function handleMouseUp(this_rect, segment) {
    return function() {
        var startOrEnd = ocargo.levelEditor.endFlag || ocargo.levelEditor.startFlag
        if (!startOrEnd) {
            ocargo.levelEditor.end = segment;
            var getBBox = this_rect.getBBox();
            var coord = new ocargo.Coordinate(getBBox.x / 100, getBBox.y / 100);
            if (ocargo.levelEditor.deleteFlag) {
                ocargo.levelEditor.finaliseDelete(coord);
            } else {
                ocargo.levelEditor.finaliseMove(coord);
            }
            paper.clear();
            ocargo.levelEditor.start = null;
            createRoad(ocargo.levelEditor.nodes);
            ocargo.levelEditor.createGrid(paper)
            ocargo.levelEditor.drawDecor();
            if (ocargo.levelEditor.pathStart !== null) {
                coord = ocargo.levelEditor.translate(ocargo.levelEditor.pathStart.coordinate);
                ocargo.levelEditor.mark(coord, 'red', 0.7, true);
            }
            if (ocargo.levelEditor.destination !== null) {
                coord = ocargo.levelEditor.translate(ocargo.levelEditor.destination.coordinate);
                ocargo.levelEditor.mark(coord, 'blue', 0.7, true);
            }
        }
    }
}

ocargo.LevelEditor.prototype.drawDecor = function() {
    for (var i = 0; i < ocargo.levelEditor.decor.length; i++) {
        var obj = ocargo.levelEditor.decor[i];
        var coord = obj['coordinate'];
        var img = paper.image(obj['url'], coord.x, PAPER_HEIGHT - coord.y - DECOR_SIZE, 
            DECOR_SIZE, DECOR_SIZE);
        img.draggableDecor(coord.x, PAPER_HEIGHT - coord.y - DECOR_SIZE);
    }
}

ocargo.LevelEditor.prototype.finaliseDelete = function(coord) {
    var x, y;
    if (this.start.x <= coord.x) {
        for (x = this.start.x; x <= coord.x; x++) {
            deleteNode(x, this.start.y);
        }
    } else {
        for (x = this.start.x; x >= coord.x; x--) {
            deleteNode(x, this.start.y);
        }
    }
    if (this.start.y <= coord.y) {
        for (y = this.start.y + 1; y <= coord.y; y++) {
            deleteNode(coord.x, y);
        }
    } else {
        for (y = this.start.y - 1; y >= coord.y; y--) {
            deleteNode(coord.x, y);
        }
    }

    function deleteNode(x, y) {
        console.info("deleting at " + x + ", " + y);
        var coord = ocargo.levelEditor.translate(new ocargo.Coordinate(x, y));
        var nodeIndex = ocargo.levelEditor.findNodeByCoordinate(ocargo.levelEditor.nodes, coord);
        if (nodeIndex > -1) {
            var node = ocargo.levelEditor.nodes[nodeIndex];
            // Remove all the references to the node we're removing.
            for (var i = 0; i < node.connectedNodes.length; i++) {
                node.removeDoublyConnectedNode(node.connectedNodes[i]);
            }
            var index = ocargo.levelEditor.nodes.indexOf(node);
            ocargo.levelEditor.nodes.splice(index, 1);
        }
    }
};

ocargo.LevelEditor.prototype.finaliseMove = function(coord) {
    var current;
    var prev;

    for (var i = 0; i < ocargo.levelEditor.currentStrike.length; i++) {
        current = ocargo.levelEditor.currentStrike[i];
        var index = this.findNodeByCoordinate(this.nodes, current.coordinate);

        if (index > -1) {
            var existing = ocargo.levelEditor.nodes[index];
            var list = []
            for (var j = 0; j < current.connectedNodes.length; j++) {
{'fraction_non_alphanumeric': 0.12243211334120425, 'fraction_numerical': 0.00885478158205431, 'mean_word_length': 2.0999085086916742, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 15, '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': '3850220', 'n_tokens_mistral': 5315, 'n_tokens_neox': 5079, 'n_words': 998}