-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (104 loc) · 3.09 KB
/
server.js
File metadata and controls
148 lines (104 loc) · 3.09 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Create server
let port = process.env.PORT || 8000;
let express = require('express');
let app = express();
let server = require('http').createServer(app).listen(port, function () {
console.log('Server listening at port: ', port);
});
// Tell server where to look for files
app.use(express.static('public'));
// Create socket connection
let io = require('socket.io').listen(server);
function generateMapping(){
let alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
let shuffledAlphabet = alphabet.slice();
shuffle(shuffledAlphabet);
function shuffle(a) {
let j, x, i;
for ( i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
}
let dictionary = {};
for (let i = 0; i < shuffledAlphabet.length; i++) {
dictionary[alphabet[i]] = shuffledAlphabet[i];
}
return dictionary;
}
//
let current_orgKey = null;
let current_mappedkey = null;
let current_pos = 0;
let current_user = null;
let user_list = [];
let key_mapping = generateMapping();
let listOfWords = ["ELEPHANT", "BASEBALL", "HOUSEPARTY"];
//choose a random word
let word = listOfWords[Math.floor(Math.random() * listOfWords.length)];
// Listen for individual clients to connect
io.sockets.on('connection',
// Callback function on connection
// Comes back with a socket object
function (socket) {
console.log("We have a new client: " + socket.id);
//randomly pick a word send back to clients
// Add socket to queue
user_list.push(socket.id);
if(current_user === null){
current_user = socket.id;
}
let data = {
"word": word,
"current_user": current_user,
"current_pos": current_pos,
"current_mappedkey": current_mappedkey,
"current_orgKey": current_orgKey
};
socket.emit('init', data);
socket.on('keyPressed', function(message){
let keyCode = message["keyCode"];
current_orgKey = String.fromCharCode(keyCode);
sync();
if (current_user !== socket.id){
return;
}
current_mappedkey = key_mapping[current_orgKey];
sync();
if (word[current_pos] === current_mappedkey){
current_pos++;
next();
}
});
socket.on('disconnected', function(message){
for(let s = 0; s < user_list.length; s++) {
if(user_list[s].id == socket.id) {
user_list.splice(s, 1);
}
}
// If current client disconnected, move onto next client
if (socket.id === current_user) {
next();
}
});
});
function next(){
console.log(current_user);
let currentUserIndex = user_list.indexOf(current_user);
// console.log(currentUserIndex);
current_user = user_list[(currentUserIndex + 1)%user_list.length];
sync();
// console.log(current_user);
// console.log(user_list);
}
function sync(){
let message = {
"current_user": current_user,
"current_pos": current_pos,
"current_mappedkey": current_mappedkey,
"current_orgKey": current_orgKey
};
io.sockets.emit('sync', message);
}