fix bugs, add chset
This commit is contained in:
parent
0bca3c6460
commit
bce9e3d155
|
@ -11,7 +11,8 @@ class Client extends EventEmitter {
|
||||||
this.channel;
|
this.channel;
|
||||||
this.ws = ws;
|
this.ws = ws;
|
||||||
this.req = req;
|
this.req = req;
|
||||||
this.ip = (req.connection.remoteAddress).replace("::ffff:","");
|
this.ip = (req.connection.remoteAddress).replace("::ffff:", "");
|
||||||
|
this.destroied = false;
|
||||||
this.bindEventListeners();
|
this.bindEventListeners();
|
||||||
require('./Message.js')(this);
|
require('./Message.js')(this);
|
||||||
}
|
}
|
||||||
|
@ -45,36 +46,39 @@ class Client extends EventEmitter {
|
||||||
}
|
}
|
||||||
destroy() {
|
destroy() {
|
||||||
this.ws.close();
|
this.ws.close();
|
||||||
|
if (this.channel) {
|
||||||
|
this.channel.emit("bye", this)
|
||||||
|
}
|
||||||
this.user;
|
this.user;
|
||||||
this.participantId;
|
this.participantId;
|
||||||
this.channel;
|
this.channel;
|
||||||
this.connectionid;
|
this.connectionid;
|
||||||
this.server.connections.delete(this.connectionid);
|
this.server.connections.delete(this.connectionid);
|
||||||
if (this.channel) {
|
this.destroied = true;
|
||||||
this.channel.emit("bye", this)
|
|
||||||
}
|
|
||||||
console.log(`Removed Connection ${this.connectionid}.`);
|
console.log(`Removed Connection ${this.connectionid}.`);
|
||||||
}
|
}
|
||||||
bindEventListeners() {
|
bindEventListeners() {
|
||||||
this.ws.on("message", (evt) => {
|
this.ws.on("message", (evt) => {
|
||||||
try {
|
try {
|
||||||
var transmission = JSON.parse(evt);
|
let transmission = JSON.parse(evt);
|
||||||
} catch (e) {
|
|
||||||
this.destroy();
|
|
||||||
} finally {
|
|
||||||
for (let msg of transmission) {
|
for (let msg of transmission) {
|
||||||
if (!msg.hasOwnProperty("m")) return;
|
if (!msg.hasOwnProperty("m")) return;
|
||||||
if (!this.server.legit_m.includes(msg.m)) return;
|
if (!this.server.legit_m.includes(msg.m)) return;
|
||||||
this.emit(msg.m, msg);
|
this.emit(msg.m, msg);
|
||||||
console.log(`RECIEVE: `, JSON.colorStringify(msg));
|
console.log(`RECIEVE: `, JSON.colorStringify(msg));
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
|
this.destroy();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.ws.on("close", () => {
|
this.ws.on("close", () => {
|
||||||
|
if (!this.destroied)
|
||||||
this.destroy();
|
this.destroy();
|
||||||
});
|
});
|
||||||
this.ws.addEventListener("error", (err) => {
|
this.ws.addEventListener("error", (err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
if (!this.destroied)
|
||||||
this.destroy();
|
this.destroy();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,16 +40,31 @@ module.exports = (cl) => {
|
||||||
})
|
})
|
||||||
cl.on("chown", msg => {
|
cl.on("chown", msg => {
|
||||||
if (!(cl.channel && cl.participantId)) return;
|
if (!(cl.channel && cl.participantId)) return;
|
||||||
console.log((Date.now() - cl.channel.crown.time))
|
//console.log((Date.now() - cl.channel.crown.time))
|
||||||
console.log(!(cl.channel.crown.userId != cl.user._id), !((Date.now() - cl.channel.crown.time) > 15000));
|
//console.log(!(cl.channel.crown.userId != cl.user._id), !((Date.now() - cl.channel.crown.time) > 15000));
|
||||||
if (!(cl.channel.crown.userId == cl.user._id) && !((Date.now() - cl.channel.crown.time) > 15000)) return;
|
if (!(cl.channel.crown.userId == cl.user._id) && !((Date.now() - cl.channel.crown.time) > 15000)) return;
|
||||||
if (msg.hasOwnProperty("id")) {
|
if (msg.hasOwnProperty("id")) {
|
||||||
console.log(cl.channel.crown)
|
// console.log(cl.channel.crown)
|
||||||
if (cl.user._id == cl.channel.crown.userId || cl.channel.crown.dropped)
|
if (cl.user._id == cl.channel.crown.userId || cl.channel.crowndropped)
|
||||||
cl.channel.chown(msg.id);
|
cl.channel.chown(msg.id);
|
||||||
} else {
|
} else {
|
||||||
if (cl.user._id == cl.channel.crown.userId || cl.channel.crown.dropped)
|
if (cl.user._id == cl.channel.crown.userId || cl.channel.crowndropped)
|
||||||
cl.channel.chown();
|
cl.channel.chown();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
cl.on("chset", msg => {
|
||||||
|
if (!(cl.channel && cl.participantId)) return;
|
||||||
|
if (!(cl.user._id == cl.channel.crown.userId)) return;
|
||||||
|
if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {};
|
||||||
|
let settings = {};
|
||||||
|
settings.lobby = cl.channel.isLobby(cl.channel._id);
|
||||||
|
settings.visible = !!msg.set.visible;
|
||||||
|
settings.crownsolo = !!msg.set.crownsolo;
|
||||||
|
settings.chat = !!msg.set.chat;
|
||||||
|
settings.color = cl.channel.verifyColor(msg.set.color) || cl.channel.settings.color;
|
||||||
|
settings.color2 = cl.channel.verifyColor(msg.set.color2) || cl.channel.settings.color2;
|
||||||
|
cl.channel.settings = settings;
|
||||||
|
console.log(settings)
|
||||||
|
cl.channel.updateCh();
|
||||||
|
})
|
||||||
}
|
}
|
113
src/Room.js
113
src/Room.js
|
@ -10,6 +10,7 @@ class Room extends EventEmitter {
|
||||||
this._id = _id;
|
this._id = _id;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.crown = null;
|
this.crown = null;
|
||||||
|
this.crowndropped = false;
|
||||||
this.settings = {
|
this.settings = {
|
||||||
lobby: this.isLobby(_id),
|
lobby: this.isLobby(_id),
|
||||||
visible: settings.visible || true,
|
visible: settings.visible || true,
|
||||||
|
@ -29,16 +30,22 @@ class Room extends EventEmitter {
|
||||||
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;
|
||||||
if ((this.connections.length == 0 && Array.from(this.ppl.values()).length == 0) && !this.isLobby(this._id)) { //user that created the room, give them the crown.
|
if ((this.connections.length == 0 && Array.from(this.ppl.values()).length == 0) && !this.isLobby(this._id) || (this.crown && this.crown.userId == cl.user._id)) { //user that created the room, give them the crown.
|
||||||
this.crown = {
|
this.crown = {
|
||||||
participantId: cl.participantId,
|
participantId: cl.participantId,
|
||||||
userId: cl.user._id,
|
userId: cl.user._id,
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
startPos: {x: 50, y: 50},
|
startPos: {
|
||||||
endPos: {x: this.getCrownX(), y: this.getCrownY()},
|
x: 50,
|
||||||
dropped: false
|
y: 50
|
||||||
|
},
|
||||||
|
endPos: {
|
||||||
|
x: this.getCrownX(),
|
||||||
|
y: this.getCrownY()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.crowndropped = false;
|
||||||
|
}
|
||||||
this.ppl.set(participantId, cl);
|
this.ppl.set(participantId, cl);
|
||||||
this.connections.push(cl);
|
this.connections.push(cl);
|
||||||
|
|
||||||
|
@ -59,8 +66,8 @@ class Room extends EventEmitter {
|
||||||
this.updateCh(cl);
|
this.updateCh(cl);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
remove(p) { //this is complicated too
|
remove(p) { //this is complicated too
|
||||||
let otheruser = this.connections.filter((a) => a.user._id == p.user._id);
|
let otheruser = this.connections.filter((a) => a.user._id == p.user._id);
|
||||||
if (!(otheruser.length > 1)) {
|
if (!(otheruser.length > 1)) {
|
||||||
this.ppl.delete(p.participantId);
|
this.ppl.delete(p.participantId);
|
||||||
|
@ -70,27 +77,30 @@ remove(p) { //this is complicated too
|
||||||
m: "bye",
|
m: "bye",
|
||||||
p: p.participantId
|
p: p.participantId
|
||||||
}], p);
|
}], p);
|
||||||
|
if (this.crown.userId == p.user._id && !this.crowndropped) {
|
||||||
|
this.chown();
|
||||||
|
}
|
||||||
this.updateCh();
|
this.updateCh();
|
||||||
} else {
|
} else {
|
||||||
this.connections.splice(this.connections.findIndex((a) => a.connectionid == p.connectionid), 1);
|
this.connections.splice(this.connections.findIndex((a) => a.connectionid == p.connectionid), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
updateCh(cl) { //update channel for all people in channel
|
updateCh(cl) { //update channel for all people in channel
|
||||||
if (Array.from(this.ppl.values()).length <= 0) this.destroy();
|
if (Array.from(this.ppl.values()).length <= 0) this.destroy();
|
||||||
this.connections.forEach((usr) => {
|
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 room
|
destroy() { //destroy room
|
||||||
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.connnections;
|
this.connnections;
|
||||||
this.server.rooms.delete(this._id);
|
this.server.rooms.delete(this._id);
|
||||||
}
|
}
|
||||||
sendArray(arr, not) {
|
sendArray(arr, not) {
|
||||||
this.connections.forEach((usr) => {
|
this.connections.forEach((usr) => {
|
||||||
if (!not || usr.participantId != not.participantId) {
|
if (!not || usr.participantId != not.participantId) {
|
||||||
try {
|
try {
|
||||||
|
@ -100,19 +110,18 @@ sendArray(arr, not) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
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);
|
||||||
})
|
})
|
||||||
let crown = this.crown;
|
|
||||||
let data = {
|
let data = {
|
||||||
m: "ch",
|
m: "ch",
|
||||||
p: "ofo",
|
p: "ofo",
|
||||||
ch: {
|
ch: {
|
||||||
count: chppl.length,
|
count: chppl.length,
|
||||||
crown: crown,
|
crown: this.crown,
|
||||||
settings: this.settings,
|
settings: this.settings,
|
||||||
_id: this._id
|
_id: this._id
|
||||||
},
|
},
|
||||||
|
@ -129,30 +138,31 @@ fetchData(usr, cl) {
|
||||||
}
|
}
|
||||||
if (data.ch.crown == null) {
|
if (data.ch.crown == null) {
|
||||||
delete data.ch.crown;
|
delete data.ch.crown;
|
||||||
|
this.crown = crown;
|
||||||
} else {
|
} else {
|
||||||
delete data.ch.crown.dropped;
|
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
verifyColor(color) {
|
verifyColor(color) {
|
||||||
return color; //TODO make this
|
return color; //TODO make this
|
||||||
}
|
}
|
||||||
getColor(_id) {
|
getColor(_id) {
|
||||||
if (this.isLobby(_id)) {
|
if (this.isLobby(_id)) {
|
||||||
return this.server.defaultLobbyColor;
|
return this.server.defaultLobbyColor;
|
||||||
} else {
|
} else {
|
||||||
return this.server.defaultRoomColor;
|
return this.server.defaultRoomColor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getColor2(_id) {
|
getColor2(_id) {
|
||||||
if (this.isLobby(_id)) {
|
if (this.isLobby(_id)) {
|
||||||
return this.server.defaultLobbyColor2;
|
return this.server.defaultLobbyColor2;
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
delete this.settings.color2;
|
delete this.settings.color2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
isLobby(_id) {
|
isLobby(_id) {
|
||||||
if (_id.startsWith("lobby")) {
|
if (_id.startsWith("lobby")) {
|
||||||
if (_id == "lobby") {
|
if (_id == "lobby") {
|
||||||
return true;
|
return true;
|
||||||
|
@ -171,42 +181,59 @@ isLobby(_id) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
getCrownY() {
|
getCrownY() {
|
||||||
return 50 - 30;
|
return 50 - 30;
|
||||||
}
|
}
|
||||||
getCrownX() {
|
getCrownX() {
|
||||||
return 50;
|
return 50;
|
||||||
}
|
}
|
||||||
chown(id) {
|
chown(id) {
|
||||||
let prsn = this.ppl.get(id);
|
let prsn = this.ppl.get(id);
|
||||||
if (prsn) {
|
if (prsn) {
|
||||||
this.crown = {
|
this.crown = {
|
||||||
participantId: prsn.participantId,
|
participantId: prsn.participantId,
|
||||||
userId: prsn.user._id,
|
userId: prsn.user._id,
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
startPos: {x: 50, y: 50},
|
startPos: {
|
||||||
endPos: {x: this.getCrownX(), y: this.getCrownY()},
|
x: 50,
|
||||||
dropped: false
|
y: 50
|
||||||
|
},
|
||||||
|
endPos: {
|
||||||
|
x: this.getCrownX(),
|
||||||
|
y: this.getCrownY()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
this.crowndropped = false;
|
||||||
} else {
|
} else {
|
||||||
this.crown = {
|
this.crown = {
|
||||||
userId: this.crown.userId,
|
userId: this.crown.userId,
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
startPos: {x: 50, y: 50},
|
startPos: {
|
||||||
endPos: {x: this.getCrownX(), y: this.getCrownY()},
|
x: 50,
|
||||||
dropped: true
|
y: 50
|
||||||
|
},
|
||||||
|
endPos: {
|
||||||
|
x: this.getCrownX(),
|
||||||
|
y: this.getCrownY()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.crowndropped = true;
|
||||||
|
}
|
||||||
this.updateCh();
|
this.updateCh();
|
||||||
}
|
}
|
||||||
setCords(p, x, y) {
|
setCords(p, x, y) {
|
||||||
if (p.participantId)
|
if (p.participantId)
|
||||||
x ? this.ppl.get(p.participantId).x = x : {};
|
x ? this.ppl.get(p.participantId).x = x : {};
|
||||||
y ? this.ppl.get(p.participantId).y = y : {};
|
y ? this.ppl.get(p.participantId).y = y : {};
|
||||||
this.sendArray([{m: "m", id: p.participantId, x: this.ppl.get(p.participantId).x, y: this.ppl.get(p.participantId).y}], p);
|
this.sendArray([{
|
||||||
}
|
m: "m",
|
||||||
bindEventListeners() {
|
id: p.participantId,
|
||||||
|
x: this.ppl.get(p.participantId).x,
|
||||||
|
y: this.ppl.get(p.participantId).y
|
||||||
|
}], p);
|
||||||
|
}
|
||||||
|
bindEventListeners() {
|
||||||
this.on("bye", participant => {
|
this.on("bye", participant => {
|
||||||
this.remove(participant);
|
this.remove(participant);
|
||||||
})
|
})
|
||||||
|
@ -214,7 +241,7 @@ bindEventListeners() {
|
||||||
this.on("m", (participant, x, y) => {
|
this.on("m", (participant, x, y) => {
|
||||||
this.setCords(participant, x, y);
|
this.setCords(participant, x, y);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
module.exports = Room;
|
module.exports = Room;
|
Loading…
Reference in New Issue