-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
69 lines (54 loc) · 1.96 KB
/
app.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
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const Streams = require('./Streams.class')();
const Scheduler = require('./Scheduler.class')(Streams);
const VisitHistory = require('./VisitHistory.class')();
const db = require('./DB.class');
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.on('initSession', async (eSession) => {
if (!eSession.session) {
socket.emit('initSession', await VisitHistory.generateNewSession());
}
});
socket.on('getFirstData', () => {
socket.emit('getFirstData', {
streamersCount: Streams.streamsCount(),
});
});
socket.on('getGames', () => {
socket.emit('getGames', Streams.getGames());
});
socket.on('getStreams', () => {
socket.emit('getStreams', Streams.streams);
});
socket.on('pickRandom', async (obj) => {
let pickedStream = Streams.pickRandomStream(obj.games);
VisitHistory.newHistoryRow(obj.session, pickedStream.user_name);
socket.emit('pickRandom', pickedStream);
});
socket.on('streamsCount', (games) => {
socket.emit('streamsCount', Streams.getStreamsCount(games));
});
socket.on('getStats', async (obj) => {
socket.emit('getStats', await Scheduler.getWeeksStats(obj.token, obj.week));
});
socket.on('getStatsWeeks', async (obj) => {
socket.emit('getStatsWeeks', await Scheduler.getStatsWeeks(obj.token));
});
socket.on('getVisits', async (obj) => {
socket.emit('getVisits', await VisitHistory.getVisits(obj.session));
});
});
http.listen(3000, function () {
console.log('listening on *:3000'.red);
Streams.onFetchFinished(() => {
io.emit('newFetch', {
streamersCount: Streams.streamsCount(),
});
io.emit('getGames', Streams.getGames());
});
});