Skip to content

Commit e04c96c

Browse files
committed
adding the left out files
1 parent 9572bdf commit e04c96c

8 files changed

+422
-0
lines changed

play.js

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
var Play;
2+
3+
Play = function(game, store, io) {
4+
this.game = game;
5+
this.store = store;
6+
this.io = io;
7+
8+
this.playerA;
9+
this.playerB;
10+
};
11+
12+
Play.prototype.start = function() {
13+
var self = this;
14+
self.store.getSocketForUser(self.game.createdBy, function(socketId) {
15+
self.playerA = {
16+
userId: self.game.createdBy,
17+
name: self.game.createdByName,
18+
socketId: socketId,
19+
score: 0
20+
};
21+
22+
self.store.getSocketForUser(self.game.joinedBy, function(socketId) {
23+
self.playerB = {
24+
userId: self.game.joinedBy,
25+
name: self.game.joinedByName,
26+
socketId: socketId,
27+
score: 0
28+
};
29+
30+
self.startGame();
31+
});
32+
});
33+
};
34+
35+
Play.prototype.startGame = function() {
36+
this.bootstrapBurst();
37+
this.bootstrapPlayerMovements();
38+
this.io.sockets.socket(this.playerA.socketId).emit("startGame", this.game);
39+
this.io.sockets.socket(this.playerB.socketId).emit("startGame", this.game);
40+
this.run();
41+
};
42+
43+
Play.prototype.bootstrapBurst = function() {
44+
var self = this, currentScoreA, currentScoreB;
45+
self.io.sockets.socket(self.playerA.socketId).on("didBurst", function(balloon) {
46+
currentScoreA = self.playerA.score;
47+
self.playerA.score = balloon.type == 0 ? currentScoreA + 1 : currentScoreA - 1;
48+
console.log(self.playerA.name+" Score:: "+self.playerA.score);
49+
self.io.sockets.socket(self.playerB.socketId).emit("doBurst", balloon.id);
50+
});
51+
self.io.sockets.socket(self.playerB.socketId).on("didBurst", function(balloon) {
52+
currentScoreB = self.playerB.score;
53+
self.playerB.score = balloon.type == 1 ? currentScoreB + 1 : currentScoreB - 1;
54+
self.io.sockets.socket(self.playerA.socketId).emit("doBurst", balloon.id);
55+
console.log(self.playerB.name+" Score:: "+self.playerB.score);
56+
});
57+
};
58+
59+
Play.prototype.bootstrapPlayerMovements = function() {
60+
var self = this;
61+
self.io.sockets.socket(self.playerA.socketId).on("didMove", function(position) {
62+
self.io.sockets.socket(self.playerB.socketId).emit("doMove", position);
63+
});
64+
self.io.sockets.socket(self.playerB.socketId).on("didMove", function(position) {
65+
self.io.sockets.socket(self.playerA.socketId).emit("doMove", position);
66+
});
67+
};
68+
69+
Play.prototype.run = function() {
70+
var self = this;
71+
counter = 0,
72+
max = 10;
73+
74+
var intervalID = setInterval(function() {
75+
if(counter < max) {
76+
counter+=1;
77+
var balloons = self.generateBalloons();
78+
self.io.sockets.socket(self.playerA.socketId).emit("dropBalloons", balloons);
79+
self.io.sockets.socket(self.playerB.socketId).emit("dropBalloons", balloons);
80+
}
81+
else {
82+
clearInterval(intervalID);
83+
self.stop();
84+
}
85+
}, 2000);
86+
};
87+
88+
Play.prototype.stop = function() {
89+
var isTie = false,
90+
winner = '',
91+
scoreA = this.playerA.score,
92+
scoreB = this.playerB.score;
93+
94+
if (scoreA === scoreB){
95+
isTie = true;
96+
} else {
97+
winner = scoreA > scoreB ? this.playerA.name : this.playerB.name;
98+
}
99+
100+
var gameResult = {
101+
winner: winner,
102+
nameA: this.playerA.name,
103+
scoreA: scoreA,
104+
nameB: this.playerB.name,
105+
scoreB: scoreB,
106+
isTie: isTie
107+
};
108+
this.io.sockets.socket(this.playerA.socketId).emit("stopGame", gameResult);
109+
this.io.sockets.socket(this.playerB.socketId).emit("stopGame", gameResult);
110+
};
111+
112+
Play.prototype.generateBalloons = function() {
113+
var count = 1 + Math.floor(Math.random()*20),
114+
balloons = [];
115+
116+
for(var i = 0; i < count; i+=1) {
117+
balloons.push({
118+
id: 1 + Math.floor(Math.random()*10000000000000),
119+
type: Math.floor(Math.random()*2), //0 = playerA; 1 = playerB
120+
position: Math.floor(Math.random()*100) + 1
121+
});
122+
}
123+
124+
return balloons;
125+
};
126+
127+
128+
module.exports = Play;

