2013-09-19 19:51:51 +00:00
|
|
|
// User.js
|
|
|
|
|
// =======
|
|
|
|
|
// Defines the user data model (schema) for use via the API.
|
|
|
|
|
|
|
|
|
|
// Dependencies
|
|
|
|
|
// ------------
|
2013-08-31 21:47:03 +00:00
|
|
|
var mongoose = require("mongoose");
|
|
|
|
|
var Schema = mongoose.Schema;
|
|
|
|
|
var helpers = require('habitrpg-shared/script/helpers');
|
|
|
|
|
var _ = require('lodash');
|
2013-08-29 05:45:19 +00:00
|
|
|
|
2013-09-19 19:51:51 +00:00
|
|
|
// User Schema
|
|
|
|
|
// -----------
|
|
|
|
|
|
2013-08-31 21:47:03 +00:00
|
|
|
var UserSchema = new Schema({
|
2013-09-19 19:51:51 +00:00
|
|
|
// ### UUID and API Token
|
2013-08-29 05:45:19 +00:00
|
|
|
_id: {
|
|
|
|
|
type: String,
|
|
|
|
|
'default': helpers.uuid
|
|
|
|
|
},
|
|
|
|
|
apiToken: {
|
|
|
|
|
type: String,
|
|
|
|
|
'default': helpers.uuid
|
|
|
|
|
},
|
|
|
|
|
|
2013-09-19 20:30:22 +00:00
|
|
|
// ### Mongoose Update Object
|
2013-09-19 19:51:51 +00:00
|
|
|
// We want to know *every* time an object updates. Mongoose uses __v to designate when an object contains arrays which
|
2013-09-09 02:01:20 +00:00
|
|
|
// have been updated (http://goo.gl/gQLz41), but we want *every* update
|
2013-08-29 05:45:19 +00:00
|
|
|
_v: {
|
|
|
|
|
type: Number,
|
|
|
|
|
'default': 0
|
|
|
|
|
},
|
|
|
|
|
achievements: {
|
|
|
|
|
originalUser: Boolean,
|
|
|
|
|
helpedHabit: Boolean,
|
|
|
|
|
ultimateGear: Boolean,
|
|
|
|
|
beastMaster: Boolean,
|
2013-10-27 09:18:23 +00:00
|
|
|
streak: {type: Number, 'default': 0}
|
2013-08-29 05:45:19 +00:00
|
|
|
},
|
|
|
|
|
auth: {
|
|
|
|
|
facebook: Schema.Types.Mixed,
|
|
|
|
|
local: {
|
|
|
|
|
email: String,
|
|
|
|
|
hashed_password: String,
|
|
|
|
|
salt: String,
|
|
|
|
|
username: String
|
|
|
|
|
},
|
|
|
|
|
timestamps: {
|
|
|
|
|
created: {
|
|
|
|
|
type: Date,
|
|
|
|
|
'default': Date.now
|
|
|
|
|
},
|
|
|
|
|
loggedin: Date
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2013-09-13 19:10:47 +00:00
|
|
|
backer: {
|
|
|
|
|
tier: Number,
|
|
|
|
|
admin: Boolean,
|
|
|
|
|
npc: String,
|
|
|
|
|
contributor: String,
|
|
|
|
|
tokensApplied: Boolean
|
|
|
|
|
},
|
2013-08-29 05:45:19 +00:00
|
|
|
|
|
|
|
|
balance: Number,
|
|
|
|
|
habitIds: Array,
|
|
|
|
|
dailyIds: Array,
|
|
|
|
|
todoIds: Array,
|
|
|
|
|
rewardIds: Array,
|
2013-09-09 03:28:34 +00:00
|
|
|
filters: {type: Schema.Types.Mixed, 'default': {}},
|
2013-08-29 05:45:19 +00:00
|
|
|
|
2013-10-22 19:19:43 +00:00
|
|
|
purchased: {
|
|
|
|
|
ads: {type: Boolean, 'default': false},
|
2013-10-22 20:37:55 +00:00
|
|
|
skin: {type: Schema.Types.Mixed, 'default': {}}, // eg, {skeleton: true, pumpkin: true, eb052b: true}
|
|
|
|
|
hair: {type: Schema.Types.Mixed, 'default': {}}
|
2013-10-22 19:19:43 +00:00
|
|
|
},
|
|
|
|
|
|
2013-08-29 05:45:19 +00:00
|
|
|
flags: {
|
2013-09-17 18:57:40 +00:00
|
|
|
customizationsNotification: {type: Boolean, 'default': false},
|
2013-09-10 22:06:16 +00:00
|
|
|
showTour: {type: Boolean, 'default': true},
|
|
|
|
|
dropsEnabled: {type: Boolean, 'default': false},
|
|
|
|
|
itemsEnabled: {type: Boolean, 'default': false},
|
2013-10-23 01:50:50 +00:00
|
|
|
newStuff: {type: Boolean, 'default': false},
|
2013-09-10 22:06:16 +00:00
|
|
|
rewrite: {type: Boolean, 'default': true},
|
|
|
|
|
partyEnabled: Boolean, // FIXME do we need this?
|
|
|
|
|
petsEnabled: {type: Boolean, 'default': false},
|
|
|
|
|
rest: {type: Boolean, 'default': false} // fixme - change to preferences.resting once we're off derby
|
2013-08-29 05:45:19 +00:00
|
|
|
},
|
|
|
|
|
history: {
|
|
|
|
|
exp: [
|
|
|
|
|
{
|
|
|
|
|
date: Date,
|
|
|
|
|
value: Number
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
todos: [
|
|
|
|
|
{
|
|
|
|
|
data: Date,
|
|
|
|
|
value: Number
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
/* FIXME remove?*/
|
|
|
|
|
|
|
|
|
|
invitations: {
|
2013-09-11 21:16:23 +00:00
|
|
|
guilds: {type: Array, 'default': []},
|
2013-08-29 05:45:19 +00:00
|
|
|
party: Schema.Types.Mixed
|
|
|
|
|
},
|
|
|
|
|
items: {
|
|
|
|
|
armor: Number,
|
|
|
|
|
weapon: Number,
|
|
|
|
|
head: Number,
|
|
|
|
|
shield: Number,
|
2013-09-11 20:51:18 +00:00
|
|
|
|
2013-08-29 05:45:19 +00:00
|
|
|
/*FIXME - tidy this up, not the best way to store current pet*/
|
|
|
|
|
|
|
|
|
|
currentPet: {
|
|
|
|
|
/*Cactus*/
|
|
|
|
|
|
|
|
|
|
text: String,
|
|
|
|
|
/*Cactus*/
|
|
|
|
|
|
|
|
|
|
name: String,
|
|
|
|
|
/*3*/
|
|
|
|
|
|
|
|
|
|
value: Number,
|
|
|
|
|
/*"Find a hatching potion to pour on this egg, and one day it will hatch into a loyal pet.",*/
|
|
|
|
|
|
|
|
|
|
notes: String,
|
|
|
|
|
/*Skeleton*/
|
|
|
|
|
|
|
|
|
|
modifier: String,
|
|
|
|
|
/*Cactus-Skeleton*/
|
|
|
|
|
|
|
|
|
|
str: String
|
|
|
|
|
},
|
2013-09-02 19:54:32 +00:00
|
|
|
|
2013-09-11 20:51:18 +00:00
|
|
|
eggs: [
|
|
|
|
|
{
|
2013-09-19 19:51:51 +00:00
|
|
|
// example: You've found a Wolf Egg! Find a hatching potion to pour on this egg, and one day it will hatch into a loyal pet
|
|
|
|
|
dialog: String,
|
|
|
|
|
// example: Wolf
|
|
|
|
|
name: String,
|
|
|
|
|
// example: Find a hatching potion to pour on this egg, and one day it will hatch into a loyal pet.
|
|
|
|
|
notes: String,
|
|
|
|
|
// example: Wolf
|
|
|
|
|
text: String,
|
|
|
|
|
/* type: String, //Egg // this is forcing mongoose to return object as "[object Object]", but I don't think this is needed anyway? */
|
|
|
|
|
// example: 3
|
|
|
|
|
value: Number
|
2013-09-11 20:51:18 +00:00
|
|
|
}
|
|
|
|
|
],
|
2013-09-19 19:51:51 +00:00
|
|
|
hatchingPotions: Array, // ["Base", "Skeleton",...]
|
2013-08-29 05:45:19 +00:00
|
|
|
lastDrop: {
|
2013-09-11 20:51:18 +00:00
|
|
|
date: {type: Date, 'default': Date.now},
|
|
|
|
|
count: {type: Number, 'default': 0}
|
2013-08-29 05:45:19 +00:00
|
|
|
},
|
2013-09-19 19:51:51 +00:00
|
|
|
// ["BearCub-Base", "Cactus-Base", ...]
|
2013-08-29 05:45:19 +00:00
|
|
|
|
|
|
|
|
pets: Array
|
|
|
|
|
},
|
2013-09-19 19:51:51 +00:00
|
|
|
// FIXME store as Date?
|
2013-08-29 05:45:19 +00:00
|
|
|
|
|
|
|
|
lastCron: {
|
2013-09-08 00:00:39 +00:00
|
|
|
type: Date,
|
2013-09-08 00:24:25 +00:00
|
|
|
'default': Date.now
|
2013-08-29 05:45:19 +00:00
|
|
|
},
|
2013-09-19 19:51:51 +00:00
|
|
|
// FIXME remove?
|
2013-08-29 05:45:19 +00:00
|
|
|
|
|
|
|
|
party: {
|
2013-09-19 19:51:51 +00:00
|
|
|
//party._id // FIXME make these populate docs?
|
2013-09-09 02:01:20 +00:00
|
|
|
current: String, // party._id
|
|
|
|
|
invitation: String, // party._id
|
2013-08-29 05:45:19 +00:00
|
|
|
lastMessageSeen: String,
|
|
|
|
|
leader: Boolean
|
|
|
|
|
},
|
|
|
|
|
preferences: {
|
|
|
|
|
armorSet: String,
|
2013-09-13 19:10:47 +00:00
|
|
|
dayStart: {type:Number, 'default': 0},
|
|
|
|
|
gender: {type:String, 'default': 'm'},
|
|
|
|
|
hair: {type:String, 'default':'blond'},
|
|
|
|
|
hideHeader: {type:Boolean, 'default':false},
|
|
|
|
|
showHelm: {type:Boolean, 'default':true},
|
|
|
|
|
skin: {type:String, 'default':'white'},
|
2013-08-29 05:45:19 +00:00
|
|
|
timezoneOffset: Number
|
|
|
|
|
},
|
|
|
|
|
profile: {
|
|
|
|
|
blurb: String,
|
|
|
|
|
imageUrl: String,
|
|
|
|
|
name: String,
|
2013-09-19 19:51:51 +00:00
|
|
|
websites: Array // styled like --> ["http://ocdevel.com" ]
|
2013-08-29 05:45:19 +00:00
|
|
|
},
|
|
|
|
|
stats: {
|
|
|
|
|
hp: Number,
|
|
|
|
|
exp: Number,
|
|
|
|
|
gp: Number,
|
|
|
|
|
lvl: Number
|
|
|
|
|
},
|
|
|
|
|
tags: [
|
|
|
|
|
{
|
2013-09-19 19:51:51 +00:00
|
|
|
// FIXME use refs?
|
2013-08-29 05:45:19 +00:00
|
|
|
id: String,
|
|
|
|
|
name: String
|
|
|
|
|
}
|
|
|
|
|
],
|
2013-09-19 19:51:51 +00:00
|
|
|
|
|
|
|
|
// ### Tasks Definition
|
|
|
|
|
// We can't define `tasks` until we move off Derby, since Derby requires dictionary of objects. When we're off, migrate
|
|
|
|
|
// to array of subdocs
|
2013-08-29 05:45:19 +00:00
|
|
|
|
|
|
|
|
tasks: Schema.Types.Mixed
|
|
|
|
|
/*
|
|
|
|
|
# history: {date, value}
|
|
|
|
|
# id
|
|
|
|
|
# notes
|
|
|
|
|
# tags { "4ddf03d9-54bd-41a3-b011-ca1f1d2e9371" : true },
|
|
|
|
|
# text
|
|
|
|
|
# type
|
|
|
|
|
# up
|
|
|
|
|
# down
|
|
|
|
|
# value
|
|
|
|
|
# completed
|
|
|
|
|
# priority: '!!'
|
|
|
|
|
# repeat {m: true, t: true}
|
|
|
|
|
# streak
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
}, {
|
|
|
|
|
strict: true
|
|
|
|
|
});
|
|
|
|
|
|
2013-09-19 19:51:51 +00:00
|
|
|
// Legacy Derby Function?
|
|
|
|
|
// ----------------------
|
|
|
|
|
// Derby requires a strange storage format for somethign called "refLists". Here we hook into loading the data, so we
|
|
|
|
|
// can provide a more "expected" storage format for our various helper methods. Since the attributes are passed by reference,
|
|
|
|
|
// the underlying data will be modified too - so when we save back to the database, it saves it in the way Derby likes.
|
|
|
|
|
// This will go away after the rewrite is complete
|
|
|
|
|
|
2013-09-09 00:56:28 +00:00
|
|
|
function transformTaskLists(doc) {
|
2013-09-02 22:40:20 +00:00
|
|
|
_.each(['habit', 'daily', 'todo', 'reward'], function(type) {
|
|
|
|
|
// we use _.transform instead of a simple _.where in order to maintain sort-order
|
2013-09-09 00:56:28 +00:00
|
|
|
doc[type + "s"] = _.reduce(doc[type + "Ids"], function(m, tid) {
|
2013-09-09 13:12:00 +00:00
|
|
|
if (!doc.tasks[tid]) return m; // FIXME tmp hotfix, people still have null tasks?
|
2013-09-09 03:28:34 +00:00
|
|
|
if (!doc.tasks[tid].tags) doc.tasks[tid].tags = {}; // FIXME remove this when we switch tasks to subdocs and can define tags default in schema
|
2013-09-09 00:56:28 +00:00
|
|
|
m.push(doc.tasks[tid]);
|
|
|
|
|
return m;
|
|
|
|
|
}, []);
|
2013-08-29 05:45:19 +00:00
|
|
|
});
|
2013-09-09 00:56:28 +00:00
|
|
|
}
|
2013-08-29 05:45:19 +00:00
|
|
|
|
2013-09-09 00:56:28 +00:00
|
|
|
UserSchema.post('init', function(doc) {
|
|
|
|
|
transformTaskLists(doc);
|
|
|
|
|
});
|
2013-08-29 05:45:19 +00:00
|
|
|
|
|
|
|
|
UserSchema.methods.toJSON = function() {
|
2013-08-31 21:47:03 +00:00
|
|
|
var doc = this.toObject();
|
2013-08-29 05:45:19 +00:00
|
|
|
doc.id = doc._id;
|
2013-09-09 00:56:28 +00:00
|
|
|
transformTaskLists(doc); // we need to also transform for our server-side routes
|
|
|
|
|
|
2013-09-19 19:52:47 +00:00
|
|
|
// FIXME? Is this a reference to `doc.filters` or just disabled code? Remove?
|
|
|
|
|
/*
|
2013-09-09 02:01:20 +00:00
|
|
|
// Remove some unecessary data as far as client consumers are concerned
|
2013-09-09 15:43:24 +00:00
|
|
|
//_.each(['habit', 'daily', 'todo', 'reward'], function(type) {
|
|
|
|
|
// delete doc["#{type}Ids"]
|
|
|
|
|
//});
|
|
|
|
|
//delete doc.tasks
|
2013-09-19 19:52:47 +00:00
|
|
|
*/
|
2013-09-09 04:13:11 +00:00
|
|
|
doc.filters = {};
|
2013-09-17 16:16:16 +00:00
|
|
|
doc._tmp = this._tmp; // be sure to send down drop notifs
|
2013-09-09 00:56:28 +00:00
|
|
|
|
2013-08-29 05:45:19 +00:00
|
|
|
return doc;
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-19 19:51:51 +00:00
|
|
|
// FIXME - since we're using special @post('init') above, we need to flag when the original path was modified.
|
|
|
|
|
// Custom setter/getter virtuals?
|
|
|
|
|
|
2013-08-29 05:45:19 +00:00
|
|
|
UserSchema.pre('save', function(next) {
|
|
|
|
|
this.markModified('tasks');
|
2013-08-31 21:47:03 +00:00
|
|
|
//our own version incrementer
|
2013-08-29 05:45:19 +00:00
|
|
|
this._v++;
|
2013-08-31 21:47:03 +00:00
|
|
|
next();
|
2013-08-29 05:45:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports.schema = UserSchema;
|
|
|
|
|
module.exports.model = mongoose.model("User", UserSchema);
|