-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
256 lines (211 loc) · 7.24 KB
/
app.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var socketio = require('socket.io');
var app = express();
// All environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon(path.join(__dirname, 'public/images/favicon.ico')));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
// Development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// Create the server
var server = http.createServer(app);
//Start the web socket server
var io = socketio.listen(server);
// Mongoose
//Create to the DB if it doesn't exist and connect to it
var mongoose = require('mongoose');
mongoURI = process.env.MONGODB_URI || 'mongodb://localhost/monsterthanks';
mongoose.connect(mongoURI);
//setup our MongoDB Card collection
var models = require('./models/cards');
var Card = mongoose.model('Card', models.cardSchema);
var Monster = mongoose.model('Monster', models.monsterSchema);
app.get('/', routes.index);
// app.get('/users', user.list);
app.get('/new', function(req, res){
//Create a new Card document
var card = new models.Card();
// Initialize 50 monsters whenever a Card is created
for (var i = 0; i < 50; i++) {
card.monsters.push(new Monster({monsterId: i}));
}
var id = card._id;
var path = '/' + id;
//Save the Card document
card.save(function(err){
if (err) {
res.send(500, 'Card is NOT saved');
}
else {
res.redirect(path);
}
});
});
app.get('/:id', function(req, res) {
Card.findOne({_id: req.params.id}, function(err, card){
res.render('card', card);
});
});
io.sockets.on('connection', function(socket) {
//socket is the specific connection that's fired
socket.on('cardId', function(cardId) {
Card.findOne({_id: cardId}, function(err, card){
if (err) {
res.send(500, 'Card is NOT found');
}
else {
// When I request a card, it only comes back to me instead of everyone else
socket.emit('cardLoaded', card);
}
});
});
socket.on('draggedMonster', function(draggedMonster) {
Card.findOne({_id: draggedMonster.id}, function(err, card){
for (var i = 0; i < card.monsters.length; i++) {
if (card.monsters[i].monsterId === draggedMonster.monsterId) {
card.monsters[i].top = draggedMonster.top;
card.monsters[i].left = draggedMonster.left;
// console.log('found monster: ', card.monsters[i]);
card.save(function (err, card) {
if (err) {
res.send(500, "Monster's new position is NOT saved");
}
else {
socket.broadcast.emit('cardSaved', card);
// card = {
// __v: 0,
// _id: 52799a4a7776e20000000002,
// createdAt: Tue Nov 05 2013 18:24:26 GMT-0700 (MST),
// background: 'backyard.jpg',
// monsters:
// [ { _id: 52799a4a7776e20000000003,
// left: 25.905511811023622,
// monsterId: 0,
// speechBubble: 'this is my message',
// top: 41.77274870344178,
// height: 100,
// width: 100
// },
}
});
}
}
});
});
socket.on('resizedMonster', function(resizedMonster) {
Card.findOne({_id: resizedMonster.id}, function(err, card){
for (var i = 0; i < card.monsters.length; i++) {
if (card.monsters[i].monsterId === resizedMonster.monsterId) {
card.monsters[i].width = resizedMonster.width;
card.monsters[i].height = resizedMonster.height;
// console.log('found monster: ', card.monsters[i]);
card.save(function (err, card) {
if (err) {
res.send(500, "Monster's new size is NOT saved");
}
else {
socket.broadcast.emit('cardSaved', card);
}
});
}
}
});
});
socket.on('messageEntered', function(messageEntered) {
Card.findOne({_id: messageEntered.id}, function(err, card){
for (var i = 0; i < card.monsters.length; i++) {
if (card.monsters[i].monsterId === messageEntered.monsterId) {
card.monsters[i].speechBubble = messageEntered.message;
// console.log('found monster: ', card.monsters[i]);
card.save(function (err, card) {
if (err) {
res.send(500, "Monster's new message is NOT saved");
}
else {
socket.broadcast.emit('msgSaved', card);
}
});
}
}
});
});
var backgroundArray = ['backyard', 'desert2', 'fishes', 'hills', 'mars', 'red-room', 'aquarium', 'bench', 'seascape', 'blue-stage', 'city', 'corridor', 'desert-oasis', 'sheep', 'desert', 'egypt', 'field', 'mtns', 'summer-sea-view', 'park', 'sheeps-on-a-hill', 'sea-voyage', 'seascape2', 'snow', 'spaceship-on-mars', 'spaceship', 'stage', 'sunset-sheep', 'underwater', 'watertower', 'yellow-room', 'dark-forest'];
var goToNextImg = function(currentImg) {
if (currentImg === 'dark-forest') {
return 'backyard';
}
else {
var currentIndex = backgroundArray.indexOf(currentImg);
return backgroundArray[currentIndex + 1];
}
};
var goToPrevImg = function(currentImg) {
if (currentImg === 'backyard') {
return 'dark-forest';
}
else {
var currentIndex = backgroundArray.indexOf(currentImg);
return backgroundArray[currentIndex - 1];
}
};
socket.on('rightArrowClicked', function(currentCard) {
Card.findOne({_id: currentCard.id}, function(err, card){
var newBackground = goToNextImg(currentCard.background);
card.background = newBackground;
card.save(function (err, card) {
if (err) {
res.send(500, "New background is NOT saved");
}
else {
io.sockets.emit('backgroundSaved', card);
}
});
});
});
socket.on('leftArrowClicked', function(currentCard) {
Card.findOne({_id: currentCard.id}, function(err, card){
var newBackground = goToPrevImg(currentCard.background);
card.background = newBackground;
card.save(function (err, card) {
if (err) {
res.send(500, "Card's new background is NOT saved");
}
else {
io.sockets.emit('backgroundSaved', card);
}
});
});
});
socket.on('recipientEntered', function(recipient) {
Card.findOne({_id: recipient.id}, function(err, card){
card.recipient = recipient.name;
card.save(function (err, card) {
if (err) {
res.send(500, "Recipient is NOT saved");
}
else {
socket.emit('cardLoaded', card);
}
});
});
});
}); // Socket connection CLOSES
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});