From 78d23ec181a4f27721fbf820c77e7dcc3542f9b0 Mon Sep 17 00:00:00 2001 From: BopItFreak Date: Fri, 7 Jun 2019 17:10:38 -0400 Subject: [PATCH] add better channel settings, add m event. --- src/Client.js | 1 + src/Message.js | 11 +++++- src/Room.js | 105 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 99 insertions(+), 18 deletions(-) diff --git a/src/Client.js b/src/Client.js index 887a629..d7af903 100644 --- a/src/Client.js +++ b/src/Client.js @@ -22,6 +22,7 @@ class Client extends EventEmitter { return this.ws && this.ws.readyState === WebSocket.CONNECTING; } setChannel(_id, settings) { + if (this.channel && this.channel._id == _id) return; if (this.server.rooms.get(_id)) { let channel = this.channel; if (channel) this.channel.emit("bye", this); diff --git a/src/Message.js b/src/Message.js index 549f6a8..9c9c0a4 100644 --- a/src/Message.js +++ b/src/Message.js @@ -23,10 +23,19 @@ cl.on("t", msg => { }]) }) cl.on("ch", msg => { - if (!msg.hasOwnProperty("set")) msg.set = {}; + if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {}; if (msg.hasOwnProperty("_id") && typeof msg._id == "string") { if (msg._id.length > 512) return; cl.setChannel(msg._id, msg.set); } }) +cl.on("m", msg => { + if (!(cl.channel && cl.participantId)) return; + if (!msg.hasOwnProperty("x")) msg.x = null; + if (!msg.hasOwnProperty("y")) msg.y = null; + if (parseInt(msg.x) == NaN) msg.x = null; + if (parseInt(msg.y) == NaN) msg.y = null; + cl.channel.emit("m", cl, msg.x, msg.y) + +}) } \ No newline at end of file diff --git a/src/Room.js b/src/Room.js index 2c73c1b..5bf1a7a 100644 --- a/src/Room.js +++ b/src/Room.js @@ -9,27 +9,37 @@ class Room extends EventEmitter { EventEmitter.call(this); this._id = _id; this.server = server; + this.crown = null; this.settings = { - lobby: true, + lobby: this.isLobby(_id), visible: settings.visible || true, crownsolo: settings.crownsolo || false, chat: settings.chat || true, - color: this.verifyColor(settings.color) || this.server.defaultRoomColor, - color2: this.verifyColor(settings.color) || this.defaultLobbyColor2 + color: this.verifyColor(settings.color) || this.getColor(_id), + color2: this.verifyColor(settings.color) || this.getColor2(_id) } this.ppl = new Map(); this.connections = []; this.bindEventListeners(); this.server.rooms.set(_id, this); } - 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) { + join(cl) { //this stuff is complicated + let otheruser = this.connections.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); cl.user.id = participantId; cl.participantId = participantId; + if (this.connections.length == 0 && Array.from(this.ppl.values()).length == 0) { //user that created the room, give them the crown. + this.crown = { + participantId: cl.participantId, + userId: cl.user._id, + time: Date.now(), + startPos: {x: 50, y: 50}, + endPos: {x: this.getCrownX(), y: this.getCrownY()} + } + } this.ppl.set(participantId, cl); - this.connections.push(cl); + this.connections.push(cl); this.sendArray([{ color: this.ppl.get(cl.participantId).user.color, @@ -44,13 +54,13 @@ class Room extends EventEmitter { } else { cl.user.id = otheruser.participantId; cl.participantId = otheruser.participantId; - this.connections.push(cl); + this.connections.push(cl); this.updateCh(cl); } - + } -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); +remove(p) { //this is complicated too + let otheruser = this.connections.filter((a) => a.user._id == p.user._id); if (!(otheruser.length > 1)) { this.ppl.delete(p.participantId); this.connections.splice(this.connections.findIndex((a) => a.connectionid == p.connectionid), 1); @@ -58,28 +68,29 @@ remove(p) { this.sendArray([{ m: "bye", p: p.participantId - }]); + }], p); this.updateCh(); } else { this.connections.splice(this.connections.findIndex((a) => a.connectionid == p.connectionid), 1); - } + } } -updateCh(cl) { +updateCh(cl) { //update channel for all people in channel 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)]) }) } -destroy() { +destroy() { //destroy room this._id; console.log(`Deleted room ${this._id}`); this.settings = {}; this.ppl; - this.server.rooms.delete(_id); + this.connnections; + this.server.rooms.delete(this._id); } sendArray(arr, not) { - this.ppl.forEach((usr) => { + this.connections.forEach((usr) => { if (!not || usr.participantId != not.participantId) { try { this.server.connections.get(usr.connectionid).sendArray(arr) @@ -96,8 +107,10 @@ fetchData(usr, cl) { }) let data = { m: "ch", + p: "ofo", ch: { count: chppl.length, + crown: this.crown, settings: this.settings, _id: this._id }, @@ -106,17 +119,75 @@ fetchData(usr, cl) { if (cl) { if (usr.user.id == cl.user.id) { data.p = cl.participantId; + } else { + delete data.p; } + } else { + delete data.p; + } + if (data.ch.crown == null) { + delete data.ch.crown; } return data; } verifyColor(color) { return color; //TODO make this } +getColor(_id) { + if (this.isLobby(_id)) { + return this.server.defaultLobbyColor; + } else { + return this.server.defaultRoomColor; + } +} +getColor2(_id) { + if (this.isLobby(_id)) { + return this.server.defaultLobbyColor2; + } else { + return; + delete this.settings.color2; + } +} +isLobby(_id) { + if (_id.startsWith("lobby")) { + if (_id == "lobby") { + return true; + } else if (parseFloat(_id.split("lobby")[1] % 1) === 0) { + return true; + } else { + return false; + } + } else if (_id.startsWith("test/")) { + if (_id == "test/") { + return false; + } else { + return true; + } + } else { + return false; + } + +} +getCrownY() { + return 50 - 30; +} +getCrownX() { + return 50; +} +setCords(p, x, y) { + if (p.participantId) + x ? this.ppl.get(p.participantId).x = x : {}; + 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); +} bindEventListeners() { this.on("bye", participant => { this.remove(participant); }) + + this.on("m", (participant, x, y) => { + this.setCords(participant, x, y); + }) } }