some bugs which are a result of user not being tested as already dead.

using scoring.setUser() so we don't have to pass user all over the
place, there's probably a better approach -- revisit
This commit is contained in:
Tyler Renelle 2012-09-20 10:56:36 -04:00
parent 53659d7ac8
commit 8480ef3564
6 changed files with 82 additions and 65 deletions

View file

@ -53,6 +53,7 @@ get('/:uidParam?', function(page, model, _arg, next) {
ready(function(model) {
var poormanscron, setupSortable, step, tour, type, _i, _j, _len, _len1, _ref1, _ref2;
scoring.setUser(model.at('_user'));
$('[rel=tooltip]').tooltip();
$('[rel=popover]').popover();
model.on('set', '*', function() {
@ -116,7 +117,6 @@ ready(function(model) {
};
task = model.at("_user.tasks." + i);
scoring.score({
user: model.at('_user'),
task: task,
direction: direction()
});
@ -196,7 +196,6 @@ ready(function(model) {
} else {
task.set('type', 'habit');
scoring.score({
user: model.at('_user'),
task: task,
direction: 'down'
});
@ -293,7 +292,6 @@ ready(function(model) {
user = model.at('_user');
task = model.at($(el).parents('li')[0]);
return scoring.score({
user: user,
task: task,
direction: direction
});
@ -322,7 +320,7 @@ ready(function(model) {
return _(daysPassed).times(function(n) {
var tallyFor;
tallyFor = moment(lastCron).add('d', n);
return scoring.tally(model.at('_user'), tallyFor);
return scoring.tally(tallyFor);
});
}
};
@ -333,6 +331,6 @@ ready(function(model) {
return poormanscron();
}, 3600000);
return exports.endOfDayTally = function(e, el) {
return scoring.tally(model);
return scoring.tally();
};
});

View file

@ -1,9 +1,18 @@
// Generated by CoffeeScript 1.3.3
var content, expModifier, hpModifier, score, statsNotification, updateStats;
var content, expModifier, hpModifier, score, statsNotification, updateStats, user;
content = require('./content');
user = void 0;
module.exports.setUser = function(u) {
return user = u;
};
statsNotification = function(html, type) {
if (user.get('stats.lvl') === 0) {
return;
}
return $.bootstrapGrowl(html, {
type: type,
top_offset: 20,
@ -15,7 +24,7 @@ statsNotification = function(html, type) {
});
};
expModifier = function(user, value) {
expModifier = function(value) {
var dmg, modified;
dmg = user.get('items.weapon') * .03;
dmg += user.get('stats.lvl') * .03;
@ -23,7 +32,7 @@ expModifier = function(user, value) {
return modified;
};
hpModifier = function(user, value) {
hpModifier = function(value) {
var ac, modified;
ac = user.get('items.armor') * .03;
ac += user.get('stats.lvl') * .03;
@ -31,8 +40,20 @@ hpModifier = function(user, value) {
return modified;
};
updateStats = function(user, stats) {
updateStats = function(stats) {
var money, tnl;
if (user.get('stats.lvl') === 0) {
return;
}
if (stats.hp != null) {
if (stats.hp <= 0) {
user.set('stats.lvl', 0);
user.set('stast.hp', 0);
return;
} else {
user.set('stats.hp', stats.hp);
}
}
if (stats.exp != null) {
tnl = user.get('_tnl');
if (stats.exp >= tnl) {
@ -54,13 +75,6 @@ updateStats = function(user, stats) {
}
user.set('stats.exp', stats.exp);
}
if (stats.hp != null) {
if (stats.hp < 0) {
user.set('stats.lvl', 0);
} else {
user.set('stats.hp', stats.hp);
}
}
if (stats.money != null) {
if (!(typeof money !== "undefined" && money !== null) || money < 0) {
money = 0.0;
@ -70,27 +84,26 @@ updateStats = function(user, stats) {
};
module.exports.score = score = function(spec) {
var adjustvalue, cron, delta, direction, exp, hp, lvl, modified, money, num, sign, task, type, user, value, _ref, _ref1, _ref2;
var adjustvalue, cron, delta, direction, exp, hp, lvl, modified, money, num, sign, task, type, value, _ref, _ref1, _ref2;
if (spec == null) {
spec = {
user: null,
task: null,
direction: null,
cron: null
};
}
_ref = [spec.user, spec.task, spec.direction, spec.cron], user = _ref[0], task = _ref[1], direction = _ref[2], cron = _ref[3];
_ref = [spec.task, spec.direction, spec.cron], task = _ref[0], direction = _ref[1], cron = _ref[2];
if (!task) {
_ref1 = [user.get('stats.money'), user.get('stats.hp'), user.get('stats.exp')], money = _ref1[0], hp = _ref1[1], exp = _ref1[2];
if (direction === "up") {
modified = expModifier(user, 1);
modified = expModifier(1);
money += modified;
exp += modified;
} else {
modified = hpModifier(user, 1);
modified = hpModifier(1);
hp -= modified;
}
updateStats(user, {
updateStats({
hp: hp,
exp: exp,
money: money
@ -129,7 +142,7 @@ module.exports.score = score = function(spec) {
}
}
if ((delta > 0 || (type === 'daily' || type === 'todo')) && !cron) {
modified = expModifier(user, delta);
modified = expModifier(delta);
exp += modified;
money += modified;
if (modified > 0) {
@ -138,11 +151,11 @@ module.exports.score = score = function(spec) {
statsNotification("<i class='icon-star'></i>Exp,GP " + (modified.toFixed(2)), 'warning');
}
} else if (type !== 'reward' && type !== 'todo') {
modified = hpModifier(user, delta);
modified = hpModifier(delta);
hp += modified;
statsNotification("<i class='icon-heart'></i>HP " + (modified.toFixed(2)), 'error');
}
updateStats(user, {
updateStats({
hp: hp,
exp: exp,
money: money
@ -150,7 +163,7 @@ module.exports.score = score = function(spec) {
return delta;
};
module.exports.tally = function(user, momentDate) {
module.exports.tally = function(momentDate) {
var expTally, lvl, todoTally;
todoTally = 0;
_.each(user.get('tasks'), function(taskObj, taskId, list) {
@ -175,7 +188,6 @@ module.exports.tally = function(user, momentDate) {
dueToday = repeat && repeat[dayMapping[momentDate.day()]] === true;
if (dueToday || type === 'todo') {
score({
user: user,
task: task,
direction: 'down',
cron: true

View file

@ -7,16 +7,13 @@ module.exports = function(expressApp, root, derby) {
expressApp.get('/:uid/up/:score?', function(req, res) {
var model, score;
score = parseInt(req.params.score) || 1;
console.log({
score: score
});
model = req.getModel();
model.fetch("users." + req.params.uid, function(err, user) {
if (err || !user.get()) {
return;
}
scoring.setUser(user);
return scoring.score({
user: user,
direction: 'up'
});
});
@ -25,16 +22,13 @@ module.exports = function(expressApp, root, derby) {
expressApp.get('/:uid/down/:score?', function(req, res) {
var model, score;
score = parseInt(req.params.score) || 1;
console.log({
score: score
});
model = req.getModel();
model.fetch("users." + req.params.uid, function(err, user) {
if (err || !user.get()) {
return;
}
scoring.setUser(user);
return scoring.score({
user: user,
direction: 'down'
});
});

View file

@ -51,6 +51,7 @@ get '/:uidParam?', (page, model, {uidParam}, next) ->
# ========== CONTROLLER FUNCTIONS ==========
ready (model) ->
scoring.setUser(model.at('_user'))
$('[rel=tooltip]').tooltip()
$('[rel=popover]').popover()
@ -101,7 +102,7 @@ ready (model) ->
# Score the user based on todo task
task = model.at("_user.tasks.#{i}")
scoring.score({user:model.at('_user'), task:task, direction:direction()})
scoring.score({task:task, direction:direction()})
# Then move the todos to/from _todoList/_completedList
if task.get('type') == 'todo'
@ -154,7 +155,7 @@ ready (model) ->
return # Cancel. Don't delete, don't hurt user
else
task.set('type','habit') # hack to make sure it hits HP, instead of performing "undo checkbox"
scoring.score({user:model.at('_user'), task:task, direction:'down'})
scoring.score({task:task, direction:'down'})
# prevent accidently deleting long-standing tasks
else
@ -236,7 +237,7 @@ ready (model) ->
direction = 'down' if direction == 'false/'
user = model.at('_user')
task = model.at $(el).parents('li')[0]
scoring.score({user:user, task:task, direction:direction})
scoring.score({task:task, direction:direction})
exports.revive = (e, el) ->
stats = model.at '_user.stats'
@ -258,7 +259,7 @@ ready (model) ->
model.set('_user.lastCron', today) # reset cron
_(daysPassed).times (n) ->
tallyFor = moment(lastCron).add('d',n)
scoring.tally(model.at('_user'), tallyFor)
scoring.tally(tallyFor)
# FIXME seems can't call poormanscron() instantly, have to call after some time (2s here)
# Doesn't do anything otherwise. Don't know why... model not initialized enough yet?
setTimeout () -> # Run once on refresh
@ -271,7 +272,7 @@ ready (model) ->
# ========== DEBUGGING ==========
exports.endOfDayTally = (e, el) ->
scoring.tally(model)
scoring.tally()
# Temporary solution to running updates against the schema when the code changes
# exports.updateSchema = (e, el) ->

View file

@ -1,7 +1,14 @@
content = require('./content')
# This is required by all the functions, make sure it's set before anythign else is called
user = undefined
module.exports.setUser = (u) ->
user = u
statsNotification = (html, type) ->
#don't show notifications if user dead
return if user.get('stats.lvl') == 0
$.bootstrapGrowl html, {
type: type # (null, 'info', 'error', 'success')
top_offset: 20
@ -13,21 +20,33 @@ statsNotification = (html, type) ->
}
# Calculates Exp modification based on weapon & lvl
expModifier = (user, value) ->
expModifier = (value) ->
dmg = user.get('items.weapon') * .03 # each new weapon adds an additional 3% experience
dmg += user.get('stats.lvl') * .03 # same for lvls
modified = value + (value * dmg)
return modified
# Calculates HP-loss modification based on armor & lvl
hpModifier = (user, value) ->
hpModifier = (value) ->
ac = user.get('items.armor') * .03 # each new armor blocks an additional 3% damage
ac += user.get('stats.lvl') * .03 # same for lvls
modified = value - (value * ac)
return modified
# Setter for user.stats: handles death, leveling up, etc
updateStats = (user, stats) ->
updateStats = (stats) ->
# if user is dead, dont do anything
return if user.get('stats.lvl') == 0
if stats.hp?
# game over
if stats.hp <= 0
user.set 'stats.lvl', 0 # this signifies dead
user.set 'stast.hp', 0
return
else
user.set 'stats.hp', stats.hp
if stats.exp?
# level up & carry-over exp
tnl = user.get '_tnl'
@ -51,33 +70,26 @@ updateStats = (user, stats) ->
user.set 'stats.exp', stats.exp
if stats.hp?
# game over
if stats.hp < 0
user.set 'stats.lvl', 0 # this signifies dead
else
user.set 'stats.hp', stats.hp
if stats.money?
money = 0.0 if (!money? or money<0)
user.set 'stats.money', stats.money
module.exports.score = score = (spec = {user:null, task:null, direction:null, cron:null}) ->
[user, task, direction, cron] = [spec.user, spec.task, spec.direction, spec.cron]
module.exports.score = score = (spec = {task:null, direction:null, cron:null}) ->
[task, direction, cron] = [spec.task, spec.direction, spec.cron]
# up / down was called by itself, probably as REST from 3rd party service
if !task
[money, hp, exp] = [user.get('stats.money'), user.get('stats.hp'), user.get('stats.exp')]
if (direction == "up")
modified = expModifier(user, 1)
modified = expModifier(1)
money += modified
exp += modified
# statsNotification "<i class='icon-star'></i>Exp,GP +#{modified.toFixed(2)}", 'success'
else
modified = hpModifier(user, 1)
modified = hpModifier(1)
hp -= modified
# statsNotification "<i class='icon-heart'></i>HP #{modified.toFixed(2)}", 'error'
updateStats(user, {hp: hp, exp: exp, money: money})
updateStats({hp: hp, exp: exp, money: money})
return
@ -118,7 +130,7 @@ module.exports.score = score = (spec = {user:null, task:null, direction:null, cr
# Add points to exp & money if positive delta
# Only take away mony if it was a mistake (aka, a checkbox)
if (delta > 0 or ( type in ['daily', 'todo'])) and !cron
modified = expModifier(user, delta)
modified = expModifier(delta)
exp += modified
money += modified
if modified > 0
@ -128,17 +140,17 @@ module.exports.score = score = (spec = {user:null, task:null, direction:null, cr
statsNotification "<i class='icon-star'></i>Exp,GP #{modified.toFixed(2)}", 'warning'
# Deduct from health (rewards case handled above)
else unless type in ['reward', 'todo']
modified = hpModifier(user, delta)
modified = hpModifier(delta)
hp += modified
statsNotification "<i class='icon-heart'></i>HP #{modified.toFixed(2)}", 'error'
updateStats(user, {hp: hp, exp: exp, money: money})
updateStats({hp: hp, exp: exp, money: money})
return delta
# At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
# For incomplete Dailys, deduct experience
module.exports.tally = (user, momentDate) ->
module.exports.tally = (momentDate) ->
todoTally = 0
_.each user.get('tasks'), (taskObj, taskId, list) ->
#FIXME is it hiccuping here? taskId == "$_65255f4e-3728-4d50-bade-3b05633639af_2", & taskObj.id = undefined
@ -152,7 +164,7 @@ module.exports.tally = (user, momentDate) ->
dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s',7:'su'}
dueToday = (repeat && repeat[dayMapping[momentDate.day()]]==true)
if dueToday or type=='todo'
score({user:user, task:task, direction:'down', cron:true})
score({task:task, direction:'down', cron:true})
if type == 'daily'
task.push "history", { date: new Date(momentDate), value: value }
else

View file

@ -4,20 +4,20 @@ module.exports = (expressApp, root, derby) ->
expressApp.get '/:uid/up/:score?', (req, res) ->
score = parseInt(req.params.score) || 1
console.log {score:score}
model = req.getModel()
model.fetch "users.#{req.params.uid}", (err, user) ->
return if err || !user.get()
scoring.score({user:user, direction:'up'})
scoring.setUser(user)
scoring.score({direction:'up'})
res.send(200)
expressApp.get '/:uid/down/:score?', (req, res) ->
score = parseInt(req.params.score) || 1
console.log {score:score}
model = req.getModel()
model.fetch "users.#{req.params.uid}", (err, user) ->
return if err || !user.get()
scoring.score({user:user, direction:'down'})
scoring.setUser(user)
scoring.score({direction:'down'})
res.send(200)
expressApp.get '/privacy', (req, res) ->