sockets.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var cookie = require('cookie'),
2+
meta = require('./meta');
3+
4+
exports.setup = function(io, store) {
5+
io.sockets.on('connection', function (socket) {
6+
if(socket.handshake.headers.cookie) {
7+
var userId = cookie.parse(socket.handshake.headers.cookie)[meta.userId];
8+
if(userId) {
9+
store.setSocketForUser(socket.id, userId);
10+
socket.emit("registeredUser", {});
11+
}
12+
}
13+
});
14+
};
15+
16+
exports.broadCastNewGame = function(io, game) {
17+
io.sockets.emit("newGame", game);
18+
};

store.js

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
var Store = function(db) {
2+
3+
this.db = db;
4+
this.userCollection = db.collection("users");
5+
this.gameCollection = db.collection("games");
6+
};
7+
8+
Store.prototype.setUser = function(userId, name, callback) {
9+
var user = {
10+
userId: userId,
11+
name: name
12+
};
13+
this.userCollection.insert(user, function (err, inserted) {
14+
if(err) throw err;
15+
callback();
16+
});
17+
};
18+
19+
Store.prototype.getUser = function(userId, callback) {
20+
this.userCollection.find({userId: userId}).toArray(function(err, results) {
21+
if(err) throw err;
22+
callback(results.length ? results[0] : undefined);
23+
});
24+
};
25+
26+
Store.prototype.deleteUser = function(userId, callback) {
27+
var self = this;
28+
self.gameCollection.remove({
29+
createdBy: {
30+
$in: [userId]
31+
}
32+
}, function(err) {
33+
if(err) throw err;
34+
self.userCollection.remove({userId: userId}, function(err, results) {
35+
if(err) throw err;
36+
callback(results);
37+
});
38+
});
39+
};
40+
41+
Store.prototype.getGameCreatedByUser = function(userId, callback) {
42+
this.gameCollection.find({
43+
createdBy: {
44+
$in: [userId]
45+
}
46+
}).toArray(function(err, results) {
47+
if(err) throw err;
48+
callback(results.length ? results[0] : undefined);
49+
});
50+
};
51+
52+
Store.prototype.getGamesReadyToPlay = function(userId, callback) {
53+
this.gameCollection.find({
54+
joinedBy: {
55+
$exists: false
56+
},
57+
createdBy: {
58+
$ne: userId
59+
}
60+
}).toArray(function(err, results) {
61+
if(err) throw err;
62+
callback(results);
63+
});
64+
};
65+
66+
Store.prototype.getGamesBeingPlayed = function(callback) {
67+
this.gameCollection.find({joinedBy: {$exists: true}}).toArray(function(err, results) {
68+
if(err) throw err;
69+
callback(results);
70+
});
71+
};
72+
73+
Store.prototype.createGame = function(gameId, user, callback) {
74+
var game = {
75+
gameId: gameId,
76+
createdBy: user.userId,
77+
createdByName: user.name,
78+
joinedByName: ''
79+
};
80+
this.gameCollection.insert(game, function(err, results) {
81+
if(err) {
82+
throw err;
83+
}
84+
callback(results[0]);
85+
});
86+
};
87+
88+
Store.prototype.joinGame = function(gameId, user, callback) {
89+
var self = this;
90+
self.gameCollection.update({gameId: gameId}, {$set: {joinedBy: user.userId, joinedByName: user.name}}, function(err) {
91+
if(err) {
92+
throw err;
93+
}
94+
self.gameCollection.find({gameId: gameId}).toArray(function(err, results) {
95+
if(err) throw err;
96+
callback(results.length ? results[0] : undefined);
97+
});
98+
});
99+
};
100+
101+
Store.prototype.deleteAll = function(callback) {
102+
var self = this;
103+
self.gameCollection.remove(function(err) {
104+
if(err) {
105+
throw err;
106+
}
107+
self.userCollection.remove(function(err) {
108+
if(err) {
109+
throw err;
110+
}
111+
callback();
112+
});
113+
});
114+
};
115+
116+
Store.prototype.setSocketForUser = function(socketId, userId) {
117+
this.userCollection.update({userId: userId}, {$set: {socketId: socketId }}, function(err) {
118+
if(err) {
119+
throw err;
120+
}
121+
console.dir("updated socketId for User: " + userId);
122+
});
123+
};
124+
125+
Store.prototype.getSocketForUser = function(userId, callback) {
126+
this.userCollection.find({userId: userId}).toArray(function(err, results) {
127+
if(err) throw err;
128+
var socketId;
129+
if(results.length && results[0].socketId) {
130+
socketId = results[0].socketId;
131+
}
132+
callback(socketId);
133+
});
134+
};
135+
136+
module.exports = Store;

