forked from Hri7566/mpp-server-dev2
remove stacking clients
This commit is contained in:
parent
3d4c8430cf
commit
61e6625201
54
src/Room.js
54
src/Room.js
|
@ -3,7 +3,7 @@
|
||||||
//room deleter
|
//room deleter
|
||||||
//databases in Map
|
//databases in Map
|
||||||
|
|
||||||
class Room extends EventEmitter { //clean?
|
class Room extends EventEmitter {
|
||||||
constructor(server, _id, settings) {
|
constructor(server, _id, settings) {
|
||||||
super();
|
super();
|
||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
|
@ -18,14 +18,19 @@ class Room extends EventEmitter { //clean?
|
||||||
color2: this.verifyColor(settings.color) || this.defaultLobbyColor2
|
color2: this.verifyColor(settings.color) || this.defaultLobbyColor2
|
||||||
}
|
}
|
||||||
this.ppl = new Map();
|
this.ppl = new Map();
|
||||||
|
this.connections = [];
|
||||||
this.bindEventListeners();
|
this.bindEventListeners();
|
||||||
this.server.rooms.set(_id, this);
|
this.server.rooms.set(_id, this);
|
||||||
}
|
}
|
||||||
join(cl) {
|
join(cl) {
|
||||||
|
let otheruser = this.connections.find((a) => a.user._id == cl.user._id)// Array.from(this.ppl.values()).find((a) => a.user._id == cl.user._id);//
|
||||||
|
if (!otheruser) {
|
||||||
let participantId = createKeccakHash('keccak256').update((Math.random().toString() + cl.ip)).digest('hex').substr(0, 24);
|
let participantId = createKeccakHash('keccak256').update((Math.random().toString() + cl.ip)).digest('hex').substr(0, 24);
|
||||||
cl.user.id = participantId;
|
cl.user.id = participantId;
|
||||||
cl.participantId = participantId;
|
cl.participantId = participantId;
|
||||||
this.ppl.set(participantId, cl);
|
this.ppl.set(participantId, cl);
|
||||||
|
this.connections.push(cl);
|
||||||
|
|
||||||
this.sendArray([{
|
this.sendArray([{
|
||||||
color: this.ppl.get(cl.participantId).user.color,
|
color: this.ppl.get(cl.participantId).user.color,
|
||||||
id: this.ppl.get(cl.participantId).participantId,
|
id: this.ppl.get(cl.participantId).participantId,
|
||||||
|
@ -36,31 +41,44 @@ class Room extends EventEmitter { //clean?
|
||||||
_id: cl.user._id
|
_id: cl.user._id
|
||||||
}], cl)
|
}], cl)
|
||||||
this.updateCh(cl);
|
this.updateCh(cl);
|
||||||
|
} else {
|
||||||
|
cl.user.id = otheruser.participantId;
|
||||||
|
cl.participantId = otheruser.participantId;
|
||||||
|
this.connections.push(cl);
|
||||||
|
this.updateCh(cl);
|
||||||
}
|
}
|
||||||
remove(p) {
|
|
||||||
//this.participants.splice(this.participants.findIndex((cl) => cl.participantId == p.participantId), 1);
|
}
|
||||||
|
remove(p) {
|
||||||
|
let otheruser = this.connections.filter((a) => a.user._id == p.user._id);//Array.from(this.ppl.values()).filter((a) => a.user._id == p._id);
|
||||||
|
if (!(otheruser.length > 1)) {
|
||||||
this.ppl.delete(p.participantId);
|
this.ppl.delete(p.participantId);
|
||||||
|
this.connections.splice(this.connections.findIndex((a) => a.connectionid == p.connectionid), 1);
|
||||||
|
console.log(`Deleted client ${p.user.id}`);
|
||||||
this.sendArray([{
|
this.sendArray([{
|
||||||
m: "bye",
|
m: "bye",
|
||||||
p: p.participantId
|
p: p.participantId
|
||||||
}]);
|
}]);
|
||||||
this.updateCh();
|
this.updateCh();
|
||||||
|
} else {
|
||||||
|
this.connections.splice(this.connections.findIndex((a) => a.connectionid == p.connectionid), 1);
|
||||||
}
|
}
|
||||||
updateCh(cl) {
|
|
||||||
if (this.ppl.keys().next().value.length <= 0) this.destroy();
|
}
|
||||||
this.ppl.forEach((usr) => {
|
updateCh(cl) {
|
||||||
|
if (Array.from(this.ppl.values()).length <= 0) this.destroy();
|
||||||
|
this.connections.forEach((usr) => {
|
||||||
this.server.connections.get(usr.connectionid).sendArray([this.fetchData(usr, cl)])
|
this.server.connections.get(usr.connectionid).sendArray([this.fetchData(usr, cl)])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
destroy() {
|
destroy() {
|
||||||
this._id;
|
this._id;
|
||||||
console.log(`Deleted room ${this._id}`);
|
console.log(`Deleted room ${this._id}`);
|
||||||
this.settings = {};
|
this.settings = {};
|
||||||
this.ppl;
|
this.ppl;
|
||||||
this.server.rooms.delete(_id);
|
this.server.rooms.delete(_id);
|
||||||
}
|
}
|
||||||
sendArray(arr, not) {
|
sendArray(arr, not) {
|
||||||
this.ppl.forEach((usr) => {
|
this.ppl.forEach((usr) => {
|
||||||
if (!not || usr.participantId != not.participantId) {
|
if (!not || usr.participantId != not.participantId) {
|
||||||
try {
|
try {
|
||||||
|
@ -70,8 +88,8 @@ class Room extends EventEmitter { //clean?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fetchData(usr, cl) {
|
fetchData(usr, cl) {
|
||||||
let chppl = [];
|
let chppl = [];
|
||||||
[...this.ppl.values()].forEach((a) => {
|
[...this.ppl.values()].forEach((a) => {
|
||||||
chppl.push(a.user);
|
chppl.push(a.user);
|
||||||
|
@ -91,15 +109,15 @@ class Room extends EventEmitter { //clean?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
verifyColor(color) {
|
verifyColor(color) {
|
||||||
return color; //TODO make this
|
return color; //TODO make this
|
||||||
}
|
}
|
||||||
bindEventListeners() {
|
bindEventListeners() {
|
||||||
this.on("bye", participant => {
|
this.on("bye", participant => {
|
||||||
this.remove(participant);
|
this.remove(participant);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
module.exports = Room;
|
module.exports = Room;
|
Loading…
Reference in New Issue