habitrpg-shared: use newUser from shared helpers instead of in character.coffee

This commit is contained in:
Tyler Renelle 2013-05-24 11:32:04 +01:00
parent 3ed2412f87
commit 62c9e45e2e
5 changed files with 8 additions and 69 deletions

View file

@ -82,68 +82,6 @@ module.exports.app = (appExports, model) ->
model.del "users.#{user.get('id')}", ->
window.location.href = "/logout"
userSchema =
# _id
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', dayStart:0, showHelm: true }
habitIds: []
dailyIds: []
todoIds: []
rewardIds: []
apiToken: null # set in newUserObject below
lastCron: 'new' #this will be replaced with `+new Date` on first run
balance: 0
tasks: {}
flags:
partyEnabled: false
itemsEnabled: false
tags: []
# ads: 'show' # added on registration
module.exports.newUserObject = ->
# deep clone, else further new users get duplicate objects
newUser = _.cloneDeep userSchema
newUser.apiToken = derby.uuid()
repeat = {m:true,t:true,w:true,th:true,f:true,s:true,su:true}
defaultTasks = [
{type: 'habit', text: '1h Productive Work', notes: '-- Habits: Constantly Track --\nFor some habits, it only makes sense to *gain* points (like this one).', value: 0, up: true, down: false }
{type: 'habit', text: 'Eat Junk Food', notes: 'For others, it only makes sense to *lose* points', value: 0, up: false, down: true}
{type: 'habit', text: 'Take The Stairs', notes: 'For the rest, both + and - make sense (stairs = gain, elevator = lose)', value: 0, up: true, down: true}
{type: 'daily', text: '1h Personal Project', notes: '-- Dailies: Complete Once a Day --\nAt the end of each day, non-completed Dailies dock you points.', value: 0, completed: false, repeat: repeat }
{type: 'daily', text: 'Exercise', notes: "If you are doing well, they turn green and are less valuable (experience, gold) and less damaging (HP). This means you can ease up on them for a bit.", value: 3, completed: false, repeat: repeat }
{type: 'daily', text: '45m Reading', notes: 'But if you are doing poorly, they turn red. The worse you do, the more valuable (exp, gold) and more damaging (HP) these goals become. This encourages you to focus on your shortcomings, the reds.', value: -10, completed: false, repeat: repeat }
{type: 'todo', text: 'Call Mom', notes: "-- Todos: Complete Eventually --\nNon-completed Todos won't hurt you, but they will become more valuable over time. This will encourage you to wrap up stale Todos.", value: -3, completed: false }
{type: 'reward', text: '1 Episode of Game of Thrones', notes: '-- Rewards: Treat Yourself! --\nAs you complete goals, you earn gold to buy rewards. Buy them liberally - rewards are integral in forming good habits.', value: 20 }
{type: 'reward', text: 'Cake', notes: 'But only buy if you have enough gold - you lose HP otherwise.', value: 10 }
]
defaultTags = [
{name: 'morning'}
{name: 'afternoon'}
{name: 'evening'}
]
for task in defaultTasks
guid = task.id = derby.uuid()
newUser.tasks[guid] = task
switch task.type
when 'habit' then newUser.habitIds.push guid
when 'daily' then newUser.dailyIds.push guid
when 'todo' then newUser.todoIds.push guid
when 'reward' then newUser.rewardIds.push guid
for tag in defaultTags
tag.id = derby.uuid()
newUser.tags.push tag
return newUser
module.exports.BatchUpdate = BatchUpdate = (model) ->
user = model.at("_user")
transactionInProgress = false

View file

@ -121,7 +121,6 @@ get '/', (page, model, params, next) ->
ready (model) ->
user = model.at('_user')
model.setNull '_user.apiToken', derby.uuid()
browser = require './browser'
require('./character').app(exports, model)

View file

@ -12,6 +12,8 @@ priv = require './private'
habitrpgStore = require './store'
middleware = require './middleware'
helpers = require("habitrpg-shared/script/helpers")
## RACER CONFIGURATION ##
#racer.io.set('transports', ['xhr-polling'])
@ -50,7 +52,7 @@ strategies =
options =
domain: process.env.BASE_URL || 'http://localhost:3000'
allowPurl: true
schema: require('../app/character').newUserObject()
schema: helpers.newUser(true)
# This has to happen before our middleware stuff
auth.store(store, habitrpgStore.customAccessControl)

View file

@ -22,7 +22,7 @@ expect.Assertion::be = expect.Assertion::equal = (obj) ->
origBe.call this, obj
# Custom modules
character = require '../src/app/character'
helpers = require 'habitrpg-shared/script/helpers'
###### Helpers & Variables ######
@ -48,7 +48,7 @@ describe 'API', ->
#store.flush()
model = store.createModel()
model.set '_userId', uid = model.id()
user = character.newUserObject()
user = helpers.newUser(true)
user.apiToken = model.id()
model.session = {userId:uid}
model.set "users.#{uid}", user

View file

@ -7,7 +7,7 @@ moment = require 'moment'
# Custom modules
scoring = require '../src/app/scoring'
schema = require '../src/app/character'
helpers = require '../src/app/helpers'
helpers = require 'habitrpg-shared/script/helpers'
###### Helpers & Variables ######
@ -24,7 +24,7 @@ pathSnapshots = (paths) ->
statsTask = -> pathSnapshots(['_user.stats', taskPath]) # quick snapshot of user.stats & task
cleanUserObj = ->
userObj = schema.newUserObject()
userObj = helpers.newUser()
userObj.tasks = {}
userObj.habitIds = []
userObj.dailyIds = []
@ -84,7 +84,7 @@ describe 'User', ->
before ->
model = new Model
model.set '_user', schema.newUserObject()
model.set '_user', helpers.newUser()
scoring.setModel model
it 'sets correct user defaults', ->