server/frontend/static/js/challenge.js

488 lines
15 KiB
JavaScript

angular.module("FICApp", ["ngRoute", "ngSanitize"])
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("/rules", {
controller: "HomeController",
templateUrl: "views/rules.html"
})
.when("/edit", {
controller: "MyTeamController",
templateUrl: "views/team-edit.html"
})
.when("/rank", {
controller: "RankController",
templateUrl: "views/rank.html"
})
.when("/register", {
controller: "RegisterController",
templateUrl: "views/register.html"
})
.when("/videos", {
controller: "VideosController",
templateUrl: "views/videos.html"
})
.when("/:theme", {
controller: "ExerciceController",
templateUrl: "views/theme.html"
})
.when("/:theme/:exercice", {
controller: "ExerciceController",
templateUrl: "views/theme.html"
})
.when("/", {
controller: "HomeController",
templateUrl: "views/home.html"
})
.otherwise({
redirectTo: "/"
});
$locationProvider.html5Mode(true);
})
.run(function($rootScope, $interval) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.current_exercice_my = 0;
$rootScope.time = {};
$('[data-toggle="popover"]').popover();
function updTime() {
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)) {
$rootScope.refresh(true);
}
if (time.st > 0 && time.st <= srv_cur) {
remain = time.st + time.du - srv_cur;
}
if (remain < 0) {
remain = 0;
$rootScope.time.end = true;
$rootScope.time.expired = true;
} else if (remain < 60) {
$rootScope.time.end = false;
$rootScope.time.expired = true;
} else {
$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);
$rootScope.time.seconds = Math.floor(remain % 60);
}
}
updTime();
$interval(updTime, 1000);
})
.controller("DataController", function($sce, $scope, $http, $rootScope, $timeout, $location) {
var actMenu = function() {
if ($scope.my && $scope.themes) {
angular.forEach($scope.themes, function(theme, key) {
$scope.themes[key].exercice_solved = 0;
angular.forEach(theme.exercices, function(exercice, k) {
if ($scope.my.exercices && $scope.my.exercices[k] && $scope.my.exercices[k].solved) {
$scope.themes[key].exercice_solved++;
}
});
});
}
}
$rootScope.refresh = function(justMy) {
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;
});
$http.get("/themes.json").then(function(response) {
$scope.themes = response.data;
$scope.max_gain = 0;
$scope.max_solved = 0;
$scope.themesUrl = {};
$scope.exercicesUrl = {};
angular.forEach(response.data, function(theme, key) {
$scope.themesUrl[theme.urlid] = key;
this[key].exercice_count = Object.keys(theme.exercices).length;
this[key].exercice_coeff_max = 0;
this[key].gain = 0;
this[key].solved = 0;
angular.forEach(theme.exercices, function(ex, k) {
$scope.exercicesUrl[ex.urlid] = k;
this.gain += ex.gain;
this.solved += ex.solved;
this.exercice_coeff_max = Math.max(this.exercice_coeff_max, ex.curcoeff);
}, theme);
$scope.max_gain += theme.gain;
$scope.max_solved = Math.max($scope.max_solved, theme.solved);
}, response.data);
actMenu();
});
$http.get("/teams.json").then(function(response) {
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;
this.push(team);
}, $scope.rank);
});
}
$http.get("/my.json").then(function(response) {
$scope.my = response.data;
angular.forEach($scope.my.exercices, function(exercice, eid) {
exercice.solved = exercice.solved_rank > 0;
if (exercice.video_uri) {
exercice.video_uri = $sce.trustAsResourceUrl(exercice.video_uri);
}
});
actMenu();
if ($scope.my.team_id == 0) {
angular.forEach($scope.my.exercices, function(exercice, eid) {
angular.forEach(exercice.hints, function(hint, hid) {
$scope.my.exercices[eid].hints[hid].hidden = true;
});
});
}
}, function(response) {
if (!$scope.my && response.status == 404) {
$location.url("/register");
}
});
console.log("refresh!");
}
$rootScope.refresh();
})
.controller("ExerciceController", function($scope, $routeParams, $http, $rootScope, $timeout) {
$rootScope.current_theme = $scope.themesUrl[$routeParams.theme];
if ($routeParams.exercice) {
$rootScope.current_exercice = $scope.exercicesUrl[$routeParams.exercice];
} else {
if ($scope.themes && $scope.my && $scope.themes[$scope.current_theme]) {
var exos = $scope.themes[$scope.current_theme].exercices;
var i = 0;
for (; i < exos.length; i++) {
if (!$scope.my.exercices || !$scope.my.exercices[exos[i].id] || !$scope.my.exercices[exos[i].id].solved)
break;
}
if (i < exos.length) {
$rootScope.current_exercice = i;
} else {
$rootScope.current_exercice = 0;
}
} else {
$rootScope.current_exercice = 0;
}
}
$rootScope.current_exercice_my = $scope.themes[$scope.current_theme].exercices[$rootScope.current_exercice].id;
$scope.hsubmit = function(hint) {
hint.submitted = true;
$http({ url: "/openhint/" + $rootScope.current_exercice_my, method: "POST", data: { id: hint.id } }).then(function(response, status, header, config) {
var checkDiffHint = function() {
$http.get("/my.json").then(function(response) {
var my = response.data;
angular.forEach(my.exercices[$rootScope.current_exercice_my].hints, function(h,hid){
if (hint.id == h.id) {
if (hint.content != h.content) {
$rootScope.refresh();
} else {
$timeout.cancel($scope.cbh);
$scope.cbh = $timeout(checkDiffHint, 750);
}
}
});
});
};
checkDiffHint();
}, function(response, status, header, config) {
hint.submitted = false;
console.error(response.data.errmsg);
});
};
})
.controller("SubmissionController", function($scope, $http, $rootScope, $timeout) {
$scope.flags = [];
$rootScope.sberr = "";
var waitMy = function() {
if (!$scope.my || !$scope.my.exercices || !$scope.my.exercices[$rootScope.current_exercice_my]) {
$timeout.cancel($scope.cbs);
$scope.cbs = $timeout(waitMy, 420);
} else {
$scope.flags = [];
angular.forEach($scope.my.exercices[$rootScope.current_exercice_my].keys, function(key,kid) {
var o = {
id: kid,
name: key,
value: ""
};
if ($scope.my.exercices[$rootScope.current_exercice_my].solved_matrix != null)
o.found = $scope.my.exercices[$rootScope.current_exercice_my].solved_matrix[kid];
this.push(o);
}, $scope.flags);
$scope.mcqs = [];
angular.forEach($scope.my.exercices[$rootScope.current_exercice_my].mcqs, function(mcq,qid) {
var o = {
title: mcq.title,
kind: mcq.kind,
solved: mcq.solved,
choices: {}
};
angular.forEach(mcq["choices"], function(choice,cid) {
this[cid] = {
label: choice,
value: false
};
}, o["choices"]);
this.push(o);
}, $scope.mcqs);
}
}
waitMy();
$scope.ssubmit = function() {
var resp = {}
if ($scope.flags && $scope.flags.length)
{
resp["flags"] = {};
angular.forEach($scope.flags, function(flag,kid) {
resp["flags"][flag.name] = flag.value;
});
}
if ($scope.mcqs && $scope.mcqs.length)
{
resp["mcqs"] = {};
angular.forEach($scope.mcqs, function(mcq) {
angular.forEach(mcq.choices, function(choice, cid) {
if (choice.value) resp["mcqs"][cid] = choice.value;
})
});
}
$http({ url: "/submit/" + $rootScope.current_exercice_my, method: "POST", data: resp }).then(function(response, status, header, config) {
$rootScope.messageClass = {"text-success": true};
$rootScope.message = response.data.errmsg;
$rootScope.sberr = "";
angular.forEach($scope.flags, function(flag,kid) {
flag.value = "";
});
var checkDiff = function() {
$http.get("/my.json").then(function(response) {
var my = response.data;
if ($scope.my.exercices[$rootScope.current_exercice_my].tries != my.exercices[$rootScope.current_exercice_my].tries || $scope.my.exercices[$rootScope.current_exercice_my].solved_time != my.exercices[$rootScope.current_exercice_my].solved_time) {
$rootScope.refresh();
$scope.my = my;
waitMy();
} else {
$timeout.cancel($scope.cbd);
$scope.cbd = $timeout(checkDiff, 750);
}
});
};
checkDiff();
}, function(response, status, header, config) {
if (status >= 500) {
$scope.my.exercices[$rootScope.current_exercice_my].submitted = false;
}
$rootScope.messageClass = {"text-danger": true};
$rootScope.message = response.data.errmsg;
if (status != 402) {
$rootScope.sberr = "Une erreur est survenue lors de l'envoi. Veuillez réessayer dans quelques instants.";
}
});
$scope.my.exercices[$rootScope.current_exercice_my].submitted = true;
};
})
.controller("MyTeamController", function($scope, $http, $rootScope, $timeout) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.current_exercice_my = 0;
if ($scope.my) {
$rootScope.title = $scope.my.name;
$rootScope.authors = $scope.my.members.map(function (cur) {
return cur.firstname.capitalize() + " " + cur.lastname.capitalize();
}).join(", ");
}
$scope.newName = "";
$rootScope.message = "";
$rootScope.sberr = "";
$scope.tsubmit = function() {
$rootScope.sberr = "";
if ($scope.newName.length < 1) {
$rootScope.messageClass = {"text-danger": true};
$rootScope.sberr = "Nom d'équipe invalide: pas d'entrée.";
return false;
}
else if ($scope.newName.length > 32) {
$rootScope.messageClass = {"text-danger": true};
$rootScope.sberr = "Nom d'équipe invalide: pas plus de 32 caractères.";
return false;
}
else if (!$scope.newName.match(/^[A-Za-z0-9 àéèêëîïôùûü_-]+$/)) {
$rootScope.messageClass = {"text-danger": true};
$rootScope.sberr = "Nom d'équipe invalide: seuls les caractères alpha-numériques sont autorisés.";
return false;
}
$http({
url: "/submit/name",
method: "POST",
data: {newName: $scope.newName}
}).then(function(response, status, header, config) {
$rootScope.messageClass = {"text-success": true};
$rootScope.message = response.data.errmsg;
var checkDiff = function() {
$http.get("/my.json").then(function(response) {
if ($scope.my.name != response.data.name) {
$scope.newName = "";
$rootScope.message = "";
$rootScope.refresh();
} else {
$timeout.cancel($scope.cbt);
$scope.cbt = $timeout(checkDiff, 750);
}
});
};
checkDiff();
}, function(response, status, header, config) {
$rootScope.messageClass = {"text-danger": true};
$rootScope.message = response.data.errmsg;
if (status != 402) {
$rootScope.sberr = "Une erreur est survenue lors de l'envoi. Veuillez réessayer dans quelques instants.";
}
});
};
})
.controller("RegisterController", function($scope, $rootScope, $location, $http) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.current_exercice_my = 0;
$rootScope.title = "Bienvenue au challenge forensic !";
$rootScope.authors = null;
$scope.members = [{}];
$scope.AddMember = function() {
$scope.members.push({});
}
$scope.RemoveMember = function(k) {
$scope.members.splice(k, 1);
}
$scope.Validate = function() {
if ($scope.teamName.length <= 3) {
$('#teamName').addClass("is-invalid")
return;
} else {
$('#vldBtn').removeClass("input-group-btn");
$('#vldBtn').css("display", "none");
$scope.part2 = true;
}
}
$scope.rsubmit = function() {
if (!$scope.part2)
return $scope.Validate();
// Remove empty members
$scope.members = $scope.members.filter(function(m) {
return ((m.lastname != undefined && m.lastname != "") || (m.firstname != undefined && m.firstname != "") || (m.nickname != undefined && m.nickname != ""));
});
if ($scope.members.length == 0) {
$scope.messageClass = {"text-danger": true};
$scope.message = "Veuillez ajouter au moins un membre dans votre équipe !";
$scope.members.push({});
return;
}
$http({
url: "/registration",
method: "POST",
data: {
teamName: $scope.teamName,
members: $scope.members,
}
}).then(function(response, status, header, config) {
$scope.messageClass = {"text-success": true};
$scope.message = response.data.errmsg;
$rootScope.refresh();
if ($scope.my)
$location.url("/");
}, function(response) {
$scope.messageClass = {"text-danger": true};
console.log(response);
if (response.data && response.data.errmsg)
$scope.message = response.data.errmsg;
else
$scope.message = "Une erreur est survenue lors de l'inscription de l'équipe. Veuillez réessayer dans quelques instants.";
});
}
if ($scope.my) {
$location.url("/");
}
})
.controller("RankController", function($scope, $rootScope) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.current_exercice_my = 0;
$rootScope.title = "Classement général";
$rootScope.authors = "";
$scope.fields = ["rank", "name", "score"];
$scope.rankOrder = "rank";
$scope.reverse = false;
$scope.order = function(fld) {
if ($scope.rankOrder == fld) {
$scope.reverse = !$scope.reverse;
} else {
$scope.rankOrder = fld;
$scope.reverse = (fld == "score");
}
};
})
.controller("VideosController", function($scope, $rootScope) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.current_exercice_my = 0;
$rootScope.title = "Vidéos de résolution";
$rootScope.authors = "";
})
.controller("HomeController", function($scope, $rootScope) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.current_exercice_my = 0;
$rootScope.title = "";
$rootScope.authors = "";
});
function sready() {
if ($("#solution").val().length) {
$("#sbmt").removeClass("disabled");
} else {
$("#sbmt").addClass("disabled");
}
};