-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminstant.js
More file actions
284 lines (273 loc) · 8.05 KB
/
minstant.js
File metadata and controls
284 lines (273 loc) · 8.05 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
Chats = new Mongo.Collection("chats");
Chats.allow({
insert: function (userId, doc) {
// the user must be logged in, and the document must be owned by the user
return (userId && (doc.user1Id === userId || doc.user2Id === userId));
},
update: function (userId, doc, fields, modifier) {
// can only change your own documents
//console.log(doc);
//console.log(doc.user1Id);
//console.log(doc.user2Id);
//console.log(userId);
//console.log((userId && (doc.user1Id === userId || doc.user2Id === userId)));
//return true;
return (userId && (doc.user1Id === userId || doc.user2Id === userId));
},
remove: function (userId, doc) {
// can only remove your own documents
return (userId && (doc.user1Id === userId || doc.user2Id === userId));
},
fetch: ['user1Id','user2Id']
});
if (Meteor.isClient) {
Meteor.subscribe("chatsData");
Meteor.subscribe("userData");
Meteor.subscribe("allUserData");
Meteor.subscribe('emojis');
// set up the main template the the router will use to build pages
Router.configure({
layoutTemplate: 'ApplicationLayout'
});
// specify the top level route, the page users see when they arrive at the site
Router.route('/', function () {
console.log("rendering root /");
this.render("navbar", {to:"header"});
this.render("lobby_page", {to:"main"});
});
// specify a route that allows the current user to chat to another users
Router.route('/chat/:_id', function () {
// the user they want to chat to has id equal to
// the id sent in after /chat/...
var otherUserId = this.params._id;
// 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 chat = Chats.findOne(filter);
//console.log(chat._id);
if (!chat){// no chat matching the filter - need to insert a new one
if (Meteor.userId()) {
chatId = Chats.insert({user1Id:Meteor.userId(), user2Id:otherUserId});
}
}
else {// there is a chat going already - use that.
chatId = chat._id;
}
if (chatId){// looking good, save the id to the session
Session.set("chatId",chatId);
}
this.render("navbar", {to:"header"});
this.render("chat_page", {to:"main"});
});
///
// helper functions
///
Template.available_user_list.helpers({
users:function(){
return Meteor.users.find();
}
})
Template.available_user.helpers({
getUsername:function(userId){
user = Meteor.users.findOne({_id:userId});
return user.profile.username;
},
isMyUser:function(userId){
if (userId == Meteor.userId()){
return true;
}
else {
return false;
}
}
})
Template.chat_page.helpers({
messages:function(){
var chat = Chats.findOne({_id:Session.get("chatId")});
return chat.messages;
},
field:function(row, col){
//user = Meteor.users.findOne({_id:userId});
var chat = Chats.findOne({_id:Session.get("chatId")});
var v = chat["f"+row+col];
//console.log(v);
if (v=="1") {
return "X";
}
if (v=="2") {
return "O";
}
return " ";
},
other_user:function(){
return ""
},
message:function(){
return Session.get("message");
},
})
function makeMove(event, field) {
var chat = Chats.findOne({_id:Session.get("chatId")});
console.log(chat);
if (chat[field]) {
Session.set("message","Incorrect move! Square is not empty");
return;
}
move = chat.move;
if (!move) {
move = 1;
}
var v = 1;
if (chat.user1Id == this.userId) {
v = 2;
if (move==1) {
Session.set("message","Not your turn!");
return;
}
} else {
if (move==2) {
Session.set("message","Not your turn!");
return;
}
}
Chats.update({
_id: chat._id
}, {
$set: {
[field]: v,
move: 3 - move
}
});
Session.set("message","");
}
Template.chat_page.events({
// this event fires when the user sends a message on the chat page
'click .f00':function(event){
makeMove(event, "f00");
},
'click .f01':function(event){
makeMove(event, "f01");
},
'click .f02':function(event){
makeMove(event, "f02");
},
'click .f10':function(event){
makeMove(event, "f10");
},
'click .f11':function(event){
makeMove(event, "f11");
},
'click .f12':function(event){
makeMove(event, "f12");
},
'click .f20':function(event){
makeMove(event, "f20");
},
'click .f21':function(event){
makeMove(event, "f21");
},
'click .f22':function(event){
makeMove(event, "f22");
},
'click .clear':function(event){
console.log("clicked");
var chat = Chats.findOne({_id:Session.get("chatId")});
Chats.update({
_id: chat._id
}, {
$set: {
f00: 0,
f01: 0,
f02: 0,
f10: 0,
f11: 0,
f12: 0,
f20: 0,
f21: 0,
f22: 0,
move: 1
}
});
Session.set("message","Cleared");
},
'submit .js-send-chat':function(event){
// stop the form from triggering a page reload
event.preventDefault();
// see if we can find a chat object in the database
// to which we'll add the message
var chat = Chats.findOne({_id:Session.get("chatId")});
if (chat){// ok - we have a chat to use
var msgs = chat.messages; // pull the messages property
if (!msgs){// no messages yet, create a new array
msgs = [];
}
// is a good idea to insert data straight from the form
// (i.e. the user) into the database?? certainly not.
// push adds the message to the end of the array
uID = Meteor.userId();
user = Meteor.users.findOne({_id:uID});
msgs.push({text: event.target.chat.value, user: user.profile.username, avatar: user.profile.avatar});
// reset the form
event.target.chat.value = "";
// put the messages array onto the chat object
// chat.messages = msgs;
// update the chat object in the database.
//Chats.update(chat._id, chat);
console.log(chat.user1Id);
console.log(chat.user2Id);
Chats.update({
_id: chat._id
}, {
$set: {
messages: msgs
}
});
}
}
})
}
// start up script that creates some users for testing
// users have the username 'user1@test.com' .. 'user8@test.com'
// and the password test123
if (Meteor.isServer) {
Meteor.publish("chatsData", function () {
//console.log("Chatsc"+Chats.find().count());
return Chats.find({
$or: [
{ user1Id: this.userId },
{ user2Id: this.userId }
]
});
// return Chats.find({
// $and: [{userId:this.user1Id},
// {userId:this.user2Id}]
// })
});
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId},
{fields: {'other': 1, 'things': 1}});
});
Meteor.publish("allUserData", function () {
console.log("using allUserData");
return Meteor.users.find({});
});
Meteor.publish('emojis', function() {
// Here you can choose to publish a subset of all emojis
// if you'd like to.
return Emojis.find();
});
Meteor.startup(function () {
if (!Meteor.users.findOne()){
for (var i=1;i<9;i++){
var email = "user"+i+"@test.com";
var username = "user"+i;
var avatar = "ava"+i+".png"
console.log("creating a user with password 'test123' and username/ email: "+email);
Meteor.users.insert({profile:{username:username, avatar:avatar}, emails:[{address:email}],services:{ password:{"bcrypt" : "$2a$10$I3erQ084OiyILTv8ybtQ4ON6wusgPbMZ6.P33zzSDei.BbDL.Q4EO"}}});
}
}
});
}