-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
110 lines (92 loc) · 2.81 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
var express = require('express');
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var timer = require('./public/shuffle.js');
app.use(express.static('public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
var port = process.env.PORT || 5000;
http.listen(port, function () {
console.log('listening on *:5000');
});
// Socket connection as well as data transmission happens below:
var totalCoder = 0;
var currentCoders = [];
var code = '';
io.on('connection', function (socket) {
// to properly calculate total nums:
var addedUser = false;
var coder = {
name: '',
};
// connects and registers name
socket.on('userName', function (in_name) {
if (addedUser) return;
addCoder();
addedUser = true;
coder.name = in_name;
currentCoders.push(in_name);
io.sockets.emit('update', {
num: totalCoder,
who: currentCoders,
news: in_name, // updates on who joined
});
});
// when client pushes code, it's stored in the global code
socket.on('pushCode', function (data) {
console.log(`${socket.id} pushed code.`);
code = data;
io.sockets.emit('consoleUpdate', { person: coder.name, action: 'push' });
});
// when coders pull, it't updated to that one client only
socket.on('pullCode', function (data) {
console.log(`${socket.id} pulled update.`);
io.to(socket.id).emit('update_global', code);
io.sockets.emit('consoleUpdate', { person: coder.name, action: 'pull' });
});
// force updates to all
socket.on('updateAll', function (data) {
console.log(`${socket.id} updated to all users.`);
code = data;
io.sockets.emit('update_global', code);
io.sockets.emit('consoleUpdate', {
person: coder.name,
action: 'push_all',
});
});
// reactions:
socket.on('reaction', function (data) {
io.sockets.emit('reactions', { person: coder.name, reaction: data });
});
// Timer
socket.on('startTimedPlay', function (data) {
console.log(timer.shuffle(currentCoders));
var shuffle = timer.shuffle(currentCoders);
io.sockets.emit('startGlobalTimer', shuffle);
});
socket.on('stopTime', function (data) {
io.sockets.emit('stopGlobalTimer', '');
});
// when coder disconencts, update number:
socket.on('disconnect', function (data) {
// only disconnects user when it's connected already:
if (addedUser) {
subCoder();
currentCoders.splice(currentCoders.indexOf(coder.name), 1);
io.sockets.emit('update_leave', {
num: totalCoder, // updates on total number
who: currentCoders, // updates on who's in the room
news: coder.name, // updates on who left
});
}
});
});
// methods:
function addCoder() {
totalCoder++;
}
function subCoder() {
totalCoder--;
}