Retrieve time through X-FIC-Time header instead of time.json

This commit is contained in:
nemunaire 2018-08-17 22:17:51 +02:00 committed by Pierre-Olivier Mercier
commit 6034246015
11 changed files with 107 additions and 131 deletions

View file

@ -19,22 +19,22 @@
<div class="navbar navbar-expand-lg navbar-dark bg-dark text-light">
<div class="container">
<div class="col-md-auto">
<a href="https://www.forum-fic.com/" ng-if="!(time.start || my.team_id)">
<a href="https://www.forum-fic.com/" ng-if="!(time.remaining === undefined || my.team_id)">
<img src="/img/fic.png" alt="Forum International de la Cybersécurité" class="center-block">
</a>
<a href="/" ng-if="(time.start || my.team_id)" ng-cloak>
<a href="/" ng-if="(time.remaining === undefined || my.team_id)" ng-cloak>
<img src="/img/fic.png" alt="Forum International de la Cybersécurité" class="center-block">
</a>
</div>
<div id="clock" class="col-md-auto text-center" ng-hide="1">Chargement...</div>
<div id="clock" class="col-md-auto text-center" ng-class="{expired: time.expired, end: time.end}" ng-if="time.start || my.team_id" ng-cloak>
<div id="clock" class="col-md-auto text-center" ng-class="{expired: time.expired, end: time.end}" ng-if="time.remaining !== undefined && settings.end - settings.start > 0" ng-cloak>
<span id="hours">{{ time.hours | time }}</span>
<span class="point">:</span>
<span id="min">{{ time.minutes | time }}</span>
<span class="point">:</span>
<span id="sec">{{ time.seconds | time }}</span>
</div>
<div id="clock" class="col-md-auto btn-group btn-group-lg" ng-if="!(time.start || my.team_id)" ng-cloak>
<div id="clock" class="col-md-auto btn-group btn-group-lg" ng-if="time.remaining === undefined || settings.end - settings.start <= 0" ng-cloak>
<a class="btn btn-light" href="/">
<span class="glyphicon glyphicon-home"></span> Accueil
</a>
@ -83,7 +83,7 @@
<span class="teamname">{{ my.name }}</span>
</a>
</span>
<span class="navbar-text text-light" ng-show="!my.team_id && time.start" ng-cloak>
<span class="navbar-text text-light" ng-show="!my.team_id && time.remaining === undefined" ng-cloak>
<a ng-href="/register" class="badge badge-warning" role="button">
Inscription
</a>

View file

@ -45,17 +45,37 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
$('[data-toggle="popover"]').popover();
$rootScope.recvTime = function(response) {
sessionStorage.userService = angular.toJson({
"cu": Math.floor(response.headers("x-fic-time") * 1000),
"he": (new Date()).getTime(),
});
}
function updTime() {
if (sessionStorage.userService) {
if (sessionStorage.userService && $rootScope.settings) {
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)) {
var settings = $rootScope.settings;
var srv_cur = new Date(Date.now() + (time.cu - time.he));
if (Math.floor(settings.start / 1000) == Math.floor(srv_cur / 1000)) {
$rootScope.refresh(true);
}
if (time.st > 0 && time.st <= srv_cur) {
remain = time.st + time.du - srv_cur;
var remain = 0;
if (settings.start == 0) {
$rootScope.time = {};
return
} else if (settings.start > srv_cur) {
$rootScope.startIn = Math.floor((settings.start - srv_cur) / 1000);
remain = settings.end - settings.start;
} else if (settings.end > srv_cur) {
$rootScope.startIn = 0;
remain = settings.end - srv_cur;
}
remain = remain / 1000;
if (remain < 0) {
remain = 0;
$rootScope.time.end = true;
@ -67,8 +87,7 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
$rootScope.time.end = false;
$rootScope.time.expired = false;
}
$rootScope.time.start = time.st * 1000;
$rootScope.time.duration = time.du;
$rootScope.time.remaining = remain;
$rootScope.time.hours = Math.floor(remain / 3600);
$rootScope.time.minutes = Math.floor((remain % 3600) / 60);
@ -95,13 +114,12 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
if (!justMy) {
$timeout.cancel($scope.cbr);
$scope.cbr = $timeout($rootScope.refresh, 42000);
$http.get("/time.json").then(function(response) {
var time = response.data;
time.he = (new Date()).getTime();
sessionStorage.userService = angular.toJson(time);
});
$http.get("/settings.json").then(function(response) {
$scope.settings = response.data;
$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);
$rootScope.settings = response.data;
});
$http.get("/themes.json").then(function(response) {
$scope.themes = response.data;