From 0bca3c6460fecb8d8540af7634eb955e0612b0d4 Mon Sep 17 00:00:00 2001 From: BopItFreak Date: Sun, 9 Jun 2019 14:10:25 -0400 Subject: [PATCH] add crown stuff --- src/Message.js | 86 +++++++++++++++++++++++++++++--------------------- src/Room.js | 32 +++++++++++++++++-- 2 files changed, 79 insertions(+), 39 deletions(-) diff --git a/src/Message.js b/src/Message.js index 9c9c0a4..076398f 100644 --- a/src/Message.js +++ b/src/Message.js @@ -1,41 +1,55 @@ const User = require("./User.js"); const Room = require("./Room.js"); -module.exports = (cl) => { -cl.once("hi", () => { - let user = new User(cl); - user.getUserData().then((data) => { - let msg = {}; - msg.m = "hi"; - msg.motd = cl.server.welcome_motd; - msg.t = Date.now(); - msg.u = data; - msg.v = "1.0 Alpha"; - cl.sendArray([msg]) - cl.user = data; +module.exports = (cl) => { + cl.once("hi", () => { + let user = new User(cl); + user.getUserData().then((data) => { + let msg = {}; + msg.m = "hi"; + msg.motd = cl.server.welcome_motd; + msg.t = Date.now(); + msg.u = data; + msg.v = "1.0 Alpha"; + cl.sendArray([msg]) + cl.user = data; + }) }) -}) -cl.on("t", msg => { - if (msg.hasOwnProperty("e") && !isNaN(msg.e)) - cl.sendArray([{ - m: "t", - t: Date.now(), - e: msg.e - }]) -}) -cl.on("ch", msg => { - 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) + cl.on("t", msg => { + if (msg.hasOwnProperty("e") && !isNaN(msg.e)) + cl.sendArray([{ + m: "t", + t: Date.now(), + e: msg.e + }]) + }) + cl.on("ch", msg => { + 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) -}) + }) + cl.on("chown", msg => { + if (!(cl.channel && cl.participantId)) return; + console.log((Date.now() - cl.channel.crown.time)) + 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 (msg.hasOwnProperty("id")) { + console.log(cl.channel.crown) + if (cl.user._id == cl.channel.crown.userId || cl.channel.crown.dropped) + cl.channel.chown(msg.id); + } else { + if (cl.user._id == cl.channel.crown.userId || cl.channel.crown.dropped) + cl.channel.chown(); + } + }) } \ No newline at end of file diff --git a/src/Room.js b/src/Room.js index 5bf1a7a..61cf49c 100644 --- a/src/Room.js +++ b/src/Room.js @@ -29,13 +29,14 @@ class Room extends EventEmitter { 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. + 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. this.crown = { participantId: cl.participantId, userId: cl.user._id, time: Date.now(), startPos: {x: 50, y: 50}, - endPos: {x: this.getCrownX(), y: this.getCrownY()} + endPos: {x: this.getCrownX(), y: this.getCrownY()}, + dropped: false } } this.ppl.set(participantId, cl); @@ -105,12 +106,13 @@ fetchData(usr, cl) { [...this.ppl.values()].forEach((a) => { chppl.push(a.user); }) + let crown = this.crown; let data = { m: "ch", p: "ofo", ch: { count: chppl.length, - crown: this.crown, + crown: crown, settings: this.settings, _id: this._id }, @@ -127,6 +129,8 @@ fetchData(usr, cl) { } if (data.ch.crown == null) { delete data.ch.crown; + } else { + delete data.ch.crown.dropped; } return data; } @@ -174,6 +178,28 @@ getCrownY() { getCrownX() { return 50; } +chown(id) { + let prsn = this.ppl.get(id); + if (prsn) { + this.crown = { + participantId: prsn.participantId, + userId: prsn.user._id, + time: Date.now(), + startPos: {x: 50, y: 50}, + endPos: {x: this.getCrownX(), y: this.getCrownY()}, + dropped: false + } + } else { + this.crown = { + userId: this.crown.userId, + time: Date.now(), + startPos: {x: 50, y: 50}, + endPos: {x: this.getCrownX(), y: this.getCrownY()}, + dropped: true + } + } + this.updateCh(); +} setCords(p, x, y) { if (p.participantId) x ? this.ppl.get(p.participantId).x = x : {};