-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputed-collection.js
74 lines (69 loc) · 2.27 KB
/
computed-collection.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
Games = new Meteor.Collection("games");
if (Meteor.isClient) {
Session.setDefault("position", 0);
Meteor.loginWithPassword("joe-user", "foobar");
Deps.autorun(function () {
Meteor.subscribe("games", Session.get("position"));
});
Template.games.helpers({
games: function () {
return Games.find({},{sort: {relevance: -1, timeFromNow: 1}});
},
involvement: function () {
return (_.contains(this.players, Meteor.userId())) ?
"playing" : "not playing";
},
distance: function () {
return Math.abs(this.position - Session.get("position")).toFixed(1);
},
when: function () {
if (this.startsAt < Date.now()) {
return "happened " + moment(this.startsAt).fromNow();
} else {
return "will happen " + moment(this.startsAt).fromNow();
}
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
if (Meteor.users.find().count() === 0) {
var joeId = Accounts.createUser({
username: "joe-user", password: "foobar"
});
_.times(100, function () {
Games.insert({
position: 100 * (Random.fraction() - 0.5),
players: (Random.fraction() > 0.9) ? [joeId] : [],
startsAt: +moment().add('hours', 48 * (Random.fraction() - 0.5))
});
});
}
});
Meteor.publish("games", function (position) {
var self = this;
var RelevantGames = new Meteor.Collection(null);
var handle = Games.find({
$or: [{'players': self.userId},
{'position': {$gte: position - 10}},
{'position': {$lte: position + 10}}]
}).observeChanges({
added: function (id, fields) {
RelevantGames.insert(_.extend(fields, {
// extensible to several levels of relevance
relevance: ((fields.startsAt > Date.now()) ? 10 : 0)
+ ((_.contains(fields.players, self.userId)) ? 1 : 0),
timeFromNow: Math.abs(fields.startsAt - Date.now())
}));
}
// I realize this is not a complete solution, having only
// an `added` callback. However, I think the code as is illustrates
// the meat of my problem.
});
self.onStop(function () { handle.stop(); });
return RelevantGames.find({}, {
sort: {relevance: -1, timeFromNow: 1},
limit: 20
});
});
}