import './LeaderboardController'; import './PredictController'; import './PredictorController'; import './predictorMatch'; import './roundSummaryComponent'; import 'angular-route/angular-route.min'; angular.module('footyroom').requires.push('ngRoute'); angular.module('footyroom') .factory('leaderboardService', ['$http', function ($http) { return { getLeaderboard: function (phase) { return $http.get('/predictor/leaderboard.json', { params: { phase, }, }) .then(function (response) { return response.data; }); }, }; }]) .factory('predictService', ['$http', function ($http) { var state = {}; return { loadViewAs: (userId) => { if (!userId) { state.viewAs = null; return Promise.resolve(); } if (state.viewAs && state.viewAs.userId === userId) { return Promise.resolve(); } return fetchPlayer(userId).then((player) => { state.viewAs = player; }); }, getViewAs: () => state.viewAs, getPlayer: function () { if (state.player) { return Promise.resolve(state.player); } return fetchPlayer().then((player) => { state.player = player; return player; }); }, getMatches: function (round) { return $http.get('/predictor/matches.json', { params: { round }, }) .then(function (response) { return response.data; }); }, getRounds: function (round, userId) { return $http.get('/predictor/rounds.json', { params: { round, userId, }, }) .then(function (response) { state.game = response.data; return state.game; }); }, predict: function (prediction) { prediction.stake += prediction.raise; return $http.post('/predictor/predict', prediction).then(function () { state.player.points -= prediction.raise; state.game.rounds.selected.player.stake += prediction.raise; }); }, getGameState: function () { return state.game; }, }; function fetchPlayer(userId) { return $http.get('/predictor/player.json', { params: { userId, }, }) .then(function (response) { return response.data; }); } }]) .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { var defaultRoute = { templateUrl: '/views/ng/predictor/predict/index.html?7', controller: 'PredictController', selectedTab: 'predict', resolve: { rounds: ['predictService', '$route', function (predictService, $route) { return predictService.getRounds( $route.current.params.id, $route.current.params.viewAs ); }], }, }; $routeProvider .when('/', defaultRoute) .when('/predictor', defaultRoute) .when('/predictor/round/:id', defaultRoute) .when('/predictor/leaderboard/:phase?', { templateUrl: '/views/ng/predictor/leaderboard/index.html?9', controller: 'LeaderboardController', selectedTab: 'leaderboard', resolve: { leaderboard: ['leaderboardService', '$route', function (leaderboardService, $route) { return leaderboardService.getLeaderboard($route.current.params.phase); }], }, }) .when('/predictor/help', { templateUrl: '/views/ng/predictor/help.html?1', selectedTab: 'help', }) .when('/predictor/prizes', { templateUrl: '/views/ng/predictor/prizes.html?8', selectedTab: 'prizes', }); $locationProvider.html5Mode({enabled: true, requireBase: false}); }]) .run(['$rootScope', '$window', function ($rootScope, $window) { $rootScope.$on('$locationChangeStart', function (event, next, current) { if (!next.match(/\/predictor(\/.*)?/) && next !== current) { event.preventDefault(); $window.location.href = next; } }); $rootScope.$on('$routeChangeStart', function () { $rootScope.predictorLoading = true; }); $rootScope.$on('$routeChangeSuccess', function () { $rootScope.predictorLoading = false; }); }]);