-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
413 lines (342 loc) · 12 KB
/
server.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
require('dotenv').load();
var CasparCG = require('caspar-cg');
ccg = new CasparCG('127.0.0.1', 5250);
var express = require('express');
var router = express.Router();
var fs = require('fs');
var iconvlite = require('iconv-lite');
var bodyParser = require('body-parser');
var sanitize = require('sanitize-filename');
var io = require('socket.io');
var http = require('http');
var formidable = require('formidable');
var app = express();
var path = require('path');
var ffdevices = require('ffdevices');
var DataVolley = require('./lib/datavolley');
console.log(__dirname);
const PORT = process.env.PORT || 3000;
app.get('/init.js', function(req, res) {
var config = {
cloudinary: {
cloudName: process.env.CLOUDINARY_CLOUD_NAME || '',
apiKey: process.env.CLOUDINARY_API_KEY || '',
uploadPreset: process.env.CLOUDINARY_UPLOAD_PRESET || '',
},
};
var output = 'window.CONFIG = ' + JSON.stringify(config) + ';';
output +=
'jQuery.cloudinary.config({ cloud_name: CONFIG.cloudinary.cloudName, api_key: CONFIG.cloudinary.apiKey});';
res.send(output);
});
router.get('/datavolley/:fedCode/:gameId/update-score', function(req, res) {
var match = DataVolley.Match.getInstance(
req.params.fedCode,
req.params.gameId
);
match
.getGameInfo(true)
.then(function(data) {
res.send(JSON.stringify(data));
})
.catch(function() {
console.log('Error!');
res.send(JSON.stringify(null));
});
});
router.get('/datavolley/:fedCode/:gameId/game-info', function(req, res) {
var match = DataVolley.Match.getInstance(
req.params.fedCode,
req.params.gameId
);
match
.getGameInfo()
.then(function(data) {
console.log('Get game info');
console.log(data);
res.send(JSON.stringify(data));
})
.catch(function(err) {
console.log('Error!');
res.send(JSON.stringify(null));
});
});
router.get('/datavolley/current-matches', function(req, res, next) {
var ml = new DataVolley.MatchList();
ml.getCurrentMatches().then(function(games) {
res.send(JSON.stringify(games));
}).catch(err => {
console.log('Error', err);
next(err);
})
});
router.post('/caspar/play-stream', function(req, res) {
//
ccg.connect(function() {
console.log('Connected to Caspar-server');
var addCommand = 'PLAY 1-1 "' + req.body.stream + '"';
console.log(addCommand);
ccg.sendCommand(addCommand, function() {
res.send('Command was sent');
ccg.disconnect();
});
});
});
router.post('/caspar/templates/:template/:what', function(req, res) {
//
console.log('Loading template: ' + req.params.template);
ccg.connect(function() {
console.log('Connected to Caspar-server');
if (req.params.what == 'play') {
var path = `http://127.0.0.1:${PORT}/templates/${
req.params.template
}`;
var addCommand = 'PLAY 1-11 [HTML] "' + path + '"';
console.log('Loading ' + path);
var updateCommand =
'CALL 1-11 UPDATE ' +
JSON.stringify(JSON.stringify(req.body)) +
'';
ccg.sendCommand(addCommand, function() {
ccg.sendCommand(updateCommand, function() {
console.log('Command was sent');
ccg.disconnect();
res.send('Command was sent');
});
});
} else if (req.params.what == 'remove') {
var removeCommand = 'CALL 1-11 REMOVE ""';
ccg.sendCommand(removeCommand, function() {
console.log('Ran remove');
res.send('Command was sent');
ccg.disconnect();
});
} else if (req.params.what == 'update') {
var updateCommand =
'CALL 1-11 UPDATE ' +
JSON.stringify(JSON.stringify(req.body)) +
'';
console.log(updateCommand);
ccg.sendCommand(updateCommand, function() {
console.log('Ran update');
res.send('Command was sent');
ccg.disconnect();
});
}
});
});
router.get('/directshow/devices', function(req, res) {
//
//ffdevices.ffmpegPath = path.dirname(__dirname) + '/ffmpeg//bin/ffmpeg.exe'
console.log(path.dirname(__dirname) + '/bin/ffmpeg/ffmpeg.exe');
ffdevices.getAll(function(error, devices) {
console.log(error);
if (!error) {
devices.push({
name: 'NTNUI - Førde',
deviceType: 'file',
path: 'NTNUI - Førde.mp4',
});
res.send(JSON.stringify(devices));
} else {
res.send(JSON.stringify({ error: true }));
}
});
});
router.get('/graphics/:club/players/:id/image', function(req, res) {
//
var safeClubName = sanitize(req.params.club);
var clubDir = __dirname + '/data/' + safeClubName;
var playersDir = clubDir + '/' + '/players/';
var playerImagePath = playersDir + '/' + req.params.id + '.jpg';
console.log(playerImagePath);
if (!fs.existsSync(playerImagePath)) {
playerImagePath = __dirname + '/data/player-nopicture.png';
}
console.log(playerImagePath);
res.sendFile(playerImagePath);
});
app.route('/upload-profile-image').post(function(req, res, next) {
// create an incoming form object
var form = new formidable.IncomingForm();
// specify that we want to allow the user to upload multiple files in a single request
form.multiples = false;
// store all uploads in the /uploads directory
form.uploadDir = path.join(__dirname, '/tmp/uploads');
// every time a file has been uploaded successfully,
// rename it to it's orignal name
var ulPath = '';
form.on('file', function(field, file) {
ulPath = file.path;
});
// log any errors that occur
form.on('error', function(err) {
console.log(
'<script>alert("An error has occured: \n' + err + '");</script>'
);
});
var data = {};
form.parse(req, function(err, fields, files) {
data = fields;
});
// once all the files have been uploaded, send a response to the client
form.on('end', function() {
if (ulPath) {
var safeClubPath = sanitize(data.team_name);
var destPath = __dirname + '/data/' + safeClubPath + '/';
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}
destPath += 'players/';
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}
destPath += sanitize(data.player_sid) + '.jpg';
console.log('Writing ' + destPath);
fs.rename(ulPath, destPath);
}
res.end("<script>alert('Added image');</script>");
});
// parse the incoming request containing the form data
form.parse(req);
});
router.get('/graphics/:club/logo.jpg', function(req, res) {
//
var safeClubName = sanitize(req.params.club);
var clubDir = __dirname + '/data/' + safeClubName;
var filename = clubDir + '/logo.jpg';
var svgFilename =
__dirname + '/app/graphics/logo/' + safeClubName.toLowerCase() + '.svg';
console.log(svgFilename);
if (fs.existsSync(svgFilename)) {
filename = svgFilename;
}
console.log(filename);
res.sendFile(filename);
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Static stuff:
app.use(express.static('app'));
// Rewrite
app.use('/lib', express.static(__dirname + '/node_modules/jquery/dist/'));
app.use('/lib', express.static(__dirname + '/node_modules/angular/'));
app.use(
'/lib/bootstrap',
express.static(__dirname + '/node_modules/angular-ui-bootstrap/dist/')
);
app.use(
'/lib/bootstrap',
express.static(__dirname + '/node_modules/bootstrap/dist/css/')
);
app.use(
'/lib',
express.static(__dirname + '/node_modules/socket.io-client/dist/')
);
app.get('/game/:gameId/control', function(req, res) {
res.sendFile('game/index.html', { root: './app' });
});
app.get('/game/:gameId/app.js', function(req, res) {
res.sendFile('game/app.js', { root: './app' });
});
app.get('/game/:gameId/overlay', function(req, res) {
res.sendFile('obs/index.html', { root: './app' });
});
app.use('/', router);
app.use('*', function(req, res) {
res.sendFile(__dirname + '/app/404.html');
});
/*
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
*/
var server = http.createServer(app).listen(PORT);
var clients = {};
io = io.listen(server);
io.sockets.on('connection', function(socket) {
/*Associating the callback function to be executed when client visits the page and
websocket connection is made */
var message_to_client = {
status: 'connected',
data: 'Connection with the server established',
};
socket.send(JSON.stringify(message_to_client));
/*sending data to the client , this triggers a message event at the client side */
console.log('Socket.io Connection with the client established');
socket.on('message', function(data) {
/*This event is triggered at the server side when client sends the data using socket.send() method */
data = JSON.parse(data);
console.log(data);
/*Printing the data */
var ack_to_client = {
data: 'Server Received the message',
};
if (data.action == 'add_client') {
console.log(data.data.gameCode);
if (!data.data.gameCode) {
return;
}
if (typeof clients[data.data.gameCode] == 'undefined') {
console.log('Setting client to game code');
clients[data.data.gameCode] = [];
}
var f = false;
for (var i in clients[data.data.gameCode]) {
if (clients[data.data.gameCode][i].conn.id == socket.id) {
// Can only sign up once:
f = true;
break;
}
}
if (!f) {
console.log('Adding client for ' + data.data.gameCode);
clients[data.data.gameCode].push(socket);
}
return;
}
// All others go to the specific clients:
if (clients[data.gameCode]) {
for (var i in clients[data.gameCode]) {
if (clients[data.gameCode][i].conn.id == socket.id) {
continue;
}
console.log('Sending message to client...');
clients[data.gameCode][i].send(JSON.stringify(data));
}
}
//socket.send(JSON.stringify(ack_to_client));
/*Sending the Acknowledgement back to the client , this will trigger "message" event on the clients side*/
});
socket.on('disconnect', function(d1, d2) {
var c = {};
var numClients = 0;
for (var gameCode in clients) {
for (var i in clients[gameCode]) {
if (clients[gameCode][i].conn.id == this.conn.id) {
continue;
}
if (typeof c[gameCode] == 'undefined') {
c[gameCode] = [];
}
c[gameCode].push(clients[gameCode][i]);
numClients++;
}
}
clients = c;
console.log('Number of clients: ' + numClients);
console.log('The client has disconnected!');
});
//clients.push(socket);
//console.log('Number of clients: ' + clients.length)
});
// Add a disconnect listener
io.sockets.on('disconnect', function() {
console.log('The client has disconnected!');
});
/*
var ml = new DataVolley.MatchList();
ml.getCurrentMatches().then(function(games) {
console.log('Games', games);
})
*/