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) {
|
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() {
|
||||||
|
|||||||
@@ -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,22 +9,53 @@ 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();
|
||||||
};
|
};
|
||||||
|
|
||||||
statsNotification = function(html, type) {
|
setupNotifications = function() {
|
||||||
if (user.get('stats.lvl') === 0) {
|
var statsNotification;
|
||||||
return;
|
statsNotification = function(html, type) {
|
||||||
}
|
if (user.get('stats.lvl') === 0) {
|
||||||
return $.bootstrapGrowl(html, {
|
return;
|
||||||
type: type,
|
}
|
||||||
top_offset: 20,
|
return $.bootstrapGrowl(html, {
|
||||||
align: 'right',
|
type: type,
|
||||||
width: 250,
|
top_offset: 20,
|
||||||
delay: 3000,
|
align: 'right',
|
||||||
allow_dismiss: true,
|
width: 250,
|
||||||
stackup_spacing: 10
|
delay: 3000,
|
||||||
|
allow_dismiss: true,
|
||||||
|
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');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -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
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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'
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -2,25 +2,50 @@ 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, {
|
||||||
|
type: type # (null, 'info', 'error', 'success')
|
||||||
|
top_offset: 20
|
||||||
|
align: 'right' # ('left', 'right', or 'center')
|
||||||
|
width: 250 # (integer, or 'auto')
|
||||||
|
delay: 3000
|
||||||
|
allow_dismiss: true
|
||||||
|
stackup_spacing: 10 # spacing between consecutive stacecked growls.
|
||||||
|
}
|
||||||
|
|
||||||
$.bootstrapGrowl html, {
|
# Setup listeners which trigger notifications
|
||||||
type: type # (null, 'info', 'error', 'success')
|
user.on 'set', 'stats.hp', (captures, args, out, isLocal, passed) ->
|
||||||
top_offset: 20
|
num = captures - args
|
||||||
align: 'right' # ('left', 'right', or 'center')
|
rounded = Math.abs(num.toFixed(1))
|
||||||
width: 250 # (integer, or 'auto')
|
if num < 0
|
||||||
delay: 3000
|
statsNotification "<i class='icon-heart'></i>HP -#{rounded}", 'error' # lost hp from purchase
|
||||||
allow_dismiss: true
|
|
||||||
stackup_spacing: 10 # spacing between consecutive stacecked growls.
|
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) ->
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user