[#1394] move notifications from controller to service, lots of notification

fixes, put streak notification back in
This commit is contained in:
Tyler Renelle 2013-09-17 11:43:05 -04:00
parent 1d3728ea77
commit 0fc413f3b7
3 changed files with 114 additions and 227 deletions

View file

@ -1,120 +1,56 @@
'use strict'; 'use strict';
habitrpg.controller('NotificationCtrl', habitrpg.controller('NotificationCtrl',
['$scope', '$rootScope', 'User', 'Guide', function ($scope, $rootScope, User, Guide) { ['$scope', '$rootScope', 'User', 'Guide', 'Notification', function ($scope, $rootScope, User, Guide, Notification) {
Guide.initTour(); Guide.initTour();
function growlNotification(html, type) {
$.bootstrapGrowl(html, {
ele: '#notification-area',
type: type, //(null, 'info', 'error', 'success', 'gp', 'xp', 'hp', 'lvl','death')
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.
});
};
/*
Sets up "+1 Exp", "Level Up", etc notifications
*/
function setupGrowlNotifications() {
function statsNotification(html, type) {
// don't show notifications if user dead
if (User.user.stats.lvl == 0) return;
growlNotification(html, type);
};
/**
Show "+ 5 {gold_coin} 3 {silver_coin}"
*/
function showCoins(money) {
var absolute, gold, silver;
absolute = Math.abs(money);
gold = Math.floor(absolute);
silver = Math.floor((absolute - gold) * 100);
if (gold && silver > 0) {
return "" + gold + " <i class='icon-gold'></i> " + silver + " <i class='icon-silver'></i>";
} else if (gold > 0) {
return "" + gold + " <i class='icon-gold'></i>";
} else if (silver > 0) {
return "" + silver + " <i class='icon-silver'></i>";
}
};
$rootScope.$watch('user.stats.hp', function(after, before) { $rootScope.$watch('user.stats.hp', function(after, before) {
if (after == before) return; if (after == before) return;
var num = after - before; Notification.hp(after - before, 'hp');
var rounded = Math.abs(num.toFixed(1));
if (num < 0) {
//lost hp from purchase
statsNotification("<i class='icon-heart'></i> - " + rounded + " HP", 'hp');
} else if (num > 0) {
// gained hp from potion/level?
statsNotification("<i class='icon-heart'></i> + " + rounded + " HP", 'hp');
}
}); });
$rootScope.$watch('user.stats.exp', function(after, before) { $rootScope.$watch('user.stats.exp', function(after, before) {
if (after == before) return; if (after == before) return;
var num = after - before; Notification.exp(after - before);
var rounded = Math.abs(num.toFixed(1));
// TODO fix hackey negative notification supress
if (num < 0 && num > -50) {
statsNotification("<i class='icon-star'></i> - " + rounded + " XP", 'xp');
} else if (num > 0) {
statsNotification("<i class='icon-star'></i> + " + rounded + " XP", 'xp');
}
}); });
$rootScope.$watch('user.stats.gp', function(after, before) { $rootScope.$watch('user.stats.gp', function(after, before) {
if (after == before) return; if (after == before) return;
var bonus, money, sign; var bonus, money;
money = after - before; var money = after - before;
Notification.gp(money);
//why is this happening? gotta find where stats.gp is being set from (-)habit //Append Bonus
//if (!money) { bonus = User.user._tmp.streakBonus;
// return;
//}
sign = money < 0 ? '-' : '+';
statsNotification("" + sign + " " + (showCoins(money)), 'gp');
//Append Bonus TODO
//bonus = model.get('_streakBonus');
if ((money > 0) && !!bonus) { if ((money > 0) && !!bonus) {
if (bonus < 0.01) { if (bonus < 0.01) {
bonus = 0.01; bonus = 0.01;
} }
statsNotification("+ " + (showCoins(bonus)) + " Streak Bonus!"); Notification.text("+ " + Notification.coins(bonus) + " Streak Bonus!");
//model.del('_streakBonus'); delete User.user._tmp.streakBonus;
} }
}); });
// FIXME // FIXME: this isn't working for some reason
// user.on('set', 'items.*', function(item, after, before) { /*_.each(['weapon', 'head', 'chest', 'shield'], function(watched){
// if ((item === 'armor' || item === 'weapon' || item === 'shield' || item === 'head') && parseInt(after) < parseInt(before)) { $rootScope.$watch('user.items.' + watched, function(before, after){
// //don't want to day "lost a head" if (after == before) return;
// if (item === 'head') { if (+after < +before) {
// item = 'helm'; Notification.death();
// } //don't want to day "lost a head"
// return statsNotification("<i class='icon-death'></i> Respawn!", "death"); if (watched === 'head') watched = 'helm';
// } Notification.text('Lost GP, 1 LVL, ' + watched);
// }); }
})
});*/
$rootScope.$watch('user.stats.lvl', function(after, before) { $rootScope.$watch('user.stats.lvl', function(after, before) {
if (after == before) return; if (after == before) return;
if (after > before) { if (after > before) {
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'lvl'); Notification.lvl();
} }
}); });
};
setupGrowlNotifications();
} }
]); ]);

View file

