From dae3ca635fe833d3fc08c28a93d0234d65524ee7 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Thu, 30 May 2013 14:54:00 -0400 Subject: [PATCH] duplicate pets being pushed into stable, fixes #1086 --- src/app/index.coffee | 38 +------------------------------------- src/app/misc.coffee | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/app/index.coffee b/src/app/index.coffee index ad63683073..ec5772a4dc 100644 --- a/src/app/index.coffee +++ b/src/app/index.coffee @@ -22,42 +22,6 @@ misc.viewHelpers view _ = require('lodash') algos = require 'habitrpg-shared/script/algos' -helpers = require 'habitrpg-shared/script/helpers' - -### - 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 - Gotta love refLists! see https://github.com/lefnire/habitrpg/issues/803 & https://github.com/lefnire/habitrpg/issues/6343 -### -cleanupCorruptTasks = (model) -> - user = model.at('_user') - tasks = user.get('tasks') - - ## Remove corrupted tasks - _.each tasks, (task, key) -> - unless task?.id? and task?.type? - user.del("tasks.#{key}") - delete tasks[key] - true - - misc.batchTxn model, (uObj, paths, batch) -> - ## Task List Cleanup - ['habit','daily','todo','reward'].forEach (type) -> - - # 1. remove duplicates - # 2. restore missing zombie tasks back into list - idList = uObj["#{type}Ids"] - taskIds = _.pluck( _.where(tasks, {type:type}), 'id') - union = _.union idList, taskIds - - # 2. remove empty (grey) tasks - preened = _.filter union, (id) -> id and _.contains(taskIds, id) - - # There were indeed issues found, set the new list - if !_.isEqual(idList, preened) - batch.set("#{type}Ids", preened) - console.error uObj.id + "'s #{type}s were corrupt." - true ### @@ -103,7 +67,7 @@ get '/', (page, model, params, next) -> # removed force-ssl (handled in nginx), see git for code setupSubscriptions page, model, params, next, -> - cleanupCorruptTasks(model) # https://github.com/lefnire/habitrpg/issues/634 + misc.fixCorruptUser(model) # https://github.com/lefnire/habitrpg/issues/634 require('./items').server(model) #refLists _.each ['habit', 'daily', 'todo', 'reward'], (type) -> diff --git a/src/app/misc.coffee b/src/app/misc.coffee index 2e427a951d..e2b0743aa0 100644 --- a/src/app/misc.coffee +++ b/src/app/misc.coffee @@ -60,6 +60,48 @@ module.exports.hydrate = hydrate = (spec) -> hydrated else spec + +### + 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 + Gotta love refLists! see https://github.com/lefnire/habitrpg/issues/803 & https://github.com/lefnire/habitrpg/issues/6343 +### +module.exports.fixCorruptUser = (model) -> + user = model.at('_user') + tasks = user.get('tasks') + + ## Remove corrupted tasks + _.each tasks, (task, key) -> + unless task?.id? and task?.type? + user.del("tasks.#{key}") + delete tasks[key] + true + + batchTxn model, (uObj, paths, batch) -> + + ## fix https://github.com/lefnire/habitrpg/issues/1086 + uniqPets = _.uniq(uObj.items.pets) + batch.set('items.pets', uniqPets) if !_.isEqual(uniqPets, uObj.items.pets) + console.log {uniqPets, count:_.size(uniqPets)} + + ## Task List Cleanup + ['habit','daily','todo','reward'].forEach (type) -> + + # 1. remove duplicates + # 2. restore missing zombie tasks back into list + idList = uObj["#{type}Ids"] + taskIds = _.pluck( _.where(tasks, {type:type}), 'id') + union = _.union idList, taskIds + + # 2. remove empty (grey) tasks + preened = _.filter union, (id) -> id and _.contains(taskIds, id) + + # There were indeed issues found, set the new list + if !_.isEqual(idList, preened) + batch.set("#{type}Ids", preened) + console.error uObj.id + "'s #{type}s were corrupt." + true + module.exports.viewHelpers = (view) -> #misc