APIv2: instead of passing req to callback, passing response object so API can return the object when wrapped on the server

This commit is contained in:
Tyler Renelle 2013-12-28 11:31:21 -07:00
parent bafa3ffd21
commit dfd2a17ef9
3 changed files with 147 additions and 129 deletions

View file

@ -10619,7 +10619,7 @@ var global=self;/**
},{"lodash":3,"moment":4}],6:[function(require,module,exports){ },{"lodash":3,"moment":4}],6:[function(require,module,exports){
var process=require("__browserify_process");(function() { var process=require("__browserify_process");(function() {
var api, content, dayMapping, moment, preenHistory, sanitizeOptions, _, var $w, api, content, dayMapping, moment, preenHistory, sanitizeOptions, _,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
moment = require('moment'); moment = require('moment');
@ -10630,6 +10630,10 @@ var process=require("__browserify_process");(function() {
api = module.exports = {}; api = module.exports = {};
$w = function(s) {
return s.split(' ');
};
/* /*
------------------------------------------------------ ------------------------------------------------------
Time / Day Time / Day
@ -10880,6 +10884,9 @@ var process=require("__browserify_process");(function() {
api.taskDefaults = function(task) { api.taskDefaults = function(task) {
var defaults, _ref, _ref1, _ref2; var defaults, _ref, _ref1, _ref2;
if (task == null) {
task = {};
}
if (!(task.type && ((_ref = task.type) === 'habit' || _ref === 'daily' || _ref === 'todo' || _ref === 'reward'))) { if (!(task.type && ((_ref = task.type) === 'habit' || _ref === 'daily' || _ref === 'todo' || _ref === 'reward'))) {
task.type = 'habit'; task.type = 'habit';
} }
@ -11168,11 +11175,11 @@ var process=require("__browserify_process");(function() {
user.fns.dotSet(k, v); user.fns.dotSet(k, v);
return true; return true;
}); });
return typeof cb === "function" ? cb(null, req) : void 0; return typeof cb === "function" ? cb(null, user) : void 0;
}, },
sleep: function(req, cb) { sleep: function(req, cb) {
user.preferences.sleep = !user.preferences.sleep; user.preferences.sleep = !user.preferences.sleep;
return cb(null, req); return typeof cb === "function" ? cb(null, {}) : void 0;
}, },
revive: function(req, cb) { revive: function(req, cb) {
var item, lostItem, lostStat; var item, lostItem, lostStat;
@ -11214,7 +11221,7 @@ var process=require("__browserify_process");(function() {
return typeof cb === "function" ? cb((item ? { return typeof cb === "function" ? cb((item ? {
code: 200, code: 200,
message: "Your " + item.text + " broke." message: "Your " + item.text + " broke."
} : null), req) : void 0; } : null), user) : void 0;
}, },
reset: function(req, cb) { reset: function(req, cb) {
var gear; var gear;
@ -11240,77 +11247,78 @@ var process=require("__browserify_process");(function() {
user.markModified('items.gear.owned'); user.markModified('items.gear.owned');
} }
user.preferences.costume = false; user.preferences.costume = false;
return cb(null, req); return typeof cb === "function" ? cb(null, user) : void 0;
}, },
reroll: function(req, cb) { reroll: function(req, cb) {
if (user.balance < 1) { if (user.balance < 1) {
return cb({ return typeof cb === "function" ? cb({
code: 401, code: 401,
message: "Not enough gems." message: "Not enough gems."
}, req); }, req) : void 0;
} }
user.balance--; user.balance--;
_.each(user.tasks, function(task) { _.each(user.tasks, function(task) {
return task.value = 0; return task.value = 0;
}); });
user.stats.hp = 50; user.stats.hp = 50;
return cb(null, req); return typeof cb === "function" ? cb(null, user) : void 0;
}, },
clearCompleted: function(req, cb) { clearCompleted: function(req, cb) {
user.todos = _.where(user.todos, { user.todos = _.where(user.todos, {
completed: false completed: false
}); });
return cb(null, req); return typeof cb === "function" ? cb(null, user.todos) : void 0;
}, },
sortTask: function(req, cb) { sortTask: function(req, cb) {
var from, id, task, to, _ref; var from, id, task, tasks, to, _ref;
id = req.params.id; id = req.params.id;
_ref = req.query, to = _ref.to, from = _ref.from; _ref = req.query, to = _ref.to, from = _ref.from;
task = user.tasks[id]; task = user.tasks[id];
if (!task) { if (!task) {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: "Task not found." message: "Task not found."
}); }) : void 0;
} }
if (!((to != null) && (from != null))) { if (!((to != null) && (from != null))) {
return cb('?to=__&from=__ are required'); return typeof cb === "function" ? cb('?to=__&from=__ are required') : void 0;
} }
user["" + task.type + "s"].splice(to, 0, user["" + task.type + "s"].splice(from, 1)[0]); tasks = user["" + task.type + "s"];
return cb(null, req); tasks.splice(to, 0, tasks.splice(from, 1)[0]);
return typeof cb === "function" ? cb(null, tasks) : void 0;
}, },
updateTask: function(req, cb) { updateTask: function(req, cb) {
var _base, _ref; var task, _ref;
if (!user.tasks[(_ref = req.params) != null ? _ref.id : void 0]) { if (!(task = user.tasks[(_ref = req.params) != null ? _ref.id : void 0])) {
return typeof cb === "function" ? cb("Task not found") : void 0; return typeof cb === "function" ? cb("Task not found") : void 0;
} }
_.merge(user.tasks[req.params.id], req.body); _.merge(task, req.body);
if (typeof (_base = user.tasks[req.params.id]).markModified === "function") { if (typeof task.markModified === "function") {
_base.markModified('tags'); task.markModified('tags');
} }
return typeof cb === "function" ? cb(null, req) : void 0; return typeof cb === "function" ? cb(null, task) : void 0;
}, },
deleteTask: function(req, cb) { deleteTask: function(req, cb) {
var i, task, _ref; var i, task, _ref;
task = user.tasks[(_ref = req.params) != null ? _ref.id : void 0]; task = user.tasks[(_ref = req.params) != null ? _ref.id : void 0];
if (!task) { if (!task) {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: 'Task not found' message: 'Task not found'
}); }) : void 0;
} }
i = user[task.type + "s"].indexOf(task); i = user[task.type + "s"].indexOf(task);
if (~i) { if (~i) {
user[task.type + "s"].splice(i, 1); user[task.type + "s"].splice(i, 1);
} }
return cb(null, req); return typeof cb === "function" ? cb(null, {}) : void 0;
}, },
addTask: function(req, cb) { addTask: function(req, cb) {
var task; var task;
task = api.taskDefaults(req.body); task = api.taskDefaults(req.body);
user["" + task.type + "s"].unshift(task); user["" + task.type + "s"].unshift(task);
if (typeof cb === "function") { if (typeof cb === "function") {
cb(null, req); cb(null, task);
} }
return task; return task;
}, },
@ -11323,7 +11331,7 @@ var process=require("__browserify_process");(function() {
user.tags.push({ user.tags.push({
name: name name: name
}); });
return typeof cb === "function" ? cb(null, req) : void 0; return typeof cb === "function" ? cb(null, user.tags) : void 0;
}, },
updateTag: function(req, cb) { updateTag: function(req, cb) {
var i, tid; var i, tid;
@ -11332,10 +11340,10 @@ var process=require("__browserify_process");(function() {
id: tid id: tid
}); });
if (!~i) { if (!~i) {
return cb('Tag not found', req); return typeof cb === "function" ? cb('Tag not found', req) : void 0;
} }
user.tags[i].name = req.body.name; user.tags[i].name = req.body.name;
return typeof cb === "function" ? cb(null, req) : void 0; return typeof cb === "function" ? cb(null, user.tags[i]) : void 0;
}, },
deleteTag: function(req, cb) { deleteTag: function(req, cb) {
var i, tag, tid; var i, tag, tid;
@ -11344,7 +11352,7 @@ var process=require("__browserify_process");(function() {
id: tid id: tid
}); });
if (!~i) { if (!~i) {
return cb('Tag not found', req); return typeof cb === "function" ? cb('Tag not found', req) : void 0;
} }
tag = user.tags[i]; tag = user.tags[i];
delete user.filters[tag.id]; delete user.filters[tag.id];
@ -11355,7 +11363,7 @@ var process=require("__browserify_process");(function() {
_.each(['habits', 'dailys', 'todos', 'rewards'], function(type) { _.each(['habits', 'dailys', 'todos', 'rewards'], function(type) {
return typeof user.markModified === "function" ? user.markModified(type) : void 0; return typeof user.markModified === "function" ? user.markModified(type) : void 0;
}); });
return cb(null, req); return typeof cb === "function" ? cb(null, user.tags) : void 0;
}, },
feed: function(req, cb) { feed: function(req, cb) {
var egg, evolve, food, message, pet, potion, userPets, _ref, _ref1, _ref2; var egg, evolve, food, message, pet, potion, userPets, _ref, _ref1, _ref2;
@ -11364,28 +11372,28 @@ var process=require("__browserify_process");(function() {
_ref1 = pet.split('-'), egg = _ref1[0], potion = _ref1[1]; _ref1 = pet.split('-'), egg = _ref1[0], potion = _ref1[1];
userPets = user.items.pets; userPets = user.items.pets;
if (!userPets[pet]) { if (!userPets[pet]) {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: ":pet not found in user.items.pets" message: ":pet not found in user.items.pets"
}); }) : void 0;
} }
if (!((_ref2 = user.items.food) != null ? _ref2[food.key] : void 0)) { if (!((_ref2 = user.items.food) != null ? _ref2[food.key] : void 0)) {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: ":food not found in user.items.food" message: ":food not found in user.items.food"
}); }) : void 0;
} }
if (content.specialPets[pet]) { if (content.specialPets[pet]) {
return cb({ return typeof cb === "function" ? cb({
code: 401, code: 401,
message: "Can't feed this pet." message: "Can't feed this pet."
}); }) : void 0;
} }
if (user.items.mounts[pet] && (userPets[pet] >= 50 || food.key === 'Saddle')) { if (user.items.mounts[pet] && (userPets[pet] >= 50 || food.key === 'Saddle')) {
return cb({ return typeof cb === "function" ? cb({
code: 401, code: 401,
message: "You already have that mount" message: "You already have that mount"
}); }) : void 0;
} }
message = ''; message = '';
evolve = function() { evolve = function() {
@ -11411,33 +11419,33 @@ var process=require("__browserify_process");(function() {
} }
} }
user.items.food[food.key]--; user.items.food[food.key]--;
return cb({ return typeof cb === "function" ? cb({
code: 200, code: 200,
message: message message: message
}, req); }, userPets[pet]) : void 0;
}, },
purchase: function(req, cb) { purchase: function(req, cb) {
var item, key, type, _ref; var item, key, type, _ref;
_ref = req.params, type = _ref.type, key = _ref.key; _ref = req.params, type = _ref.type, key = _ref.key;
if (type !== 'eggs' && type !== 'hatchingPotions' && type !== 'food' && type !== 'quests' && type !== 'special') { if (type !== 'eggs' && type !== 'hatchingPotions' && type !== 'food' && type !== 'quests' && type !== 'special') {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: ":type must be in [hatchingPotions,eggs,food,quests,special]" message: ":type must be in [hatchingPotions,eggs,food,quests,special]"
}, req); }, req) : void 0;
} }
item = content[type][key]; item = content[type][key];
if (!item) { if (!item) {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: ":key not found for Content." + type message: ":key not found for Content." + type
}, req); }, req) : void 0;
} }
if (!user.items[type][key]) { if (!user.items[type][key]) {
user.items[type][key] = 0; user.items[type][key] = 0;
} }
user.items[type][key]++; user.items[type][key]++;
user.balance -= item.value / 4; user.balance -= item.value / 4;
return cb(null, req); return typeof cb === "function" ? cb(null, _.pick(user, $w('items balance'))) : void 0;
}, },
buy: function(req, cb) { buy: function(req, cb) {
var item, key, message, _ref; var item, key, message, _ref;
@ -11475,26 +11483,26 @@ var process=require("__browserify_process");(function() {
return typeof cb === "function" ? cb({ return typeof cb === "function" ? cb({
code: 200, code: 200,
message: message message: message
}, req) : void 0; }, _.pick(user, $w('items achievements stats'))) : void 0;
}, },
sell: function(req, cb) { sell: function(req, cb) {
var key, type, _ref; var key, type, _ref;
_ref = req.params, key = _ref.key, type = _ref.type; _ref = req.params, key = _ref.key, type = _ref.type;
if (type !== 'eggs' && type !== 'hatchingPotions' && type !== 'food') { if (type !== 'eggs' && type !== 'hatchingPotions' && type !== 'food') {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: ":type not found. Must bes in [eggs, hatchingPotions, food]" message: ":type not found. Must bes in [eggs, hatchingPotions, food]"
}); }) : void 0;
} }
if (!user.items[type][key]) { if (!user.items[type][key]) {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: ":key not found for user.items." + type message: ":key not found for user.items." + type
}); }) : void 0;
} }
user.items[type][key]--; user.items[type][key]--;
user.stats.gp += content[type][key].value; user.stats.gp += content[type][key].value;
return typeof cb === "function" ? cb(null, req) : void 0; return typeof cb === "function" ? cb(null, _.pick(user, $w('stats items'))) : void 0;
}, },
equip: function(req, cb) { equip: function(req, cb) {
var item, key, message, type, _ref; var item, key, message, type, _ref;
@ -11512,29 +11520,29 @@ var process=require("__browserify_process");(function() {
user.items.gear[type][item.type] = item.key; user.items.gear[type][item.type] = item.key;
message = user.fns.handleTwoHanded(item, type); message = user.fns.handleTwoHanded(item, type);
} }
return cb({ return typeof cb === "function" ? cb({
code: 200, code: 200,
message: message message: message
}, req); }, user.items) : void 0;
}, },
hatch: function(req, cb) { hatch: function(req, cb) {
var egg, hatchingPotion, pet, _ref; var egg, hatchingPotion, pet, _ref;
_ref = req.params, egg = _ref.egg, hatchingPotion = _ref.hatchingPotion; _ref = req.params, egg = _ref.egg, hatchingPotion = _ref.hatchingPotion;
if (!(egg && hatchingPotion)) { if (!(egg && hatchingPotion)) {
return cb({ return typeof cb === "function" ? cb({
code: 404, code: 404,
message: "Please specify query.egg & query.hatchingPotion" message: "Please specify query.egg & query.hatchingPotion"
}); }) : void 0;
} }
if (!(user.items.eggs[egg] > 0 && user.items.hatchingPotions[hatchingPotion] > 0)) { if (!(user.items.eggs[egg] > 0 && user.items.hatchingPotions[hatchingPotion] > 0)) {
return cb({ return typeof cb === "function" ? cb({
code: 401, code: 401,
message: "You're missing either that egg or that potion" message: "You're missing either that egg or that potion"
}); }) : void 0;
} }
pet = "" + egg + "-" + hatchingPotion; pet = "" + egg + "-" + hatchingPotion;
if (user.items.pets[pet]) { if (user.items.pets[pet]) {
return cb("You already have that pet. Try hatching a different combination!"); return typeof cb === "function" ? cb("You already have that pet. Try hatching a different combination!") : void 0;
} }
user.items.pets[pet] = 5; user.items.pets[pet] = 5;
user.items.eggs[egg]--; user.items.eggs[egg]--;
@ -11542,7 +11550,7 @@ var process=require("__browserify_process");(function() {
return typeof cb === "function" ? cb({ return typeof cb === "function" ? cb({
code: 200, code: 200,
message: "Your egg hatched! Visit your stable to equip your pet." message: "Your egg hatched! Visit your stable to equip your pet."
}, req) : void 0; }, user.items) : void 0;
}, },
unlock: function(req, cb) { unlock: function(req, cb) {
var alreadyOwns, cost, fullSet, k, path, split, v; var alreadyOwns, cost, fullSet, k, path, split, v;
@ -11575,7 +11583,7 @@ var process=require("__browserify_process");(function() {
if (typeof user.markModified === "function") { if (typeof user.markModified === "function") {
user.markModified('purchased'); user.markModified('purchased');
} }
return typeof cb === "function" ? cb(null, req) : void 0; return typeof cb === "function" ? cb(null, _.pick(user, $w('purchased preferences'))) : void 0;
}, },
changeClass: function(req, cb) { changeClass: function(req, cb) {
var klass, _ref; var klass, _ref;
@ -11600,13 +11608,13 @@ var process=require("__browserify_process");(function() {
} else { } else {
if (user.preferences.disableClasses) { if (user.preferences.disableClasses) {
user.preferences.disableClasses = false; user.preferences.disableClasses = false;
user.autoAllocate = false; user.preferences.autoAllocate = false;
} else { } else {
if (!(user.balance >= .75)) { if (!(user.balance >= .75)) {
return cb({ return typeof cb === "function" ? cb({
code: 401, code: 401,
message: "Not enough gems" message: "Not enough gems"
}); }) : void 0;
} }
user.balance -= .75; user.balance -= .75;
} }
@ -11619,7 +11627,7 @@ var process=require("__browserify_process");(function() {
}); });
user.flags.classSelected = false; user.flags.classSelected = false;
} }
return typeof cb === "function" ? cb(null, req) : void 0; return typeof cb === "function" ? cb(null, _.pick(user, $w('stats flags items preferences'))) : void 0;
}, },
disableClasses: function(req, cb) { disableClasses: function(req, cb) {
user.stats["class"] = 'warrior'; user.stats["class"] = 'warrior';
@ -11628,7 +11636,7 @@ var process=require("__browserify_process");(function() {
user.preferences.autoAllocate = true; user.preferences.autoAllocate = true;
user.stats.str = user.stats.lvl; user.stats.str = user.stats.lvl;
user.stats.points = 0; user.stats.points = 0;
return cb(null, req); return typeof cb === "function" ? cb(null, _.pick(user, $w('stats flags preferences'))) : void 0;
}, },
allocate: function(req, cb) { allocate: function(req, cb) {
var stat; var stat;
@ -11640,7 +11648,7 @@ var process=require("__browserify_process");(function() {
user.stats.mp++; user.stats.mp++;
} }
} }
return typeof cb === "function" ? cb(null, req) : void 0; return typeof cb === "function" ? cb(null, _.pick(user, $w('stats'))) : void 0;
}, },
score: function(req, cb) { score: function(req, cb) {
var addPoints, calculateDelta, delta, direction, id, num, options, stats, subtractPoints, task, th, _ref; var addPoints, calculateDelta, delta, direction, id, num, options, stats, subtractPoints, task, th, _ref;
@ -11663,7 +11671,7 @@ var process=require("__browserify_process");(function() {
task.priority = 1; task.priority = 1;
} }
if (task.value > stats.gp && task.type === 'reward') { if (task.value > stats.gp && task.type === 'reward') {
return cb('Not enough Gold'); return typeof cb === "function" ? cb('Not enough Gold') : void 0;
} }
delta = 0; delta = 0;
calculateDelta = function() { calculateDelta = function() {
@ -11779,7 +11787,7 @@ var process=require("__browserify_process");(function() {
} }
} }
if (typeof cb === "function") { if (typeof cb === "function") {
cb(null, req); cb(null, user);
} }
return delta; return delta;
} }

View file

@ -4,6 +4,9 @@ content = require('./content.coffee')
api = module.exports = {} api = module.exports = {}
# little helper for large arrays of strings. %w"this that another" equivalent from Rails, I really miss that function
$w = (s)->s.split(' ')
### ###
------------------------------------------------------ ------------------------------------------------------
Time / Day Time / Day
@ -163,7 +166,7 @@ api.uuid = ->
Even though Mongoose handles task defaults, we want to make sure defaults are set on the client-side before Even though Mongoose handles task defaults, we want to make sure defaults are set on the client-side before
sending up to the server for performance sending up to the server for performance
### ###
api.taskDefaults = (task) -> api.taskDefaults = (task={}) ->
task.type = 'habit' unless task.type and task.type in ['habit','daily','todo','reward'] task.type = 'habit' unless task.type and task.type in ['habit','daily','todo','reward']
defaults = defaults =
id: api.uuid() id: api.uuid()
@ -347,11 +350,11 @@ api.wrap = (user) ->
update: (req, cb) -> update: (req, cb) ->
_.each req.body, (v,k) -> _.each req.body, (v,k) ->
user.fns.dotSet(k,v);true user.fns.dotSet(k,v);true
cb? null, req cb? null, user
sleep: (req, cb) -> sleep: (req, cb) ->
user.preferences.sleep = !user.preferences.sleep user.preferences.sleep = !user.preferences.sleep
cb null, req cb? null, {}
revive: (req, cb) -> revive: (req, cb) ->
# Reset stats # Reset stats
@ -371,7 +374,7 @@ api.wrap = (user) ->
user.items.gear.equipped[item.type] = "#{item.type}_base_0" if user.items.gear.equipped[item.type] is lostItem user.items.gear.equipped[item.type] = "#{item.type}_base_0" if user.items.gear.equipped[item.type] is lostItem
user.items.gear.costume[item.type] = "#{item.type}_base_0" if user.items.gear.costume[item.type] is lostItem user.items.gear.costume[item.type] = "#{item.type}_base_0" if user.items.gear.costume[item.type] is lostItem
user.markModified? 'items.gear' user.markModified? 'items.gear'
cb? (if item then {code:200,message:"Your #{item.text} broke."} else null), req cb? (if item then {code:200,message:"Your #{item.text} broke."} else null), user
reset: (req, cb) -> reset: (req, cb) ->
user.habits = [] user.habits = []
@ -392,16 +395,16 @@ api.wrap = (user) ->
user.items.gear.owned = {weapon_warrior_0:true} user.items.gear.owned = {weapon_warrior_0:true}
user.markModified? 'items.gear.owned' user.markModified? 'items.gear.owned'
user.preferences.costume = false user.preferences.costume = false
cb null, req cb? null, user
reroll: (req, cb) -> reroll: (req, cb) ->
if user.balance < 1 if user.balance < 1
return cb {code:401,message: "Not enough gems."}, req return cb? {code:401,message: "Not enough gems."}, req
user.balance-- user.balance--
_.each user.tasks, (task) -> _.each user.tasks, (task) ->
task.value = 0 task.value = 0
user.stats.hp = 50 user.stats.hp = 50
cb null, req cb? null, user
# ------ # ------
# Tasks # Tasks
@ -409,34 +412,35 @@ api.wrap = (user) ->
clearCompleted: (req, cb) -> clearCompleted: (req, cb) ->
user.todos = _.where(user.todos, {completed: false}) user.todos = _.where(user.todos, {completed: false})
cb null, req cb? null, user.todos
sortTask: (req, cb) -> sortTask: (req, cb) ->
{id} = req.params {id} = req.params
{to, from} = req.query {to, from} = req.query
task = user.tasks[id] task = user.tasks[id]
return cb({code:404, message: "Task not found."}) unless task return cb?({code:404, message: "Task not found."}) unless task
return cb('?to=__&from=__ are required') unless to? and from? return cb?('?to=__&from=__ are required') unless to? and from?
user["#{task.type}s"].splice to, 0, user["#{task.type}s"].splice(from, 1)[0] tasks = user["#{task.type}s"]
cb null, req tasks.splice to, 0, tasks.splice(from, 1)[0]
cb? null, tasks
updateTask: (req, cb) -> updateTask: (req, cb) ->
return cb?("Task not found") unless user.tasks[req.params?.id] return cb?("Task not found") unless task = user.tasks[req.params?.id]
_.merge user.tasks[req.params.id], req.body _.merge task, req.body
user.tasks[req.params.id].markModified? 'tags' user.markModified? 'tags'
cb? null, req cb? null, task
deleteTask: (req, cb) -> deleteTask: (req, cb) ->
task = user.tasks[req.params?.id] task = user.tasks[req.params?.id]
return cb({code:404,message:'Task not found'}) unless task return cb?({code:404,message:'Task not found'}) unless task
i = user[task.type + "s"].indexOf(task) i = user[task.type + "s"].indexOf(task)
user[task.type + "s"].splice(i, 1) if ~i user[task.type + "s"].splice(i, 1) if ~i
cb null, req cb? null, {}
addTask: (req, cb) -> addTask: (req, cb) ->
task = api.taskDefaults(req.body) task = api.taskDefaults(req.body)
user["#{task.type}s"].unshift(task) user["#{task.type}s"].unshift(task)
cb? null, req cb? null, task
task task
# ------ # ------
@ -447,19 +451,19 @@ api.wrap = (user) ->
{name} = req.body {name} = req.body
user.tags ?= [] user.tags ?= []
user.tags.push({name}) user.tags.push({name})
cb? null, req cb? null, user.tags
updateTag: (req, cb) -> updateTag: (req, cb) ->
tid = req.params.id tid = req.params.id
i = _.findIndex user.tags, {id: tid} i = _.findIndex user.tags, {id: tid}
return cb('Tag not found', req) if !~i return cb?('Tag not found', req) if !~i
user.tags[i].name = req.body.name user.tags[i].name = req.body.name
cb? null, req cb? null, user.tags[i]
deleteTag: (req, cb) -> deleteTag: (req, cb) ->
tid = req.params.id tid = req.params.id
i = _.findIndex user.tags, {id: tid} i = _.findIndex user.tags, {id: tid}
return cb('Tag not found', req) if !~i return cb?('Tag not found', req) if !~i
tag = user.tags[i] tag = user.tags[i]
delete user.filters[tag.id] delete user.filters[tag.id]
user.tags.splice i, 1 user.tags.splice i, 1
@ -470,7 +474,7 @@ api.wrap = (user) ->
_.each ['habits','dailys','todos','rewards'], (type) -> _.each ['habits','dailys','todos','rewards'], (type) ->
user.markModified? type user.markModified? type
cb null, req cb? null, user.tags
# ------ # ------
# Inventory # Inventory
@ -482,10 +486,10 @@ api.wrap = (user) ->
[egg, potion] = pet.split('-') [egg, potion] = pet.split('-')
userPets = user.items.pets userPets = user.items.pets
return cb({code:404, message:":pet not found in user.items.pets"}) unless userPets[pet] return cb?({code:404, message:":pet not found in user.items.pets"}) unless userPets[pet]
return cb({code:404, message:":food not found in user.items.food"}) unless user.items.food?[food.key] return cb?({code:404, message:":food not found in user.items.food"}) unless user.items.food?[food.key]
return cb({code:401, message:"Can't feed this pet."}) if content.specialPets[pet] return cb?({code:401, message:"Can't feed this pet."}) if content.specialPets[pet]
return cb({code:401, message:"You already have that mount"}) if user.items.mounts[pet] and (userPets[pet] >= 50 or food.key is 'Saddle') return cb?({code:401, message:"You already have that mount"}) if user.items.mounts[pet] and (userPets[pet] >= 50 or food.key is 'Saddle')
message = '' message = ''
evolve = -> evolve = ->
@ -506,18 +510,18 @@ api.wrap = (user) ->
if userPets[pet] >= 50 and !user.items.mounts[pet] if userPets[pet] >= 50 and !user.items.mounts[pet]
evolve() evolve()
user.items.food[food.key]-- user.items.food[food.key]--
cb {code:200, message}, req cb? {code:200, message}, userPets[pet]
# buy is for gear, purchase is for gem-purchaseables (i know, I know...) # buy is for gear, purchase is for gem-purchaseables (i know, I know...)
purchase: (req, cb) -> purchase: (req, cb) ->
{type,key} = req.params {type,key} = req.params
return cb({code:404,message:":type must be in [hatchingPotions,eggs,food,quests,special]"},req) unless type in ['eggs','hatchingPotions','food','quests','special'] return cb?({code:404,message:":type must be in [hatchingPotions,eggs,food,quests,special]"},req) unless type in ['eggs','hatchingPotions','food','quests','special']
item = content[type][key] item = content[type][key]
return cb({code:404,message:":key not found for Content.#{type}"},req) unless item return cb?({code:404,message:":key not found for Content.#{type}"},req) unless item
user.items[type][key] = 0 unless user.items[type][key] user.items[type][key] = 0 unless user.items[type][key]
user.items[type][key]++ user.items[type][key]++
user.balance -= (item.value / 4) user.balance -= (item.value / 4)
cb null, req cb? null, _.pick(user,$w 'items balance')
# buy is for gear, purchase is for gem-purchaseables (i know, I know...) # buy is for gear, purchase is for gem-purchaseables (i know, I know...)
buy: (req, cb) -> buy: (req, cb) ->
@ -536,15 +540,15 @@ api.wrap = (user) ->
if item.klass in ['warrior','wizard','healer','rogue'] and user.fns.getItem('weapon').last and user.fns.getItem('armor').last and user.fns.getItem('head').last and (user.fns.getItem('shield').last or user.fns.getItem('weapon').twoHanded) if item.klass in ['warrior','wizard','healer','rogue'] and user.fns.getItem('weapon').last and user.fns.getItem('armor').last and user.fns.getItem('head').last and (user.fns.getItem('shield').last or user.fns.getItem('weapon').twoHanded)
user.achievements.ultimateGear = true user.achievements.ultimateGear = true
user.stats.gp -= item.value user.stats.gp -= item.value
cb? {code:200, message}, req cb? {code:200, message}, _.pick(user,$w 'items achievements stats')
sell: (req, cb) -> sell: (req, cb) ->
{key, type} = req.params {key, type} = req.params
return cb({code:404,message:":type not found. Must bes in [eggs, hatchingPotions, food]"}) unless type in ['eggs','hatchingPotions', 'food'] return cb?({code:404,message:":type not found. Must bes in [eggs, hatchingPotions, food]"}) unless type in ['eggs','hatchingPotions', 'food']
return cb({code:404,message:":key not found for user.items.#{type}"}) unless user.items[type][key] return cb?({code:404,message:":key not found for user.items.#{type}"}) unless user.items[type][key]
user.items[type][key]-- user.items[type][key]--
user.stats.gp += content[type][key].value user.stats.gp += content[type][key].value
cb? null, req cb? null, _.pick(user,$w 'stats items')
equip: (req, cb) -> equip: (req, cb) ->
[type, key] = [req.params.type || 'equipped', req.params.key] [type, key] = [req.params.type || 'equipped', req.params.key]
@ -557,18 +561,18 @@ api.wrap = (user) ->
item = content.gear.flat[key] item = content.gear.flat[key]
user.items.gear[type][item.type] = item.key user.items.gear[type][item.type] = item.key
message = user.fns.handleTwoHanded(item,type) message = user.fns.handleTwoHanded(item,type)
cb {code:200,message}, req cb? {code:200,message}, user.items
hatch: (req, cb) -> hatch: (req, cb) ->
{egg, hatchingPotion} = req.params {egg, hatchingPotion} = req.params
return cb({code:404,message:"Please specify query.egg & query.hatchingPotion"}) unless egg and hatchingPotion return cb?({code:404,message:"Please specify query.egg & query.hatchingPotion"}) unless egg and hatchingPotion
return cb({code:401,message:"You're missing either that egg or that potion"}) unless user.items.eggs[egg] > 0 and user.items.hatchingPotions[hatchingPotion] > 0 return cb?({code:401,message:"You're missing either that egg or that potion"}) unless user.items.eggs[egg] > 0 and user.items.hatchingPotions[hatchingPotion] > 0
pet = "#{egg}-#{hatchingPotion}" pet = "#{egg}-#{hatchingPotion}"
return cb("You already have that pet. Try hatching a different combination!") if user.items.pets[pet] return cb?("You already have that pet. Try hatching a different combination!") if user.items.pets[pet]
user.items.pets[pet] = 5 user.items.pets[pet] = 5
user.items.eggs[egg]-- user.items.eggs[egg]--
user.items.hatchingPotions[hatchingPotion]-- user.items.hatchingPotions[hatchingPotion]--
cb? {code:200, message:"Your egg hatched! Visit your stable to equip your pet."}, req cb? {code:200, message:"Your egg hatched! Visit your stable to equip your pet."}, user.items
unlock: (req, cb) -> unlock: (req, cb) ->
{path} = req.query {path} = req.query
@ -587,7 +591,7 @@ api.wrap = (user) ->
user.fns.dotSet "purchased." + path, true user.fns.dotSet "purchased." + path, true
user.balance -= cost user.balance -= cost
user.markModified? 'purchased' user.markModified? 'purchased'
cb? null, req cb? null, _.pick(user,$w 'purchased preferences')
# ------ # ------
# Classes # Classes
@ -620,14 +624,14 @@ api.wrap = (user) ->
# Null ?class value means "reset class" # Null ?class value means "reset class"
if user.preferences.disableClasses if user.preferences.disableClasses
user.preferences.disableClasses = false user.preferences.disableClasses = false
user.autoAllocate = false user.preferences.autoAllocate = false
else else
return cb({code:401,message:"Not enough gems"}) unless user.balance >= .75 return cb?({code:401,message:"Not enough gems"}) unless user.balance >= .75
user.balance -= .75 user.balance -= .75
_.merge user.stats, {str: 0, con: 0, per: 0, int: 0, points: user.stats.lvl} _.merge user.stats, {str: 0, con: 0, per: 0, int: 0, points: user.stats.lvl}
user.flags.classSelected = false user.flags.classSelected = false
#'stats.points': this is handled on the server #'stats.points': this is handled on the server
cb? null, req cb? null, _.pick(user,$w 'stats flags items preferences')
disableClasses: (req, cb) -> disableClasses: (req, cb) ->
user.stats.class = 'warrior' user.stats.class = 'warrior'
@ -636,7 +640,7 @@ api.wrap = (user) ->
user.preferences.autoAllocate = true user.preferences.autoAllocate = true
user.stats.str = user.stats.lvl user.stats.str = user.stats.lvl
user.stats.points = 0 user.stats.points = 0
cb null, req cb? null, _.pick(user,$w 'stats flags preferences')
allocate: (req, cb) -> allocate: (req, cb) ->
stat = req.query.stat or 'str' stat = req.query.stat or 'str'
@ -644,7 +648,7 @@ api.wrap = (user) ->
user.stats[stat]++ user.stats[stat]++
user.stats.points-- user.stats.points--
user.stats.mp++ if stat is 'int' #increase their MP along with their max MP user.stats.mp++ if stat is 'int' #increase their MP along with their max MP
cb? null, req cb? null, _.pick(user,$w 'stats')
# ------ # ------
# Score # Score
@ -665,7 +669,7 @@ api.wrap = (user) ->
# If they're trying to purhcase a too-expensive reward, don't allow them to do that. # If they're trying to purhcase a too-expensive reward, don't allow them to do that.
if task.value > stats.gp and task.type is 'reward' if task.value > stats.gp and task.type is 'reward'
return cb('Not enough Gold'); return cb? 'Not enough Gold'
delta = 0 delta = 0
@ -787,7 +791,7 @@ api.wrap = (user) ->
if typeof window is 'undefined' if typeof window is 'undefined'
user.fns.randomDrop({task, delta}) if direction is 'up' user.fns.randomDrop({task, delta}) if direction is 'up'
cb? null, req cb? null, user
return delta return delta
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------

View file

@ -71,20 +71,26 @@ angular.module('userServices', []).
// Update user // Update user
_.extend(user, data); _.extend(user, data);
if (!user._wrapped){ if (!user._wrapped){
// This wraps user with `ops`, which are functions shared both on client and mobile. When performed on client,
// they update the user in the browser and then send the request to the server, where the same operation is
// replicated. We need to wrap each op to provide a callback to send that operation
$window.habitrpgShared.wrap(user); $window.habitrpgShared.wrap(user);
_.each(user.ops, function(op,k){ _.each(user.ops, function(op,k){
user.ops[k] = _.partialRight(op, function(err, req){ user.ops[k] = function(req,cb){
if (err) { if (cb) return op(req,cb);
var message = err.code ? err.message : err; op(req,function(err,response){
console.log(message); if (err) {
if (MOBILE_APP) Notification.push({type:'text',text:message}); var message = err.code ? err.message : err;
else Notification.text(message); console.log(message);
// In the case of 200s, they're friendly alert messages like "You're pet has hatched!" - still send the op if (MOBILE_APP) Notification.push({type:'text',text:message});
if ((err.code && err.code >= 400) || !err.code) return; else Notification.text(message);
// In the case of 200s, they're friendly alert messages like "You're pet has hatched!" - still send the op
} if ((err.code && err.code >= 400) || !err.code) return;
userServices.log({op:k, params: req.params, query:req.query, body:req.body}); }
}); userServices.log({op:k, params: req.params, query:req.query, body:req.body});
});
}
}); });
} }