-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
181 lines (162 loc) · 4.79 KB
/
app.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
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const settings = require('./settings.json');
const Utils = require("./utils.js");
const Filter = require('bad-words');
const filter = new Filter();
let rooms = {};
const io = require('socket.io')(server, {
cors: {
origin: ['*'],
}
});
app.use(express.static(__dirname + "/client"));
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log("BonziWORLD Rewrite online!");
})
function newRoom(rid) {
rooms[rid] = new Room(rid);
}
io.on('connection', (socket) => {
socket.login = false;
socket.guid = Utils.guidGen();
let bc = settings.colors;
socket.pitch = Utils.randomRangeInt(settings.pitch.min, settings.pitch.max);
socket.speed = Utils.randomRangeInt(settings.speed.min, settings.speed.max);
socket.name = "BonziBUDDY";
socket.room = mainroom;
let cool; // Declare the cool variable here
for (let u in socket.room.users) {
const users = socket.room.users;
socket.emit("adduser", {
loginData: {
name: users[u].name
},
color: users[u].color,
pitch: users[u].pitch,
speed: users[u].speed,
id: users[u].id
});
}
socket.on("set_color", (data) => {
socket.room.updateUser(socket, {
name: socket.name,
color: data.color,
pitch: socket.pitch,
speed: socket.speed,
id: socket.guid
});
socket.color = data.color;
})
socket.on("login", (data) => {
if (!socket.login) {
let rid = data.rid;
if (typeof rid == "undefined" || rid === "" || rid === "default") {
} else {
socket.room.leaveLocal(socket);
newRoom(rid);
socket.room = rooms[rid];
}
var color = bc[Math.floor(Math.random() * bc.length)];
socket.userdata = {
name: data.name,
color: color,
pitch: socket.pitch,
speed: socket.speed,
id: socket.guid
}
socket.room.join(socket, socket.userdata)
socket.room.emit("adduser", {
loginData: data,
color: color,
pitch: socket.pitch,
speed: socket.speed,
id: socket.guid
})
socket.name = data.name;
socket.color = color;
socket.login = true;
}
});
socket.on("disconnect", () => {
console.log("User left: " + socket.guid)
socket.room.leave(socket, socket.userdata)
})
socket.on("talk", (data) => {
console.log(data);
socket.room.emit("talk", {
text: data,
id: socket.guid,
pitch: socket.pitch,
speed: socket.speed
});
var text = filter.clean(data);
if (text.length < 1000 && !cool) {
try {
cool = true;
setTimeout(function () {
cool = false;
}, 3000);
} catch (e) {
console.log("WTF?: " + e);
}
}
})
})
class Room {
constructor(rid) {
this.rid = rid;
this.users = [];
this.userSockets = [];
}
join(user, userdata) {
user.join(this.rid);
this.users.push(userdata)
this.userSockets.push(user)
}
updateUser(user, userdata) {
console.log('New color: ' + userdata.color)
let userIndex = this.users.indexOf(user);
if (userIndex == -1) return;
this.users.splice(userIndex, 1);
this.users.push(userdata)
user.color = userdata.color;
user.userdata = userdata;
io.emit("updateColor", {
color: userdata.color,
id: user.guid
})
}
emit(cmd, data) {
io.to(this.rid).emit(cmd, data);
}
leave(user, userdata) {
try {
io.emit("leave", {
id: user.guid,
});
let userSocketIndex = this.userSockets.indexOf(user);
if (userSocketIndex == -1) return;
this.userSockets.splice(userSocketIndex, 1);
let userIndex = this.users.indexOf(userdata);
if (userIndex == -1) return;
this.users.splice(userIndex, 1);
} catch (e) {
console.error(e)
}
}
leaveLocal(socket) {
try {
this.userSockets.forEach((user) => {
socket.emit("leave", {
id: user.guid,
});
})
} catch (e) {
console.error(e)
}
}
}
const mainroom = new Room('default');