<commit_before>
var app = angular.module('DailyFantasyFootball');

app.controller('HomeCtrl', ['$scope', '$resource', '$location',
    function($scope, $resource, $location) {
        var Weeks = $resource('/api/v1/weeks');
        var SingleWeek = $resource('/api/v1/weeks/:id');

        Weeks.query({ contests: true }, function(weeks) {
            $scope.weeks = weeks;

            SingleWeek.get({ id: $scope.weeks[weeks.length - 1].weekNumber, contests: true }, function(currentWeek) {
                $scope.currentWeek = currentWeek;
                $scope.friendlyLockDate = moment(currentWeek.weekLockDate).calendar();
                $scope.contestCreateAllowed = (currentWeek.state === 'open');
            });
        });

        $scope.selectContest = function(contestId) {
            $location.path('/contest/' + contestId);
        };

        $scope.createContest = function() {
            $location.path('/newcontest/' + $scope.currentWeek._id);
        };
    }
]);

app.controller('CreateContestCtrl', ['$scope', '$rootScope', '$resource', '$routeParams', '$location',
    function($scope, $rootScope, $resource, $routeParams, $location) {
        $scope.contest = {
            salaryCap: 50000,
            positionCounts: {
                QB: '1',
                RB: '2',
                WR: '3',
                TE: '1',
                FLEX: '1',
                DST: '1'
            },
            qbFlex: false,
            rbFlex: true,
            wrFlex: true,
            teFlex: true
        };

        $scope.save = function() {
            $scope.contest.week = $routeParams.weekId;
            $scope.contest.owner = $rootScope.user._id;
            Contests = $resource('/api/v1/contests');

            Contests.save($scope.contest, function(contest) {
                $location.path('/contest/' + contest._id);
            });
        };
    }
]);

app.controller('ContestCtrl', ['$scope', '$rootScope', '$resource', '$routeParams', '$location', '$uibModal',
    function($scope, $rootScope, $resource, $routeParams, $location, $uibModal) {
        var Contests = $resource('/api/v1/contests/:id');

        Contests.get({ id: $routeParams.id }, function(contest) {
            $scope.contest = contest;
        
            // If the current user owns this contest or is an admin, they can delete it
            $scope.canDelete = ($rootScope.user.isAdmin || ($rootScope.user._id === contest.owner._id));
        });

        $scope.delete = function() {
            Contests.delete({ id: $scope.contest._id }, function() {
                $location.path('/');
            });
        };
    }
]);

