-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (109 loc) · 3.43 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
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
// app.js
var app = require('express')();
var express = require('express');
app.use(express.static('public'));
//
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(4200, function(){
console.log('listening on *:4200');
});
var online = 0 ;
var numUsers = 0;
//holds up whatever maps are already selected
var mapCache = [];
// This object should be an array of arrays, first object being the socket, second being the name
var allClients = [];
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
app.get('/reset', function(req, res,next) {
mapCache = [];
allClients = [];
res.sendFile(__dirname + '/index.html');
});
io.on('connection',function(socket) {
var addedUser = false;
allClients.push([socket,""])
socket.on('add user', function (username) {
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
console.log("Users online: "+numUsers + " " + socket.username + " joined from " + socket.request.connection.remoteAddress)
addedUser = true;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers
});
socket.emit('update cache', mapCache);
io.sockets.emit('order_changed', allClients[0][0].username );
});
socket.on('disconnect', function () {
if (addedUser) {
--numUsers;
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
console.log("Users online: "+numUsers + " " + socket.username + " left!")
}
// Remove the entry from within the general queue:
// Select the proper socket
connected = false;
while (connected == false)
try {
if (allClients[0][0].connected) {
//noop
connected = true;
}
else {
allClients.shift();
try {
io.sockets.emit('order_changed', allClients[0][0].username );
}
catch(err) {
console.log("Error happened: "+err.message)
}
}
} catch(err) {
connected = true
console.log("No clients connected! (Clearing cache)")
// Empty cache
mapCache = []
}
});
socket.on('make_selection',function(data) {
console.log("User " + data.uuid + " clicked:" + data.map);
connected = false;
while (connected == false){
// This request can only come from client in charge
// Move it to the back of the queue
allClients.push(allClients[0]);
allClients.shift();
//verify that next client is still connected
if (allClients[0][0].connected) {
connected = true;
}
else {
console.log("User: " + allClients[0][0].username + "is no longer connected");
//delete first element then
allClients.unshift();
}
}
io.sockets.emit('order_changed', allClients[0][0].username );
console.log("waiting for user: "+ allClients[0][0].username )
io.sockets.emit('map_selected', data.map)
mapCache.push(data.map);
});
socket.on('name change',function(data){
//Need to store in the correct place
socket.name = data.name
console.log("Name changed to: " + data.name + " (uuid: "+data.uuid+") ");
});
});