-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (93 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const express = require('express');
const { createServer } = require('node:http');
const fs = require("fs");
const { Server } = require('socket.io');
let PORT = process.env.PORT || 3001;
const app = express();
const server = createServer(app);
const io = new Server(server);
app.set('view engine', 'ejs');
let timer = 600;
let timA = "";
let scorea = 0;
let falla = 0;
let timB = "";
let scoreb = 0;
let fallb = 0;
let timerCount = null;
io.on('connection', (socket) => {
socket.on("timerStart", () => {
console.log("TIMER START");
timerCount = setInterval(() => {
if (timer > 0) {
timer--;
io.emit("countdown", timer);
writeFile(secondToMinute(timer), "Timer.txt");
} else clearInterval(timerCount);
}, 1000);
});
socket.on("timerStop", () => {
console.log("TIMER STOP");
clearInterval(timerCount);
});
socket.on("timer", (sec) => {
timer = sec;
io.emit("timer", sec)
writeFile(secondToMinute(sec), "Timer.txt");
});
socket.on("scorea", (score) => {
scorea = score;
io.emit("scorea", score)
writeFile(score, "Tim A Score.txt");
});
socket.on("scoreb", (score) => {
scoreb = score;
io.emit("scoreb", score)
writeFile(score, "Tim B Score.txt");
});
socket.on("falla", (fall) => {
falla = fall;
io.emit("falla", fall)
writeFile(fall, "Tim A Fall.txt");
});
socket.on("fallb", (fall) => {
fallb = fall;
io.emit("fallb", fall)
writeFile(fall, "Tim B Fall.txt");
});
socket.on("tima", (tim) => {
timA = tim;
io.emit("tima", tim)
writeFile(timA, "Tim A.txt");
});
socket.on("timb", (tim) => {
timB = tim;
io.emit("timb", tim)
writeFile(timB, "Tim B.txt");
});
});
app.get("/", (req, res) => {
res.render("index", { timer, isStart: !!timerCount, scorea, scoreb, falla, fallb, timA, timB });
});
server.listen(PORT, () => {
console.log(`Server running on port: ${PORT}`);
});
server.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
console.error('Address in use, retrying...');
setTimeout(() => {
server.close();
PORT++;
server.listen(PORT);
}, 1000);
}
});
function writeFile(value, pathFile) {
fs.writeFileSync(`./obs/${pathFile}`, value)
}
function secondToMinute(second) {
return zeroPad(Math.floor(timer / 60)) + ":" + zeroPad(Math.floor(timer % 60))
}
function zeroPad(num) {
return num.toString().padStart(2, "0");
}