diff --git a/CHANGELOG.md b/CHANGELOG.md index 1333ed77b7..5f1508613a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,2 @@ -TODO +3/3/2013 + * Add custom day start: https://trello.com/card/custom-day-start/50e5d3684fe3a7266b0036d6/15 \ No newline at end of file diff --git a/src/app/character.coffee b/src/app/character.coffee index 155ea7961e..d9c3208c22 100644 --- a/src/app/character.coffee +++ b/src/app/character.coffee @@ -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: [] diff --git a/src/app/helpers.coffee b/src/app/helpers.coffee index 5c2cccaa15..ff0c82459d 100644 --- a/src/app/helpers.coffee +++ b/src/app/helpers.coffee @@ -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'} diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index 6818db1bac..f9f13a909c 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -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() diff --git a/test/user.mocha.coffee b/test/user.mocha.coffee index 3f4cc1ec5c..33ab7ee431 100644 --- a/test/user.mocha.coffee +++ b/test/user.mocha.coffee @@ -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 diff --git a/views/app/settings.html b/views/app/settings.html index 0bba816477..d9715751f2 100644 --- a/views/app/settings.html +++ b/views/app/settings.html @@ -9,8 +9,19 @@
{_user.apiToken}
+