<commit_before>/**
* directive.diDesignPiece Module
*
* Description
*/
angular.module('directive.diDesignPiece', [
    'ngMessages',
    'resource.GameData',
    'service.ROTATION_MATRIX',
    'service.GameManager'
])
.factory('CustomPiece', [
    'GameData',
    'ROTATION_MATRIX',
    function (
    GameData,
    ROTATION_MATRIX
){
    var custom = {},
        posToCoord = _.memoize(function (i) {
            var x = i % getCustomPieceWidth(),
                y = (i - x) / getCustomPieceWidth();

            return {
                x: x,
                y: y
            };
        }),
        patternCoord2d = [],
        pattern2d = [];

    function getCustomPieceWidth() {
        return GameData.customPieceWidth;
    }

    function getRotatePattern(matrix, deg) {
        var pattern = [],
            rotationArray = ROTATION_MATRIX[deg];
        // [y, x], index 0 is y, 1 is x
        _.each(rotationArray, function (elem, index) {
            var pos = !_.chain(matrix)
                    .find(function (obj, key) {
                        return elem[1] === obj.x &&
                                elem[0] === obj.y;
                    })
                    .isUndefined()
                    .value();
            if (pos) {
                pattern.push(index);
            }
        });
        pattern2d.push(pattern);
    }

    function setPatternCoord2d() {
        patternCoord2d = [];
        _.each(pattern2d, function (pattern) {
            setPatternToCoord(pattern);
        });
    }

    function setPatternToCoord(pattern) {
        var patternCoord = [];
        _.each(pattern, function (i) {
            patternCoord.push(posToCoord(i));
        });
        patternCoord2d.push(patternCoord);
    }

    custom.generatePatterns = function generatePatterns(pieces) {
        var pattern = [];
        pattern2d = [];
        patternCoord2d = [];
        _.each(pieces, function (piece, index) {
            if (piece.isSelected) {
                pattern.push(index);
            }
        });
        pattern2d.push(pattern);
        custom.generatePatternCoord();
    };

    custom.generatePatternCoord = function generatePatternCoord() {
        // set up the first patter coord
        setPatternToCoord(pattern2d[0]);

        for(var deg in [90, 180, 270]) {
            // deg is index [0, 1, 2]
            // patternCoor2d has init with orgin coord
            // so index + 1 to move to next rotation
            getRotatePattern(patternCoord2d[0], parseInt(deg, 10) + 1);
        }

        setPatternCoord2d();
    };

    custom.getPattern = function getPattern(rotation) {
        return pattern2d[rotation];
    };

    custom.getPatternCoord = function getPatternCoord(rotation) {
        return patternCoord2d[rotation];
    };

    custom.getCustomPiece = function getCustomPiece() {
        return {
            pattern2d: pattern2d,
            patternCoord2d: patternCoord2d
        };
    };

    custom.setCustomPiece = function setCustomPiece(customPiece) {
        pattern2d = customPiece.pattern2d;
        patternCoord2d = customPiece.patternCoord2d;
    };

    custom.hasCustomPiece = function hasCustomPiece() {
        return pattern2d.length > 0;
    };

    return custom;
}])
.controller('DesignPieceCtrl', [
    '$scope',
    'GameData',
    'CustomPiece',
    'GameManager',
function (
    $scope,
    GameData,
    CustomPiece,<commit_msg>design piece best practice<commit_after>/**
* directive.diDesignPiece Module
*
* Description
*/
angular.module('directive.diDesignPiece', [
    'ngMessages',
    'resource.GameData',
    'service.ROTATION_MATRIX',
    'service.GameManager'
])
.factory('CustomPiece', [
    'GameData',
    'ROTATION_MATRIX',
    function (
    GameData,
    ROTATION_MATRIX
){
    var custom = {
        generatePatterns: generatePatterns,
        generatePatternCoord: generatePatternCoord,

        getPattern: getPattern,
        getPatternCoord: getPatternCoord,

        getCustomPiece: getCustomPiece,
        setCustomPiece: setCustomPiece,
        hasCustomPiece: hasCustomPiece
    },
    posToCoord = _.memoize(function (i) {
        var x = i % getCustomPieceWidth(),
            y = (i - x) / getCustomPieceWidth();

        return {
            x: x,
            y: y
        };
    }),
    patternCoord2d = [],
    pattern2d = [];

    return custom;

    function generatePatterns(pieces) {
        var pattern = [];
        pattern2d = [];
        patternCoord2d = [];
        _.each(pieces, function (piece, index) {
            if (piece.isSelected) {
                pattern.push(index);
            }
        });
        pattern2d.push(pattern);
        custom.generatePatternCoord();
    }

    function generatePatternCoord() {
        // set up the first patter coord
        setPatternToCoord(pattern2d[0]);

        for(var deg in [90, 180, 270]) {
            // deg is index [0, 1, 2]
            // patternCoor2d has init with orgin coord
            // so index + 1 to move to next rotation
            getRotatePattern(patternCoord2d[0], parseInt(deg, 10) + 1);
        }

        setPatternCoord2d();
    }

    function getPattern(rotation) {
        return pattern2d[rotation];
    }

    function getPatternCoord(rotation) {
        return patternCoord2d[rotation];
    }

    function getCustomPiece() {
        return {
            pattern2d: pattern2d,
            patternCoord2d: patternCoord2d
        };
    }

    function setCustomPiece(customPiece) {
        pattern2d = customPiece.pattern2d;
        patternCoord2d = customPiece.patternCoord2d;
    }

    function hasCustomPiece() {
        return pattern2d.length > 0;
    }

    // private methods
    function getCustomPieceWidth() {
        return GameData.customPieceWidth;
    }

    function getRotatePattern(matrix, deg) {
        var pattern = [],
            rotationArray = ROTATION_MATRIX[deg];
        // [y, x], index 0 is y, 1 is x
        _.each(rotationArray, function (elem, index) {
            var pos = !_.chain(matrix)
                    .find(function (obj, key) {
                        return elem[1] === obj.x &&
                                elem[0] === obj.y;
                    })
                    .isUndefined()
                    .value();
            if (pos) {
                pattern.push(index);
            }
        });
        pattern2d.push(pattern);
    }

    // private methods
    function setPatternCoord2d() {
        patternCoord2d = [];
        _.each(pattern2d, function (pattern) {
            setPatternToCoord(pattern);
        });
    }

    // private methods
    function setPatternToCoord(pattern) {
        var patternCoord = [];
        _.each(pattern, function (i) {
            patternCoord.push(posToCoord(i));
        });
        patternCoord2d.push(patternCoord);
    }
}])
.controller('DesignPieceCtrl', [
    '$scope',
    'GameData',
    'CustomPiece',
    'GameManager',
function (
    $scope,
    GameData,
    CustomPiece,
{'fraction_non_alphanumeric': 0.11317519291226065, 'fraction_numerical': 0.013432409259788511, 'mean_word_length': 1.7984806077568973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2554889', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1994, 'n_words': 397}