HTML and CSS Reference
In-Depth Information
}));"/click",authenticated(function(req,res) {
dbMethods.fetchUser(req.session,function(user) {
dbMethods.clickUser(user,function(clicked) {
if(clicked) {
var now = new Date().getTime(),
nextClick = (user.next_click - now)/1000;
res.json({ clicked: true, user: clicked, nextClick: nextClick
} else {
res.json({ clicked: false })
app.get('/top-ten',authenticated(function(req,res) {
dbMethods.topTen(function(results) {
res.json({ users: results });
The top var declaration sets up the clickTime variable, which controls how long a player needs to wait
to click, and a dbMethods object, which will be filled with the database methods.
Next, the application pulls in the mongodb driver and connects to the database. Again, an environment vari-
able is used if it exists; otherwise, the local database blob_clicker is used.
Next, the system connects to the users collection and defines the three database methods: fetchUser ,
clickUser , and topTen . The first uses collection.findOne to look up a user by her Facebook ID,
and if one is found it returns that. Otherwise it sets up a new object that can be used instead. The user object is
then passed to the callback. The system relies on the user's Facebook ID as the unique identifier by which to
look up users.
The second method, clickUser , takes in the user object and sees if that object is ready to be clicked again.
If so, it updates the click count and the next click and saves the object, returning true to the callback after the
object is saved. If not, it calls the callback with false .
The third method, topTen , uses the find , sort , and limit methods to return a list of the top-ten click-
ers, sorted by the clicks field in descending order. The MongoDB collection method supports chaining
multiple query methods together, with a final call to toArray returning the array of results.
Search WWH ::

Custom Search