From 703deac359d6fa6b759ccb111108cfc72f076348 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Fri, 1 Feb 2013 13:22:00 -0500 Subject: [PATCH] cbug fixes. also have to explicitely get all user properties one-by-one, instead of user.get() for some reason --- src/app/index.coffee | 6 +++--- src/app/schema.coffee | 44 ++++++++++++++++++++++++------------------ src/app/scoring.coffee | 14 ++++++++------ 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/app/index.coffee b/src/app/index.coffee index 158b1fe342..92c748c1ab 100644 --- a/src/app/index.coffee +++ b/src/app/index.coffee @@ -46,7 +46,7 @@ get '/', (page, model, next) -> #user = result.at(0) model.ref '_user', user batch = new schema.BatchUpdate(model) - userObj = batch.getUser() + userObj = batch.userObj return page.redirect '/500.html' unless userObj? #this should never happen, but it is. Looking into it @@ -82,13 +82,13 @@ resetDom = (model) -> ready (model) -> user = model.at('_user') + scoring.setModel(model) #set cron immediately lastCron = user.get('lastCron') - user.set('lastCron', +new Date) if (!lastCron or lastCron == 'new') + user.set('lastCron', +new Date) if (!lastCron? or lastCron == 'new') # Setup model in scoring functions - scoring.setModel(model) scoring.cron(resetDom) # Load all the jQuery, Growl, Tour, etc diff --git a/src/app/schema.coffee b/src/app/schema.coffee index f63088a719..498dc1c82d 100644 --- a/src/app/schema.coffee +++ b/src/app/schema.coffee @@ -4,22 +4,24 @@ _ = require 'underscore' lodash = require 'lodash' derby = require 'derby' +userSchema = + lastCron: 'new' #this will be replaced with `+new Date` on first run + balance: 2 + stats: { money: 0, exp: 0, lvl: 1, hp: 50 } + items: { itemsEnabled: false, armor: 0, weapon: 0 } + notifications: { kickstarter: 'show' } + preferences: { gender: 'm', armorSet: 'v1' } + flags: { partyEnabled: false } + friends: [] + tasks: {} + habitIds: [] + dailyIds: [] + todoIds: [] + rewardIds: [] + module.exports.newUserObject = -> # deep clone, else further new users get duplicate objects - newUser = require('lodash').cloneDeep - lastCron: 'new' #this will be replaced with `+new Date` on first run - balance: 2 - stats: { money: 0, exp: 0, lvl: 1, hp: 50 } - items: { itemsEnabled: false, armor: 0, weapon: 0 } - notifications: { kickstarter: 'show' } - preferences: { gender: 'm', armorSet: 'v1' } - flags: { partyEnabled: false } - friends: [] - tasks: {} - habitIds: [] - dailyIds: [] - todoIds: [] - rewardIds: [] + newUser = require('lodash').cloneDeep userSchema for task in content.defaultTasks guid = task.id = require('racer').uuid() newUser.tasks[guid] = task @@ -31,7 +33,7 @@ module.exports.newUserObject = -> return newUser module.exports.updateUser = (batch) -> - userObj = batch.getUser() + userObj = batch.userObj batch.queue('notifications.kickstarter', 'show') unless userObj.notifications?.kickstarter? batch.queue('friends', []) unless !_.isEmpty(userObj.friends) @@ -63,13 +65,17 @@ module.exports.updateUser = (batch) -> module.exports.BatchUpdate = BatchUpdate = (model) -> user = model.at('_user') - userObj = undefined + + # this is really stupid, but i can't find how to get around user.get() making only available what has been gotten specifically before + obj = {} + _.each Object.keys(userSchema), (key) -> obj[key] = user.get(key) + userObj = lodash.cloneDeep obj # whaaa??? modifying userObj modifies the value of user.get() at that path? + updates = {} { queue: (path, val) -> updates[path] = val - getUser: -> - userObj ?= lodash.cloneDeep(user.get()) # whaaa??? modifying userObj modifies the value of user.get() at that path? + userObj: userObj ### Handles updating the user model. If this is an en-mass operation (eg, server cron), pass the user object as {update}. @@ -83,7 +89,7 @@ module.exports.BatchUpdate = BatchUpdate = (model) -> if (arr.length - 1) == index curr[next] = val curr[next] - , @getUser() + , userObj commit: -> commit = model._commit diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index c294639b30..501a2f8b21 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -59,7 +59,7 @@ taskDeltaFormula = (currentValue, direction) -> {update} if aggregated changes, pass in userObj as update. otherwise commits will be made immediately ### updateStats = (newStats, batch) -> - userObj = batch.getUser() + userObj = batch.userObj # if user is dead, dont do anything return if userObj.stats.lvl == 0 @@ -98,9 +98,11 @@ updateStats = (newStats, batch) -> score = (taskId, direction, times, batch) -> times ?= 1 - commit = !batch? - batch ?= new schema.BatchUpdate(model) - userObj = batch.getUser() + commit = false + unless batch? + commit = true + batch = new schema.BatchUpdate(model) + userObj = batch.userObj {money, hp, exp, lvl} = userObj.stats @@ -178,7 +180,7 @@ cron = (resetDom_cb) -> if daysPassed > 0 user.set 'lastCron', today batch = new schema.BatchUpdate(model) - userObj = batch.getUser() + userObj = batch.userObj hpBefore = userObj.stats.hp #we'll use this later so we can animate hp loss # Tally each task todoTally = 0 @@ -200,7 +202,7 @@ cron = (resetDom_cb) -> thatDay = moment().subtract('days', n+1) if repeat[helpers.dayMapping[thatDay.day()]]==true daysFailed++ - score id, 'down', daysFailed, userObj + score id, 'down', daysFailed, batch value = taskObj.value #get updated value if type == 'daily'