-
Notifications
You must be signed in to change notification settings - Fork 6
/
randomGameController.js
133 lines (129 loc) · 4.44 KB
/
randomGameController.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
var User = require('../models/userModel.js');
var Game = require('../models/gameModel.js');
var GameData = require('../models/gameDataModel.js');
var hashtags = require('../data/hashtags.js');
var prompts = require('../data/prompts.js');
var clients = require('../config/socketEvents.js').clients;
var _ = require('underscore');
exports.join = function(req, res) {
var userId = req.params.id;
var switchLooking = function (err, obj) {
if(err) {
console.log(err);
return;
} else {
var looking = obj.get('looking');
if(!looking) {
obj.set('looking', true);
obj.pendingGames.push({title: '#1 Best Random Game'});
}
obj.save(function (err) {
if(err) {
console.log(err)
} else {
User.find({}).where('looking', 'true').exec(makeGame);
}
});
}
};
var makeGame = function(err, obj) {
if(err) {
console.log(err);
} else {
if(obj.length === 4) {
createGame(obj);
} else {
res.send(userId);
}
};
};
var createGame = function(players) {
var game = new Game();
var gameData = new GameData();
var allPrompt = _.shuffle(prompts);
var thisPrompt = allPrompt.pop();
gameData.set('prompts', allPrompt.slice(0, 37));
gameData.set('gameId', game._id);
var tags = _.shuffle(hashtags);
var judgeArr = [];
game.set('title', '#1 Best Random Game');
game.set('gameData', gameData._id);
game.set('prompt', thisPrompt);
game.set('round', 0);
game.set('numberOfSub', 0);
game.set('judge', {username: players[0].username, avatarURL: players[0].avatarURL, userGlobalId: players[0].user});
var playersHash = {};
for(var i = 0; i < players.length; i ++) {
//Set up status for each player in game
judgeArr.push(players[i]._id);
playersHash[players[i]._id] = {};
playersHash[players[i]._id].username = players[i].username;
playersHash[players[i]._id].avatarURL = players[i].avatarURL;
playersHash[players[i]._id].userGlobalId = players[i]._id;
playersHash[players[i]._id].submitted = false;
playersHash[players[i]._id].submission = {};
playersHash[players[i]._id].continued = true;
playersHash[players[i]._id].score = 0;
playersHash[players[i]._id].isJ = false;
playersHash[players[i]._id].hand = tags.splice(0, 5);
}
gameData.set('hashtags', tags);
playersHash[judgeArr[0]].isJ = true;
game.set('judgingOrder', judgeArr);
game.set('players', playersHash);
gameData.save(function (err) {
if(err) {
console.log(err);
} else {
game.save(function (err, newGame) {
var keys = newGame.judgingOrder;
var userGame = {};
console.log(keys);
userGame.gameId = newGame._id;
userGame.judge = {};
userGame.judge.username = newGame.judge.username;
userGame.judge.avatarURL = newGame.judge.avatarURL;
userGame.prompt = newGame.prompt;
userGame.title = newGame.title;
userGame.players = [];
_.each(keys, function (pId) {
var player = {};
player.username = newGame.players[pId].username;
player.avatarURL = newGame.players[pId].avatarURL;
player.score = 0;
userGame.players.push(player);
});
for(var i = 0; i < keys.length; i ++) {
User.findById(keys[i], function (err, user) {
//Add newly saved game to each user's game array
if(err) {
console.log(err);
} else {
var newArr = [];
user.games.push(userGame);
for(var i = 0; i < user.pendingGames.length; i ++) {
if(user.pendingGames[i].title !== '#1 Best Random Game') {
newArr.push(user.pendingGames[i]);
}
}
user.set('pendingGames', newArr);
user.set('looking', false);
user.save(function (err, user) {
if(err) console.log(err);
if(user._id === userId) {
res.send(userId);
} else {
if (clients[user._id]) {
clients[user._id].emit('changeInUser');
}
}
});
}
});
}
});
}
});
};
User.findById(userId).exec(switchLooking);
}