angular.module("FICApp", ["ngSanitize", "ngAnimate"]); angular.module("FICApp") .filter("objectLength", function() { return function(input) { if (input !== undefined) return Object.keys(input).length; else return ""; } }); String.prototype.capitalize = function() { return this .toLowerCase() .replace( /(^|\s)([a-z])/g, function(m,p1,p2) { return p1+p2.toUpperCase(); } ); } angular.module("FICApp") .controller("TimeController", function($scope, $rootScope, $http, $timeout) { $scope.time = {}; var initTime = function() { $timeout.cancel($scope.cbi); $scope.cbi = $timeout(initTime, 10000); $http.get("/time.json").then(function(response) { var time = response.data; console.log("upd time"); time.he = (new Date()).getTime(); sessionStorage.userService = angular.toJson(time); }); }; initTime(); function updTime() { $timeout.cancel($scope.cb); $scope.cb = $timeout(updTime, 1000); if (sessionStorage.userService) { var time = angular.fromJson(sessionStorage.userService); var srv_cur = (Date.now() + (time.cu * 1000 - time.he)) / 1000; var remain = time.du; if (time.st == Math.floor(srv_cur)) { $scope.refresh(true); $rootScope.startIn = 0; } if (time.st > 0 && time.st <= srv_cur) { remain = time.st + time.du - srv_cur; } else if (time.st > 0) { $rootScope.startAt = time.st; } if (remain < 0) { remain = 0; $scope.time.end = true; $scope.time.expired = true; } else if (remain < 60) { $scope.time.end = false; $scope.time.expired = true; } else { $scope.time.end = false; $scope.time.expired = false; } $scope.time.start = time.st * 1000; $scope.time.duration = time.du; $scope.time.remaining = remain; $scope.time.hours = Math.floor(remain / 3600); $scope.time.minutes = Math.floor((remain % 3600) / 60); $scope.time.seconds = Math.floor(remain % 60); } } updTime(); }) .controller("EventsController", function($scope, $http, $interval) { $scope.events = []; var refreshEvents = function() { // Update times var now = new Date().getTime(); $scope.events.forEach(function(ev) { ev.since = now - ev.time; }); $http.get("/events.json").then(function(response) { // Don't make anything if the page hasn't changed if ($scope.lasteventsetag != undefined && $scope.lasteventsetag == response.headers().etag) return; $scope.lasteventsetag = response.headers().etag; var events = response.data; var kEvents = {}; events.forEach(function(ev) { kEvents[ev.id] = ev; }); var kxEvents = {} $scope.events.forEach(function(ev) { kxEvents[ev.id] = ev; }); var oldEvents = $(Object.keys(kxEvents)).not(Object.keys(kEvents)).get(); var newEvents = $(Object.keys(kEvents)).not(Object.keys(kxEvents)).get(); $scope.events.filter(function(val) { return !oldEvents.inArray(val.id); }); newEvents.forEach(function(ev) { var event = kEvents[ev]; event.time = Date.parse(event.time); event.since = now - event.time; $scope.events.unshift(event); }); }); } refreshEvents() $interval(refreshEvents, 2100); }) .controller("CountdownController", function($scope, $interval) { $scope.duration = 0; $scope.init = function(end) { $scope.initT(Date.parse(end)/1000); } $scope.initT = function(end) { var time = angular.fromJson(sessionStorage.userService); var srv_cur = (Date.now() + (time.cu * 1000 - time.he)) / 1000; $scope.duration += Math.floor(end - srv_cur); } var stop = $interval(function() { $scope.duration -= 1; if ($scope.duration < -10) $interval.cancel(stop); }, 1000); }) .controller("DataController", function($scope, $http, $rootScope, $interval) { var pathname = window.location.pathname; if (pathname == "/") pathname = "/public0.html" var refreshScene = function() { $http.get(pathname.replace(".html", ".json")).then(function(response) { if ($scope.lastpublicetag != undefined && $scope.lastpublicetag == response.headers().etag) return; $scope.lastpublicetag = response.headers().etag; $scope.scene = response.data; }); } var refreshData = function() { $http.get("/my.json").then(function(response) { if ($scope.lastmyetag != undefined && $scope.lastmyetag == response.headers().etag) return; $scope.lastmyetag = response.headers().etag; $scope.my = response.data; }); $http.get("/stats.json").then(function(response) { $scope.stats = response.data; }); $http.get("/settings.json").then(function(response) { $scope.settings = response.data; }); $http.get("/themes.json").then(function(response) { if ($scope.lastthemeetag != undefined && $scope.lastthemeetag == response.headers().etag) return; $scope.lastthemeetag = response.headers().etag; var themes = response.data; $scope.themes = themes; $scope.max_gain = 0; angular.forEach(themes, function(theme, key) { if (theme.exercices) this[key].exercice_count = Object.keys(theme.exercices).length; else this[key].exercice_count = 0; this[key].gain = 0; angular.forEach(theme.exercices, function(ex, k) { this.gain += ex.gain; }, theme); $scope.max_gain += theme.gain; }, themes); }); $http.get("/teams.json").then(function(response) { if ($scope.lastteametag != undefined && $scope.lastteametag == response.headers().etag) return; $scope.lastteametag = response.headers().etag; var teams = response.data; $scope.teams_count = Object.keys(teams).length $scope.teams = teams; $scope.rank = []; angular.forEach($scope.teams, function(team, tid) { team.id = tid; if (team.rank) { this.push(team); } }, $scope.rank); $scope.pagesrank = Array(Math.ceil($scope.rank.length / 7)).fill(0).map(function(v, i){ return i; }); }); } refreshData(); refreshScene(); $interval(refreshData, 4200); $interval(refreshScene, 900); }) .controller("TeamController", function($scope, $http, $interval) { $scope.mystats = null; $http.get("/api/teams/" + $scope.team.id + "/stats.json").then(function(response) { $scope.mystats = response.data; }); });