/
server.js
203 lines (169 loc) · 5.9 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
require('dotenv').config();
const fs = require('fs');
const key = fs.readFileSync(process.env.SSL_KEY);
const cert = fs.readFileSync(process.env.SSL_CERT);
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const https = require('https').createServer({key: key, cert: cert}, app);
http.listen(3001, () => { console.log('http listening on *:3001'); });
https.listen(3000, () => { console.log('https listening on *:3000'); });
const io = require('socket.io')(https);
const Sound = require('./sound/server-sound-browser');
const Lights = require('./lights/server-lights-dmx');
const { getChatSubData } = require('./server-fileUtils');
let chatSubs;
getChatSubData(process.env.LANG0).then(data => chatSubs = data).catch(err => console.log('error', err));
let curEmotion;
// Webpack setup for hot reloading dev environment
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
const compiler = webpack(webpackConfig);
app.use(require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
writeToDisk: true,
}));
app.use(require('webpack-hot-middleware')(compiler, {
path: '/__webpack_hmr', heartbeat: 10 * 1000
}));
// DATA INIT
const emotions = JSON.parse(fs.readFileSync('all-emotions.json')); // read all emotions
const baseEmotions = generateBases();
const imagesManifest = generateImagesManifest();
const emotionName = fs.readFileSync('current.txt', {encoding:'utf8', flag:'r'}).replace(/\s/g, ''); // remove whitespace
setEmotion(emotionName, true);
// SOCKET
io.on('connection', (socket) => {
socket.emit('emotion:update', curEmotion);
socket.on('disconnect', () => { });
socket.on('emotion:pick', setEmotion);
socket.on('emotion:get', () => {
socket.emit('emotion:update', curEmotion);
});
socket.on('chat:send', handleChat);
socket.on('reflection:end', restartReflectionAudio);
// debug stuff
socket.on('debug:toggle', msg => { io.emit('debug:toggle', msg); });
socket.on('debug:reload', msg => { io.emit('debug:reload', msg); });
socket.on('sound:off', Sound.stopAll);
socket.on('lights:off', Lights.stopAll);
socket.on('volume:set', msg => { Sound.setVolume(msg.val); });
Sound.setup(io, curEmotion);
});
// SERVER SETUP
app.use('/', express.static('dist'));
app.use('/sound', express.static('sound'));
app.use('/images', express.static('images'));
app.use('/videos', express.static('videos'));
app.get('/emotions', (req, res) => { res.json(emotions); });
// responds with array of image urls for base emotion
app.get('/images/:baseEmotion/manifest', (req, res) => {
try {
const baseEmotion = req.params.baseEmotion;
if (!baseEmotion) {
throw new Error('No emotion in url');
}
res.status(200).send(JSON.stringify(imagesManifest[baseEmotion]));
} catch (err) {
console.log(err);
res.status(400).send(err.message);
}
});
function setEmotion(emotionName, init) {
console.log('SET EMOTION');
curEmotion = emotions[emotionName];
curEmotion.volume = Sound.volume;
curEmotion.seed = Math.round(Math.random() * 10000);
console.log(curEmotion);
Lights.playEmotion(curEmotion);
if (!init) {
io.emit('emotion:update', curEmotion);
fs.writeFileSync('current.txt', emotionName);
Sound.playEmotion(curEmotion);
// Sound.playEmotionReflection(curEmotion);
}
}
function restartReflectionAudio() {
let opt = { 'seed' : Math.round(Math.random() * 10000)};
io.emit('reflection:restart', opt);
// Sound.playEmotionReflection(curEmotion);
}
function handleChat(data) {
// called by area 04 when user hits "send" on a chat message
const wordsToSubArray = chatSubs[curEmotion.base];
const subArray = chatSubs[`${curEmotion.base}-subs`];
// splits message into array of words, spaces and punctuation marks
const msgWordArray = data.original.split(/([\.!\?\,\-])|([\s])/g);
for (let index = 0; index < msgWordArray.length; index++) {
const currentWord = msgWordArray[index];
const matchedIndex = wordsToSubArray.findIndex(word => currentWord && (word.toLowerCase() === currentWord.toLowerCase()));
if (matchedIndex >= 0) {
msgWordArray[index] = subArray[matchedIndex];
}
}
data.modified = msgWordArray.join('');
io.emit('chat:new', data);
}
function generateBases() {
let bases = [];
Object.keys(emotions)
.sort()
.forEach((emotion, i) => {
let base = emotions[emotion].base;
if (!bases.includes(base)) {
bases.push(base);
}
});
return bases;
}
function generateImagesManifest() {
let manifest = {};
// GET BASES
for (let base of baseEmotions) {
if (!manifest.hasOwnProperty(base)) {
manifest[base] = [];
for (let i = 0; i < 3; i++) {
manifest[base][i] = getAllFiles(`/images/${base}/${i + 1}/`);
}
}
}
manifest['popups'] = getAllFiles('/images/popups/');
return manifest;
}
function getAllFiles(dir) {
const list = [];
const files = fs.readdirSync(`.${dir}`);
// get rid of hidden files (.DS_STORE, etc)
const imageFiles = files.filter(item => !(/(^|\/)\.[^\/\.]/g).test(item));
// URL client can use to get the image in css or img src attribute
const staticURLPrefix = dir;
imageFiles.forEach(file => {
list.push(staticURLPrefix + encodeURI(file));
});
return list;
}
// CLEANUP
// function cleanup() {
// console.log('stop all');
// Sound.stopAll();
// Lights.stopAll();
// };
// process.on('cleanup', cleanup);
// // do app specific cleaning before exiting
// process.on('exit', function () {
// cleanup();
// process.emit('cleanup');
// });
// // catch ctrl+c event and exit normally
// process.on('SIGINT', function () {
// console.log('Ctrl-C...');
// cleanup();
// process.exit(2);
// });
// //catch uncaught exceptions, trace, then exit normally
// process.on('uncaughtException', function(e) {
// console.log('Uncaught Exception...');
// console.log(e.stack);
// cleanup();
// process.exit(99);
// });