-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.js
More file actions
266 lines (231 loc) · 7.17 KB
/
memory.js
File metadata and controls
266 lines (231 loc) · 7.17 KB
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
var CARDS_NUMBER = 20 ;
Games = new Mongo.Collection("games");
if (Meteor.isClient) {
Meteor.subscribe("games");
Meteor.subscribe("users");
Router.configure({
layoutTemplate: 'ApplicationLayout'
});
Router.route('/', function () {
this.render("navbar", {to:"header"});
this.render("lobby_page", {to:"main"});
});
Router.route('/game/:_id', function () {
// the user they want to chat to has id equal to
// the id sent in after /chat/...
var otherUserId = this.params._id;
Session.set("otherUserId", otherUserId);
// find a chat that has two users that match current user id
// and the requested user id
var filter = {$or:[
{user1Id:Meteor.userId(), user2Id:otherUserId},
{user2Id:Meteor.userId(), user1Id:otherUserId}
]};
var game = Games.findOne(filter);
if (!game){// no chat matching the filter - need to insert a new one
Meteor.call('newGame', Meteor.userId(), otherUserId, function (err, gameId) {
Session.set("gameId",gameId);
});
}
else {// there is a chat going already - use that.
Session.set("gameId",game._id);
}
this.render("navbar", {to:"header"});
this.render("gamePage", {to:"main"});
});
Accounts.ui.config({
passwordSignupFields: "USERNAME_AND_EMAIL"
});
Template.gamePage.helpers({
getCards:function() {
var game = Games.findOne({_id:Session.get("gameId")});
return game.cards;
},
getImageSrc: function(card) {
if (card.isCovered) {
return '/pictures/sky.jpg';
}
else if (card.isDisabled) {
return '/pictures/disable.png';
}
else {
return card.img_src;
}
},
getOpponentName: function() {
var otherUserId = Session.get("otherUserId");
return Meteor.users.findOne({_id: otherUserId}).username;
},
getYourScore: function() {
var game = Games.findOne({_id:Session.get("gameId")});
if(Meteor.userId() === game.user1Id) {
return game.score.user1;
}
else {
return game.score.user2;
}
},
getOpponentScore: function() {
var game = Games.findOne({_id:Session.get("gameId")});
if(Meteor.userId() === game.user1Id) {
return game.score.user2;
}
else {
return game.score.user1;
}
},
getCurrentUser: function() {
var game = Games.findOne({_id:Session.get("gameId")});
return Meteor.users.findOne({_id: game.turn}).username;
},
currentUserWon: function() {
var game = Games.findOne({_id:Session.get("gameId")});
if(Meteor.userId() === game.user1Id) {
return game.score.user1 > game.score.user2;
}
else {
return game.score.user2 > game.score.user1;
}
},
gameOver: function() {
var game = Games.findOne({_id:Session.get("gameId")});
return game.score.user1 + game.score.user2 >= game.cards.length/2;
},
drawGame: function() {
var game = Games.findOne({_id:Session.get("gameId")});
return game.score.user1 === game.score.user2;
},
gameReady: function() {
return !(Session.get("otherUserId") === null || Meteor.userId() === null);
},
});
Template.ApplicationLayout.helpers({
getCurrentUser: function() {
var game = Games.findOne({_id:Session.get("gameId")});
return Meteor.users.findOne({_id: game.turn}).username;
}
});
Template.navbar.helpers({
users:function() {
return Meteor.users.find();
}
});
Template.available_user.helpers({
getUsername:function(userId){
user = Meteor.users.findOne({_id:userId});
return user.username;
},
isMyUser:function(userId){
return userId == Meteor.userId();
}
});
Template.gamePage.events({
'click .js-card-clicked':function(event){
event.preventDefault();
var cardId = $(event.currentTarget).context['id'];
console.log(cardId);
if (Session.get("gameId")){
Meteor.call('makeMove', Session.get("gameId"), Meteor.userId(), cardId);
}
},
'click .js-restart':function(event){
event.preventDefault();
if (Session.get("gameId")){
Meteor.call('restart', Session.get("gameId"));
}
}
})
}
if (Meteor.isServer) {
Meteor.publish("games", function(){
return Games.find();
});
Meteor.publish("users", function(){
return Meteor.users.find();
});
}
Meteor.methods({
newGame: function (userId, otherUserId) {
var cardsBuffer = [];
for (var i = 1; i <= CARDS_NUMBER/2; i++) {
cardsBuffer.push({
id: cardsBuffer.length,
img_src: "/pictures/pic"+i+".jpg",
isCovered: true,
isDisabled: false
});
cardsBuffer.push({
id: cardsBuffer.length,
img_src: "/pictures/pic"+i+".jpg",
isCovered: true,
isDisabled: false
});
}
cardsBuffer = _.shuffle(cardsBuffer);
var gameId = Games.insert({
user1Id: userId,
user2Id: otherUserId,
cards: cardsBuffer,
turn: userId, //which user can make the next move
score: {user1: 0, user2: 0},
firstClickedCardId: null
});
return gameId;
},
restart: function (gameId) {
var game = Games.findOne(gameId);
game.cards.forEach(function(card){
card.isCovered = true;
card.isDisabled = false;
});
game.cards = _.shuffle(game.cards);
//choose who will start the new game randomly
game.turn = (Math.random()*10)%2 ? game.user1Id : game.user2Id;
game.score.user1 = 0;
game.score.user2 = 0;
game.firstClickedCardId = null;
return Games.update({_id: gameId}, game);
},
makeMove: function (gameId, userId, cardId) {
var game = Games.findOne(gameId);
var currentCard = lodash.find(game.cards, {id: parseInt(cardId)});
//only if it is the users turn he can make a move
if( game.turn === userId &&
cardId !== game.firstClickedCardId &&
!currentCard.isDisabled )
{
currentCard.isCovered = !currentCard.isCovered;
}
else {
//it is not this users turn, so do nothing!
return;
}
var firstClickedCard = null;
if (game.firstClickedCardId === null) {
game.firstClickedCardId = cardId;
}
else {
firstClickedCard = lodash.find(game.cards, {id: parseInt(game.firstClickedCardId)});
if (firstClickedCard.img_src === currentCard.img_src) {
firstClickedCard.isDisabled = true;
currentCard.isDisabled = true;
if(Meteor.userId() === game.user1Id) {
game.score.user1 += 1;
}
else {
game.score.user2 += 1;
}
}
else {
game.turn = ((Meteor.userId() == game.user1Id) ? game.user2Id : game.user1Id);
Meteor.setTimeout(function() {
firstClickedCard.isCovered = true;
currentCard.isCovered = true;
Games.update({_id: gameId}, game);
}, 2000);
}
game.firstClickedCardId = null;
}
return Games.update({_id: gameId}, game);
}
});