Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add timer for counting game time.

  • Loading branch information...
commit 207e10c7133a8d0989317822f2811556192bf1ec 1 parent 5155a21
@jimmycuadra authored
View
3  source/javascripts/application.js.coffee
@@ -1,6 +1,7 @@
#= require ./vendor/underscore
#= require ./vendor/backbone
#= require ./vendor/amplify.store
+#= require ./vendor/timer
#= require bootstrap-modal
#= require hamlcoffee
#= require core
@@ -35,7 +36,7 @@ class ph.Application
@music.play() if @store.get("autoPlayMusic")
setUpHUD: ->
- @stats = new ph.StatsView
+ @stats = new ph.StatsView(store: @store)
@musicPlayer = new ph.MusicPlayerView(model: @music)
@settings = new ph.SettingsView(model: @store)
View
13 source/javascripts/core.js.coffee
@@ -1 +1,14 @@
window.ph = {}
+
+HAML.globals = ->
+ {
+ formatTime: (ticks) ->
+ hours = parseInt(ticks / 3600)
+ minutes = parseInt((ticks / 60) % 60)
+ seconds = ticks % 60
+
+ minutes = "0#{minutes}" if minutes < 10
+ seconds = "0#{seconds}" if seconds < 10
+
+ "#{hours}:#{minutes}:#{seconds}"
+ }
View
13 source/javascripts/models/store.js.coffee
@@ -2,11 +2,16 @@ class ph.Store extends Backbone.Model
defaults:
highScore: 0
gamesPlayed: 0
+ totalTime: 0
autoPlayMusic: true
playSoundEffects: true
initialize: ->
+ @set(currentTime: 0)
@on("change", @persist, this)
+ @timer = new Timer
+ @timer.every(1, @addTick)
+ @timer.start()
persist: (store) ->
amplify.store("pushing-hands", @toJSON())
@@ -19,4 +24,10 @@ class ph.Store extends Backbone.Model
resetStats: ->
if confirm "Are you sure you want to permanently reset your stats?"
- @set(highScore: 0, gamesPlayed: 0)
+ @timer.reset()
+ @set(highScore: 0, gamesPlayed: 0, currentTime: 0, totalTime: 0)
+ @timer.start()
+
+ addTick: =>
+ ticks = @timer.ticks()
+ @set(currentTime: ticks, totalTime: @get("totalTime") + 1)
View
4 source/javascripts/templates/stats.hamlc
@@ -10,7 +10,7 @@
%tr
%td 0
%td 0
- %td 0
+ %td= @formatTime(@currentTime)
%h4 All time
%table.table.table-bordered.table-condensed
%thead
@@ -23,5 +23,5 @@
%tr
%td 0
%td 0
- %td 0
+ %td= @formatTime(@totalTime)
%td 0
View
153 source/javascripts/vendor/timer.js
@@ -0,0 +1,153 @@
+/*
+ * Timer.js: A periodic timer for Node.js and the browser.
+ *
+ * Copyright (c) 2012 Arthur Klepchukov, Jarvis Badgley, Florian Schäfer
+ * Licensed under the BSD license (BSD_LICENSE.txt)
+ *
+ * Version: 0.0.1
+ *
+ */ (function(root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory();
+ } else if (typeof define === 'function' && define.amd) {
+ define(factory);
+ } else {
+ root.Timer = factory();
+ }
+})(this, function() {
+
+ function timeStringToMilliseconds(timeString) {
+ if (typeof timeString === 'string') {
+
+ if (isNaN(parseInt(timeString, 10))) {
+ timeString = '1' + timeString;
+ }
+
+ var match = timeString.replace(/[^a-z0-9\.]/g, '').match(/(?:(\d+(?:\.\d+)?)(?:days?|d))?(?:(\d+(?:\.\d+)?)(?:hours?|hrs?|h))?(?:(\d+(?:\.\d+)?)(?:minutes?|mins?|m\b))?(?:(\d+(?:\.\d+)?)(?:seconds?|secs?|s))?(?:(\d+(?:\.\d+)?)(?:milliseconds?|ms))?/);
+
+ if (match[0]) {
+ return parseFloat(match[1] || 0) * 86400000 + // days
+ parseFloat(match[2] || 0) * 3600000 + // hours
+ parseFloat(match[3] || 0) * 60000 + // minutes
+ parseFloat(match[4] || 0) * 1000 + // seconds
+ parseInt(match[5] || 0, 10); // milliseconds
+ }
+
+ if (!isNaN(parseInt(timeString, 10))) {
+ return parseInt(timeString, 10);
+ }
+ }
+
+ if (typeof timeString === 'number') {
+ return timeString;
+ }
+
+ return 0;
+ }
+
+ function millisecondsToTicks(milliseconds, resolution) {
+ return parseInt(milliseconds / resolution, 10) || 1;
+ }
+
+ function Timer(resolution) {
+ if (this instanceof Timer === false) {
+ return new Timer(resolution);
+ }
+
+ this._notifications = [];
+ this._resolution = timeStringToMilliseconds(resolution) || 1000;
+ this._running = false;
+ this._ticks = 0;
+ this._timer = null;
+ }
+
+ Timer.prototype = {
+ start: function() {
+ var self = this;
+ if (!this._running) {
+ this._running = !this._running;
+ setTimeout(function loopsyloop() {
+ self._ticks++;
+ for (var i = 0, l = self._notifications.length; i < l; i++) {
+ if (self._ticks % self._notifications[i].ticks === 0) {
+ self._notifications[i].callback.call(self._notifications[i], {
+ ticks: self._ticks,
+ resolution: self._resolution
+ });
+ }
+ }
+ if (self._running) {
+ self._timer = setTimeout(loopsyloop, self._resolution);
+ }
+ }, this._resolution);
+ }
+ return this;
+ },
+ stop: function() {
+ if (this._running) {
+ this._running = !this._running;
+ clearTimeout(this._timer);
+ }
+ return this;
+ },
+ reset: function() {
+ this.stop();
+ this._ticks = 0;
+ return this;
+ },
+ clear: function() {
+ this.reset();
+ this._notifications = [];
+ return this;
+ },
+ ticks: function() {
+ return this._ticks;
+ },
+ resolution: function() {
+ return this._resolution;
+ },
+ running: function() {
+ return this._running;
+ },
+ bind: function(when, callback) {
+ if (when && callback) {
+ var ticks;
+ if (typeof when === 'number') {
+ ticks = when;
+ } else {
+ ticks = millisecondsToTicks(timeStringToMilliseconds(when), this._resolution);
+ }
+ this._notifications.push({
+ ticks: ticks,
+ callback: callback
+ });
+ }
+ return this;
+ },
+ unbind: function(callback) {
+ if (!callback) {
+ this._notifications = [];
+ } else {
+ for (var i = 0, l = this._notifications.length; i < l; i++) {
+ if (this._notifications[i].callback === callback) {
+ this._notifications.splice(i, 1);
+ }
+ }
+ }
+ return this;
+ }
+
+ };
+
+ Timer.prototype.every = Timer.prototype.bind;
+ Timer.prototype.after = function(when, callback) {
+ var self = this;
+ Timer.prototype.bind.call(self, when, function fn() {
+ Timer.prototype.unbind.call(self, fn);
+ callback.apply(this, arguments);
+ });
+ };
+
+ return Timer;
+
+});
View
6 source/javascripts/views/stats_view.js.coffee
@@ -3,6 +3,10 @@ class ph.StatsView extends Backbone.View
template: JST.stats
+ initialize: ->
+ @store = @options.store
+ @store.on("change", @render, this)
+
render: ->
- @$el.html(@template({}))
+ @$el.html(@template(@store.toJSON()))
this
Please sign in to comment.
Something went wrong with that request. Please try again.