From 0fc413f3b73ea6c1797a713ba63320486d978456 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Tue, 17 Sep 2013 11:43:05 -0400 Subject: [PATCH] [#1394] move notifications from controller to service, lots of notification fixes, put streak notification back in --- public/js/controllers/notificationCtrl.js | 144 ++++++--------------- public/js/controllers/tasksCtrl.js | 57 +------- public/js/services/notificationServices.js | 140 ++++++++++---------- 3 files changed, 114 insertions(+), 227 deletions(-) diff --git a/public/js/controllers/notificationCtrl.js b/public/js/controllers/notificationCtrl.js index d32d9db0bd..f1f408694c 100644 --- a/public/js/controllers/notificationCtrl.js +++ b/public/js/controllers/notificationCtrl.js @@ -1,120 +1,56 @@ 'use strict'; 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(); - 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. - }); - }; + $rootScope.$watch('user.stats.hp', function(after, before) { + if (after == before) return; + Notification.hp(after - before, 'hp'); + }); - /* - Sets up "+1 Exp", "Level Up", etc notifications - */ - function setupGrowlNotifications() { + $rootScope.$watch('user.stats.exp', function(after, before) { + if (after == before) return; + Notification.exp(after - before); + }); - function statsNotification(html, type) { - // don't show notifications if user dead - if (User.user.stats.lvl == 0) return; - growlNotification(html, type); - }; + $rootScope.$watch('user.stats.gp', function(after, before) { + if (after == before) return; + var bonus, money; + var money = after - before; + Notification.gp(money); - /** - 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 + " " + silver + " "; - } else if (gold > 0) { - return "" + gold + " "; - } else if (silver > 0) { - return "" + silver + " "; + //Append Bonus + bonus = User.user._tmp.streakBonus; + + if ((money > 0) && !!bonus) { + if (bonus < 0.01) { + bonus = 0.01; } - }; + Notification.text("+ " + Notification.coins(bonus) + " Streak Bonus!"); + delete User.user._tmp.streakBonus; + } + }); - $rootScope.$watch('user.stats.hp', function(after, before) { + // FIXME: this isn't working for some reason + /*_.each(['weapon', 'head', 'chest', 'shield'], function(watched){ + $rootScope.$watch('user.items.' + watched, function(before, after){ if (after == before) return; - var num = after - before; - var rounded = Math.abs(num.toFixed(1)); - if (num < 0) { - //lost hp from purchase - statsNotification(" - " + rounded + " HP", 'hp'); - } else if (num > 0) { - // gained hp from potion/level? - statsNotification(" + " + rounded + " HP", 'hp'); + if (+after < +before) { + Notification.death(); + //don't want to day "lost a head" + if (watched === 'head') watched = 'helm'; + Notification.text('Lost GP, 1 LVL, ' + watched); } - }); + }) + });*/ - $rootScope.$watch('user.stats.exp', function(after, before) { - if (after == before) return; - var num = after - before; - var rounded = Math.abs(num.toFixed(1)); - // TODO fix hackey negative notification supress - if (num < 0 && num > -50) { - statsNotification(" - " + rounded + " XP", 'xp'); - } else if (num > 0) { - statsNotification(" + " + rounded + " XP", 'xp'); - } - }); - - $rootScope.$watch('user.stats.gp', function(after, before) { - if (after == before) return; - var bonus, money, sign; - money = after - before; - - //why is this happening? gotta find where stats.gp is being set from (-)habit - //if (!money) { - // return; - //} - - sign = money < 0 ? '-' : '+'; - statsNotification("" + sign + " " + (showCoins(money)), 'gp'); - - //Append Bonus TODO - //bonus = model.get('_streakBonus'); - - if ((money > 0) && !!bonus) { - if (bonus < 0.01) { - bonus = 0.01; - } - statsNotification("+ " + (showCoins(bonus)) + " Streak Bonus!"); - //model.del('_streakBonus'); - } - }); - - // FIXME -// user.on('set', 'items.*', function(item, after, before) { -// if ((item === 'armor' || item === 'weapon' || item === 'shield' || item === 'head') && parseInt(after) < parseInt(before)) { -// //don't want to day "lost a head" -// if (item === 'head') { -// item = 'helm'; -// } -// return statsNotification(" Respawn!", "death"); -// } -// }); - - $rootScope.$watch('user.stats.lvl', function(after, before) { - if (after == before) return; - if (after > before) { - statsNotification(' Level Up!', 'lvl'); - } - }); - }; - - setupGrowlNotifications(); + $rootScope.$watch('user.stats.lvl', function(after, before) { + if (after == before) return; + if (after > before) { + Notification.lvl(); + } + }); } ]); diff --git a/public/js/controllers/tasksCtrl.js b/public/js/controllers/tasksCtrl.js index eb29be6877..bfee26ebd1 100644 --- a/public/js/controllers/tasksCtrl.js +++ b/public/js/controllers/tasksCtrl.js @@ -32,43 +32,9 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', ' } ]; $scope.score = function(task, direction) { - /*save current stats to compute the difference after scoring. - */ - - var oldStats, statsDiff; - statsDiff = {}; - oldStats = _.clone(User.user.stats); + if (task.type === "reward" && User.user.stats.gp < task.value) + return Notification.text('Not enough GP.'); 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) { @@ -129,26 +95,15 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', ' $scope.itemStore = sorted; }); $scope.buy = function(type) { - var hasEnough; - hasEnough = window.habitrpgShared.items.buyItem(User.user, type); + var hasEnough = window.habitrpgShared.items.buyItem(User.user, type); if (hasEnough) { - User.log({ - op: "buy", - type: type - }); - Notification.push({ - type: "text", - text: "Item bought!" - }); + User.log({op: "buy",type: type}); + Notification.text("Item purchased."); } else { - Notification.push({ - type: "text", - text: "Not enough GP." - }); + Notification.text("Not enough GP."); } }; - $scope.clearCompleted = function() { User.user.todos = _.reject(User.user.todos, {completed:true}); User.log({op: 'clear-completed'}); diff --git a/public/js/services/notificationServices.js b/public/js/services/notificationServices.js index 739683af61..91ca69d8a1 100644 --- a/public/js/services/notificationServices.js +++ b/public/js/services/notificationServices.js @@ -1,75 +1,71 @@ - -angular.module("notificationServices", []).factory("Notification", function() { - var active, data, fixMe, timer; - fixMe = { - push: function() {}, - get: function() {}, - animate: function() {}, - clearTimer: function() {}, - init: function() {} - }; - return fixMe; - data = { - message: "" - }; - active = false; - timer = null; - return { - hide: function() { - $("#notification").fadeOut(function() { - $("#notification").css("webkit-transform", "none"); - $("#notification").css("top", "-63px"); - $("#notification").css("left", "0px"); - setTimeout((function() { - $("#notification").show(); - }), 190); +/** + 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. }); - active = false; - timer = null; - }, - animate: function() { - if (timer) { - clearTimeout(timer); - timer = setTimeout(this.hide, 2000); + }; + + /** + Show "+ 5 {gold_coin} 3 {silver_coin}" + */ + function coins(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 + " " + silver + " "; + } else if (gold > 0) { + return "" + gold + " "; + } else if (silver > 0) { + return "" + silver + " "; } - if (active === false) { - active = true; - $("#notification").transition({ - y: 63, - x: 0 - }); - timer = setTimeout(this.hide, 2000); - } - }, - push: function(message) { - data.message = ""; - switch (message.type) { - case "stats": - if ((message.stats.exp != null) && (message.stats.gp != null)) { - data.message = "Experience: " + message.stats.exp + "
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 = "
GP: " + message.stats.gp.toFixed(2); - } - break; - case "text": - data.message = message.text; - } - this.animate(); - }, - get: function() { - return data; - }, - clearTimer: function() { - clearTimeout(timer); - timer = null; - active = false; - }, - init: function() { - timer = setTimeout(this.hide, 2000); + }; + + var sign = function(number){ + return number?number<0?'-':'+':'+'; } - }; -}); + + var round = function(number){ + return Math.abs(number.toFixed(1)); + } + + return { + coins: coins, + hp: function(val) { + // don't show notifications if user dead + if (User.user.stats.lvl == 0) return; + growl(" " + sign(val) + " " + round(val) + " HP", 'hp'); + }, + exp: function(val) { + if (User.user.stats.lvl == 0) return; + if (val < -50) return; // don't show when they level up (resetting their exp) + growl(" " + sign(val) + " " + round(val) + " HP", 'xp'); + }, + gp: function(val) { + if (User.user.stats.lvl == 0) return; + growl(sign(val) + " " + coins(val), 'gp'); + }, + text: function(val){ + growl(val); + }, + lvl: function(){ + growl(' Level Up!', 'lvl'); + }, + death: function(){ + growl(" Respawn!", "death"); + } + }; + } +]);