// TODO: This depends on the parent scope from ContestCtrl. That's probably okay for now but it would be
// better to reduce the tight coupling here.
app.controller('ContestOpenCtrl', ['$scope', '$rootScope', '$resource', '$routeParams', '$location', '$uibModal', '$timeout',
    function($scope, $rootScope, $resource, $routeParams, $location, $uibModal, $timeout) {
        var Contests = $resource('/api/v1/contests/:id');
        var Players = $resource('/api/v1/weeks/:weekNumber/players');

        // TODO: Create some real objects with methods for managing the roster, etc. Right now there are too
        // many one-off pieces of state that all have to be kept in sync, and this won't scale (and it's ugly).
        $scope.rosterSalary = 0;
        $scope.contestLineup = [];

        $scope.roster = [];
        $scope.saveResult = {};

        $scope.sortType = 'salary';
        $scope.sortReverse = true;
        $scope.searchField = '';

        var tabFilter = 'QB';

        $scope.setTab = function(filter) {
            tabFilter = filter;
        };

        $scope.playerFilter = function(item) {
            if (item.player.displayName.toLowerCase().indexOf($scope.searchField.toLowerCase()) != -1) {
                // Matches the search box. See if the player is already in our roster or not
                for (var i = 0; i < $scope.roster.length; i++) {
                    if ($scope.roster[i].player._id === item.player._id) {
                        return false;
                    }
                }

                switch(tabFilter) {
                    case 'All':
                        return true;

                    case 'FLEX':
                        return flexAllowed(item.player.position);

                    default:
                        return (tabFilter === item.player.position);
                }
            }

            return false;
        };

        Contests.get({ id: $routeParams.id }, function(contest) {
            // Set the lineup array that the roster will fill-in.
            // TODO: Refactor this into a helper method that takes the position as a param
            // QBs
            for (var i = 0; i < contest.positionCounts.QB; i++) {
                $scope.contestLineup.push({
                    position: 'QB'
                });
            }

            // RB
            for (var i = 0; i < contest.positionCounts.RB; i++) {
                $scope.contestLineup.push({
                    position: 'RB'
                });
            }

            // WR
            for (var i = 0; i < contest.positionCounts.WR; i++) {
                $scope.contestLineup.push({
                    position: 'WR'
                });
            }

            // TE
            for (var i = 0; i < contest.positionCounts.TE; i++) {
                $scope.contestLineup.push({
                    position: 'TE'
                });
            }

            // FLEX
            for (var i = 0; i < contest.positionCounts.FLEX; i++) {
                $scope.contestLineup.push({
                    position: 'FLEX'
                });
            }

            // DST
            for (var i = 0; i < contest.positionCounts.DST; i++) {
                $scope.contestLineup.push({
                    position: 'DST'
                });
            }

            for (var i = 0; i < $scope.contest.entries.length; i++) {
                if ($scope.contest.entries[i].user._id === $rootScope.user._id) {
                    $scope.myEntry = $scope.contest.entries[i];
                    $scope.roster = $scope.myEntry.roster;

                    for (var i = 0; i < $scope.roster.length; i++) {
                        $scope.addPlayer($scope.roster[i]);
                    }
                    break;
                }
            }

            Players.get({ weekNumber: contest.week.weekNumber }).$promise.then(function(week) {
                $scope.players = week.players;
            });
        });

        function flexAllowed(position) {
            switch (position) {
                case 'QB':
                    return $scope.contest.qbFlex;
                case 'RB':
                    return $scope.contest.rbFlex;
                case 'WR':
                    return $scope.contest.wrFlex;
                case 'TE': 
                    return $scope.contest.teFlex;
                default:
                    return false;
            };
        };

        function addPlayerHelper(contestLineupSlot, item, roster) {<commit_msg>Comparing usage of resource promise<commit_after>
var app = angular.module('DailyFantasyFootball');

app.controller('HomeCtrl', ['$scope', '$resource', '$location',
    function($scope, $resource, $location) {
        var Weeks = $resource('/api/v1/weeks');
        var SingleWeek = $resource('/api/v1/weeks/:id');

        Weeks.query({ contests: true }, function(weeks) {
            $scope.weeks = weeks;

            SingleWeek.get({ id: $scope.weeks[weeks.length - 1].weekNumber, contests: true }, function(currentWeek) {
                $scope.currentWeek = currentWeek;
                $scope.friendlyLockDate = moment(currentWeek.weekLockDate).calendar();
                $scope.contestCreateAllowed = (currentWeek.state === 'open');
            });
        });

        $scope.selectContest = function(contestId) {
            $location.path('/contest/' + contestId);
        };

        $scope.createContest = function() {
            $location.path('/newcontest/' + $scope.currentWeek._id);
        };
    }
]);

app.controller('CreateContestCtrl', ['$scope', '$rootScope', '$resource', '$routeParams', '$location',
    function($scope, $rootScope, $resource, $routeParams, $location) {
        $scope.contest = {
            salaryCap: 50000,
            positionCounts: {
                QB: '1',
                RB: '2',
                WR: '3',
                TE: '1',
                FLEX: '1',
                DST: '1'
            },
            qbFlex: false,
            rbFlex: true,
            wrFlex: true,
            teFlex: true
        };

        $scope.save = function() {
            $scope.contest.week = $routeParams.weekId;
            $scope.contest.owner = $rootScope.user._id;
            Contests = $resource('/api/v1/contests');

            Contests.save($scope.contest, function(contest) {
                $location.path('/contest/' + contest._id);
            });
        };
    }
]);

app.controller('ContestCtrl', ['$scope', '$rootScope', '$resource', '$routeParams', '$location', '$uibModal',
    function($scope, $rootScope, $resource, $routeParams, $location, $uibModal) {
        var Contests = $resource('/api/v1/contests/:id');

        Contests.get({ id: $routeParams.id }, function(contest) {
            $scope.contest = contest;
        
            // If the current user owns this contest or is an admin, they can delete it
            $scope.canDelete = ($rootScope.user.isAdmin || ($rootScope.user._id === contest.owner._id));
        });

        $scope.delete = function() {
            Contests.delete({ id: $scope.contest._id }, function() {
                $location.path('/');
            });
        };
    }
]);

// TODO: This depends on the parent scope from ContestCtrl. That's probably okay for now but it would be
// better to reduce the tight coupling here.
app.controller('ContestOpenCtrl', ['$scope', '$rootScope', '$resource', '$routeParams', '$location', '$uibModal', '$timeout',
    function($scope, $rootScope, $resource, $routeParams, $location, $uibModal, $timeout) {
        var Contests = $resource('/api/v1/contests/:id');
        var Players = $resource('/api/v1/weeks/:weekNumber/players');

        // TODO: Create some real objects with methods for managing the roster, etc. Right now there are too
        // many one-off pieces of state that all have to be kept in sync, and this won't scale (and it's ugly).
        $scope.rosterSalary = 0;
        $scope.contestLineup = [];

        $scope.roster = [];
        $scope.saveResult = {};

        $scope.sortType = 'salary';
        $scope.sortReverse = true;
        $scope.searchField = '';

        var tabFilter = 'QB';

        $scope.setTab = function(filter) {
            tabFilter = filter;
        };

        $scope.playerFilter = function(item) {
            if (item.player.displayName.toLowerCase().indexOf($scope.searchField.toLowerCase()) != -1) {
                // Matches the search box. See if the player is already in our roster or not
                for (var i = 0; i < $scope.roster.length; i++) {
                    if ($scope.roster[i].player._id === item.player._id) {
                        return false;
                    }
                }

                switch(tabFilter) {
                    case 'All':
                        return true;

                    case 'FLEX':
                        return flexAllowed(item.player.position);

                    default:
                        return (tabFilter === item.player.position);
                }
            }

            return false;
        };

        Contests.get({ id: $routeParams.id }).$promise.then(function(contest) {
            Players.get({ weekNumber: contest.week.weekNumber }).$promise.then(function(week) {
                $scope.players = week.players;
            });
            // Set the lineup array that the roster will fill-in.
            // TODO: Refactor this into a helper method that takes the position as a param
            // QBs
            for (var i = 0; i < contest.positionCounts.QB; i++) {
                $scope.contestLineup.push({
                    position: 'QB'
                });
            }

            // RB
            for (var i = 0; i < contest.positionCounts.RB; i++) {
                $scope.contestLineup.push({
                    position: 'RB'
                });
            }

            // WR
            for (var i = 0; i < contest.positionCounts.WR; i++) {
                $scope.contestLineup.push({
                    position: 'WR'
                });
            }

            // TE
            for (var i = 0; i < contest.positionCounts.TE; i++) {
                $scope.contestLineup.push({
                    position: 'TE'
                });
            }

            // FLEX
            for (var i = 0; i < contest.positionCounts.FLEX; i++) {
                $scope.contestLineup.push({
                    position: 'FLEX'
                });
            }

            // DST
            for (var i = 0; i < contest.positionCounts.DST; i++) {
                $scope.contestLineup.push({
                    position: 'DST'
                });
            }

            for (var i = 0; i < $scope.contest.entries.length; i++) {
                if ($scope.contest.entries[i].user._id === $rootScope.user._id) {
                    $scope.myEntry = $scope.contest.entries[i];
                    $scope.roster = $scope.myEntry.roster;

                    for (var i = 0; i < $scope.roster.length; i++) {
                        $scope.addPlayer($scope.roster[i]);
                    }
                    break;
                }
            }
        });

        function flexAllowed(position) {
            switch (position) {
                case 'QB':
                    return $scope.contest.qbFlex;
                case 'RB':
                    return $scope.contest.rbFlex;
                case 'WR':
                    return $scope.contest.wrFlex;
                case 'TE': 
                    return $scope.contest.teFlex;
                default:
                    return false;
            };
        };

        function addPlayerHelper(contestLineupSlot, item, roster) {
{'fraction_non_alphanumeric': 0.13639836099729147, 'fraction_numerical': 0.004028057503993333, 'mean_word_length': 1.465331278890601, 'pattern_counts': {'":': 0, '<': 21, '<?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': '509258', 'n_tokens_mistral': 4105, 'n_tokens_neox': 3841, 'n_words': 876}