use model events to handle stats notifications. not perfect, need to

fix handling of "undo exp/gp" operation
This commit is contained in:
Tyler Renelle
2012-09-24 08:48:05 -04:00
parent 0ba7ccd370
commit d929905c48
6 changed files with 95 additions and 59 deletions

View File

@@ -53,7 +53,7 @@ get('/:uidParam?', function(page, model, _arg, next) {
ready(function(model) { ready(function(model) {
var setupSortable, step, tour, type, _i, _j, _len, _len1, _ref1, _ref2; var setupSortable, step, tour, type, _i, _j, _len, _len1, _ref1, _ref2;
scoring.setUser(model.at('_user')); scoring.setModel(model);
$('[rel=tooltip]').tooltip(); $('[rel=tooltip]').tooltip();
$('[rel=popover]').popover(); $('[rel=popover]').popover();
model.on('set', '*', function() { model.on('set', '*', function() {

View File

@@ -1,5 +1,5 @@
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.3.3
var MODIFIER, content, cron, expModifier, helpers, hpModifier, score, setUser, statsNotification, tally, updateStats, user; var MODIFIER, content, cron, expModifier, helpers, hpModifier, model, score, setModel, setupNotifications, tally, updateStats, user;
content = require('./content'); content = require('./content');
@@ -9,10 +9,16 @@ MODIFIER = .03;
user = void 0; user = void 0;
setUser = function(u) { model = void 0;
return user = u;
setModel = function(m) {
model = m;
user = model.at('_user');
return setupNotifications();
}; };
setupNotifications = function() {
var statsNotification;
statsNotification = function(html, type) { statsNotification = function(html, type) {
if (user.get('stats.lvl') === 0) { if (user.get('stats.lvl') === 0) {
return; return;
@@ -27,6 +33,31 @@ statsNotification = function(html, type) {
stackup_spacing: 10 stackup_spacing: 10
}); });
}; };
user.on('set', 'stats.hp', function(captures, args, out, isLocal, passed) {
var num, rounded;
num = captures - args;
rounded = Math.abs(num.toFixed(1));
if (num < 0) {
return statsNotification("<i class='icon-heart'></i>HP -" + rounded, 'error');
}
});
user.on('set', 'stats.money', function(captures, args, out, isLocal, passed) {
var num, rounded;
num = captures - args;
rounded = Math.abs(num.toFixed(1));
if (num < 0) {
return statsNotification("<i class='icon-star'></i>GP -" + rounded, 'success');
} else if (num > 0) {
num = Math.abs(num);
return statsNotification("<i class='icon-star'></i>Exp,GP +" + rounded, 'success');
}
});
return user.on('set', 'stats.lvl', function(captures, args, out, isLocal, passed) {
if (captures > args) {
return statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info');
}
});
};
expModifier = function(value) { expModifier = function(value) {
var dmg, modified; var dmg, modified;
@@ -64,7 +95,6 @@ updateStats = function(stats) {
stats.exp -= tnl; stats.exp -= tnl;
user.set('stats.lvl', user.get('stats.lvl') + 1); user.set('stats.lvl', user.get('stats.lvl') + 1);
user.set('stats.hp', 50); user.set('stats.hp', 50);
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info');
} }
if (!user.get('items.itemsEnabled') && stats.exp >= 15) { if (!user.get('items.itemsEnabled') && stats.exp >= 15) {
user.set('items.itemsEnabled', true); user.set('items.itemsEnabled', true);
@@ -138,10 +168,8 @@ score = function(spec) {
if (type === 'reward') { if (type === 'reward') {
money -= task.get('value'); money -= task.get('value');
num = parseFloat(task.get('value')).toFixed(2); num = parseFloat(task.get('value')).toFixed(2);
statsNotification("<i class='icon-star'></i>GP -" + num, 'success');
if (money < 0) { if (money < 0) {
hp += money; hp += money;
statsNotification("<i class='icon-heart'></i>HP " + (money.toFixed(2)), 'error');
money = 0; money = 0;
} }
} }
@@ -149,15 +177,9 @@ score = function(spec) {
modified = expModifier(delta); modified = expModifier(delta);
exp += modified; exp += modified;
money += modified; money += modified;
if (modified > 0) {
statsNotification("<i class='icon-star'></i>Exp,GP +" + (modified.toFixed(2)), 'success');
} else {
statsNotification("<i class='icon-star'></i>Exp,GP " + (modified.toFixed(2)), 'warning');
}
} else if (type !== 'reward' && type !== 'todo') { } else if (type !== 'reward' && type !== 'todo') {
modified = hpModifier(delta); modified = hpModifier(delta);
hp += modified; hp += modified;
statsNotification("<i class='icon-heart'></i>HP " + (modified.toFixed(2)), 'error');
} }
updateStats({ updateStats({
hp: hp, hp: hp,
@@ -248,7 +270,7 @@ tally = function(momentDate) {
module.exports = { module.exports = {
MODIFIER: MODIFIER, MODIFIER: MODIFIER,
setUser: setUser, setModel: setModel,
score: score, score: score,
cron: cron cron: cron
}; };

View File

@@ -12,7 +12,7 @@ module.exports = function(expressApp, root, derby) {
if (err || !user.get()) { if (err || !user.get()) {
return; return;
} }
scoring.setUser(user); scoring.setModel(model);
return scoring.score({ return scoring.score({
direction: 'up' direction: 'up'
}); });
@@ -27,7 +27,7 @@ module.exports = function(expressApp, root, derby) {
if (err || !user.get()) { if (err || !user.get()) {
return; return;
} }
scoring.setUser(user); scoring.setModel(model);
return scoring.score({ return scoring.score({
direction: 'down' direction: 'down'
}); });

View File

@@ -51,7 +51,7 @@ get '/:uidParam?', (page, model, {uidParam}, next) ->
# ========== CONTROLLER FUNCTIONS ========== # ========== CONTROLLER FUNCTIONS ==========
ready (model) -> ready (model) ->
scoring.setUser(model.at('_user')) scoring.setModel(model)
$('[rel=tooltip]').tooltip() $('[rel=tooltip]').tooltip()
$('[rel=popover]').popover() $('[rel=popover]').popover()

View File

@@ -2,16 +2,18 @@ content = require('./content')
helpers = require('./helpers') helpers = require('./helpers')
MODIFIER = .03 # each new level, armor, weapon add 3% modifier (this number may change) MODIFIER = .03 # each new level, armor, weapon add 3% modifier (this number may change)
user = undefined user = undefined
model = undefined
# This is required by all the functions, make sure it's set before anythign else is called # This is required by all the functions, make sure it's set before anythign else is called
setUser = (u) -> setModel = (m) ->
user = u model = m
user = model.at('_user')
setupNotifications()
# FIXME move to index.coffee as module.on('set','*') setupNotifications = ->
statsNotification = (html, type) -> statsNotification = (html, type) ->
#don't show notifications if user dead #don't show notifications if user dead
return if user.get('stats.lvl') == 0 return if user.get('stats.lvl') == 0
$.bootstrapGrowl html, { $.bootstrapGrowl html, {
type: type # (null, 'info', 'error', 'success') type: type # (null, 'info', 'error', 'success')
top_offset: 20 top_offset: 20
@@ -22,6 +24,29 @@ statsNotification = (html, type) ->
stackup_spacing: 10 # spacing between consecutive stacecked growls. stackup_spacing: 10 # spacing between consecutive stacecked growls.
} }
# Setup listeners which trigger notifications
user.on 'set', 'stats.hp', (captures, args, out, isLocal, passed) ->
num = captures - args
rounded = Math.abs(num.toFixed(1))
if num < 0
statsNotification "<i class='icon-heart'></i>HP -#{rounded}", 'error' # lost hp from purchase
user.on 'set', 'stats.money', (captures, args, out, isLocal, passed) ->
num = captures - args
rounded = Math.abs(num.toFixed(1))
# made purchase
if num < 0
# FIXME use 'warning' when unchecking an accidently completed daily/todo, and notify of exp too
statsNotification "<i class='icon-star'></i>GP -#{rounded}", 'success'
# gained money (and thereby exp)
else if num > 0
num = Math.abs(num)
statsNotification "<i class='icon-star'></i>Exp,GP +#{rounded}", 'success'
user.on 'set', 'stats.lvl', (captures, args, out, isLocal, passed) ->
if captures > args
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info')
# Calculates Exp modification based on weapon & lvl # Calculates Exp modification based on weapon & lvl
expModifier = (value) -> expModifier = (value) ->
dmg = user.get('items.weapon') * MODIFIER # each new weapon increases exp gain dmg = user.get('items.weapon') * MODIFIER # each new weapon increases exp gain
@@ -57,7 +82,6 @@ updateStats = (stats) ->
stats.exp -= tnl stats.exp -= tnl
user.set 'stats.lvl', user.get('stats.lvl') + 1 user.set 'stats.lvl', user.get('stats.lvl') + 1
user.set 'stats.hp', 50 user.set 'stats.hp', 50
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info')
if !user.get('items.itemsEnabled') and stats.exp >=15 if !user.get('items.itemsEnabled') and stats.exp >=15
user.set 'items.itemsEnabled', true user.set 'items.itemsEnabled', true
$('ul.items').popover $('ul.items').popover
@@ -87,11 +111,9 @@ score = (spec = {task:null, direction:null, cron:null}) ->
modified = expModifier(1) modified = expModifier(1)
money += modified money += modified
exp += modified exp += modified
# statsNotification "<i class='icon-star'></i>Exp,GP +#{modified.toFixed(2)}", 'success'
else else
modified = hpModifier(1) modified = hpModifier(1)
hp -= modified hp -= modified
# statsNotification "<i class='icon-heart'></i>HP #{modified.toFixed(2)}", 'error'
updateStats({hp: hp, exp: exp, money: money}) updateStats({hp: hp, exp: exp, money: money})
return return
@@ -123,11 +145,9 @@ score = (spec = {task:null, direction:null, cron:null}) ->
# purchase item # purchase item
money -= task.get('value') money -= task.get('value')
num = parseFloat(task.get('value')).toFixed(2) num = parseFloat(task.get('value')).toFixed(2)
statsNotification "<i class='icon-star'></i>GP -#{num}", 'success'
# if too expensive, reduce health & zero money # if too expensive, reduce health & zero money
if money < 0 if money < 0
hp += money# hp - money difference hp += money# hp - money difference
statsNotification "<i class='icon-heart'></i>HP #{money.toFixed(2)}", 'error'
money = 0 money = 0
# Add points to exp & money if positive delta # Add points to exp & money if positive delta
@@ -136,16 +156,10 @@ score = (spec = {task:null, direction:null, cron:null}) ->
modified = expModifier(delta) modified = expModifier(delta)
exp += modified exp += modified
money += modified money += modified
if modified > 0
statsNotification "<i class='icon-star'></i>Exp,GP +#{modified.toFixed(2)}", 'success'
else
# unchecking an accidently completed daily/todo
statsNotification "<i class='icon-star'></i>Exp,GP #{modified.toFixed(2)}", 'warning'
# Deduct from health (rewards case handled above) # Deduct from health (rewards case handled above)
else unless type in ['reward', 'todo'] else unless type in ['reward', 'todo']
modified = hpModifier(delta) modified = hpModifier(delta)
hp += modified hp += modified
statsNotification "<i class='icon-heart'></i>HP #{modified.toFixed(2)}", 'error'
updateStats({hp: hp, exp: exp, money: money}) updateStats({hp: hp, exp: exp, money: money})
@@ -198,7 +212,7 @@ tally = (momentDate) ->
module.exports = { module.exports = {
MODIFIER: MODIFIER MODIFIER: MODIFIER
setUser: setUser setModel: setModel
score: score score: score
cron: cron cron: cron
} }

View File

@@ -7,7 +7,7 @@ module.exports = (expressApp, root, derby) ->
model = req.getModel() model = req.getModel()
model.fetch "users.#{req.params.uid}", (err, user) -> model.fetch "users.#{req.params.uid}", (err, user) ->
return if err || !user.get() return if err || !user.get()
scoring.setUser(user) scoring.setModel(model)
scoring.score({direction:'up'}) scoring.score({direction:'up'})
res.send(200) res.send(200)
@@ -16,7 +16,7 @@ module.exports = (expressApp, root, derby) ->
model = req.getModel() model = req.getModel()
model.fetch "users.#{req.params.uid}", (err, user) -> model.fetch "users.#{req.params.uid}", (err, user) ->
return if err || !user.get() return if err || !user.get()
scoring.setUser(user) scoring.setModel(model)
scoring.score({direction:'down'}) scoring.score({direction:'down'})
res.send(200) res.send(200)