mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
use model events to handle stats notifications. not perfect, need to
fix handling of "undo exp/gp" operation
This commit is contained in:
@@ -53,7 +53,7 @@ get('/:uidParam?', function(page, model, _arg, next) {
|
||||
|
||||
ready(function(model) {
|
||||
var setupSortable, step, tour, type, _i, _j, _len, _len1, _ref1, _ref2;
|
||||
scoring.setUser(model.at('_user'));
|
||||
scoring.setModel(model);
|
||||
$('[rel=tooltip]').tooltip();
|
||||
$('[rel=popover]').popover();
|
||||
model.on('set', '*', function() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// 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');
|
||||
|
||||
@@ -9,10 +9,16 @@ MODIFIER = .03;
|
||||
|
||||
user = void 0;
|
||||
|
||||
setUser = function(u) {
|
||||
return user = u;
|
||||
model = void 0;
|
||||
|
||||
setModel = function(m) {
|
||||
model = m;
|
||||
user = model.at('_user');
|
||||
return setupNotifications();
|
||||
};
|
||||
|
||||
setupNotifications = function() {
|
||||
var statsNotification;
|
||||
statsNotification = function(html, type) {
|
||||
if (user.get('stats.lvl') === 0) {
|
||||
return;
|
||||
@@ -27,6 +33,31 @@ statsNotification = function(html, type) {
|
||||
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) {
|
||||
var dmg, modified;
|
||||
@@ -64,7 +95,6 @@ updateStats = function(stats) {
|
||||
stats.exp -= tnl;
|
||||
user.set('stats.lvl', user.get('stats.lvl') + 1);
|
||||
user.set('stats.hp', 50);
|
||||
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info');
|
||||
}
|
||||
if (!user.get('items.itemsEnabled') && stats.exp >= 15) {
|
||||
user.set('items.itemsEnabled', true);
|
||||
@@ -138,10 +168,8 @@ score = function(spec) {
|
||||
if (type === 'reward') {
|
||||
money -= task.get('value');
|
||||
num = parseFloat(task.get('value')).toFixed(2);
|
||||
statsNotification("<i class='icon-star'></i>GP -" + num, 'success');
|
||||
if (money < 0) {
|
||||
hp += money;
|
||||
statsNotification("<i class='icon-heart'></i>HP " + (money.toFixed(2)), 'error');
|
||||
money = 0;
|
||||
}
|
||||
}
|
||||
@@ -149,15 +177,9 @@ score = function(spec) {
|
||||
modified = expModifier(delta);
|
||||
exp += 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') {
|
||||
modified = hpModifier(delta);
|
||||
hp += modified;
|
||||
statsNotification("<i class='icon-heart'></i>HP " + (modified.toFixed(2)), 'error');
|
||||
}
|
||||
updateStats({
|
||||
hp: hp,
|
||||
@@ -248,7 +270,7 @@ tally = function(momentDate) {
|
||||
|
||||
module.exports = {
|
||||
MODIFIER: MODIFIER,
|
||||
setUser: setUser,
|
||||
setModel: setModel,
|
||||
score: score,
|
||||
cron: cron
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ module.exports = function(expressApp, root, derby) {
|
||||
if (err || !user.get()) {
|
||||
return;
|
||||
}
|
||||
scoring.setUser(user);
|
||||
scoring.setModel(model);
|
||||
return scoring.score({
|
||||
direction: 'up'
|
||||
});
|
||||
@@ -27,7 +27,7 @@ module.exports = function(expressApp, root, derby) {
|
||||
if (err || !user.get()) {
|
||||
return;
|
||||
}
|
||||
scoring.setUser(user);
|
||||
scoring.setModel(model);
|
||||
return scoring.score({
|
||||
direction: 'down'
|
||||
});
|
||||
|
||||
@@ -51,7 +51,7 @@ get '/:uidParam?', (page, model, {uidParam}, next) ->
|
||||
# ========== CONTROLLER FUNCTIONS ==========
|
||||
|
||||
ready (model) ->
|
||||
scoring.setUser(model.at('_user'))
|
||||
scoring.setModel(model)
|
||||
|
||||
$('[rel=tooltip]').tooltip()
|
||||
$('[rel=popover]').popover()
|
||||
|
||||
@@ -2,16 +2,18 @@ content = require('./content')
|
||||
helpers = require('./helpers')
|
||||
MODIFIER = .03 # each new level, armor, weapon add 3% modifier (this number may change)
|
||||
user = undefined
|
||||
model = undefined
|
||||
|
||||
# This is required by all the functions, make sure it's set before anythign else is called
|
||||
setUser = (u) ->
|
||||
user = u
|
||||
setModel = (m) ->
|
||||
model = m
|
||||
user = model.at('_user')
|
||||
setupNotifications()
|
||||
|
||||
# FIXME move to index.coffee as module.on('set','*')
|
||||
setupNotifications = ->
|
||||
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
|
||||
@@ -22,6 +24,29 @@ statsNotification = (html, type) ->
|
||||
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
|
||||
expModifier = (value) ->
|
||||
dmg = user.get('items.weapon') * MODIFIER # each new weapon increases exp gain
|
||||
@@ -57,7 +82,6 @@ updateStats = (stats) ->
|
||||
stats.exp -= tnl
|
||||
user.set 'stats.lvl', user.get('stats.lvl') + 1
|
||||
user.set 'stats.hp', 50
|
||||
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info')
|
||||
if !user.get('items.itemsEnabled') and stats.exp >=15
|
||||
user.set 'items.itemsEnabled', true
|
||||
$('ul.items').popover
|
||||
@@ -87,11 +111,9 @@ score = (spec = {task:null, direction:null, cron:null}) ->
|
||||
modified = expModifier(1)
|
||||
money += modified
|
||||
exp += modified
|
||||
# statsNotification "<i class='icon-star'></i>Exp,GP +#{modified.toFixed(2)}", 'success'
|
||||
else
|
||||
modified = hpModifier(1)
|
||||
hp -= modified
|
||||
# statsNotification "<i class='icon-heart'></i>HP #{modified.toFixed(2)}", 'error'
|
||||
updateStats({hp: hp, exp: exp, money: money})
|
||||
return
|
||||
|
||||
@@ -123,11 +145,9 @@ score = (spec = {task:null, direction:null, cron:null}) ->
|
||||
# purchase item
|
||||
money -= task.get('value')
|
||||
num = parseFloat(task.get('value')).toFixed(2)
|
||||
statsNotification "<i class='icon-star'></i>GP -#{num}", 'success'
|
||||
# if too expensive, reduce health & zero money
|
||||
if money < 0
|
||||
hp += money# hp - money difference
|
||||
statsNotification "<i class='icon-heart'></i>HP #{money.toFixed(2)}", 'error'
|
||||
money = 0
|
||||
|
||||
# Add points to exp & money if positive delta
|
||||
@@ -136,16 +156,10 @@ score = (spec = {task:null, direction:null, cron:null}) ->
|
||||
modified = expModifier(delta)
|
||||
exp += 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)
|
||||
else unless type in ['reward', 'todo']
|
||||
modified = hpModifier(delta)
|
||||
hp += modified
|
||||
statsNotification "<i class='icon-heart'></i>HP #{modified.toFixed(2)}", 'error'
|
||||
|
||||
updateStats({hp: hp, exp: exp, money: money})
|
||||
|
||||
@@ -198,7 +212,7 @@ tally = (momentDate) ->
|
||||
|
||||
module.exports = {
|
||||
MODIFIER: MODIFIER
|
||||
setUser: setUser
|
||||
setModel: setModel
|
||||
score: score
|
||||
cron: cron
|
||||
}
|
||||
@@ -7,7 +7,7 @@ module.exports = (expressApp, root, derby) ->
|
||||
model = req.getModel()
|
||||
model.fetch "users.#{req.params.uid}", (err, user) ->
|
||||
return if err || !user.get()
|
||||
scoring.setUser(user)
|
||||
scoring.setModel(model)
|
||||
scoring.score({direction:'up'})
|
||||
res.send(200)
|
||||
|
||||
@@ -16,7 +16,7 @@ module.exports = (expressApp, root, derby) ->
|
||||
model = req.getModel()
|
||||
model.fetch "users.#{req.params.uid}", (err, user) ->
|
||||
return if err || !user.get()
|
||||
scoring.setUser(user)
|
||||
scoring.setModel(model)
|
||||
scoring.score({direction:'down'})
|
||||
res.send(200)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user