<commit_before>define(['board', 'kinetic'], function(Board, Kinetic) {

var PieceUI = function(board, obj) {
    var self = this;
    self.obj = obj;
    self.board = board;

    self.proxy = new Kinetic.Image({
        x: obj.x * self.board.size, y: obj.y * self.board.size,
        width: self.board.size, height: self.board.size,
        image: PieceUI.imgs[obj.player][obj.type]
    });

    self.proxy.on('click', function() {
        board.moveLayer.removeChildren();
        self.addMoveUI(board.moveLayer);
    });

    self.proxy.on('mouseover', function() {
        self.addMoveUI(board.hoverLayer);
    });

    self.proxy.on('mouseout', function() {
        document.body.style.cursor = 'default';
        board.hoverLayer.removeChildren();
        board.hoverLayer.draw();
    });
    board.pieceLayer.add(self.proxy);
}
PieceUI.imgs = {white: {}, black: {}};

/* Post the specified move to the server. */
PieceUI.prototype.postMove = function(x, y, promotion) {
    var self = this;

    var cols = "abcdefgh";
    var move = cols[self.obj.x] + self.obj.y + "-" + cols[x] + y;
    if(promotion !== null) {
        move += promotion;
    }

    var url = self.board.url + "?move=" + move + "&turn=" + self.board.obj.turn;
    $.post(url, function() {
        console.log("POST", move);
        self.board.update();
    }).fail(function() {
        alert("POST to server failed. Try reloading this page.");
    });
}

PieceUI.prototype.moveTo = function(x, y, promotion, animate) {
    var self = this;

    if(animate) {
        self.board.moveLayer.hide();
        self.board.hoverLayer.hide();
    }

    var distance = Math.max(Math.abs(self.obj.x -x), Math.abs(self.obj.y - y));
    var moveTime = 0.125 + (1.0 * distance / 8);

    self.obj.ifCastle(x, y, function(rook, rookTo) {
        if(animate) {
            rook.ui.proxy.transitionTo({
                x: rookTo * self.board.size, duration: moveTime
            });
        }
        else {
            rook.ui.proxy.setX(rookTo * self.board.size);
        }
    });

    var doUpdates = function() {
        self.obj.ifTake(x, y, function(taken) {
            taken.ui.proxy.hide();
        });
        self.obj.moveTo(x, y, promotion);

        self.proxy.setImage(PieceUI.imgs[self.obj.player][self.obj.type]);

        self.board.moveLayer.removeChildren();
        self.board.moveLayer.show();
        self.board.hoverLayer.removeChildren();
        self.board.hoverLayer.show();

        self.board.stage.draw();
    }

    if(animate) {
        self.proxy.transitionTo({
            x: x * self.board.size, y: y * self.board.size,
            duration: moveTime, callback: doUpdates
        });<commit_msg>Fix double update bug (when an update arrives while the current move is animating, both moves are "added" to the log).<commit_after>define(['board', 'kinetic'], function(Board, Kinetic) {

var PieceUI = function(board, obj) {
    var self = this;
    self.obj = obj;
    self.board = board;

    self.proxy = new Kinetic.Image({
        x: obj.x * self.board.size, y: obj.y * self.board.size,
        width: self.board.size, height: self.board.size,
        image: PieceUI.imgs[obj.player][obj.type]
    });

    self.proxy.on('click', function() {
        board.moveLayer.removeChildren();
        self.addMoveUI(board.moveLayer);
    });

    self.proxy.on('mouseover', function() {
        self.addMoveUI(board.hoverLayer);
    });

    self.proxy.on('mouseout', function() {
        document.body.style.cursor = 'default';
        board.hoverLayer.removeChildren();
        board.hoverLayer.draw();
    });
    board.pieceLayer.add(self.proxy);
}
PieceUI.imgs = {white: {}, black: {}};

/* Post the specified move to the server. */
PieceUI.prototype.postMove = function(x, y, promotion) {
    var self = this;

    var cols = "abcdefgh";
    var move = cols[self.obj.x] + self.obj.y + "-" + cols[x] + y;
    if(promotion !== null) {
        move += promotion;
    }

    var url = self.board.url + "?move=" + move + "&turn=" + self.board.obj.turn;
    $.post(url, function() {
        console.log("POST", move);
        self.board.update();
    }).fail(function() {
        alert("POST to server failed. Try reloading this page.");
    });
}

PieceUI.prototype.moveTo = function(x, y, promotion, animate) {
    var self = this;

    if(animate) {
        self.board.moveLayer.hide();
        self.board.hoverLayer.hide();
    }

    var distance = Math.max(Math.abs(self.obj.x -x), Math.abs(self.obj.y - y));
    var moveTime = 0.125 + (1.0 * distance / 8);

    self.obj.ifCastle(x, y, function(rook, rookTo) {
        if(animate) {
            rook.ui.proxy.transitionTo({
                x: rookTo * self.board.size, duration: moveTime
            });
        }
        else {
            rook.ui.proxy.setX(rookTo * self.board.size);
        }
    });

    var taken = null;
    self.obj.ifTake(x, y, function(piece) {
        taken = piece;
    });

    var doUpdates = function() {
        if(taken !== null) {
            taken.ui.proxy.hide();
        }

        self.proxy.setImage(PieceUI.imgs[self.obj.player][self.obj.type]);

        self.board.moveLayer.removeChildren();
        self.board.moveLayer.show();
        self.board.hoverLayer.removeChildren();
        self.board.hoverLayer.show();

        self.board.stage.draw();
    }

    self.obj.moveTo(x, y, promotion);

    if(animate) {
        self.proxy.transitionTo({
            x: x * self.board.size, y: y * self.board.size,
            duration: moveTime, callback: doUpdates
        });
{'fraction_non_alphanumeric': 0.1632175634335073, 'fraction_numerical': 0.00251934497030772, 'mean_word_length': 2.238927738927739, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '885350', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1815, 'n_words': 347}