-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay.js
128 lines (110 loc) · 3.41 KB
/
play.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var Play;
Play = function(game, store, io) {
this.game = game;
this.store = store;
this.io = io;
this.playerA;
this.playerB;
};
Play.prototype.start = function() {
var self = this;
self.store.getSocketForUser(self.game.createdBy, function(socketId) {
self.playerA = {
userId: self.game.createdBy,
name: self.game.createdByName,
socketId: socketId,
score: 0
};
self.store.getSocketForUser(self.game.joinedBy, function(socketId) {
self.playerB = {
userId: self.game.joinedBy,
name: self.game.joinedByName,
socketId: socketId,
score: 0
};
self.startGame();
});
});
};
Play.prototype.startGame = function() {
this.bootstrapBurst();
this.bootstrapPlayerMovements();
this.io.sockets.socket(this.playerA.socketId).emit("startGame", this.game);
this.io.sockets.socket(this.playerB.socketId).emit("startGame", this.game);
this.run();
};
Play.prototype.bootstrapBurst = function() {
var self = this, currentScoreA, currentScoreB;
self.io.sockets.socket(self.playerA.socketId).on("didBurst", function(balloon) {
currentScoreA = self.playerA.score;
self.playerA.score = balloon.type == 0 ? currentScoreA + 1 : currentScoreA - 1;
console.log(self.playerA.name+" Score:: "+self.playerA.score);
self.io.sockets.socket(self.playerB.socketId).emit("doBurst", balloon.id);
});
self.io.sockets.socket(self.playerB.socketId).on("didBurst", function(balloon) {
currentScoreB = self.playerB.score;
self.playerB.score = balloon.type == 1 ? currentScoreB + 1 : currentScoreB - 1;
self.io.sockets.socket(self.playerA.socketId).emit("doBurst", balloon.id);
console.log(self.playerB.name+" Score:: "+self.playerB.score);
});
};
Play.prototype.bootstrapPlayerMovements = function() {
var self = this;
self.io.sockets.socket(self.playerA.socketId).on("didMove", function(position) {
self.io.sockets.socket(self.playerB.socketId).emit("doMove", position);
});
self.io.sockets.socket(self.playerB.socketId).on("didMove", function(position) {
self.io.sockets.socket(self.playerA.socketId).emit("doMove", position);
});
};
Play.prototype.run = function() {
var self = this;
counter = 0,
max = 10;
var intervalID = setInterval(function() {
if(counter < max) {
counter+=1;
var balloons = self.generateBalloons();
self.io.sockets.socket(self.playerA.socketId).emit("dropBalloons", balloons);
self.io.sockets.socket(self.playerB.socketId).emit("dropBalloons", balloons);
}
else {
clearInterval(intervalID);
self.stop();
}
}, 4000);
};
Play.prototype.stop = function() {
var isTie = false,
winner = '',
scoreA = this.playerA.score,
scoreB = this.playerB.score;
if (scoreA === scoreB){
isTie = true;
} else {
winner = scoreA > scoreB ? this.playerA.name : this.playerB.name;
}
var gameResult = {
winner: winner,
nameA: this.playerA.name,
scoreA: scoreA,
nameB: this.playerB.name,
scoreB: scoreB,
isTie: isTie
};
this.io.sockets.socket(this.playerA.socketId).emit("stopGame", gameResult);
this.io.sockets.socket(this.playerB.socketId).emit("stopGame", gameResult);
};
Play.prototype.generateBalloons = function() {
var count = 1 + Math.floor(Math.random()*20),
balloons = [];
for(var i = 0; i < count; i+=1) {
balloons.push({
id: 1 + Math.floor(Math.random()*10000000000000),
type: Math.floor(Math.random()*2), //0 = playerA; 1 = playerB
position: Math.floor(Math.random()*100) + 1
});
}
return balloons;
};
module.exports = Play;