Main timer logic, socketio server and backend endpoints
This commit is contained in:
parent
43f355765a
commit
093c8c6e24
7
scoreboard/controllers/socketio.js
Normal file
7
scoreboard/controllers/socketio.js
Normal file
@ -0,0 +1,7 @@
|
||||
const { Server } = require('socket.io');
|
||||
|
||||
const io = new Server(3001, {
|
||||
cors: { origin: "*" }
|
||||
});
|
||||
|
||||
module.exports = io
|
||||
60
scoreboard/controllers/timer.js
Normal file
60
scoreboard/controllers/timer.js
Normal file
@ -0,0 +1,60 @@
|
||||
const moment = require('moment');
|
||||
const io = require('./socketio')
|
||||
|
||||
let timerInterval;
|
||||
// let duration = moment.duration(7, 'minutes'); // Initial duration
|
||||
// let durationLeft = moment.duration(7, 'minutes'); // durationLeft after the timer has been started
|
||||
let duration = moment.duration(7, 'seconds');
|
||||
let durationLeft = moment.duration(10, 'seconds');
|
||||
|
||||
let isPaused = true; // Status of the timer
|
||||
|
||||
// Function prototypes
|
||||
function start() {};
|
||||
function pause() {};
|
||||
function reset() {};
|
||||
function end() {};
|
||||
function print() {};
|
||||
|
||||
|
||||
function start() {
|
||||
if(isPaused == true)
|
||||
{
|
||||
isPaused = false
|
||||
|
||||
timerInterval = setInterval(() => { // Create Intervalfunction every 1000ms
|
||||
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
|
||||
end()
|
||||
durationLeft.subtract(1, 'second'); // Subtract a second from from the timer
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function pause() {
|
||||
isPaused = true; // Set status of the timer
|
||||
clearInterval(timerInterval); // End the execution
|
||||
}
|
||||
|
||||
function reset() {
|
||||
durationLeft = duration.clone(); // Set durationLeft to the initial duration
|
||||
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
|
||||
}
|
||||
|
||||
function end() {
|
||||
isPaused = true // Set status of the timer
|
||||
clearInterval(timerInterval); // End the execution
|
||||
io.sockets.emit('timerEnded', print()) // Emit end to all connected sockets
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
duration, durationLeft, isPaused, start, pause, reset, end, print
|
||||
};
|
||||
@ -1,9 +1,26 @@
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
let io = require('../controllers/socketio')
|
||||
const timer = require('../controllers/timer')
|
||||
|
||||
|
||||
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function(req, res, next) {
|
||||
res.render('index', { title: 'Express' });
|
||||
res.render('index', { durationLeft: timer.print()});
|
||||
});
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log("A user connected");
|
||||
socket.emit("Hello user from server");
|
||||
|
||||
socket.on('message', (message) => {
|
||||
console.log(message)
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user