Added important admin frontend functions with error handling, the corresponding api endpoints, and the timer logic
This commit is contained in:
parent
340ab1236e
commit
536c1272d8
@ -16,26 +16,35 @@ function print() {};
|
||||
|
||||
|
||||
function start() {
|
||||
if(isPaused == true)
|
||||
{
|
||||
isPaused = false
|
||||
if(isPaused == true && durationLeft.asSeconds() != 0) { // Only allow start if timer ist currently paused and durationLeft is not 0 seconds
|
||||
console.log("Timer gestartet")
|
||||
isPaused = false // Set status of the timer
|
||||
|
||||
timerInterval = setInterval(() => { // Create Intervalfunction every 1000ms
|
||||
durationLeft.subtract(1, 'second'); // Subtract a second from from the timer
|
||||
console.log(print()); // Print durationLeft
|
||||
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
|
||||
if(durationLeft.minutes() == 0 && durationLeft.seconds() == 0) // End timer if durationLeft == 00:00
|
||||
if(durationLeft.asSeconds() == 0) // End timer if durationLeft == 00:00
|
||||
end()
|
||||
durationLeft.subtract(1, 'second'); // Subtract a second from from the timer
|
||||
}, 1000);
|
||||
}
|
||||
return true
|
||||
} else
|
||||
return false
|
||||
}
|
||||
|
||||
function pause() {
|
||||
isPaused = true; // Set status of the timer
|
||||
clearInterval(timerInterval); // End the execution
|
||||
if(!isPaused) {
|
||||
console.log("Timer pausiert");
|
||||
isPaused = true; // Set status of the timer
|
||||
clearInterval(timerInterval); // End the execution
|
||||
return true
|
||||
} else
|
||||
return false
|
||||
}
|
||||
|
||||
function reset() {
|
||||
function reset(newDuration) {
|
||||
console.log("Timer Zurückgesetzt");
|
||||
duration = moment.duration(newDuration, 'seconds').clone(); // Set initial duration to received duration in seconds
|
||||
durationLeft = duration.clone(); // Set durationLeft to the initial duration
|
||||
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
|
||||
}
|
||||
@ -46,8 +55,7 @@ function end() {
|
||||
io.sockets.emit('timerEnded', print()) // Emit end to all connected sockets
|
||||
}
|
||||
|
||||
function print()
|
||||
{
|
||||
function print() {
|
||||
var minutes = (durationLeft.minutes() < 10) ? "0" + durationLeft.minutes() : durationLeft.minutes(); // Create leading zeros for numbers <10
|
||||
var seconds = (durationLeft.seconds() < 10) ? "0" + durationLeft.seconds() : durationLeft.seconds(); // Create leading zeros for numbers <10
|
||||
return minutes + ":" + seconds
|
||||
|
||||
@ -1,11 +1,66 @@
|
||||
function updateFrontend(values) {
|
||||
values.duration = moment.duration(values.duration) // Create moment object from ISO 8601 string
|
||||
values.durationLeft = moment.duration(values.durationLeft) // Create moment object from ISO 8601 string
|
||||
document.getElementById("durationLeft").innerHTML = values.print // Display durationLeft as prettyfied string
|
||||
|
||||
function timerStart()
|
||||
{
|
||||
fetch("/admin/timerStart", {
|
||||
method: "POST",
|
||||
headers: {'Content-Type': 'text/html'},
|
||||
body: ""
|
||||
}).then(res => {
|
||||
console.log(res);
|
||||
})
|
||||
if(values.isPaused) { // Set button state depending on timer status
|
||||
document.getElementById("timerStartBtn").disabled = false
|
||||
document.getElementById("timerPauseBtn").disabled = true
|
||||
} else {
|
||||
document.getElementById("timerStartBtn").disabled = true
|
||||
document.getElementById("timerPauseBtn").disabled = false
|
||||
}
|
||||
}
|
||||
|
||||
async function initialUpdate() {
|
||||
const response = await fetch("/admin/timerGetValues"); // Call API Endpoint /admin/timerGetValues
|
||||
const data = await response.json(); // Wait for asyncronous transfer to complete and parse json (which got received by backend)
|
||||
console.log(data); // Print received data
|
||||
updateFrontend(data); // Update admin frontend with received values
|
||||
}
|
||||
|
||||
// Timerfunctions
|
||||
async function timerStart() {
|
||||
const response = await fetch("/admin/timerStart"); // Call API Endpoint /admin/timerStart
|
||||
if(response.status == 200) {
|
||||
const data = await response.json(); // Wait for asyncronous transfer to complete and parse json (which got received by backend)
|
||||
console.log(data); // Print received data
|
||||
updateFrontend(data); // Update admin frontend with received values
|
||||
} else {
|
||||
//console.log("Error, Timer bereits abgelaufen: ", response.status);
|
||||
const timerStartErrorToast = bootstrap.Toast.getOrCreateInstance(document.getElementById("timerStartErrorToast")); // Create toast instance byId timerStartErrorToast
|
||||
timerStartErrorToast.show(); // Show error toast
|
||||
}
|
||||
}
|
||||
|
||||
async function timerPause() {
|
||||
const response = await fetch("/admin/timerPause"); // Call API Endpoint /admin/timerStart
|
||||
if(response.status == 200) {
|
||||
const data = await response.json(); // Wait for asyncronous transfer to complete and parse json (which got received by backend)
|
||||
console.log(data); // Print received data
|
||||
updateFrontend(data); // Update admin frontend with received values
|
||||
} else {
|
||||
//console.log("Error, Timer bereits pausiert: ", response.status);
|
||||
const timerPauseErrorToast = bootstrap.Toast.getOrCreateInstance(document.getElementById("timerPauseErrorToast")); // Create toast instance byId timerStartErrorToast
|
||||
timerPauseErrorToast.show(); // Show error toast
|
||||
}
|
||||
}
|
||||
|
||||
async function timerReset() {
|
||||
let newDuration = document.getElementById("timerSelectDuration").value;
|
||||
const response = await fetch("/admin/timerReset", { // Call API Endpoint /admin/timerStart with selected new duration in seconds
|
||||
method: 'POST',
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({duration: newDuration})
|
||||
});
|
||||
const data = await response.json(); // Wait for asyncronous transfer to complete and parse json (which got received by backend)
|
||||
console.log(data); // Print received data
|
||||
updateFrontend(data); // Update admin frontend with received values
|
||||
}
|
||||
|
||||
// Scoreboardfunctions
|
||||
async function scoreSwitch() {
|
||||
const response = await fetch("/admin/scoreSwitch"); // Call API Endpoint /admin/timerStart
|
||||
const data = await response.json(); // Wait for asyncronous transfer to complete and parse json (which got received by backend)
|
||||
console.log(data); // Print received data
|
||||
}
|
||||
@ -3,27 +3,45 @@ var router = express.Router();
|
||||
|
||||
let timer = require('../controllers/timer');
|
||||
|
||||
router.post('/timerStart', function(req, res, next) {
|
||||
console.log("Timer gestartet");
|
||||
timer.start()
|
||||
res.render('admin');
|
||||
// Express router entpoint to start the timer
|
||||
router.get('/timerStart', function(req, res, next) {
|
||||
if(timer.start()) { // If successfully started the timer
|
||||
res.status(200); // Set http status code to 200 (success)
|
||||
res.json(timer.getValues()); // Respond with all important values to update the frontend
|
||||
} else { // If not successfull, for example because timer is already running or not time is left
|
||||
res.status(406); // Set http status code to 406 (not acceptable)
|
||||
res.send(); // Respond with empty payload
|
||||
}
|
||||
});
|
||||
|
||||
// Express router endpoint to pause the timer
|
||||
router.get('/timerPause', function(req, res, next) {
|
||||
if(timer.pause()) { // If successfully paused the timer
|
||||
res.status(200); // Set http status code to 200 (success)
|
||||
res.json(timer.getValues()); // Respond with all important values to update the frontend
|
||||
} else { // If not successfull, for example if timer already is paused
|
||||
res.status(406); // Set http status code to 406 (not acceptable)
|
||||
res.send(); // Respond with empty payload
|
||||
}
|
||||
});
|
||||
|
||||
// Express router endpoint to reset the timer
|
||||
router.post('/timerReset', function(req, res, next) {
|
||||
console.log("Timer Zurückgesetzt");
|
||||
timer.reset();
|
||||
res.render('admin');
|
||||
timer.reset(req.body.duration); // Reset the timer with the duration in seconds received from the request
|
||||
res.json(timer.getValues()); // Respond with all important values to update the frontend
|
||||
});
|
||||
|
||||
// Express router endpoint to switch the score after halftime
|
||||
router.get('/timerGetValues', function(req, res, next) {
|
||||
res.json(timer.getValues()); // Respond with important values for frontend
|
||||
});
|
||||
|
||||
router.post('/timerPause', function(req, res, next) {
|
||||
console.log("Timer pausiert");
|
||||
timer.pause();
|
||||
res.render('admin');
|
||||
});
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function(req, res, next) {
|
||||
res.render('admin', {});
|
||||
res.render('admin');
|
||||
});
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user