mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-22 03:34:14 +00:00
Add a schema and schema-updating mechanism. not ideal the way it is,
but bette than nothing
This commit is contained in:
parent
05121a2d18
commit
8c50969899
2 changed files with 82 additions and 26 deletions
|
|
@ -1,5 +1,5 @@
|
|||
// Generated by CoffeeScript 1.3.3
|
||||
var Scoring, content, derby, get, getHabits, ready, view, _ref;
|
||||
var Scoring, content, derby, get, getHabits, ready, schemaUpdates, userSchema, view, _ref;
|
||||
|
||||
derby = require('derby');
|
||||
|
||||
|
|
@ -13,6 +13,52 @@ 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;
|
||||
|
|
@ -83,25 +129,7 @@ get('/:uidParam?', function(page, model, _arg) {
|
|||
userId = model.get('_userId');
|
||||
user = users.get(userId);
|
||||
if (user == null) {
|
||||
newUser = {
|
||||
stats: {
|
||||
money: 0,
|
||||
exp: 0,
|
||||
lvl: 1,
|
||||
hp: 50
|
||||
},
|
||||
items: {
|
||||
itemsEnabled: false,
|
||||
armor: 0,
|
||||
weapon: 0
|
||||
},
|
||||
tasks: {},
|
||||
habitIds: [],
|
||||
dailyIds: [],
|
||||
todoIds: [],
|
||||
completedIds: [],
|
||||
rewardIds: []
|
||||
};
|
||||
newUser = userSchema;
|
||||
_ref1 = content.defaultTasks;
|
||||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
||||
task = _ref1[_i];
|
||||
|
|
@ -148,6 +176,7 @@ 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();
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -5,6 +5,34 @@ 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) ->
|
||||
|
|
@ -59,11 +87,7 @@ get '/:uidParam?', (page, model, {uidParam}) ->
|
|||
|
||||
# Else, select a new userId and initialize user
|
||||
unless user?
|
||||
newUser = {
|
||||
stats: { money: 0, exp: 0, lvl: 1, hp: 50 }
|
||||
items: { itemsEnabled: false, armor: 0, weapon: 0 }
|
||||
tasks: {}, habitIds: [], dailyIds: [], todoIds: [], completedIds: [], rewardIds: []
|
||||
}
|
||||
newUser = userSchema
|
||||
for task in content.defaultTasks
|
||||
guid = task.id = require('derby/node_modules/racer').uuid()
|
||||
newUser.tasks[guid] = task
|
||||
|
|
@ -90,7 +114,6 @@ getHabits = (page, model, userId) ->
|
|||
model.subscribe "users.#{userId}", (err, user) ->
|
||||
console.log {userId:userId, err:err}, "app/index.coffee: model.subscribe"
|
||||
# => {userId: 26c48325-2fea-4e2e-a60f-a5fa28d7b410, err: Unauthorized: No access control declared for path users.26c48325-2fea-4e2e-a60f-a5fa28d7b410 }
|
||||
|
||||
model.ref '_user', user
|
||||
|
||||
# Store
|
||||
|
|
@ -109,6 +132,10 @@ getHabits = (page, model, userId) ->
|
|||
model.refList "_todoList", "_user.tasks", "_user.todoIds"
|
||||
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()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue