-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
186 lines (123 loc) · 4.45 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
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
/*
___ ___ ___ ___ ___ ___ ___
/\ \ /\ \ /\ \ /\ \ /\__\ /\ \ /\ \
/::\ \ /::\ \ /::\ \ /::\ \ /:/ / /::\ \ /::\ \
/:/\:\ \ /:/\:\ \ /:/\:\ \ /:/\:\ \ /:/ / /:/\:\ \ /:/\:\ \
/::\~\:\ \ /:/ \:\ \ /::\~\:\ \ /::\~\:\ \ /:/__/ ___ /::\~\:\ \ /::\~\:\ \
/:/\:\ \:\__\/:/__/ \:\__\/:/\:\ \:\__\/:/\:\ \:\__\|:| | /\__\/:/\:\ \:\__\/:/\:\ \:\__\
\/__\:\ \/__/\:\ \ /:/ /\/_|::\/:/ /\:\~\:\ \/__/|:| |/:/ /\:\~\:\ \/__/\/_|::\/:/ /
\:\__\ \:\ /:/ / |:|::/ / \:\ \:\__\ |:|__/:/ / \:\ \:\__\ |:|::/ /
\/__/ \:\/:/ / |:|\/__/ \:\ \/__/ \::::/__/ \:\ \/__/ |:|\/__/
\::/ / |:| | \:\__\ ~~~~ \:\__\ |:| |
\/__/ \|__| \/__/ \/__/ \|__|
Forever - Algorithmic Composition - Server code (ES6)
Wrriten by juniorxsound (http://phenomenalabs.com)
*/
'use strict'
//////EXPRESS////////
const express = require('express');
const app = express();
////////HTTP/////////
const http = require('http').createServer(app);
//Port and server setup
let port = process.env.PORT || 5555;
//Server
let server = app.listen(port);
//Console the port
console.log('Server is running localhost on port: ' + port );
/////SOCKET.IO///////
const io = require('socket.io').listen(server);
////////EJS//////////
const ejs = require('ejs');
//GLOBAL VARIABLES///
let usersCounter;
let sockets = {};
let geolocations = {};
let userType;
//Setup the views folder
app.set("views", __dirname + '/views');
//Setup ejs, so I can write HTML(:
app.engine('.html', ejs.__express);
app.set('view-engine', 'html');
//Setup the public client folder
app.use(express.static(__dirname + '/public'));
//Socket setup
io.on('connection', client=>{
//Players and conductors introduce yourself
client.on('introduction', who=>{
userType = who;
if ( userType == 'conductor' ){
//Conductor connected log to server
console.log('A conductor connected');
} else if ( userType == 'player' ){
//Add the user to the identifiening object
sockets[client.id] = client;
usersCounter = Object.keys(sockets).length;
//User connected log
console.log('A player connected');
console.log('There are currently ' + usersCounter + ' players connected');
// Everytime a user connects notify all users and pass a number
io.sockets.emit('user', usersCounter);
} else {
console.log('Error, unknowen user type');
}
});
client.on('geolocation', geodata=>{
//Split Lat and Long into a 2 slot array
let processGeoData = geodata.split(',');
//If there is geo data and the user is a player make magic
if (geodata != null && userType == 'player'){
geolocations[client.id] = processGeoData;
}
});
//Disconnecting a user
client.on('disconnect',()=>{
if ( userType == 'player' ){
//Remove the user id from the userTable object
delete sockets[client.id];
//Delete the geodata stored by the client's ID
delete geolocations[client.id];
//Another one bites to dust
usersCounter = Object.keys(sockets).length;
if( usersCounter == 0 ){
console.log('A player disconnected');
console.log('There are currently no players connected');
} else {
//User disconnected log
console.log('A player disconnected');
console.log('there are currently ' + usersCounter + ' player connected');
}
userType == 'player';
} else if ( userType == 'conductor' ) {
console.log('A conductor dissconected');
userType == 'conductor';
}
// //Everytime a user disconnects notify all users and pass a number
io.sockets.emit('user', usersCounter);
});
});
/////////////////////
//////ROUTER/////////
/////////////////////
//Client view
app.get('/', (req, res) => {
res.render('index.html');
});
//Conductor view
app.get('/conductor.html', (req, res) => {
res.render('conductor.html');
});
//404 view
app.get('/*', (req, res) => {
res.render('404.html');
});
setInterval(function(){
//Logs for data in the server
// console.log(usersCounter);
// console.log(sockets);
// console.log(geolocations);
//If geolocations has geodata emit it to all users every half second
if(Object.keys(geolocations) != 0 ){
io.sockets.emit('serverGeoData', geolocations);
}
}, 500);