-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
96 lines (79 loc) · 2.35 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
'use strict';
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
//var socket = require('./sockets/sockets.js');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
/* Configuration */
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.set('port', 3000);
/** ------------------
* SCENE SETUP
*/
var Scene = require('./sockets/scene.js');
var $scene = new Scene();
/*
* Socket.io Communication
*/
io.sockets.on('connection', function (socket) {
$scene.addParticipant(socket);
if ( $scene.participants.length === 1 ) {
io.to(socket.id).emit('command:set', {command: true});
}
if ( $scene.participants.length > 2 ) {
let participant = $scene.participants[0].socket;
io.to(participant).emit('script:ready', {ready: true});
}
socket.on('script:start', function(socket) {
// start conversation
let line = $scene.getScriptLine();
let participant = $scene.participants[line.id].socket;
console.log(line.text, participant);
// send line to correct person
io.to(participant).emit('script:say', {text:line.text});
});
/**
* finish line
*/
socket.on('script:finishsay', function(socket) {
console.log("next line");
// start conversation
let line = $scene.getScriptLine();
let participant = $scene.participants[line.id].socket;
console.log(line.text, participant);
if (line.playvideo) {
io.to(participant).emit('video:play', {video: true});
}
// send line to correct person
io.to(participant).emit('script:say', {text:line.text});
});
socket.on('mode:kids', function(flag) {
$scene.switchMode('kids');
socket.broadcast.emit('mode:switch', {flag: true});
})
socket.on('mode:normal', function(flag) {
$scene.switchMode('normal');
socket.broadcast.emit('mode:switch', {flag: false});
})
/**
* disconnect
*/
socket.on('disconnect', function (socket) {
$scene.removeParticipant(socket);
if ($scene.participants.length < 3) {
io.to(participant).emit('script:ready', {ready: false});
}
});
});
/** ------------------
* start server
*/
server.listen(app.get('port'), function (){
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});
module.exports = app;