templates/game.dust

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<canvas id="canvas"></canvas>

templates/gamesInfo.dust

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<div>
2+
{?games}
3+
<div class="list-group" style="width: 80%;margin-left: 10%;">
4+
<div id="gamesList">
5+
<a class="list-group-item active" style="font-size:18px;">Open games - Select your opponent</a>
6+
{#games}
7+
<a class="list-group-item joinBtn" style="font-size:16px;" data-id="{gameId}">
8+
<img src="css/Play.png" style="width: 50px;"/> Play with <strong>{createdByName}</strong></a>
9+
{/games}
10+
</div>
11+
</div>
12+
<div class="progress" style="margin-top: 21px;">
13+
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div>
14+
</div>
15+
{:else}
16+
<p class="text-center text-danger">No open games!!</p>
17+
<div class="progress" style="margin-top: 21px;">
18+
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div>
19+
</div>
20+
{/games}
21+
22+
23+
{^userCreatedGame}
24+
<button id='startGameBtn' type="button" class="btn btn-primary btn-block">Create a New game</button>
25+
{/userCreatedGame}
26+
{?userCreatedGame}
27+
<p class="text-info text-center " style="font-size:18px;">You have created one game, wait for someone to join!<p>
28+
{/userCreatedGame}
29+
</div>

templates/master.dust

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>balloon bursters</title>
7+
<link rel="stylesheet" href="css/style.css">
8+
<link rel="stylesheet" href="css/bootstrap.css">
9+
<link rel="stylesheet" href="css/animate.css">
10+
<link href='http://fonts.googleapis.com/css?family=Nosifer' rel='stylesheet' type='text/css'>
11+
</head>
12+
13+
<body class="bg">
14+
<h2 class="page-header">balloon bursters</h2>
15+
<iframe class="vote" src="http://nodeknockout.com/iframe/synth" frameborder=0 scrolling=no allowtransparency=true width=115 height=25></iframe>
16+
<div id="container">
17+
<div id="well" class="jumbotron well well-lg" style="width: 75%; margin-top: 10%;">
18+
<div id="register" class="hide">
19+
<div class="panel panel-info">
20+
<div class="panel-heading">Enter your name to start playing!</div>
21+
<div class="panel-body">
22+
<p>Two player game, join the fun!</p>
23+
<form id="registerForm">
24+
<div class="row">
25+
<div class="col-lg-6">
26+
<div class="input-group">
27+
<input type="text" class="form-control" name="name" autocorrect="off" value="" placeholder="Name">
28+
<span class="input-group-btn">
29+
<button class="btn btn-primary" type="submit" id="registerBtn" type="button">Start</button>
30+
</span>
31+
</div>
32+
</div>
33+
</div>
34+
</form>
35+
</div>
36+
</div>
37+
</div>
38+
39+
<div id='dashboard' class="hide">
40+
<div class="panel panel-info">
41+
<div id='userInfoWrapper' class="panel-heading"></div>
42+
<div class="panel-body">
43+
<p class="">You can select any of the open games and start playing OR create a new game and ask your friends to join!</p>
44+
<div id='gamesInfoWrapper'></div>
45+
</div>
46+
</div>
47+
</div>
48+
</div>
49+
50+
<div id="game"></div>
51+
</div>
52+
53+
<script src="js/lib/socket.io.js"></script>
54+
<script src="js/lib/jquery.min.js"></script>
55+
<script src="js/lib/dust-core-2.2.0.js"></script>
56+
<script src="js/lib/bootstrap.js"></script>
57+
<script src="js/lib/box2dv2.js"></script>
58+
<script src="js/game.js"></script>
59+
<script src="js/main.js"></script>
60+
61+
<script src="views/userInfo.js"></script>
62+
<script src="views/gamesInfo.js"></script>
63+
<script src="views/game.js"></script>
64+
</body>
65+
66+
</html>

templates/userInfo.dust

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Registered as - <b>{name}</b>

0 commit comments

Comments
 (0)