-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (196 loc) · 6.87 KB
/
index.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
// System level includes
const path = require("path");
const config = require("./config.js");
const fs = require("fs");
const stream = require("stream");
// HTTP Includes
const express = require("express");
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
const ss = require("socket.io-stream");
const pug = require("pug");
// Unique ID / name library
const hat = require("hat");
const moniker = require("moniker");
// GIF maker
const PNG = require('pngjs').PNG;
const GIFEncoder = require('gifencoder');
let encoders = {};
// Create OSC server for handling data from MUSE
// let waves = {};
// let osc_server = new osc.Server(config.MUSE.PORT, config.MUSE.HOST);
// osc_server.on("message", (msg, rinfo) => {
// // See if the message contains one of the two requested brain waveforms
// let descriptors = msg.shift().split("/");
// let name = descriptors[0];
// let type = (descriptors.length > 2 ? descriptors[2].match(/(alpha|beta)/i) : null);
// if (type) {
// let defined = msg.filter(x => x !== NaN);
// if (!waves[name]) {
// waves[name] = {};
// io.emit("new-player", name);
// }
// // Save the average
// if (!waves[name][type[1]]) waves[name][type[1]] = {min: 1, max: 0, value: 0};
// waves[name][type[1]].value = defined.reduce((acc, cur) => acc + cur) / defined.length;
// // Save the range
// if (waves[name][type[1]].value > waves[name][type[1]].max)
// waves[name][type[1]].max = waves[name][type[1]].value;
// if (waves[name][type[1]].value < waves[name][type[1]].min)
// waves[name][type[1]].min = waves[name][type[1]].value;
// }
// });
// Distinct Colors
let dc = require("distinct-colors");
let chroma = require("chroma-js");
let palette = null;
// Constants
const GAME_TIME_MS = 3 * 60 * 1000;
// Mechanic Maps
let clients = {};
let bounds = {x: null, y: null};
let loopHandler = null;
let timeHandler = null;
let time_elapsed = 0;
// Express config routes
app.set("view engine", "pug");
app.use(express.static("public"));
app.get("/", (req, res) => {
res.render("index", {title: "NeuroBrush"});
});
// Game Paths
app.get("/new", (req, res) => {
let uuid = hat();
clients[uuid] = {uuid: uuid, players: {}, ready: 0, running: false};
res.redirect("/game/" + uuid);
});
app.get("/game/:id", (req, res) => {
if (!req.params.id || !clients[req.params.id]) {
return res.status(400).render("error", {title: "NeuroBrush", errorCode: 400, errorMsg: "Game with specified ID not found"});
}
if (clients[req.params.id].running) {
return res.render("spectator", {title: "NeuroBrush"});
}
res.render("game", {title: "NeuroBrush"});
});
app.get("/waves", (req, res) => {
res.json(waves);
});
// Gallery
app.get("/gallery", (req, res) => {
const gallery = path.join(__dirname, "public", "image", "gallery");
fs.readdir(gallery, (err, files) => {
files = files.filter(x => x.includes(".gif"));
res.render("gallery", {title: "NeuroBrush", paths: files});
});
});
app.get("/gallery/:id", (req, res) => {
const gallery = path.join(__dirname, "public", "image", "gallery");
fs.readdir(gallery, (err, files) => {
files = files.filter(x => x.includes(".gif"));
if (!req.params.id || files.indexOf(req.params.id) < 0) {
return res.status(400).render("error", {title: "NeuroBrush", errorCode: 400, errorMsg: "Image with specified ID not found"});
}
res.render("image", {title: "NeuroBrush", path: req.params.id});
});
});
// Catch-All 404 case
app.use("/error/:message", (req, res) => {
res.render("error", {title: "NeuroBrush", errorCode: 501, errorMsg: req.params.message});
});
app.use((req, res) => {
res.status(404).render("error", {title: "NeuroBrush", errorCode: 404, errorMsg: "Page not found"});
});
io.on("connection", (client) => {
console.log("Connected");
// Connect the client to a room specifically for their game
let uuid = client.handshake.headers.referer.split('/');
uuid = uuid[uuid.length - 1];
client.join(uuid);
// Get client name
let player = moniker.choose();
io.to(uuid).emit("player", {id: client.id, name: player});
client.emit("players", clients[uuid].players);
clients[uuid].players[client.id] = {id: client.id, name: player, ready: false};
client.on("ready", () => {
clients[uuid].players[client.id].ready = true;
io.to(uuid).emit("ready", client.id);
clients[uuid].ready += 1;
// If everyone is ready
if (Object.keys(clients[uuid].players).length == clients[uuid].ready) {
// Designate one of the clients to be the uploader for the gif
if (!clients[uuid].running) {
client.emit("uploader");
clients[uuid].running = true;
// Initialize a GIF for the game
encoders[uuid] = new GIFEncoder(1024, 480);
encoders[uuid].createReadStream()
.pipe(fs.createWriteStream(
path.join(__dirname, "public", "image", "gallery", uuid + ".gif"))
);
encoders[uuid].start();
encoders[uuid].setRepeat(0); // 0 for repeat, -1 for no-repeat
encoders[uuid].setDelay(500); // frame delay in ms
encoders[uuid].setQuality(10); // image quality. 10 is default.
}
// Send each client their color
palette = dc(clients[uuid].ready * 2);
for (var key in clients[uuid].players) {
if (clients[uuid].players.hasOwnProperty(key)) {
io.to(uuid).emit("color", {
id: clients[uuid].players[key].id,
colors: [palette.pop(), palette.pop()]
});
}
}
// Tell the clients to start playing
io.to(uuid).emit("start");
// Inform the clients every second of how much time is left
time_elapsed = 0;
timeHandler = setInterval(() => {
io.to(uuid).emit("update-time", GAME_TIME_MS / 1000 - time_elapsed);
++time_elapsed;
}, 1000);
// Get ready to restart after GAME_TIME_MS
setTimeout(() => {
clearInterval(timeHandler);
encoders[uuid].finish();
delete encoders[uuid];
io.to(uuid).emit("restart", uuid);
delete clients[uuid];
}, GAME_TIME_MS + 1000);
}
});
client.on("position", data => {
io.to(uuid).emit("position", data);
});
client.on("disconnect", _ => {
if (!clients[uuid])
return;
// Error out if someone disconnects while the game is going on (but not for spectators)
if (clients[uuid].running && clients[uuid].players[client.id].ready)
return io.to(uuid).emit("error");
if (clients[uuid].players[client.id].ready)
clients[uuid].ready -= 1;
io.to(uuid).emit("player-disconnect", {id: client.id});
delete clients[uuid].players[client.id];
});
// File uploading
ss(client).on("gif-frame", (strm, data) => {
strm.pipe(stream.Transform({
transform(chunk, enc, next) {
var base64Data = chunk.toString().replace(/^data:image\/png;base64,/, "");
this.push(new Buffer(base64Data, 'base64'));
}
}))
.pipe(new PNG({
filterType: 4
})).on("parsed", (data) => {
encoders[uuid].addFrame(data);
});
});
});
server.listen(config.WEB.PORT, config.WEB.HOST, () => {
console.log("Server is listening on: " + config.WEB.HOST + ":" + config.WEB.PORT);
});