-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
822 lines (738 loc) · 29.8 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/**
* "Rock, Paper, Scissors, Node!" Rock, Paper, Scissors implemented in node.js
Copyright (C) 2011 Ryan Johnston
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
* /
/**
* Module dependencies.
*/
var express = require('express'),
mongoose = require('mongoose'),
sanitizer = require('sanitizer'),
everyauth = require('everyauth'),
mongooseAuth = require('mongoose-auth'),
cron = require('cron'),
conf = require('./conf'),
Promise = everyauth.Promise;
everyauth.debug = true;
var app = module.exports = express.createServer(),
io = require('socket.io').listen(app);
var Schema = mongoose.Schema
, ObjectId = mongoose.SchemaTypes.ObjectId;
/*Database stuff*/
var Trophies = new Schema({
title : {type: String, default: null}
, filename : {type: String, default: null}
, description : {type: String, default: null}
});
//Need a better name than this:
var LongTermSystemData = new Schema({
lastWinner : {type: String, default: null}
, currentFirstTrophy : [Trophies]
, currentSecondTrophy : [Trophies]
, currentThirdTrophy : [Trophies]
});
var User = new Schema({
displayName : {type: String, default: null}
, avatar : {type: String, default: 'avatardefault.png'}
, wins : {type: Number, default: 0}
, losses : {type: Number, default: 0}
, rocks : {type: Number, default: 0}
, papers : {type: Number, default: 0}
, scissors : {type: Number, default: 0}
, trophycase : [Trophies]
, roundwins : {type: Number, default: 0}
, roundlosses : {type: Number, default: 0}
, roundplays : {type: Number, default: 10} //This is DAILY, rather than round-wide
});
User.plugin(mongooseAuth, {
everymodule: {
everyauth: {
User: function () {
return User;
}
}
}
, password: {
loginWith: 'email'
, everyauth: {
getLoginPath: '/login'
, postLoginPath: '/login'
, loginView: 'login.jade'
, getRegisterPath: '/register'
, postRegisterPath: '/register'
, registerView: 'register.jade'
, loginSuccessRedirect: '/'
, registerSuccessRedirect: '/'
}
}
, twitter: {
everyauth: {
myHostname: 'http://dev.rpsnode.nodejitsu.com:3000'
//myHostname: 'http://rpsnode.nodejitsu.com'
, consumerKey: conf.twit.consumerKey
, consumerSecret: conf.twit.consumerSecret
, redirectPath: '/'
}
}
, github: {
everyauth: {
myHostname: 'http://dev.rpsnode.nodejitsu.com:3000'
//myHostname: 'http://rpsnode.nodejitsu.com'
, appId: conf.github.appId
, appSecret: conf.github.appSecret
, redirectPath: '/'
}
}
});
mongoose.model('User', User);
mongoose.model('Trophies', Trophies);
mongoose.model('LongTermSystemData', LongTermSystemData);
mongoose.connect('mongodb://localhost/db');
User = mongoose.model('User');
Trophies = mongoose.model('Trophies');
LongTermSystemData = mongoose.model('LongTermSystemData');
/*This stuff is hardcoded for now. In the future, there'll be an admin panel to put it
* new trophies
*/
myTrophies = [
{ title: '1st place Alpha', filename: 'alpha-1st.png', description: 'Congrats! You got First during the Alpha Testing version of RPS Node!' },
{ title: '2nd place Alpha', filename: 'alpha-2nd.png', description: 'Congrats! You got Second during the Alpha Testing version of RPS Node!' },
{ title: '3rd place Alpha', filename: 'alpha-3rd.png', description: 'Congrats! You got Third during the Alpha Testing version of RPS Node!' },
];
myTrophies.forEach(function(trophy) {
Trophies.findOne({filename: trophy.filename}, function(err, foundTrophy) {
if (!foundTrophy) {
var newTrophy = new Trophies();
newTrophy.title = trophy.title;
newTrophy.filename = trophy.filename;
newTrophy.description = trophy.description;
newTrophy.save(function(err) {
if (!err) console.log('Saved a Trophy');
});
}
});
});
LongTermSystemData.findOne({}, function(err, result) {
if (!result) {
var newData = new LongTermSystemData();
newData.lastWinner = 'Nobody';
newData.currentFirstTrophy = myTrophies[0];
newData.currentSecondTrophy = myTrophies[1];
newData.currentThirdTrophy = myTrophies[2];
newData.save(function(err) {
if (!err) console.log('Saved Data');
});
}
});
/*End Database Stuff*/
/*Cron Stuff*/
//Sunday-Friday
//TODO: Do this as a callback
new cron.CronJob('0 0 0 * * 1-6', function() {
//Reset the number of "round plays" to 10.
User.find({}, function (err, users) {
//Each user
users.forEach(function(user) {
user.roundplays = 10;
user.save(function(err) {
if (err) { throw err; }
});
});
});
});
//Saturday
new cron.CronJob('0 0 0 * * 7', function() {
LongTermSystemData.findOne({}, function(err, result) {
User.find({}).sort('roundwins', -1).limit(3).execFind(function(err, foundusers) {
var place = -1;
var places = ['currentFirstTrophy', 'currentSecondTrophy', 'currentThirdTrophy'];
foundusers.forEach(function(user) {
//This is kind-of complicated and horrible. Pushes a new trophy into the user's
//trophycase. This works by taking result's (which is the result currently held in
//LongTermSystemData) property according to whatever the firstplace trophy is
user.trophycase.push(result[places[++place]][0]);
user.save(function(err) {
//if this is the last time we have to award a player, then FLUSH THE ROUND
if (place===foundusers.length-1) {
User.find({}, function (err, users) {
//Each user
users.forEach(function(user) {
user.roundplays = 0;
user.roundwins = 0;
user.roundlosses = 0;
user.save(function(err) {
if (err) { throw err; }
});
});
});
}
});
//Award each of these guys one 1st, one 2nd, one 3rd respectively
//Compare everyone's round score, declare 1st, 2nd, 3rd places,
//add these counts to player's db entries
});
});
});
});
//Test
/*new cron.CronJob('*6 * * * * *', function() {
LongTermSystemData.findOne({}, function(err, result) {
User.find({}).sort('roundwins', -1).limit(3).execFind(function(err, foundusers) {
var place = -1;
var places = ['currentFirstTrophy', 'currentSecondTrophy', 'currentThirdTrophy'];
foundusers.forEach(function(user) {
//This is kind-of complicated and horrible. Pushes a new trophy into the user's
//trophycase. This works by taking result's (which is the result currently held in
//LongTermSystemData) property according to whatever the firstplace trophy is
user.trophycase.push(result[places[++place]][0]);
user.save(function(err) {
//if this is the last time we have to award a player, then FLUSH THE ROUND
if (place===foundusers.length-1) {
User.find({}, function (err, users) {
//Each user
users.forEach(function(user) {
user.roundplays = 0;
user.roundwins = 0;
user.roundlosses = 0;
user.save(function(err) {
if (err) { throw err; }
});
});
});
}
});
//Award each of these guys one 1st, one 2nd, one 3rd respectively
//Compare everyone's round score, declare 1st, 2nd, 3rd places,
//add these counts to player's db entries
});
});
});
});
*/
/*End Cron Stuff*/
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(mongooseAuth.middleware());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
//Dynamic Helpers
app.dynamicHelpers({
info: function(req, res){
return req.flash('info');
},
warn: function(req, res){
return req.flash('warn');
},
});
//Route Middleware
function userCannotBeLoggedIn(req, res, next) {
if (req.loggedIn) {
res.redirect('/');
} else {
next();
}
}
function userMustBeLoggedIn(req, res, next) {
if (!req.loggedIn) {
res.redirect('/');
} else {
next();
}
}
function isdisplayNameTaken(req, res, next) {
User.findOne({displayName: req.body.displayName}, function (err, user) {
if (err) { throw err; }
if (!user) {
next();
} else {
req.flash('warn', 'displayName already taken!');
res.redirect('/register');
}
});
}
function userMustHaveDisplayName(req, res, next) {
if (req.user) {
if (!req.user.displayName) {
req.flash('info', 'Please choose a display name to display to your enemies in BATTLE!');
res.redirect('/newuser');
} else {
next();
}
} else {
next();
}
}
function userCannotHaveDisplayName(req, res, next) {
if (req.user) {
if (req.user.displayName) {
res.redirect('/');
} else {
next();
}
} else {
res.redirect('/');
}
}
// Routes
app.get('/', userMustHaveDisplayName, function(req, res){
LongTermSystemData.findOne({}, function(err, result) {
if(err) { throw err; }
console.log(result);
res.render('index', {
title: 'Rock, Paper, Scissors, Node!'
, result: result
});
});
});
app.get('/newuser', userCannotHaveDisplayName, function(req, res){
res.render('newuser', {
title: 'Rock, Paper, Scissors, Node!: New User'
});
});
app.post('/newuser', userCannotHaveDisplayName, function(req, res){
//First, display name must be more than 0 characters.
if (req.body.displayname.length > 0) {
//Second, display name should be unique
User.find({ displayName: req.body.displayname }, function(err, user) {
if (err) {throw err;}
//If we don't find someone else with this display name
if (!user.length) {
//Find this currently logged in user
User.findById(req.user._id, function(err, user) {
if(err) { throw err; }
user.displayName = req.body.displayname;
req.user.displayName = req.body.displayname;
//If today is not Saturday, turn roundplays to 10
if ((new Date().getDay) !== 6) {
user.roundplays = 10;
user.save(function(err) {
if (err) { throw err; }
req.flash('info', 'Creation of Display Name Successful!');
res.redirect('/');
});
} else { //If today is something other than Saturday
user.save(function(err) {
if (err) { throw err; }
req.flash('info', 'Creation of Display Name Successful!');
res.redirect('/');
});
}
});
} else {
req.flash('warn', 'Display name already taken. Try again.');
res.redirect('/newuser');
} //End of if name not unique
}); //End of finding unique display name
} else {
req.flash('warn', 'Display name must be at least 1 character long. Try again.');
res.redirect('/newuser');
} //End of display name being more than 0 chars
});
app.get('/play', userMustHaveDisplayName, userMustBeLoggedIn, function(req, res){
res.render('play', {
title: 'Rock, Paper, Scissors, Node!: Play'
});
});
app.get('/stats', userMustHaveDisplayName, function(req, res){
User.find({}).sort('wins', -1).limit(25).execFind(function(err, overallfoundusers) {
User.find({}).sort('roundwins', -1).limit(25).execFind(function(err, roundfoundusers) {
if (err) { throw err; }
if (overallfoundusers && roundfoundusers) {
res.render('stats', {
title: 'Rock, Paper, Scissors, Node!: Stats',
overallfoundusers: overallfoundusers,
roundfoundusers: roundfoundusers
});
} else {
req.flash('warn', 'No users found! Yet... ');
res.redirect('/');
}
});
});
});
app.get('/stats/:displayName', userMustHaveDisplayName, function(req, res){
User.findOne({displayName: sanitizer.escape(req.params.displayName)}, function(err, founduser) {
if (err) { throw err; }
if (founduser) {
res.render('stats/individualstats', {
title: 'Rock, Paper, Scissors, Node!: Stats',
founduser: founduser
});
} else {
req.flash('warn', 'No user ' + sanitizer.escape(req.params.displayName) + ' was found!');
res.redirect('stats');
}
});
});
mongooseAuth.helpExpress(app);
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
//Here's the juicy stuff
var Game = {}
io.sockets.on('connection', function(socket){
//First off, we can grab their name from a socket emission
socket.on('initial', function(msg){
console.log('User ' + socket.id + ' has connected as ' + msg.data);
//first, see if we can find this user in the db. If not, tell them to stop haxxoring.
User.findOne({displayName: msg.data}, function(err,user) {
if(err) { throw err; }
if(user) {
/////
//Game Initialization
/////
//If there isn't a game already, create one, then tell the client to
//wait
//Or, if player1 hits refresh, just put them back to the Game.player1 property
if (!Game.player1 || Game.player1.displayName===user.displayName) {
//If the user is simply re-freshing or re-joining their game, don't decrement
//their number of ranked plays left. Do if this is a fresh game
//TODO: Make it so when the user refreshes, it marks them down for roundplay=true
if (!Game.player1 && user.roundplays>0) {
Game.player1 = socket;
Game.player1.roundplay = true;
user.roundplays--;
user.save(function(err) {
if(err) {throw err;}
console.log('Round plays decremented for user');
//Send emit notice back to user
socket.emit('roundplaycount',{data: user.roundplays });
});
socket.displayName = msg.data
socket.game = Game
socket.choice = socket.game.player1choice = {} //This will be the player's choice
} else {
Game.player1 = socket;
Game.player1.roundplay = true;
socket.displayName = msg.data
socket.game = Game
socket.choice = socket.game.player1choice = {} //This will be the player's choice
}
socket.emit('wait',{data: 'Waiting for opponent'});
console.log(socket.displayName + ' is player 1');
} else { //If there is a game, have this player join the game, and then
//free up the Game variable for the next client pair
//First, decrement this person's round plays if not already at 0
if (user && user.roundplays>0) {
Game.player2 = socket
Game.player2.roundplay = true;
user.roundplays--;
user.save(function(err) {
if(err) {throw err;}
console.log('Round plays decremented for user');
//Send emit notice back to user
socket.emit('roundplaycount',{data: user.roundplays });
});
socket.displayName = msg.data
socket.game = Game
socket.choice = socket.game.player2choice = {} //This will be the player's choice
} else {
Game.player2 = socket
socket.displayName = msg.data
socket.game = Game
socket.choice = socket.game.player2choice = {} //This will be the player's choice
}
console.log(socket.displayName + ' is player 2');
socket.emit('join',{data: {player1name: socket.game.player1.displayName, player2name: socket.game.player2.displayName}});
socket.emit('gamestatus', {data: 'Game is about to begin!'});
socket.game.player1.emit('join', {data: {player1name: socket.game.player1.displayName, player2name: socket.game.player2.displayName}})
socket.game.player1.emit('gamestatus', {data: 'Game is about to begin!'})
//Does the timer stuff for the game
socket.game.timer = function(seconds, callback, next) {
var self = this;
self.player1.emit('timer', {data: seconds})
self.player2.emit('timer', {data: seconds})
if (seconds === 0) {
next.call(self);
} else {
setTimeout(function() {
callback.call(self, --seconds, callback, next);
}, 1000);
}
}
//Emits to both players
socket.game.emittoboth = function(msg, data) {
self = this
self.player1.emit(msg, data);
self.player2.emit(msg, data);
}
//Determines the winner of the game
socket.game.determinewinner = function(callback) {
switch (self.player1choice.choice) {
case 'rock':
switch (self.player2choice.choice) {
case 'rock':
//Tie. Reshoot
self.emittoboth('gamestatus', { data: 'Tie! Reshoot in 5 seconds!'});
self.player2.win = 0
self.player2.lose = 0
self.player1.win = 0
self.player1.lose = 0
break;
case 'paper':
//Player1 loses
self.emittoboth('gamestatus', { data: self.player2.displayName + ' wins!'});
//Probably a nicer way to do this
self.player2.win = 1
self.player2.lose = 0
self.player1.win = 0
self.player1.lose = 1
break;
case 'scissors':
//Player1 wins
self.emittoboth('gamestatus', { data: self.player1.displayName + ' wins!'});
self.player1.win = 1
self.player1.lose = 0
self.player2.win = 0
self.player2.lose = 1
break;
default:
//Opponent didn't make a choice
self.emittoboth('gamestatus', { data: self.player1.displayName + ' wins by default! ' + self.player2.displayName + ' didn\'t choose!'});
self.player1.win = 1
self.player1.lose = 0
self.player2.win = 0
self.player2.lose = 1
break;
}
break;
case 'paper':
switch (self.player2choice.choice) {
case 'rock':
//Player1 wins
self.emittoboth('gamestatus', { data: self.player1.displayName + ' wins!'});
self.player1.win = 1
self.player1.lose = 0
self.player2.win = 0
self.player2.lose = 1
break;
case 'paper':
//Tie. Reshoot
self.emittoboth('gamestatus', { data: 'Tie! You are awarded no points!'});
self.player2.win = 0
self.player2.lose = 0
self.player1.win = 0
self.player1.lose = 0
break;
case 'scissors':
//Player1 loses
self.emittoboth('gamestatus', { data: self.player2.displayName + ' wins!'});
self.player2.win = 1
self.player2.lose = 0
self.player1.win = 0
self.player1.lose = 1
break;
default:
//Opponent didn't make a choice
self.emittoboth('gamestatus', { data: self.player1.displayName + ' wins by default! ' + self.player2.displayName + ' didn\'t choose!'});
self.player1.win = 1
self.player1.lose = 0
self.player2.win = 0
self.player2.lose = 1
break;
}
break;
case 'scissors':
switch (self.player2choice.choice) {
case 'rock':
//Player1 loses
self.emittoboth('gamestatus', { data: self.player2.displayName + ' wins!'});
self.player2.win = 1
self.player2.lose = 0
self.player1.win = 0
self.player1.lose = 1
break;
case 'paper':
//Player1 wins
self.emittoboth('gamestatus', { data: self.player1.displayName + ' wins!'});
self.player1.win = 1
self.player1.lose = 0
self.player2.win = 0
self.player2.lose = 1
break;
case 'scissors':
//Tie. Reshoot
self.emittoboth('gamestatus', { data: 'Tie! Reshoot in 5 seconds!'});
self.player2.win = 0
self.player2.lose = 0
self.player1.win = 0
self.player1.lose = 0
break;
default:
//Opponent didn't make a choice
self.emittoboth('gamestatus', { data: self.player1.displayName + ' wins by default! ' + self.player2.displayName + ' didn\'t choose!'});
self.player1.win = 1
self.player1.lose = 0
self.player2.win = 0
self.player2.lose = 1
break;
}
break;
default:
//This client didn't make a choice
switch (self.player2choice.choice) {
case undefined:
//Neither player made a choice
self.emittoboth('gamestatus', { data: 'No one wins! Neither chose!'});
self.player2.win = 0
self.player2.lose = 1
self.player1.win = 0
self.player1.lose = 1
break;
default:
//Player1 didn't make a choice
self.emittoboth('gamestatus', { data: self.player2.displayName + ' wins by default! ' + self.player1.displayName + ' didn\'t choose!'});
self.player2.win = 1
self.player2.lose = 0
self.player1.win = 0
self.player1.lose = 1
break;
}
break;
}
self.emittoboth('results', { data: {player1choice: socket.game.player1choice.choice, player2choice: socket.game.player2choice.choice}});
callback({'displayName': self.player1.displayName, 'win': self.player1.win, 'lose': self.player1.lose}, {'displayName': self.player2.displayName, 'win': self.player2.win, 'lose': self.player2.lose});
}
Game = {}
//Ok, Finally, we do the game. This is the actual game execution
socket.game.timer(5, socket.game.timer, function() {
//Initial 5 seconds is over. Time for the players to make a choice
socket.game.emittoboth('gamestatus', { data: 'Choose your play!'});
socket.game.emittoboth('choose');
socket.game.timer(3, socket.game.timer, function() {
socket.game.determinewinner(function(player1, player2) {
//Save the two players here
User.findOne({displayName: player1.displayName}, function (err, user) {
if (err) { throw err; }
if (user) {
console.log(user)
//Update player1's stats here
user.wins += player1.win;
user.losses += player1.lose;
//If this is ranked, increase the users's roundwins/roundlosses
if (socket.game.player1.roundplay) {
user.roundwins+= player1.win;
user.roundlosses+= player1.lose;
user.save(function (err) {
if (err) { throw err; }
console.log('saved');
});
} else {
user.save(function (err) {
if (err) { throw err; }
console.log('saved');
});
}
}
User.findOne({displayName: player2.displayName}, function (err, user) {
if (err) { throw err; }
if (user) {
//Update player2's stats here
user.wins += player2.win;
user.losses += player2.lose;
//If this is ranked, increase the users's roundwins/roundlosses
if (socket.game.player2.roundplay) {
user.roundwins+= player2.win;
user.roundlosses+= player2.lose;
user.save(function (err) {
if (err) { throw err; }
console.log('saved');
});
} else {
user.save(function (err) {
if (err) { throw err; }
console.log('saved');
});
}
}
});
});
});
});
});
}
} else {
socket.emit('gamestatus', {data: 'Sorry, couldn\'t find you in our DB'});
}
});
});
socket.on('choice', function(msg){
console.log(socket.displayName + " chose " + msg.data)
if (msg.data === 'rock' || msg.data === 'paper' || msg.data === 'scissors') {
socket.choice.choice = msg.data // come up with a better name for ".choice.choice"
console.log('and it was accepted.')
//Send it back to the user for display purposes
if (socket===socket.game.player1) {
socket.emit('initialchoice', { data: { playerchoicediv: '#player1choice', playerchoice: msg.data } });
} else {
socket.emit('initialchoice', { data: { playerchoicediv: '#player2choice', playerchoice: msg.data } });
}
}
});
socket.on('disconnect', function(){ })
});
//tim_smart in IRC offered this solution:
//This solution reduces function reference counts on the heap. Implement this tomorrow.
/*
function Countdown(seconds) {
this.seconds = seconds
this._ms = null
}
Countdown.prototype._tick = function (seconds, callback, ms) {
var countdown = this
, timestamp = new Date().getTime()
ms || (ms = 1000)
this._ms || (this._ms = seconds * 1000)
setTimeout(function () {
var taken = new Date().getTime() - timestamp
, total_ms = seconds * 1000
, new_ms = countdown._ms - taken
, diff = total_ms - new_ms
, secs_diff = Math.floor(diff / 1000)
, next
countdown._ms = new_ms
for (var i = 0; i < secs_diff; i++) {
callback(--seconds)
if (seconds === 0) {
countdown._ms = null
return
}
}
next = diff >= 1000
? 1000 - (diff % 1000)
: 1000 - diff
countdown._tick(seconds, callback, next)
}, ms)
return this
}
Countdown.prototype.start = function (callback) {
callback(this.seconds)
return this._tick(this.seconds, callback)
}
var countdown = new Countdown(5)
countdown.start(function (seconds_left) {
console.log(seconds_left)
})
*/