-
Notifications
You must be signed in to change notification settings - Fork 0
/
the-name-game.js
189 lines (161 loc) · 5.07 KB
/
the-name-game.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
////////////////////////////////////////////////////////////////////////////////
// Collections //
////////////////////////////////////////////////////////////////////////////////
Games = new Mongo.Collection('games');
////////////////////////////////////////////////////////////////////////////////
// Router //
////////////////////////////////////////////////////////////////////////////////
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading'
});
Router.route('/', function() {
this.render('home');
}, {name: 'home'});
Router.route('/new', function() {
Meteor.call('newGame', function(error, gameId) {
if(!error) {
Router.go('game', {id: gameId});
}
});
});
Router.route('/game/:id', function() {
var gameId = this.params.id;
Meteor.call('joinGame', gameId, function(error, result) {
if(!error) {
Session.set('currentGameId', gameId);
}
});
this.render('game');
}, {name: 'game'});
Router.route('/about');
Router.onBeforeAction(function() {
if(!Meteor.userId()) {
if(Meteor.loggingIn()) {
this.render('loading');
} else {
this.render('login');
}
} else {
this.next();
}
}, {
except: ['home', 'about']
});
////////////////////////////////////////////////////////////////////////////////
// Client code //
////////////////////////////////////////////////////////////////////////////////
if (Meteor.isClient) {
Tracker.autorun(function() {
Meteor.subscribe('gameData', Session.get('currentGameId'));
});
Template.layout.helpers({
currentGame: function() {
var gameId = Session.get('currentGameId');
return gameId ? {id: gameId} : null;
},
inGamePage: function() {
return Router.current().route.getName() == 'game';
},
linkActiveClassFor: function(routeName) {
console.log('linkActiveClassFor ' + routeName);
console.log('current route name: ' + Router.current().route.getName());
return Router.current().route.getName() == routeName ? 'active' : '';
}
});
Template.home.events({
'click #new-game': function() {
Router.go('/new');
}
});
Template.game.helpers({
players: function() {
var game = Games.findOne({_id: Session.get('currentGameId')})
if(game) {
var players = game.players.filter(function(p) {
return Meteor.users
.find({_id: p.id, 'status.online': true})
.count() == 1;
});
var number = 1;
players = players.map(function(p) {
p.number = number++;
return p;
});
return players;
}
},
username: function(userId) {
var user = Meteor.users.findOne({_id: userId});
if(user) return user.username;
},
isCurrentUser: function(userId) {
return Meteor.userId() == userId;
}
});
Template.game.events({
'input input': function(event) {
Meteor.call('updateCard',
Session.get('currentGameId'),
this.id,
event.target.value);
}
})
Accounts.ui.config({
passwordSignupFields: 'USERNAME_ONLY'
});
}
////////////////////////////////////////////////////////////////////////////////
// Server code //
////////////////////////////////////////////////////////////////////////////////
if (Meteor.isServer) {
Meteor.publishComposite('gameData', function(gameId) {
return {
find: function() {
return Games.find({_id: gameId});
},
children: [
{
find: function(game) {
var playerIds = game.players.map(function(p) { return p.id; });
return Meteor.users.find({_id: {$in: playerIds}});
}
}
]
};
});
}
////////////////////////////////////////////////////////////////////////////////
// Methods //
////////////////////////////////////////////////////////////////////////////////
Meteor.methods({
newGame: function() {
console.log('Method "newGame" called.');
if(!this.userId) {
throw new Meteor.Error(
'login-required',
'You need to sign in before starting a new game.');
}
var gameId = Games.insert({players: []});
return gameId;
},
joinGame: function(gameId) {
console.log('Method "joinGame" called.');
if(!this.userId) {
throw new Meteor.Error(
'login-required',
'You need to sign in before joining a game.');
}
Games.update({
_id: gameId,
'players.id': {$ne: Meteor.userId()}
}, {
$push: {'players': {id: Meteor.userId(), card: ''}}
});
},
updateCard: function(gameId, userId, card) {
console.log('updateCard(' + gameId + ', ' + userId + ', ' + card + ')');
Games.update({_id: gameId, 'players.id': userId},
{$set: {'players.$.card': card}});
}
});