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

@ -38,4 +38,18 @@ cl.on("m", msg => {
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 : {};