Demande de crêpe par ${prankData[entry].creator}
Quantité: ${prankData[entry].amount}
Garniture: ${prankData[entry].supplement}
Livraison: ${prankData[entry].where}
Livraison: ${prankData[entry].note}
${(prankData[entry].state == "Pending") ? "Accepter" : "Récupérer (accepté par: "+prankData[entry].manageBy+")"}
Done
Refuser
`)
} else if(prankData[entry].state == "done") {
doneCnt++;
}
break;
case "kidnap":
// Not Handle
break;
}
}
$("#admin-content").append(`
Demande de vérification par ${treasureData[entry].creator}
Activité: ${treasureData[entry].activity.title}
Description: ${treasureData[entry].desc}
Accepter
Refuser
`)
}
}
break;
case "activity":
$("#admin-content").append(`
Event:
Description:
Lieu:
Début:
Fin:
Update
Supprimer
`)
} else if (activityData[entry].type = "treasure") {
$("#admin-content").prepend(`
Trésor:
Description:
Lieu:
Début:
Status: ${activityData[entry].treasureState}
Update
Supprimer
`)
}
}
break;
}
refreshListener();
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
}
function refreshListener() {
$(".prank-btn-accept").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "acceptPrank",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
prankUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
let btnElems = $("#"+uid).children()[6];
btnElems.children[0].style.display = "none";
btnElems.children[1].style.display = "flex";
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
})
$(".prank-btn-refused").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "refusePrank",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
prankUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$("#"+uid).remove();
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
})
$(".prank-btn-done").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "donePrank",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
prankUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$("#"+uid).remove();
$("#prankDone")[0].innerHTML = "Nombre totale de mission achevées: " + (parseInt($("#prankDone")[0].innerHTML.replace("Nombre totale de mission achevées: ", "")) + 1);
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
})
$(".treasure-btn-accept").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "acceptTreasure",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
treasureUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$("#"+uid).remove();
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
})
$(".treasure-btn-refused").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "refuseTreasure",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
treasureUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$("#"+uid).remove();
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
})
$(".activity-btn-update").click((e) => {
let uid = e.target.parentNode.parentNode.id;
let children = e.target.parentNode.parentNode.children;
$.ajax({
type: "POST",
url: base_url + "addActivity",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
activityUid: uid,
type: (children[0].innerHTML.substring(0, 5) == "Event") ? "event" : "treasure",
title: children[0].children[0].value,
desc: children[1].children[0].value,
where: children[2].children[0].value,
start: new Date(children[3].children[0].value).toString(),
end: (children[3].children.length == 0) ? null : new Date(children[4].children[0].value).toString(),
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
alert("Update réussie")
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
})
$(".activity-btn-delete").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "delActivity",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
activityUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$("#"+uid).remove();
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
})
$("#activityType").on("change", function() {
if (this.value == "treasure") {
$("#activity-end-add").css("display", "none");
} else if (this.value == "event") {
$("#activity-end-add").css("display", "flex");
}
})
$("#activity-btn-add").click((e) => {
let children = e.target.parentNode.parentNode.children;
$.ajax({
type: "POST",
url: base_url + "addActivity",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
type: $("#activityType").val(),
title: children[2].children[0].value,
desc: children[3].children[0].value,
where: children[4].children[0].value,
start: new Date(children[5].children[0].value).toString(),
end: ($("#activityType").val() == "treasure") ? null : new Date(children[6].children[0].value).toString(),
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
get_admin("activity");
} else {
if (data.why == "Not authentificated") {
show_page('#login-page');
} else {
alert(data.why);
}
}
}
});
})
}
$('#demande-button').click(function () {
$.ajax({
type: "POST",
url: base_url + "addPrank",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
type: "crêpe",
amount: $('#demande-amount').val(),
where: $('#demande-where').val(),
supplement: $('#demande-supp').val(),
note: $('#demande-note').val(),
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
if(data.success) {
$('#demande-form input').val("");
updateDemandes();
} else {
$('#error-message-demande').empty();
t = new TypeIt('#error-message-demande', {
speed: 100,
lifeLike: true
})
.type(data.why)
.go();
if(data.why.startsWith('Token expired') || data.why.startsWith('Not authen'))
show_page("#login-page");
}
}
});
});
function updateDemandes() {
$.ajax({
type: "POST",
url: base_url + "get",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
type: "prank"
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$('#demande-list').html('');
let pd = data.prankData;
for(const pd_uid in pd) {
$('#demande-list').append(`
Demande de crêpe
Quantité: ${pd[pd_uid].amount}
Garniture: ${pd[pd_uid].supplement}
Livraison: ${pd[pd_uid].where}
Notes: ${pd[pd_uid].note}
Statue: ${pd[pd_uid].state}
`)
}
}
else {
console.alert(data.why);
}
}
});
}
function updateNextActivity() {
$.ajax({
type: "POST",
url: base_url + "get",
data: JSON.stringify({
token: "",
uid: "",
type: "activity",
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: (data) => {
if(data.success) {
let curr_date = new Date();
let ad = data.activityData;
var candidate = [];
var next_acti = {title: "Pas encore d'activité", desc: "Encore aucune activité n'a été renseigné, faite le vite !"};
for(const uid in ad) {
if(ad[uid].type == "event" && curr_date < new Date( ad[uid].start )) {
candidate.push(ad[uid]);
}
}
next_acti = candidate.reduce(function(prev, curr) {
return new Date(prev.start) < new Date(curr.Cost) ? prev : curr;
});
var opt = {speed: 100};
new TypeIt("#activite-title", opt)
.type(next_acti.title)
.go();
new TypeIt("#activite-desc", opt)
.type(next_acti.desc)
.go();
new TypeIt("#activite-place", opt)
.type("Lieu : " + next_acti.where)
.go();
let diff = Math.ceil( (new Date(next_acti.start) - new Date()) / 1000 )
let days = Math.trunc(diff / (3600*24) )
let h = Math.trunc(diff / 3600) - days*24
let m = Math.trunc(diff / 60) - (h + days*24)*60
let s = diff - h*3600 - m*60 - days*24*3600
$('#timer').text(days + " jours " + (new Date('December 17, 1995 ' + [h, m, s].join(':'))).toLocaleTimeString('default', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}));
}
}
})
}
$(window).on("load", function() {
/* Register the Service Worker */
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./js/sw.js');
}
for(i in page) {
$(page[i]).css('z-index', page.length - i);
}
updateNextActivity();
updateDemandes();
/* load timer */
window.setInterval(function() {
var b = $("#timer").text().split(' jours ');
var time = b[1];
var days = b[0];
var t = time.split(":").map(t => parseInt(t));
t[2] -= 1;
if (t[2] < 0) {
t[2] = 59;
t[1] -= 1;
}
if (t[1] < 0) {
t[1] = 59;
t[0] -= 1;
}
if (t[0] < 0) {
t[0] = 23;
t[1] = 59;
t[2] = 59;
days -= 1;
}
if (days < 0) {
t[0] = 0;
t[1] = 0;
t[2] = 0;
days = 0;
updateNextActivity();
}
$('#timer').text(days + " jours " +(new Date('December 17, 1995 ' + t.join(':'))).toLocaleTimeString('default', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}));
}, 1000);
});
function toIsoString(date) {
var tzo = -date.getTimezoneOffset(),
dif = tzo >= 0 ? '+' : '-',
pad = function(num) {
return (num < 10 ? '0' : '') + num;
};
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds());
}