send growl notifications for points gained/lossed & level up

Conflicts:

	src/app/scoring.coffee
This commit is contained in:
Tyler Renelle 2012-09-01 13:59:34 -04:00
parent ec3ac04f4c
commit 68079f5bfc
4 changed files with 67 additions and 24 deletions

View file

@ -1,8 +1,20 @@
// Generated by CoffeeScript 1.3.3
var content, expModifier, hpModifier, score, updateStats;
var content, expModifier, hpModifier, score, statsNotification, updateStats;
content = require('./content');
statsNotification = function(html, type, number) {
return $.bootstrapGrowl(html, {
type: type,
top_offset: 20,
align: 'center',
width: 250,
delay: 4000,
allow_dismiss: true,
stackup_spacing: 10
});
};
expModifier = function(user, value) {
var dmg, modified;
dmg = user.get('items.weapon') * .03;
@ -33,8 +45,9 @@ updateStats = function(user, stats) {
if (stats.exp >= tnl) {
stats.exp -= tnl;
user.set('stats.lvl', user.get('stats.lvl') + 1);
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info');
}
if (!user.get('items.itemsEnabled') && stats.exp >= 50) {
if (!user.get('items.itemsEnabled') && stats.exp >= 15) {
user.set('items.itemsEnabled', true);
$('ul.items').popover({
title: content.items.unlockedMessage.title,
@ -56,7 +69,7 @@ updateStats = function(user, stats) {
};
module.exports.score = score = function(spec) {
var adjustvalue, cron, delta, direction, exp, hp, lvl, modified, money, sign, task, type, user, value, _ref, _ref1, _ref2;
var adjustvalue, cron, delta, diff, direction, exp, hp, lvl, modified, money, num, sign, task, type, user, value, _ref, _ref1, _ref2;
if (spec == null) {
spec = {
user: null,
@ -69,14 +82,14 @@ module.exports.score = score = function(spec) {
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") {
money += 1;
exp += expModifier(user, 1);
modified = expModifier(user, 1);
money += modified;
exp += modified;
statsNotification("Exp,GP +" + (modified.toFixed(2)), 'success');
} else {
modified = hpModifier(user, 1);
hp -= modified;
console.log({
modified: modified
});
statsNotification("HP " + (modified.toFixed(2)), 'error');
}
updateStats(user, {
hp: hp,
@ -108,16 +121,24 @@ module.exports.score = score = function(spec) {
_ref2 = [user.get('stats.money'), user.get('stats.hp'), user.get('stats.exp'), user.get('stats.lvl')], money = _ref2[0], hp = _ref2[1], exp = _ref2[2], lvl = _ref2[3];
if (type === 'reward') {
money -= task.get('value');
num = task.get('value').toFixed(2);
statsNotification("GP -" + num, 'success');
if (money < 0) {
hp += money;
diff = hp + money;
hp = diff;
statsNotification("HP -" + (diff.toFixed(2)), 'error');
money = 0;
}
}
if ((delta > 0 || (type === 'daily' || type === 'todo')) && !cron) {
exp += expModifier(user, delta);
money += delta;
modified = expModifier(user, delta);
exp += modified;
money += modified;
statsNotification("Exp,GP +" + (modified.toFixed(2)), 'success');
} else if (type !== 'reward' && type !== 'todo') {
hp += hpModifier(user, delta);
modified = hpModifier(user, delta);
hp += modified;
statsNotification("HP " + (modified.toFixed(2)), 'error');
}
updateStats(user, {
hp: hp,

View file

@ -0,0 +1,2 @@
/* https://github.com/ifightcrime/bootstrap-growl */
(function(e){e.bootstrapGrowl=function(t,n){var n=e.extend({},e.bootstrapGrowl.default_options,n),r=e("<div>");r.attr("class","bootstrap-growl alert"),n.type&&r.addClass("alert-"+n.type),n.allow_dismiss&&r.append('<a class="close" data-dismiss="alert" href="#">&times;</a>'),r.append(t);var i=n.top_offset,s=e(".bootstrap-growl","body");e.each(s,function(){i=i+e(this).outerHeight()+n.stackup_spacing}),r.css({position:"absolute",top:i+"px",border:"1px solid "+r.css("color"),margin:0,"z-index":"999",display:"none"}),n.width!=="auto"&&r.css("width",n.width+"px"),e("body").append(r);switch(n.align){case"center":r.css({left:"50%","margin-left":"-"+r.outerWidth()/2+"px"});break;case"left":r.css("left","20px");break;default:r.css("right","20px")}r.fadeIn().delay(n.delay).fadeOut("slow",function(){e(this).remove()})},e.bootstrapGrowl.default_options={type:null,top_offset:20,align:"right",width:250,delay:4e3,allow_dismiss:!0,stackup_spacing:10}})(jQuery);

View file

@ -1,5 +1,16 @@
content = require('./content')
statsNotification = (html, type, number) ->
$.bootstrapGrowl html, {
type: type # (null, 'info', 'error', 'success')
top_offset: 20
align: 'center' # ('left', 'right', or 'center')
width: 250 # (integer, or 'auto')
delay: 4000
allow_dismiss: true
stackup_spacing: 10 # spacing between consecutive stacecked growls.
}
# Calculates Exp modification based on weapon & lvl
expModifier = (user, value) ->
dmg = user.get('items.weapon') * .03 # each new weapon adds an additional 3% experience
@ -29,7 +40,8 @@ updateStats = (user, stats) ->
if stats.exp >= tnl
stats.exp -= tnl
user.set 'stats.lvl', user.get('stats.lvl') + 1
if !user.get('items.itemsEnabled') and stats.exp >=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
title: content.items.unlockedMessage.title
@ -56,12 +68,14 @@ module.exports.score = score = (spec = {user:null, task:null, direction:null, cr
if !task
[money, hp, exp] = [user.get('stats.money'), user.get('stats.hp'), user.get('stats.exp')]
if (direction == "up")
money += 1
exp += expModifier(user, 1)
modified = expModifier(user, 1)
money += modified
exp += modified
statsNotification "Exp,GP +#{modified.toFixed(2)}", 'success'
else
modified = hpModifier(user, 1)
hp -= modified
console.log {modified:modified}
statsNotification "HP #{modified.toFixed(2)}", 'error'
updateStats(user, {hp: hp, exp: exp, money: money})
return
@ -92,19 +106,27 @@ module.exports.score = score = (spec = {user:null, task:null, direction:null, cr
if type == 'reward'
# purchase item
money -= task.get('value')
num = task.get('value').toFixed(2)
statsNotification "GP -#{num}", 'success'
# if too expensive, reduce health & zero money
if money < 0
hp += money # hp - money difference
diff = hp + money # hp - money difference
hp = diff
statsNotification "HP -#{diff.toFixed(2)}", 'error'
money = 0
# 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
exp += expModifier(user, delta)
money += delta
modified = expModifier(user, delta)
exp += modified
money += modified
statsNotification "Exp,GP +#{modified.toFixed(2)}", 'success'
# Deduct from health (rewards case handled above)
else unless type in ['reward', 'todo']
hp += hpModifier(user, delta)
modified = hpModifier(user, delta)
hp += modified
statsNotification "HP #{modified.toFixed(2)}", 'error'
updateStats(user, {hp: hp, exp: exp, money: money})

View file

@ -54,10 +54,7 @@
</td>
</tr>
</table>
<!--<% else %>
<h1>HabitRPG</h1>
<p>A habit tracker app which treats your goals like a Role Playing Game. <a href="https://github.com/lefnire/habitrpg#habitrpg">More Information</a></p>
<% end %>-->
</div>
<Body:>
@ -296,6 +293,7 @@
<script src=/js/bootstrap.min.js></script><!-- http://twitter.github.com/bootstrap/assets/js/bootstrap.min.js -->
<script src=/js/jquery.cookie.js></script><!-- https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js -->
<script src=/js/bootstrap-tour.js></script><!-- https://raw.github.com/pushly/bootstrap-tour/master/bootstrap-tour.js -->
<script src=/js/jquery.bootstrap-growl.min.js></script>
<!-- Scripts not needed right away (google charts) or entirely optional (analytics) -->
<Tail:>