This repository has been archived by the owner on May 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
336 lines (307 loc) · 8.27 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
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
// Setup basic express server
var express = require("express");
var app = express();
var path = require("path");
var server = require("http").createServer(app);
var io = require("socket.io")(server);
var port = process.env.PORT || 3000;
var PythonShell = require("python-shell");
server.listen(port, () => {
console.log('Server listening at port %d', port);
});
// Routing
app.use(express.static(path.join(__dirname, "public")));
// Chatroom
var numUsers = 0;
var centre = " Centre: ";
// Yes, I have removed the blanks because I was lazy; you may add them
var bag = "EEIBOOLEHALAPYTERWJRANEOSIDRRFLOESETAMQGIUVIUBPIKIFATRGNIADESTANHIDMGNEXTUOCRETLOISAZOYUODCNVNEAWE";
var bag_og = bag;
var words = new Object();
words['the_void\u2122'] = [];
var active1 = true;
var active2 = false;
io.on("connection", (socket) => {
var addedUser = false;
// setInterval(() => {
// io.emit('new message', {
// username: "snatch-daemon",
// message: "just keeping this server awake, please continue"
// });
// }, 1200000);
// when the client emits "new message", this listens and executes
socket.on("new message", (data) => {
// we tell the client to execute "new message"
socket.broadcast.emit("new message", {
username: socket.username,
message: data
});
// while (active1 && active2){}
active1 = true;
active2 = true;
var options_c = {
mode: "text",
args: [centre.substring(8), data, socket.username.toLowerCase(), JSON.stringify(words)]
};
PythonShell.run("check.py", options_c, function (err, results) {
if (err){
throw err;
}
// results is an array of messages collected during execution
console.log("results: %j", results);
// if an invalid word is made
if (parseInt(results[0]) == -1){
centre = results[2];
io.emit("new message", {
username: "snatch",
message: results[1]+results[2]
});
var str = "";
for(var key in words){
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
active1 = false;
}
// if a valid word is made
if (parseInt(results[0]) == 1){
centre = results[2];
word = results[1].substring(results[1].indexOf(":") + 2);
word = word.replace(", ", "");
words[socket.username].push(word);
console.log(words);
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
var str = ""
for(var key in words){
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
active1 = false;
}
// if valid tiles are drawn
if (parseInt(results[0]) == 2){
var options_d = {
mode: "text",
args: [centre.substring(8), bag, results[1], socket.username.toLowerCase()]
}
PythonShell.run("draw.py", options_d, function(err, results) {
if (err) throw err;
// results is an array of messages collected during execution
console.log("results_d: %j", results);
if (parseInt(results[0]) == 0){
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
var str = ""
for(var key in words){
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
active1 = false;
}
if (parseInt(results[0]) == 1){
centre = results[2]
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
var str = ""
for(var key in words){
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
bag = results[3]
active1 = false;
}
});
}
if(parseInt(results[0]) == -2){
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
var str = ""
for(var key in words){
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
active1 = false;
}
if(parseInt(results[0]) == 0){
active1 = false;
}
if(parseInt(results[0]) == 3){
centre = results[2]
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
temp = words[results[3]]
temp.splice(temp.indexOf(results[4]), 1)
words[results[3]] = temp
var str = ""
for(var key in words){
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
active1 = false;
}
if(parseInt(results[0]) == -3){
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
var str = ""
for(var key in words){
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
active1 = false;
}
if(parseInt(results[0]) == 4){
centre = results[2]
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
console.log(results[1])
words[socket.username].push(results[5]);
temp = words[results[3]]
console.log(results[3])
console.log(word[results[3]])
temp.splice(temp.indexOf(results[4]), 1)
words[results[3]] = temp
var str = ""
for(var key in words){
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
active1 = false;
}
if(parseInt(results[0]) == 5){
centre = results[2]
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
}
if(parseInt(results[0]) == -9){
centre = results[2]
io.emit("new message", {
username: "snatch",
message: results[1] + results[2]
});
bag = bag_og
var str = ""
for(var key in words){
words[key] = []
if (words[key].length>0) {
str += key + ": " + words[key] + "; ";
}
}
io.emit("new message", {
username: "snatch",
message: str
});
active1 = false;
}
});
});
// when the client emits "add user", this listens and executes
socket.on("add user", (username) => {
if (addedUser)
return;
temp_username = '';
for (var i = 0, n = username.length; i < n; i++) {
if (username.charCodeAt( i ) < 255) {
temp_username = temp_username + username[i];
}
}
username = temp_username;
if (username.toLowerCase() in words){
username = username + Math.floor(Math.random() * 1000);
}
words[username.toLowerCase()] = []
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
socket.emit("login", {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit("user joined", {
username: socket.username,
numUsers: numUsers
});
});
// when the client emits "typing", we broadcast it to others
socket.on("typing", () => {
socket.broadcast.emit("typing", {
username: socket.username
});
});
// when the client emits "stop typing", we broadcast it to others
socket.on("stop typing", () => {
socket.broadcast.emit("stop typing", {
username: socket.username
});
});
// when the user disconnects.. perform this
socket.on("disconnect", () => {
if (addedUser) {
--numUsers;
words['the_void\u2122'] = words['the_void\u2122'].concat(words[socket.username])
words[socket.username] = []
// echo globally that this client has left
socket.broadcast.emit("user left", {
username: socket.username,
numUsers: numUsers
});
}
});
});