generated from drawwithcode/P5-empty-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
287 lines (219 loc) · 7 KB
/
server.js
File metadata and controls
287 lines (219 loc) · 7 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//server setup
const PORT = process.env.PORT || 3000
var express = require("express")
var app = express();
var server = app.listen(PORT);
app.use(express.static("public"));
console.log("RUNNING SERVER ON http://localhost:" + PORT)
var serverSocket = require("socket.io")
var io = serverSocket(server)
//libraries setup ends here
//users setup
const COLORS = [
"#008226",
"#00CB3B",
"#EAC400",
"#D25C5C",
"#FF0000",
"#A700D1",
"#0007A6",
"#00A9FF"
]
let colArray = []
for (let i = 0; i < COLORS.length; i++){
let colorObj = {
hex: COLORS[i],
taken: false
}
colArray.push(colorObj)
}
class User{
constructor(id) {
this.id = id;
this.pairedId = 0;
this.color = 0;
this.name = "";
this.msg = [];
this.timer = 0;
this.active = false;
this.sending = false;
this.success = false;
}
updateMsg(){
this.msg.push(this.active);
if(this.msg.length > 70){
this.msg.shift()
}
this.active = this.sending;
}
reset(){
this.pairedId = 0;
this.msg = [];
clearInterval(this.timer)
this.active = false;
this.freeColor()
}
assignColor() {
//returns index of first untaken color
let freeColors = colArray.filter(color => !color.taken);
let index;
if (freeColors.length > 0) {
index = Math.floor(Math.random() * freeColors.length)
this.color = freeColors[index].hex
freeColors[index].taken = true
}
let log = "colors: "
for (let i = 0; i < colArray.length; i++){
if (colArray[i].taken) {
log+="X"
}
else {
log+="O"
}
log += colArray[i].hex
if (i != colArray.length - 1) {
log += ", "
}
}
console.log(log)
console.log("assigned "+ freeColors[index].hex + " to "+this.id)
return index
}
freeColor() {
let colorIndex = colArray.findIndex(obj => obj.hex == this.color)
colArray[colorIndex].taken = false
console.log("freed: "+colorIndex)
}
}
const CLOCK = 600;
var users = [];
var waiting = [];
io.on("connection", newConnection)
function newConnection(socket) {
console.log(socket.id + " connected")
//check if user exists and if not, save them
if (getUser(socket.id) == undefined) {
users.push(new User(socket.id))
}
socket.on("ready", function (username) {
getUser(this.id).name = username;
//set user to waiting
waiting.push(this.id)
console.log(this.id + " ready, name: " + username + ", unpaired: " + waiting.length)
console.log("free colors: " + colArray.filter(color => !color.taken).length)
//if enough users to pair, do
if (waiting.length > 1) {
pair();
}
})
//when user sends morse message, forward to paired user
socket.on("morse", function (data) {
var sender = getUser(this.id);
var receiverId = sender.pairedId;
sender.sending = data;
if (receiverId != 0) {
io.to(receiverId).emit("morse", data)
//activate current clock cycle
if(data) sender.active = true;
}
console.log(this.id + " - morse")
})
socket.on("success", function () {
console.log("success: "+this.id)
let sender = getUser(this.id);
if(!sender.success){
let pair = [sender, getUser(sender.pairedId)]
pair[0].success = true;
pair[1].success = true;
let random = Math.round(Math.random())
for (let i = 0; i < pair.length; i++) {
while(pair[i].msg.length < 70) {
pair[i].msg.push(true)
}
}
let msg = {};
msg.msg1 = pair[random].msg
msg.color1 = pair[random].color
msg.name1 = pair[random].name
msg.msg2 = pair[1 - random].msg
msg.color2 = pair[1 - random].color
msg.name2 = pair[1 - random].name
for (let i = 0; i < pair.length; i++) {
io.to(pair[i].id).emit("success", msg)
}
}
})
//remove disconnected user and unpair them
//also informs unpaired clients of the status change
socket.on("disconnect", function () {
console.log(socket.id + " disconnected")
let userIndex = users.findIndex(user => user.id == this.id);
let disconnected = users[userIndex];
let waitIndex = waiting.indexOf(this.id);
//check if user was waiting
if (waitIndex != -1) {
//user was waiting
waiting.splice(waitIndex, 1)
//unpaired -= 1;
console.log("was unpaired")
}
else if (disconnected.success) {
disconnected.reset()
}
else {
//user was paired
if (disconnected.pairedId != 0) {
let paired = getUser(disconnected.pairedId)
//set paired user to waiting
io.to(paired.id).emit("unpaired", 0)
waiting.unshift(paired.id)
paired.reset();
//reset user
disconnected.reset();
console.log("was paired")
}
}
if (waiting.length > 1) {
pair();
}
users.splice(userIndex, 1)
})
}
//takes the first two waiting users and pairs them
function pair() {
if(colArray.filter(color => !color.taken).length > 1){
console.log("pairing")
let pairingUsers = [getUser(waiting[0]), getUser(waiting[1])]
for (let i = 0; i < pairingUsers.length; i++) {
let user = pairingUsers[i]
//assign paired id to users
user.pairedId = pairingUsers[1 - i].id
//assign colors to users
if (user.color == 0) {
user.assignColor()
}
//start timers
user.timer = setInterval(function () {
user.updateMsg()
}, CLOCK)
}
let msg = [{}, {}];
for (let i = 0; i < msg.length; i++) {
//send users pair id
msg[i].id = pairingUsers[1 - i].id
//send users their color and color to search
msg[i].userColor = pairingUsers[i].color
msg[i].pairColor = pairingUsers[1 - i].color
//send users color of paired
msg[i].pairName = pairingUsers[1 - i].name
//send the message
io.to(pairingUsers[i].id).emit("paired", msg[i])
}
console.log("paired " + pairingUsers[0].id + " and " + pairingUsers[1].id)
waiting.splice(0, 2);
}
}
//just a nicer way to write this
function getUser(id) {
return users.find(user => user.id == id)
}