-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.js
71 lines (59 loc) · 1.76 KB
/
room.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Room {
constructor(socket) {
console.log('Room created !');
this.players = new Array()
this.isFull = false
this.playerReady = 0
this.gameState = "waiting"
this.round = 1
}
askForSong() {
this.chooser = this.players[this.round % this.players.length]
console.log("Emit Pick")
this.chooser.emit('pick', '')
this.chooser.once('pick', (songData) => {
this.players[0].emit('play', songData)
this.players[1].emit('play', songData)
this.gameState = "playing"
this.round += 1
})
}
addPlayer(socket) {
this.players.push(socket)
console.log('Player Added')
if (this.players.length == 2) {
console.log('this room is now full')
this.isFull = true
}
}
roomIsFull() {
if (this.players.length == 2) {
this.isFull = true
}
return this.isFull
}
setReady(socket) {
this.playerReady += 1
console.log('Player Ready ' + this.playerReady)
if (this.playerReady == 2) {
this.askForSong()
this.playerReady = 0
}
}
playerAnswer(socket, data) {
console.log("Answered_2")
if (this.gameState != "playing")
return
this.gameState = "waiting"
console.log("Answered_3")
if (socket.id == this.players[0].id) {
this.players[0].emit("answer", data)
this.players[1].emit("otherAnswer", data)
}
else if (socket.id == this.players[1].id) {
this.players[1].emit("answer", data)
this.players[0].emit("otherAnswer", data)
}
}
}
module.exports = Room;