@ -32,43 +32,9 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', '
} }
]; ];
$scope.score = function(task, direction) { $scope.score = function(task, direction) {
/*save current stats to compute the difference after scoring. if (task.type === "reward" && User.user.stats.gp < task.value)
*/ return Notification.text('Not enough GP.');
var oldStats, statsDiff;
statsDiff = {};
oldStats = _.clone(User.user.stats);
Algos.score(User.user, task, direction); Algos.score(User.user, task, direction);
/*compute the stats change.
*/
_.each(oldStats, function(value, key) {
var newValue;
newValue = User.user.stats[key];
if (newValue !== value) {
statsDiff[key] = newValue - value;
}
});
/*notify user if there are changes in stats.
*/
if (Object.keys(statsDiff).length > 0) {
Notification.push({
type: "stats",
stats: statsDiff
});
}
if (task.type === "reward" && _.isEmpty(statsDiff)) {
Notification.push({
type: "text",
text: "Not enough GP."
});
}
User.log({
op: "score",
data: task,
dir: direction
});
}; };
$scope.addTask = function(list) { $scope.addTask = function(list) {
@ -129,26 +95,15 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', '
$scope.itemStore = sorted; $scope.itemStore = sorted;
}); });
$scope.buy = function(type) { $scope.buy = function(type) {
var hasEnough; var hasEnough = window.habitrpgShared.items.buyItem(User.user, type);
hasEnough = window.habitrpgShared.items.buyItem(User.user, type);
if (hasEnough) { if (hasEnough) {
User.log({ User.log({op: "buy",type: type});
op: "buy", Notification.text("Item purchased.");
type: type
});
Notification.push({
type: "text",
text: "Item bought!"
});
} else { } else {
Notification.push({ Notification.text("Not enough GP.");
type: "text",
text: "Not enough GP."
});
} }
}; };
$scope.clearCompleted = function() { $scope.clearCompleted = function() {
User.user.todos = _.reject(User.user.todos, {completed:true}); User.user.todos = _.reject(User.user.todos, {completed:true});
User.log({op: 'clear-completed'}); User.log({op: 'clear-completed'});

View file

@ -1,75 +1,71 @@
/**
Set up "+1 Exp", "Level Up", etc notifications
*/
angular.module("notificationServices", [])
.factory("Notification", ['User', function(User) {
function growl(html, type) {
$.bootstrapGrowl(html, {
ele: '#notification-area',
type: type, //(null, 'info', 'error', 'success', 'gp', 'xp', 'hp', 'lvl','death')
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.
});
};
angular.module("notificationServices", []).factory("Notification", function() { /**
var active, data, fixMe, timer; Show "+ 5 {gold_coin} 3 {silver_coin}"
fixMe = { */
push: function() {}, function coins(money) {
get: function() {}, var absolute, gold, silver;
animate: function() {}, absolute = Math.abs(money);
clearTimer: function() {}, gold = Math.floor(absolute);
init: function() {} silver = Math.floor((absolute - gold) * 100);
if (gold && silver > 0) {
return "" + gold + " <i class='icon-gold'></i> " + silver + " <i class='icon-silver'></i>";
} else if (gold > 0) {
return "" + gold + " <i class='icon-gold'></i>";
} else if (silver > 0) {
return "" + silver + " <i class='icon-silver'></i>";
}
}; };
return fixMe;
data = { var sign = function(number){
message: "" return number?number<0?'-':'+':'+';
}; }
active = false;
timer = null; var round = function(number){
return Math.abs(number.toFixed(1));
}
return { return {
hide: function() { coins: coins,
$("#notification").fadeOut(function() { hp: function(val) {
$("#notification").css("webkit-transform", "none"); // don't show notifications if user dead
$("#notification").css("top", "-63px"); if (User.user.stats.lvl == 0) return;
$("#notification").css("left", "0px"); growl("<i class='icon-heart'></i> " + sign(val) + " " + round(val) + " HP", 'hp');
setTimeout((function() {
$("#notification").show();
}), 190);
});
active = false;
timer = null;
}, },
animate: function() { exp: function(val) {
if (timer) { if (User.user.stats.lvl == 0) return;
clearTimeout(timer); if (val < -50) return; // don't show when they level up (resetting their exp)
timer = setTimeout(this.hide, 2000); growl("<i class='icon-heart'></i> " + sign(val) + " " + round(val) + " HP", 'xp');
}
if (active === false) {
active = true;
$("#notification").transition({
y: 63,
x: 0
});
timer = setTimeout(this.hide, 2000);
}
}, },
push: function(message) { gp: function(val) {
data.message = ""; if (User.user.stats.lvl == 0) return;
switch (message.type) { growl(sign(val) + " " + coins(val), 'gp');
case "stats":
if ((message.stats.exp != null) && (message.stats.gp != null)) {
data.message = "Experience: " + message.stats.exp + "<br />GP: " + message.stats.gp.toFixed(2);
}
if (message.stats.hp) {
data.message = "HP: " + message.stats.hp.toFixed(2);
}
if (message.stats.gp && !(message.stats.exp != null)) {
data.message = "<br />GP: " + message.stats.gp.toFixed(2);
}
break;
case "text":
data.message = message.text;
}
this.animate();
}, },
get: function() { text: function(val){
return data; growl(val);
}, },
clearTimer: function() { lvl: function(){
clearTimeout(timer); growl('<i class="icon-chevron-up"></i> Level Up!', 'lvl');
timer = null;
active = false;
}, },
init: function() { death: function(){
timer = setTimeout(this.hide, 2000); growl("<i class='icon-death'></i> Respawn!", "death");
} }
}; };
}); }
]);