custom dayStart

This commit is contained in:
Tyler Renelle 2013-03-03 00:17:39 -05:00
parent 0f0ca21f82
commit 34f5f2dc54
6 changed files with 37 additions and 10 deletions

View file

@ -1 +1,2 @@
TODO
3/3/2013
* Add custom day start: https://trello.com/card/custom-day-start/50e5d3684fe3a7266b0036d6/15

View file

@ -99,7 +99,7 @@ userSchema =
stats: { gp: 0, exp: 0, lvl: 1, hp: 50 }
party: { current: null, invitation: null }
items: { weapon: 0, armor: 0, head: 0, shield: 0 }
preferences: { gender: 'm', skin: 'white', hair: 'blond', armorSet: 'v1' }
preferences: { gender: 'm', skin: 'white', hair: 'blond', armorSet: 'v1', dayStart:0 }
habitIds: []
dailyIds: []
todoIds: []

View file

@ -1,8 +1,10 @@
moment = require('moment')
# Absolute diff between two dates, based on 12am for both days
module.exports.daysBetween = (a, b) ->
Math.abs(moment(a).startOf('day').diff(moment(b).startOf('day'), 'days'))
# Absolute diff between two dates
module.exports.daysBetween = (yesterday, now, dayStart) ->
#sanity-check reset-time (is it 24h time?)
dayStart = 0 unless (dayStart? and (dayStart = parseInt(dayStart)) and dayStart >= 0 and dayStart <= 24)
Math.abs moment(yesterday).startOf('day').add('h', dayStart).diff(moment(now), 'days')
module.exports.dayMapping = dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s',7:'su'}

View file

@ -180,7 +180,7 @@ updateStats = (model, newStats, batch) ->
cron = (model) ->
user = model.at '_user'
today = +new Date
daysPassed = helpers.daysBetween(today, user.get('lastCron'))
daysPassed = helpers.daysBetween(user.get('lastCron'), today, user.get('preferences.dayStart'))
if daysPassed > 0
batch = new character.BatchUpdate(model)
batch.startTransaction()

View file

@ -1,13 +1,14 @@
{expect} = require 'derby/node_modules/racer/test/util'
{BrowserModel: Model} = require 'derby/node_modules/racer/test/util/model'
derby = require 'derby'
clone = require 'clone'
lodash = require 'lodash'
_ = require 'underscore'
moment = require 'moment'
# Custom modules
scoring = require '../src/app/scoring'
schema = require '../src/app/schema'
schema = require '../src/app/character'
helpers = require '../src/app/helpers'
###### Helpers & Variables ######
@ -19,8 +20,8 @@ taskPath = null
# Otherwise, using model.get(path) will give the same object before as after
pathSnapshots = (paths) ->
if _.isString(paths)
return clone(model.get(paths))
_.map paths, (path) -> clone(model.get(path))
return lodash.cloneDeep(model.get(paths))
_.map paths, (path) -> lodash.cloneDeep(model.get(path))
statsTask = -> pathSnapshots(['_user.stats', taskPath]) # quick snapshot of user.stats & task
cleanUserObj = ->
@ -67,6 +68,18 @@ modificationsLookup = (direction, options = {}) ->
###### Specs ######
describe 'Cron', ->
it 'calculates day differences with dayStart properly', ->
dayStart = 4
yesterday = moment().subtract('d', 1).add('h', dayStart)
now = moment().startOf('day').add('h', dayStart-1) #today
console.log {yesterday: yesterday.format('MM/DD HH:00'), now: now.format('MM/DD HH:00')}
console.log {diff: Math.abs(moment(yesterday).diff(moment(now), 'days'))}
expect(helpers.daysBetween(yesterday, now, dayStart)).to.eql 0
now = moment().startOf('day').add('h', dayStart)
console.log {now: now.format('MM/DD HH:00')}
expect(helpers.daysBetween(yesterday, now, dayStart)).to.eql 1
describe 'User', ->
model = null

View file

@ -9,8 +9,19 @@
<h6>API Token</h6>
<pre class=prettyprint>{_user.apiToken}</pre>
<hr/>
<h4>Custom Day Start</h4>
<div class="input-append">
<input class="span2" type="number" min=0 max=24 value={_user.preferences.dayStart} />
<span class="add-on">:00 (24h)</span>
</div>
<div>
<small>Habit defaults to check and reset your dailies at midnight each day. You can customize that here (Enter number between 0 and 24).</small>
</div>
{{#if _user.auth.local}}
<hr/>
<h4>Change Password</h4>
<derby-auth:changePassword />
{{/}}