add better channel settings, add m event.

This commit is contained in:
BopItFreak 2019-06-07 17:10:38 -04:00
parent 61e6625201
commit 78d23ec181
3 changed files with 99 additions and 18 deletions

View File

@ -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);

View File

@ -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)
})
}

View File

@ -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);
})
}
}