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

            return {
                x: x,
                y: y
            };
        }),
        pattern = [],
        patternCoord = [];

    function getCustomPieceWidth() {
        return GameData.customPieceWidth;
    }

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

    custom.generatePatternCoord = function generatePatternCoord() {
        _.each(pattern, function (i) {
            patternCoord.push(posToCoord(i));
        });
    };

    custom.getPattern = function getPattern() {
        return pattern;
    };

    custom.getPatternCoord = function getPatternCoord() {
        return patternCoord;
    };

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

    return custom;
}])
.controller('DesignPieceCtrl', [
    '$scope',
    'GameData',
    'CustomPiece',
function (
    $scope,
    GameData,
    CustomPiece
){
    var isSumbit = false;
    
    $scope.pieces = [];
    $scope.errorCode = {
        maxlength: false,
        minlength: true,
        saved: false
    };
    _.each(_.range(16), function (value) {
        $scope.pieces[value] = {
            isSelected: false
        };
    });
    _.each(CustomPiece.getPattern(), function (index) {
        $scope.pieces[index].isSelected = true;
    });

    function checkSelectedField() {
        var res = _.countBy($scope.pieces, function (piece) {
            return piece.isSelected ? 'selected' : 'unselected';
        });
        if (res.selected > GameData.maxCustomPiece) {
            $scope.errorCode.maxlength = true;
        } else {
            $scope.errorCode.maxlength = false;
        }
        if (res.selected > 0) {
            $scope.errorCode.minlength = false;
        } else if (_.isUndefined(res.selected)) {
            $scope.errorCode.minlength = true;
        }
        $scope.errorCode.saved = false;
    }

    function isOk() {
        return _.chain($scope.errorCode)
                .find(function (value, key) {
                    return key === 'saved' ? value : false;
                })
                .isUndefined()
                .value();
    }

    $scope.onClick = function onClick(index) {
        var isChecked = $scope.pieces[index].isSelected;
        $scope.pieces[index].isSelected = !isChecked;
        checkSelectedField();
    };

    $scope.isSelected = function isSelected(index) {
        return $scope.pieces[index].isSelected;
    };

    $scope.savePiece = function savePiece() {
        isSumbit = true;
        if (isOk()) {
            $scope.errorCode.saved = true;
            CustomPiece.generatePatterns($scope.pieces);
        }
    };

    $scope.getMaxPiece = function getMaxPiece() {
        return GameData.maxCustomPiece;
    };

    $scope.interacted = function interacted() {
        return isSumbit;
    };
}])<commit_msg>issue to save the piece<commit_after>/**
* directive.diDesignPiece Module
*
* Description
*/
angular.module('directive.diDesignPiece', [
    'ngMessages',
    'resource.GameData'
])
.factory('CustomPiece', [
    'GameData',
    function (
    GameData
){
    var custom = {},
        posToCoord = _.memoize(function (i) {
            var x = i % getCustomPieceWidth(),
                y = (i - x) / getCustomPieceWidth();

            return {
                x: x,
                y: y
            };
        }),
        pattern = [],
        patternCoord = [];

    function getCustomPieceWidth() {
        return GameData.customPieceWidth;
    }

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

    custom.generatePatternCoord = function generatePatternCoord() {
        _.each(pattern, function (i) {
            patternCoord.push(posToCoord(i));
        });
    };

    custom.getPattern = function getPattern() {
        return pattern;
    };

    custom.getPatternCoord = function getPatternCoord() {
        return patternCoord;
    };

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

    return custom;
}])
.controller('DesignPieceCtrl', [
    '$scope',
    'GameData',
    'CustomPiece',
function (
    $scope,
    GameData,
    CustomPiece
){
    var isSumbit = false;
    
    $scope.pieces = [];
    $scope.errorCode = {
        maxlength: false,
        minlength: true,
        saved: false
    };
    _.each(_.range(16), function (value) {
        $scope.pieces[value] = {
            isSelected: false
        };
    });
    _.each(CustomPiece.getPattern(), function (index) {
        $scope.pieces[index].isSelected = true;
    });

    function checkSelectedField() {
        var res = _.countBy($scope.pieces, function (piece) {
            return piece.isSelected ? 'selected' : 'unselected';
        });
        if (res.selected > GameData.maxCustomPiece) {
            $scope.errorCode.maxlength = true;
        } else {
            $scope.errorCode.maxlength = false;
        }
        if (res.selected > 0) {
            $scope.errorCode.minlength = false;
        } else if (_.isUndefined(res.selected)) {
            $scope.errorCode.minlength = true;
        }
        $scope.errorCode.saved = false;
    }

    function isOk() {
        // check any fields except saved field 
        return _.chain($scope.errorCode)
                .find(function (value, key) {
                    return key === 'saved' ? false : value;
                })
                .isUndefined()
                .value();
    }

    $scope.onClick = function onClick(index) {
        var isChecked = $scope.pieces[index].isSelected;
        $scope.pieces[index].isSelected = !isChecked;
        checkSelectedField();
    };

    $scope.isSelected = function isSelected(index) {
        return $scope.pieces[index].isSelected;
    };

    $scope.savePiece = function savePiece() {
        isSumbit = true;
        if (isOk()) {
            $scope.errorCode.saved = true;
            CustomPiece.generatePatterns($scope.pieces);
        }
    };

    $scope.getMaxPiece = function getMaxPiece() {
        return GameData.maxCustomPiece;
    };

    $scope.interacted = function interacted() {
        return isSumbit;
    };
}])
{'fraction_non_alphanumeric': 0.12791358726549176, 'fraction_numerical': 0.0011370096645821489, 'mean_word_length': 1.859406745225518, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2008636', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1995, 'n_words': 374}