-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
384 lines (337 loc) · 10.6 KB
/
server.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGOHQ_URL);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('lol!');
});
/******* SOCKET IO STUFF *******/
// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io');
// Start the server at port 8080
var server = http.createServer();
var port = process.env.PORT || 3000;
server.listen(port);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);
// assuming io is the Socket.IO server object
socket.configure(function () {
socket.set("transports", ["xhr-polling"]);
socket.set("polling duration", 10);
});
// Add a connect listener
socket.on('connection', function(client){
// Create periodical which ends a message to the client every 5 seconds
var interval = setInterval(function() {
client.send('This is a message from the server! ' + new Date().getTime());
},5000);
// Success! Now listen to messages to be received
client.on('message',function(event){
console.log('Received message from client!',event);
});
client.on('disconnect',function(){
// clearInterval(interval);
console.log('Server has disconnected');
});
client.on('registeruser', function (data, cb) {
console.log('add user: ' + data.username);
var user = new User({ username: data.username, password: data.password, newUser: true });
user.save(function (err, user) {
if (err) {
console.log('err: ' + err);
}
else {
console.log('saved');
cb(null,'test');
}
});
});
client.on('savesnail', function (data, cb) {
console.log('save snail: ' + data.name);
var snail = new Snail(data);
snail.save(function (err, snail) {
if (err) {return cb(err)} // TODO handle the error
var snailID = snail._id;
console.log('saved snail ID ' + snailID);
// client.emit('setSnailID', snailID);
var conditions = {_id: data.ownerID},
update = {$set: {newUser: false}}
User.update(conditions,update,function(err){});
console.log('user updated');
cb(null,snailID);
});
});
client.on('updatesnail', function (data) {
// var snailData = data;
var attrToUpdate = data.attrToUpdate;
var newAttr = data.newAttr;
var snailID = data.snailID;
console.log('snailID: ' + snailID);
var conditions = {_id: snailID};
// update = {$set: {attrToUpdate: newAttr}}
var update = {};
update[attrToUpdate] = newAttr;
Snail.update(conditions,{ $set: update },function(err){});
console.log('updated ' + snailID);
});
client.on('updaterace', function (data) {
// var snailData = data;
var attrToUpdate = data.attrToUpdate;
var newAttr = data.newAttr;
var raceID = data.raceID;
console.log('raceID: ' + raceID);
var conditions = {_id: raceID};
// update = {$set: {attrToUpdate: newAttr}}
var update = {};
update[attrToUpdate] = newAttr;
Race.update(conditions,{ $set: update },function(err){});
console.log('updated ' + raceID);
});
client.on('updateuser', function (data) {
// var snailData = data;
var attrToUpdate = data.attrToUpdate;
var newAttr = data.newAttr;
var userID = data.userID;
console.log('userID: ' + userID);
var conditions = {_id: userID};
// update = {$set: {attrToUpdate: newAttr}}
var update = {};
update[attrToUpdate] = newAttr;
User.update(conditions,{ $set: update },function(err){});
console.log('updated ' + userID);
});
client.on ('loginuser', function (data, cb) {
// Gets user
var user = mongoose.model('User', userSchema);
// Find user with passed username and password
user.findOne({ 'username': data.username, 'password': data.password }, 'username password newUser _id activeJars', function (err, user) {
// If user is not found, say so to client
if (!user) {
console.log('not found');
cb( null, null);
// client.emit('setuser', { username: null });
}
// If user is found, pass logged in username
else if (user) {
console.log('%s is a %s.', user.username, user.password) // Space Ghost is a talk show host.
console.log('new user? ' + user.newUser);
// client.emit('setuser', { username: user.username, userID: user._id });
cb(null, {username: user.username, userID: user._id, activeJars: user.activeJars});
// Find all snails owned by user
if (!user.newUser) {
console.log('not newUser');
var userID = user._id;
// Find snails owned by this user ID
var snail = mongoose.model('Snail', snailSchema);
snail.find({'ownerID': userID}, function(err, docs) {
console.log('looking for snails');
var snails = docs;
if (snails !== null) {
// console.log('emit snails: ' + snails);
client.emit('setsessionsnails', snails)
}
else {
console.log('no snails found');
}
});
}
}
})
// var user = new User({ name: data.username, password: data.password });
});
client.on('createrace', function (data) {
console.log('create race: ' + data.racename + 'entrants: ' + data.reqentrants);
var race = new Race({ name: data.racename, reqEntrants: data.reqentrants, distance: data.distance, finished: false });
race.save(function (err, race) {
if (err) // TODO handle the error
console.log('saved!');
});
});
client.on('loadracelist', function (data, cb) {
console.log('load all races');
var race = mongoose.model('Race', raceSchema);
var finished = data;
if (finished !== null) {
race.find({'finished': finished}, function(err, docs) {
console.log('looking for races');
var races = docs;
if (races !== null) {
console.log('emit races: ' + races);
// client.emit('setsessionraces', races)
cb(null, races);
}
else {
console.log('no races found');
cb (null, null);
}
});
}
else {
race.find(function(err, docs) {
console.log('looking for races');
var races = docs;
if (races !== null) {
console.log('emit races: ' + races);
// client.emit('setsessionraces', races)
cb(null, races);
}
else {
console.log('no races found');
cb (null, null);
}
});
}
});
client.on('runrace', function (data, cb) {
var distance = data.race.distance;
var entrantsArr = data.entrants;
var raceResultsArr= [];
// For every step in the race...
for (var i = 0; i < distance; i++) {
// For every entrant...
for (var n = 0; n < entrantsArr.length; n++) {
var snail = entrantsArr[n];
snail.racePosition = i;
if (snail.racePosition >= distance - 1) {
raceResultsArr.push(snail._id);
console.log('pushed: ' + raceResultsArr);
}
// Apply endurance decay
if (snail.currEndurance > 0) {
snail.currEndurance--;
}
snail.health = snail.currEndurance + snail.weight;
// Set action. 1: move forward; 2: bump snail; 3: stop
// Move forard if endurance > 0 and condition !== hindered
// Stop if endurance <= 0 or condition === hindered (take off hindered state)
if (snail.raceCondition !== 'hindered' && snail.currEndurance > 0) {
snail.raceAction = 'move';
}
else {
snail.raceAction = 'stop';
continue;
}
console.log('ID: ' + snail._id + ' race position: ' + snail.racePosition);
}
}
data.race.resultsArr = raceResultsArr;
cb(null,raceResultsArr);
});
client.on('updateraceentries', function (data, cb) {
console.log('entering race');
// var snailData = data;
var raceID = data.raceID;
if (data.snailID) {
var snailID = data.snailID;
}
var conditions = {_id: raceID};
var attrToUpdate = 'entrantsArr';
var update = {}
// if snailID is present, add snail to race
if (snailID) {
update[attrToUpdate] = snailID;
Race.update(conditions,{ $addToSet: update },function(err){
if(err){
console.log('error: ' + err);
}
else {
console.log("Successfully added");
}
});
}
// If snailID is not present, simply refresh race list and send to client
var race = mongoose.model('Race', raceSchema);
// Get array of all entrants
race.findOne({ '_id': raceID }, 'entrantsArr reqEntrants distance', function (err, race) {
console.log('entrants: ' + race.entrantsArr.length);
var enteredSnailsArr = [];
for (var i = 0; i < race.entrantsArr.length; i++) {
var entryID = race.entrantsArr[i];
var snail = mongoose.model('Snail', snailSchema);
snail.findOne({'_id': entryID}, function(err, snail) {
client.emit('loadracingsnail', snail);
});
}
if (race.entrantsArr.length === race.reqEntrants) {
console.log('run race now!');
}
});
});
});
/******* END SOCKET IO STUFF *******/
var raceSchema = mongoose.Schema({
name: String,
entrantsArr: [],
winnerID: Number,
reqEntrants: Number,
distance: Number,
finished: Boolean,
resultsArr: [],
})
var userSchema = mongoose.Schema({
username: {
type:String,
unique: true,
},
password: String,
newUser: Boolean,
activeJars: Number,
})
var snailSchema = mongoose.Schema({
// Core stats
ownerID: mongoose.Schema.Types.ObjectId,
ownerUsername: String,
inDb: Boolean,
name: String,
size: {x: Number, y: Number},
age: Number,
adult: Boolean,
scale: Number,
maxScale: Number,
// maxScale: Number,
// maxSize: {x: Number, y: Number},
// Genetics
shellColor: {r: Number, g: Number, b: Number},
eyeColor: {r: Number, g: Number, b: Number},
patternColor: {r: Number, g: Number, b: Number},
patternShape: Number,
speed: Number,
currSpeed: Number,
endurance: Number,
currEndurance: Number,
weight: Number,
health: Number,
raceAction: String,
raceCondition: String,
racePosition: Number,
genes: {
shellColor: {allele1: String, allele2: String},
eyeColor: {allele1: String, allele2: String},
patternColor: {allele1: String, allele2: String},
patternShape: {allele1: String, allele2: String}
},
// Housing
inJar: Number,
parentJar: Number,
// Gender and breeding
orientation: Number,
inBreedingJar: Boolean,
mating: Boolean,
mateID: Number,
arousalDelay: Number,
stagID: Number,
doeID: Number
})
var User = mongoose.model('User', userSchema)
var Snail = mongoose.model('Snail', snailSchema)
var Race = mongoose.model('Race', raceSchema)
/* var fluffy = new User({ name: 'fluffy' });
fluffy.speak() // "Meow name is fluffy"
fluffy.save(function (err, fluffy) {
if (err) // TODO handle the error
fluffy.speak();
});
User.find(function (err, kittens) {
if (err) // TODO handle err
console.log(kittens)
}) */