mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
many bug fixes regarding schema updates
This commit is contained in:
@@ -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)) {
|
||||
|
||||
50
lib/app/schema.js
Normal file
50
lib/app/schema.js
Normal file
@@ -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;
|
||||
};
|
||||
@@ -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;
|
||||
|
||||
@@ -5,34 +5,6 @@ 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,17 +295,21 @@ 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 ==========
|
||||
|
||||
exports.shortcuts = (e) ->
|
||||
|
||||
36
src/app/schema.coffee
Normal file
36
src/app/schema.coffee
Normal file
@@ -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
|
||||
@@ -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'
|
||||
|
||||
@@ -81,6 +81,7 @@
|
||||
<i class="icon-warning-sign"></i> <b>Debugging Options</b><br/><br/>
|
||||
<a x-bind=click:poormanscron class="btn">Cron</a>
|
||||
<a x-bind=click:endOfDayTally class="btn">Tally</a>
|
||||
<a x-bind=click:updateSchema class="btn">Update Schema</a>
|
||||
</div>
|
||||
{/}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user