server/dashboard/static/js/dashboard.js

174 lines
5.6 KiB
JavaScript

angular.module("FICApp", ["ngSanitize", "ngAnimate"])
.controller("EventsController", function($scope, $rootScope, $http, $interval) {
$scope.events = [];
var refreshEvents = function() {
// Update times
var time = angular.fromJson(sessionStorage.time);
var now = Date.now();
if (time)
now += time.cu - time.he;
now = new Date(now);
$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()["last-modified"])
return;
$scope.lasteventsetag = response.headers()["last-modified"];
var lastExercice = undefined;
$scope.events = response.data;
$scope.events.forEach(function(event) {
if (!lastExercice && $scope.themes) {
var res = event.txt.match(/<strong>(\d+)<sup>e<\/sup><\/strong> défi ([^&]+)/);
if (res) {
for (var tid in $scope.themes) {
if ($scope.themes[tid].name == res[2]) {
lastExercice = Object.keys($scope.themes[tid].exercices)[parseInt(res[1])-1];
break;
}
}
}
}
event.time = Date.parse(event.time);
event.since = now - event.time;
event.kind = ["border-" + event.kind, "alert-" + event.kind];
});
$rootScope.lastExercice = lastExercice;
});
}
refreshEvents()
$interval(refreshEvents, 2100);
})
.controller("TimerController", function($scope, $rootScope, $interval, $timeout) {
$scope.duration = 0;
$scope.init = function(end) {
$scope.initT(Date.parse(end));
}
$scope.initStart = function() {
$scope.$watch("settings", function(settings){
if (settings)
$scope.initT(settings.start);
})
}
$scope.initT = function(end) {
var time = angular.fromJson(sessionStorage.time);
if (time) {
var srv_cur = new Date(Date.now() + (time.cu - time.he));
$scope.duration = Math.floor((end - srv_cur)/1000);
} else
$timeout(function() { $scope.initT(end); }, 1000);
}
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";
pathname = pathname.replace(".html", ".json");
var refreshScene = function() {
$http.get(pathname).then(function(response) {
if ($scope.lastpublicetag != undefined && $scope.lastpublicetag == response.headers()["last-modified"])
return;
$scope.lastpublicetag = response.headers()["last-modified"];
$scope.display = response.data;
});
}
refreshScene();
var refreshSceneInterval = $interval(refreshScene, 900);
var refreshSettings = function() {
$http.get("/settings.json").then(function(response) {
$rootScope.recvTime(response);
response.data.start = new Date(response.data.start);
response.data.end = new Date(response.data.end);
response.data.generation = new Date(response.data.generation);
response.data.awards = new Date("2019-01-23T15:00:00Z");
$rootScope.settings = response.data;
});
}
refreshSettings();
var refreshSettingsInterval = $interval(refreshSettings, 4200);
$rootScope.refresh = function() {
$http.get("/my.json").then(function(response) {
if ($scope.lastmyetag != undefined && $scope.lastmyetag == response.headers()["last-modified"])
return;
$scope.lastmyetag = response.headers()["last-modified"];
$scope.my = response.data;
});
$http.get("/stats.json").then(function(response) {
$scope.stats = response.data;
});
$http.get("/settings.json").then(function(response) {
$rootScope.recvTime(response);
response.data.start = new Date(response.data.start);
response.data.end = new Date(response.data.end);
response.data.generation = new Date(response.data.generation);
response.data.awards = new Date("2019-01-23T15:00:00Z");
$rootScope.settings = response.data;
});
$http.get("/themes.json").then(function(response) {
if ($scope.lastthemeetag != undefined && $scope.lastthemeetag == response.headers()["last-modified"])
return;
$scope.lastthemeetag = response.headers()["last-modified"];
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()["last-modified"])
return;
$scope.lastteametag = response.headers()["last-modified"];
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; });
});
}
$rootScope.refresh();
$interval($rootScope.refresh, 4200);
})
.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;
});
});