add crown stuff

This commit is contained in:
BopItFreak 2019-06-09 14:10:25 -04:00
parent 78d23ec181
commit 0bca3c6460
2 changed files with 79 additions and 39 deletions

View File

@ -1,41 +1,55 @@
const User = require("./User.js"); const User = require("./User.js");
const Room = require("./Room.js"); const Room = require("./Room.js");
module.exports = (cl) => { module.exports = (cl) => {
cl.once("hi", () => { cl.once("hi", () => {
let user = new User(cl); let user = new User(cl);
user.getUserData().then((data) => { user.getUserData().then((data) => {
let msg = {}; let msg = {};
msg.m = "hi"; msg.m = "hi";
msg.motd = cl.server.welcome_motd; msg.motd = cl.server.welcome_motd;
msg.t = Date.now(); msg.t = Date.now();
msg.u = data; msg.u = data;
msg.v = "1.0 Alpha"; msg.v = "1.0 Alpha";
cl.sendArray([msg]) cl.sendArray([msg])
cl.user = data; cl.user = data;
})
}) })
}) cl.on("t", msg => {
cl.on("t", msg => { if (msg.hasOwnProperty("e") && !isNaN(msg.e))
if (msg.hasOwnProperty("e") && !isNaN(msg.e)) cl.sendArray([{
cl.sendArray([{ m: "t",
m: "t", t: Date.now(),
t: Date.now(), e: msg.e
e: msg.e }])
}]) })
}) cl.on("ch", msg => {
cl.on("ch", msg => { if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {};
if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {}; if (msg.hasOwnProperty("_id") && typeof msg._id == "string") {
if (msg.hasOwnProperty("_id") && typeof msg._id == "string") { if (msg._id.length > 512) return;
if (msg._id.length > 512) return;
cl.setChannel(msg._id, msg.set); cl.setChannel(msg._id, msg.set);
} }
}) })
cl.on("m", msg => { cl.on("m", msg => {
if (!(cl.channel && cl.participantId)) return; if (!(cl.channel && cl.participantId)) return;
if (!msg.hasOwnProperty("x")) msg.x = null; if (!msg.hasOwnProperty("x")) msg.x = null;
if (!msg.hasOwnProperty("y")) msg.y = null; if (!msg.hasOwnProperty("y")) msg.y = null;
if (parseInt(msg.x) == NaN) msg.x = null; if (parseInt(msg.x) == NaN) msg.x = null;
if (parseInt(msg.y) == NaN) msg.y = null; if (parseInt(msg.y) == NaN) msg.y = null;
cl.channel.emit("m", cl, msg.x, msg.y) 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();
}
})
} }

View File

@ -29,13 +29,14 @@ 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) { //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 = { 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: {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); this.ppl.set(participantId, cl);
@ -105,12 +106,13 @@ fetchData(usr, cl) {
[...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: this.crown, crown: crown,
settings: this.settings, settings: this.settings,
_id: this._id _id: this._id
}, },
@ -127,6 +129,8 @@ fetchData(usr, cl) {
} }
if (data.ch.crown == null) { if (data.ch.crown == null) {
delete data.ch.crown; delete data.ch.crown;
} else {
delete data.ch.crown.dropped;
} }
return data; return data;
} }
@ -174,6 +178,28 @@ getCrownY() {
getCrownX() { getCrownX() {
return 50; 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) { 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 : {};