forked from Hri7566/mpp-server-dev2
fix bugs, add chset
This commit is contained in:
parent
0bca3c6460
commit
bce9e3d155
|
@ -12,6 +12,7 @@ class Client extends EventEmitter {
|
|||
this.ws = ws;
|
||||
this.req = req;
|
||||
this.ip = (req.connection.remoteAddress).replace("::ffff:", "");
|
||||
this.destroied = false;
|
||||
this.bindEventListeners();
|
||||
require('./Message.js')(this);
|
||||
}
|
||||
|
@ -45,36 +46,39 @@ class Client extends EventEmitter {
|
|||
}
|
||||
destroy() {
|
||||
this.ws.close();
|
||||
if (this.channel) {
|
||||
this.channel.emit("bye", this)
|
||||
}
|
||||
this.user;
|
||||
this.participantId;
|
||||
this.channel;
|
||||
this.connectionid;
|
||||
this.server.connections.delete(this.connectionid);
|
||||
if (this.channel) {
|
||||
this.channel.emit("bye", this)
|
||||
}
|
||||
this.destroied = true;
|
||||
console.log(`Removed Connection ${this.connectionid}.`);
|
||||
}
|
||||
bindEventListeners() {
|
||||
this.ws.on("message", (evt) => {
|
||||
try {
|
||||
var transmission = JSON.parse(evt);
|
||||
} catch (e) {
|
||||
this.destroy();
|
||||
} finally {
|
||||
let transmission = JSON.parse(evt);
|
||||
for (let msg of transmission) {
|
||||
if (!msg.hasOwnProperty("m")) return;
|
||||
if (!this.server.legit_m.includes(msg.m)) return;
|
||||
this.emit(msg.m, msg);
|
||||
console.log(`RECIEVE: `, JSON.colorStringify(msg));
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
this.destroy();
|
||||
}
|
||||
});
|
||||
this.ws.on("close", () => {
|
||||
if (!this.destroied)
|
||||
this.destroy();
|
||||
});
|
||||
this.ws.addEventListener("error", (err) => {
|
||||
console.error(err);
|
||||
if (!this.destroied)
|
||||
this.destroy();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -40,16 +40,31 @@ module.exports = (cl) => {
|
|||
})
|
||||
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));
|
||||
//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)
|
||||
// console.log(cl.channel.crown)
|
||||
if (cl.user._id == cl.channel.crown.userId || cl.channel.crowndropped)
|
||||
cl.channel.chown(msg.id);
|
||||
} 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.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();
|
||||
})
|
||||
}
|
55
src/Room.js
55
src/Room.js
|
@ -10,6 +10,7 @@ class Room extends EventEmitter {
|
|||
this._id = _id;
|
||||
this.server = server;
|
||||
this.crown = null;
|
||||
this.crowndropped = false;
|
||||
this.settings = {
|
||||
lobby: this.isLobby(_id),
|
||||
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);
|
||||
cl.user.id = 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 = {
|
||||
participantId: cl.participantId,
|
||||
userId: cl.user._id,
|
||||
time: Date.now(),
|
||||
startPos: {x: 50, y: 50},
|
||||
endPos: {x: this.getCrownX(), y: this.getCrownY()},
|
||||
dropped: false
|
||||
startPos: {
|
||||
x: 50,
|
||||
y: 50
|
||||
},
|
||||
endPos: {
|
||||
x: this.getCrownX(),
|
||||
y: this.getCrownY()
|
||||
}
|
||||
}
|
||||
this.crowndropped = false;
|
||||
}
|
||||
this.ppl.set(participantId, cl);
|
||||
this.connections.push(cl);
|
||||
|
||||
|
@ -70,6 +77,9 @@ remove(p) { //this is complicated too
|
|||
m: "bye",
|
||||
p: p.participantId
|
||||
}], p);
|
||||
if (this.crown.userId == p.user._id && !this.crowndropped) {
|
||||
this.chown();
|
||||
}
|
||||
this.updateCh();
|
||||
} else {
|
||||
this.connections.splice(this.connections.findIndex((a) => a.connectionid == p.connectionid), 1);
|
||||
|
@ -106,13 +116,12 @@ 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: crown,
|
||||
crown: this.crown,
|
||||
settings: this.settings,
|
||||
_id: this._id
|
||||
},
|
||||
|
@ -129,8 +138,9 @@ fetchData(usr, cl) {
|
|||
}
|
||||
if (data.ch.crown == null) {
|
||||
delete data.ch.crown;
|
||||
this.crown = crown;
|
||||
} else {
|
||||
delete data.ch.crown.dropped;
|
||||
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
@ -185,26 +195,43 @@ chown(id) {
|
|||
participantId: prsn.participantId,
|
||||
userId: prsn.user._id,
|
||||
time: Date.now(),
|
||||
startPos: {x: 50, y: 50},
|
||||
endPos: {x: this.getCrownX(), y: this.getCrownY()},
|
||||
dropped: false
|
||||
startPos: {
|
||||
x: 50,
|
||||
y: 50
|
||||
},
|
||||
endPos: {
|
||||
x: this.getCrownX(),
|
||||
y: this.getCrownY()
|
||||
},
|
||||
}
|
||||
this.crowndropped = 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
|
||||
startPos: {
|
||||
x: 50,
|
||||
y: 50
|
||||
},
|
||||
endPos: {
|
||||
x: this.getCrownX(),
|
||||
y: this.getCrownY()
|
||||
}
|
||||
}
|
||||
this.crowndropped = true;
|
||||
}
|
||||
this.updateCh();
|
||||
}
|
||||
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);
|
||||
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 => {
|
||||
|
|
Loading…
Reference in New Issue