From 3895d0610a336ed918ce527548675a6ed46ff04a Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Sat, 28 Jul 2012 16:26:06 -0400 Subject: [PATCH] many bug fixes regarding schema updates --- lib/app/index.js | 66 +++++++++--------------------------------- lib/app/schema.js | 50 ++++++++++++++++++++++++++++++++ lib/app/scoring.js | 4 +-- src/app/index.coffee | 46 ++++++----------------------- src/app/schema.coffee | 36 +++++++++++++++++++++++ src/app/scoring.coffee | 4 +-- views/app/index.html | 1 + 7 files changed, 114 insertions(+), 93 deletions(-) create mode 100644 lib/app/schema.js create mode 100644 src/app/schema.coffee diff --git a/lib/app/index.js b/lib/app/index.js index 3b419d274d..23da907f3a 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -1,5 +1,5 @@ // Generated by CoffeeScript 1.3.3 -var Scoring, content, derby, get, getHabits, ready, schemaUpdates, userSchema, view, _ref; +var Scoring, content, derby, get, getHabits, ready, view, _ref; derby = require('derby'); @@ -13,52 +13,6 @@ content = require('./content'); Scoring = require('./scoring'); -userSchema = { - stats: { - money: 0, - exp: 0, - lvl: 1, - hp: 50 - }, - items: { - itemsEnabled: false, - armor: 0, - weapon: 0 - }, - tasks: {}, - habitIds: [], - dailyIds: [], - todoIds: [], - completedIds: [], - rewardIds: [] -}; - -schemaUpdates = function(model) { - var completedIds, index, key, todo, todoIds, user, val, _i, _len, _ref1, _results; - user = model.at('_user'); - for (key in userSchema) { - val = userSchema[key]; - user.setNull(key, val); - } - completedIds = user.get('completedIds'); - todoIds = user.get('todoIds'); - if (completedIds.length === 0) { - _ref1 = model.get('_todoList'); - _results = []; - for (_i = 0, _len = _ref1.length; _i < _len; _i++) { - todo = _ref1[_i]; - if (todo.completed) { - index = todoIds.indexOf(todo.id); - todoIds.splice(index, 1); - _results.push(completedIds.push(todo.id)); - } else { - _results.push(void 0); - } - } - return _results; - } -}; - view.fn('taskClasses', function(type, completed, value) { var classes; classes = type; @@ -129,7 +83,7 @@ get('/:uidParam?', function(page, model, _arg) { userId = model.get('_userId'); user = users.get(userId); if (user == null) { - newUser = userSchema; + newUser = require('./schema').userSchema; _ref1 = content.defaultTasks; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { task = _ref1[_i]; @@ -176,13 +130,12 @@ getHabits = function(page, model, userId) { model.refList("_todoList", "_user.tasks", "_user.todoIds"); model.refList("_completedList", "_user.tasks", "_user.completedIds"); model.refList("_rewardList", "_user.tasks", "_user.rewardIds"); - schemaUpdates(model); return page.render(); }); }; ready(function(model) { - var endOfDayTally, poormanscron, setupSortable, step, tour, type, _i, _j, _len, _len1, _ref1, _ref2; + var poormanscron, setupSortable, step, tour, type, _i, _j, _len, _len1, _ref1, _ref2; model.set('_purl', window.location.origin + '/' + model.get('_userId')); $('[rel=popover]').popover(); model.on('set', '*', function() { @@ -301,6 +254,12 @@ ready(function(model) { model.del('_user.tasks.' + task.get('id')); return task.remove(); }; + exports.clearCompleted = function(e, el) { + return _.each(model.get('_completedList'), function(task) { + model.del('_user.tasks.' + task.id); + return model.set('_user.completedIds', []); + }); + }; exports.toggleTaskEdit = function(e, el) { var hideId, toggleId; hideId = $(el).attr('data-hide-id'); @@ -412,7 +371,7 @@ ready(function(model) { daysPassed: daysPassed, n: n }, "[debug] Cron (" + today + ", " + n + ")"); - _results.push(endOfDayTally()); + _results.push(Scoring.tally(model)); } return _results; } @@ -421,9 +380,12 @@ ready(function(model) { setInterval((function() { return poormanscron(); }), 3600000); - exports.endOfDayTally = endOfDayTally = function(e, el) { + exports.endOfDayTally = function(e, el) { return Scoring.tally(model); }; + exports.updateSchema = function(e, el) { + return require('./schema').updateSchema(model); + }; exports.shortcuts = function(e) { var code, command; if (!(e.metaKey || e.ctrlKey)) { diff --git a/lib/app/schema.js b/lib/app/schema.js new file mode 100644 index 0000000000..727ae12a78 --- /dev/null +++ b/lib/app/schema.js @@ -0,0 +1,50 @@ +// Generated by CoffeeScript 1.3.3 +var userSchema; + +module.exports.userSchema = userSchema = { + stats: { + money: 0, + exp: 0, + lvl: 1, + hp: 50 + }, + items: { + itemsEnabled: false, + armor: 0, + weapon: 0 + }, + tasks: {}, + habitIds: [], + dailyIds: [], + todoIds: [], + completedIds: [], + rewardIds: [] +}; + +module.exports.updateSchema = function(model) { + var completedIds, id, index, task, todoIds, uid, user, userObj, _ref, _ref1, _results; + _ref = model.get('users'); + _results = []; + for (uid in _ref) { + userObj = _ref[uid]; + user = model.at("users." + uid); + user.set('completedIds', []); + completedIds = user.get('completedIds'); + todoIds = user.get('todoIds'); + _ref1 = user.get('tasks'); + for (id in _ref1) { + task = _ref1[id]; + if (task.type === 'todo' && task.completed === true) { + if ((index = todoIds.indexOf(id)) !== -1) { + todoIds.splice(index, 1); + } + if ((index = completedIds.indexOf(id)) === -1) { + completedIds.push(id); + } + } + } + user.set('todoIds', todoIds); + _results.push(user.set('completedIds', completedIds)); + } + return _results; +}; diff --git a/lib/app/scoring.js b/lib/app/scoring.js index 9451d75616..a188484a48 100644 --- a/lib/app/scoring.js +++ b/lib/app/scoring.js @@ -55,7 +55,7 @@ updateStats = function(user, stats) { } }; -exports.score = score = function(spec) { +module.exports.score = score = function(spec) { var adjustvalue, cron, delta, direction, exp, hp, lvl, money, sign, task, type, user, value, _ref, _ref1; if (spec == null) { spec = { @@ -108,7 +108,7 @@ exports.score = score = function(spec) { return delta; }; -exports.tally = tally = function(model) { +module.exports.tally = tally = function(model) { var absVal, completed, expTally, key, lvl, task, todoTally, type, user, value, _ref; user = model.at('_user'); todoTally = 0; diff --git a/src/app/index.coffee b/src/app/index.coffee index 263d457473..c5180a8362 100644 --- a/src/app/index.coffee +++ b/src/app/index.coffee @@ -4,35 +4,7 @@ derby.use require('derby-ui-boot') derby.use(require('../../ui')) content = require('./content') Scoring = require('./scoring') - -# ========== MODEL SCHEMA ========== - -userSchema = - stats: { money: 0, exp: 0, lvl: 1, hp: 50 } - items: { itemsEnabled: false, armor: 0, weapon: 0 } - tasks: {} - habitIds: [] - dailyIds: [] - todoIds: [] - completedIds: [] - rewardIds: [] -# Temporary solution to running updates against the schema when the code changes -schemaUpdates = (model) -> - user = model.at('_user') - for key, val of userSchema - user.setNull key, val - - # _todoList <-> _completdList transfering code update - completedIds = user.get('completedIds') - todoIds = user.get('todoIds') - if completedIds.length==0 - for todo in model.get('_todoList') - if todo.completed - index = todoIds.indexOf(todo.id) - todoIds.splice(index, 1) - completedIds.push todo.id - # ========== VIEW HELPERS ========== view.fn 'taskClasses', (type, completed, value) -> @@ -87,7 +59,7 @@ get '/:uidParam?', (page, model, {uidParam}) -> # Else, select a new userId and initialize user unless user? - newUser = userSchema + newUser = require('./schema').userSchema for task in content.defaultTasks guid = task.id = require('derby/node_modules/racer').uuid() newUser.tasks[guid] = task @@ -133,10 +105,6 @@ getHabits = (page, model, userId) -> model.refList "_completedList", "_user.tasks", "_user.completedIds" model.refList "_rewardList", "_user.tasks", "_user.rewardIds" - # Run any database updates in case the code has been updated. Strange placement, - # I know - FIXME - schemaUpdates(model) - page.render() # ========== CONTROLLER FUNCTIONS ========== @@ -327,16 +295,20 @@ ready (model) -> model.set('_user.lastCron', today) # reset cron for n in [1..daysPassed] console.log {today: today, lastCron: lastCron, daysPassed: daysPassed, n:n}, "[debug] Cron (#{today}, #{n})" - endOfDayTally() + Scoring.tally(model) poormanscron() # Run once on refresh setInterval (-> # Then run once every hour poormanscron() ), 3600000 - # Note: Set 12am daily cron for this - #TODO: remove from exports when cron implemented - exports.endOfDayTally = endOfDayTally = (e, el) -> + # ========== DEBUGGING ========== + + exports.endOfDayTally = (e, el) -> Scoring.tally(model) + + # Temporary solution to running updates against the schema when the code changes + exports.updateSchema = (e, el) -> + require('./schema').updateSchema(model) # ========== SHORTCUTS ========== diff --git a/src/app/schema.coffee b/src/app/schema.coffee new file mode 100644 index 0000000000..807d3cabdf --- /dev/null +++ b/src/app/schema.coffee @@ -0,0 +1,36 @@ + +module.exports.userSchema = userSchema = { + stats: { money: 0, exp: 0, lvl: 1, hp: 50 } + items: { itemsEnabled: false, armor: 0, weapon: 0 } + tasks: {} + habitIds: [] + dailyIds: [] + todoIds: [] + completedIds: [] + rewardIds: [] +} + +module.exports.updateSchema = (model) -> + for uid,userObj of model.get('users') + user = model.at("users.#{uid}") + + user.set 'completedIds', [] + + # schema = jQuery.extend(true, {}, userSchema) + # # add to schema if user doesn't have these elements + # _.each schema, (val,key) -> + # user.set(key,val) unless user.get(key) + + # _todoList <-> _completedList transfering code update + completedIds = user.get('completedIds') + todoIds = user.get('todoIds') + for id,task of user.get('tasks') + if task.type=='todo' and task.completed==true + # if in todoList but shouldn't be, remove it + if (index = todoIds.indexOf(id)) != -1 + todoIds.splice(index, 1) + # if not in completedList but should be, add it + if (index = completedIds.indexOf(id)) == -1 + completedIds.push id + user.set 'todoIds', todoIds + user.set 'completedIds', completedIds \ No newline at end of file diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index f818488e3e..7b543ac486 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -48,7 +48,7 @@ updateStats = (user, stats) -> money = 0.0 if (!money? or money<0) user.set 'stats.money', stats.money -exports.score = score = (spec = {user:null, task:null, direction:null, cron:null}) -> +module.exports.score = score = (spec = {user:null, task:null, direction:null, cron:null}) -> # console.log spec, "scoring.coffee: score( ->spec<- )" [user, task, direction, cron] = [spec.user, spec.task, spec.direction, spec.cron] @@ -98,7 +98,7 @@ exports.score = score = (spec = {user:null, task:null, direction:null, cron:null # At end of day, add value to all incomplete Daily & Todo tasks (further incentive) # For incomplete Dailys, deduct experience -exports.tally = tally = (model) -> +module.exports.tally = tally = (model) -> # users = model.at('users') #TODO this isn't working, iterate over all users # for user in users user = model.at '_user' diff --git a/views/app/index.html b/views/app/index.html index dfa25c3a38..718e110f66 100644 --- a/views/app/index.html +++ b/views/app/index.html @@ -81,6 +81,7 @@ Debugging Options

Cron Tally + Update Schema {/}