2012-09-24 20:45:37 +00:00
|
|
|
derby = require 'derby'
|
2013-03-04 16:17:17 +00:00
|
|
|
app = derby.createApp module
|
2013-03-23 20:03:12 +00:00
|
|
|
|
2013-03-04 16:17:17 +00:00
|
|
|
{get, view, ready} = app
|
2013-03-17 04:48:00 +00:00
|
|
|
derby.use require('derby-ui-boot'), {styles: []}
|
2013-02-12 00:27:35 +00:00
|
|
|
derby.use require '../../ui'
|
|
|
|
|
derby.use require 'derby-auth/components'
|
2012-07-27 23:38:28 +00:00
|
|
|
|
2013-03-23 20:03:12 +00:00
|
|
|
# Translations
|
|
|
|
|
i18n = require './i18n'
|
|
|
|
|
i18n.localize app,
|
|
|
|
|
availableLocales: ['en', 'he']
|
|
|
|
|
defaultLocale: 'en'
|
|
|
|
|
|
2012-09-24 20:45:37 +00:00
|
|
|
helpers = require './helpers'
|
2013-02-05 05:20:58 +00:00
|
|
|
helpers.viewHelpers view
|
2013-02-08 07:01:48 +00:00
|
|
|
|
2013-01-21 00:35:31 +00:00
|
|
|
_ = require('underscore')
|
2012-06-20 21:30:10 +00:00
|
|
|
|
2012-07-28 01:11:53 +00:00
|
|
|
# ========== ROUTES ==========
|
2012-07-08 20:59:07 +00:00
|
|
|
|
2013-04-10 22:32:38 +00:00
|
|
|
###
|
|
|
|
|
Cleanup task-corruption (null tasks, rogue/invisible tasks, etc)
|
|
|
|
|
Obviously none of this should be happening, but we'll stop-gap until we can find & fix
|
2013-04-11 15:39:54 +00:00
|
|
|
Gotta love refLists! see https://github.com/lefnire/habitrpg/issues/803 & https://github.com/lefnire/habitrpg/issues/6343
|
2013-04-10 22:32:38 +00:00
|
|
|
###
|
|
|
|
|
cleanupCorruptTasks = (model) ->
|
|
|
|
|
user = model.at('_user')
|
|
|
|
|
tasks = user.get('tasks')
|
2013-04-18 20:52:47 +00:00
|
|
|
wasCorrupt = false
|
2013-04-10 22:32:38 +00:00
|
|
|
|
|
|
|
|
## Remove corrupted tasks
|
|
|
|
|
_.each tasks, (task, key) ->
|
2013-04-10 23:24:27 +00:00
|
|
|
unless task?.id? and task?.type?
|
2013-04-10 22:32:38 +00:00
|
|
|
user.del("tasks.#{key}")
|
|
|
|
|
delete tasks[key]
|
|
|
|
|
|
|
|
|
|
## Task List Cleanup
|
|
|
|
|
_.each ['habit','daily','todo','reward'], (type) ->
|
|
|
|
|
|
|
|
|
|
# 1. remove duplicates
|
|
|
|
|
# 2. restore missing zombie tasks back into list
|
2013-04-10 23:24:27 +00:00
|
|
|
idList = user.get("#{type}Ids")
|
2013-04-18 20:52:47 +00:00
|
|
|
taskIds = _.pluck( _.where(tasks, {type:type}), 'id' )
|
2013-04-10 22:32:38 +00:00
|
|
|
union = _.union idList, taskIds
|
|
|
|
|
|
|
|
|
|
# 2. remove empty (grey) tasks
|
2013-04-10 23:24:27 +00:00
|
|
|
preened = _.filter union, (id) -> id and _.contains(taskIds, id)
|
2013-04-10 22:32:38 +00:00
|
|
|
|
|
|
|
|
# There were indeed issues found, set the new list
|
2013-04-11 15:49:55 +00:00
|
|
|
if !_.isEqual(idList, preened)
|
2013-04-18 20:52:47 +00:00
|
|
|
user.set("#{type}Ids", preened)
|
|
|
|
|
wasCorrupt = true
|
|
|
|
|
"#{type}s were corrupt."
|
2013-04-10 22:32:38 +00:00
|
|
|
|
2013-04-18 20:52:47 +00:00
|
|
|
require('./browser').resetDom(model) if wasCorrupt
|
2013-04-11 15:33:50 +00:00
|
|
|
|
2013-02-25 23:59:14 +00:00
|
|
|
get '/', (page, model, params, next) ->
|
2013-02-21 18:40:03 +00:00
|
|
|
return page.redirect '/' if page.params?.query?.play?
|
|
|
|
|
|
2013-04-18 20:53:59 +00:00
|
|
|
# removed force-ssl (handled in nginx), see git for code
|
2012-09-15 19:06:51 +00:00
|
|
|
|
2013-04-04 20:44:54 +00:00
|
|
|
require('./party').partySubscribe page, model, params, next, ->
|
2013-04-11 15:39:54 +00:00
|
|
|
model.setNull '_user.apiToken', derby.uuid()
|
2013-04-04 19:36:52 +00:00
|
|
|
|
2013-04-11 15:39:54 +00:00
|
|
|
require('./items').server(model)
|
2013-04-10 22:32:38 +00:00
|
|
|
|
2013-04-04 19:36:52 +00:00
|
|
|
#refLists
|
|
|
|
|
_.each ['habit', 'daily', 'todo', 'reward'], (type) ->
|
|
|
|
|
model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids"
|
|
|
|
|
|
2013-02-18 17:49:53 +00:00
|
|
|
page.render()
|
2012-04-27 02:19:31 +00:00
|
|
|
|
2012-07-28 01:11:53 +00:00
|
|
|
# ========== CONTROLLER FUNCTIONS ==========
|
2012-04-27 02:19:31 +00:00
|
|
|
|
|
|
|
|
ready (model) ->
|
2013-01-21 17:06:12 +00:00
|
|
|
user = model.at('_user')
|
2013-04-18 20:52:47 +00:00
|
|
|
cleanupCorruptTasks(model) # https://github.com/lefnire/habitrpg/issues/634
|
2013-01-29 05:02:30 +00:00
|
|
|
|
|
|
|
|
#set cron immediately
|
2013-02-05 03:35:07 +00:00
|
|
|
lastCron = user.get('lastCron')
|
|
|
|
|
user.set('lastCron', +new Date) if (!lastCron? or lastCron == 'new')
|
2013-01-21 17:06:12 +00:00
|
|
|
|
2013-04-04 20:44:54 +00:00
|
|
|
require('./scoring').cron(model)
|
2013-01-21 19:36:26 +00:00
|
|
|
|
2013-04-04 20:44:54 +00:00
|
|
|
require('./character').app(exports, model)
|
|
|
|
|
require('./tasks').app(exports, model)
|
|
|
|
|
require('./items').app(exports, model)
|
|
|
|
|
require('./party').app(exports, model)
|
|
|
|
|
require('./profile').app(exports, model)
|
|
|
|
|
require('./pets').app(exports, model)
|
2013-01-15 19:35:10 +00:00
|
|
|
require('../server/private').app(exports, model)
|
2013-04-08 15:24:45 +00:00
|
|
|
require('./debug').app(exports, model) if model.flags.nodeEnv != 'production'
|
2013-04-04 20:44:54 +00:00
|
|
|
require('./browser').app(exports, model, app)
|
2013-04-16 17:46:44 +00:00
|
|
|
require('./unlock').app(exports, model)
|
2013-02-06 07:42:48 +00:00
|
|
|
|