Skip to content

Instantly share code, notes, and snippets.

@tjwebb
Created December 10, 2014 06:25
Show Gist options
  • Save tjwebb/9c3919f28030d46a7a85 to your computer and use it in GitHub Desktop.
Save tjwebb/9c3919f28030d46a7a85 to your computer and use it in GitHub Desktop.
fireball
Scores = new Meteor.Collection('scores');
if (Meteor.isClient) {
var timer = null;
/**
Sets the counter and start the game timer.
*/
var start = function () {
Session.set("counter", 10);
var startTime = new Date().getTime();
timer = setInterval(function () {
// every 10 seconds (10 * 1000), subtract 1 from the counter
var currentCounter = Session.get("counter");
if (currentCounter) {
Session.set("counter", currentCounter - 1);
} else {
var endTime = new Date().getTime();
Scores.insert({
score: endTime - startTime
});
clearInterval(timer);
alert("Game over!");
}
}, 1 * 1000);
}
// execute the start function
start();
Template.feed.helpers({
counter: function () {
return Session.get("counter");
},
mood: function () {
var food = Session.get("counter");
if (!food) {
return "Your dragon has run away!";
}
var mood = "The dragon is ";
if (food <= 3) {
return mood + "angry.";
} else if (food <= 5) {
return mood + "hungry.";
} else if (food === 20) {
return mood + "full.";
} else {
return mood + "content.";
}
}
});
Template.feed.events({
'click button': function () {
// increment the counter when button is clicked
var currentCounter = Session.get("counter");
if (currentCounter > 0 && currentCounter < 20) {
Session.set("counter", currentCounter + 1);
}
}
});
Template.reset.events({
'click button': function () {
clearInterval(timer);
start();
}
});
Template.scores.helpers({
scores: function () {
return Scores.find({}, { sort: { score: -1 }});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment