2013-08-27 23:59:53 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
/* Make user and settings available for everyone through root scope.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-01-12 23:05:15 +00:00
|
|
|
habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$http', '$state', '$stateParams', 'Notification', 'Groups', 'Shared', 'Content',
|
|
|
|
|
function($scope, $rootScope, $location, User, $http, $state, $stateParams, Notification, Groups, Shared, Content) {
|
2013-12-11 16:30:39 +00:00
|
|
|
var user = User.user;
|
|
|
|
|
|
2013-12-17 20:39:57 +00:00
|
|
|
var initSticky = _.once(function(){
|
2013-12-17 21:04:33 +00:00
|
|
|
if (window.env.IS_MOBILE || User.user.preferences.stickyHeader === false) return;
|
2013-12-17 20:39:57 +00:00
|
|
|
$('.header-wrap').sticky({topSpacing:0});
|
|
|
|
|
})
|
|
|
|
|
$rootScope.$on('userUpdated',initSticky);
|
|
|
|
|
|
2013-09-06 02:33:31 +00:00
|
|
|
$rootScope.modals = {};
|
2013-10-23 15:57:02 +00:00
|
|
|
$rootScope.modals.achievements = {};
|
2013-09-06 02:33:31 +00:00
|
|
|
$rootScope.User = User;
|
2013-12-11 16:30:39 +00:00
|
|
|
$rootScope.user = user;
|
2013-12-15 19:11:25 +00:00
|
|
|
$rootScope.moment = window.moment;
|
2013-12-18 06:08:18 +00:00
|
|
|
$rootScope._ = window._;
|
2013-09-06 02:33:31 +00:00
|
|
|
$rootScope.settings = User.settings;
|
2014-01-12 23:05:15 +00:00
|
|
|
$rootScope.Shared = Shared;
|
|
|
|
|
$rootScope.Content = Content;
|
2013-08-27 23:59:53 +00:00
|
|
|
|
2013-10-28 05:27:07 +00:00
|
|
|
// Angular UI Router
|
|
|
|
|
$rootScope.$state = $state;
|
|
|
|
|
$rootScope.$stateParams = $stateParams;
|
|
|
|
|
|
2013-10-27 00:23:52 +00:00
|
|
|
// indexOf helper
|
|
|
|
|
$scope.indexOf = function(haystack, needle){
|
2013-10-29 19:26:13 +00:00
|
|
|
return haystack && ~haystack.indexOf(needle);
|
2013-10-27 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-26 22:56:24 +00:00
|
|
|
// styling helpers
|
|
|
|
|
$scope.userLevelStyle = function(user,style){
|
|
|
|
|
style = style || '';
|
2013-11-28 06:48:48 +00:00
|
|
|
if(user && user.backer && user.backer.npc)
|
2013-11-26 22:56:24 +00:00
|
|
|
style += ' label-npc';
|
2013-11-28 06:48:48 +00:00
|
|
|
if(user && user.contributor && user.contributor.level)
|
2013-11-26 22:56:24 +00:00
|
|
|
style += ' label-contributor-'+user.contributor.level;
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 01:50:41 +00:00
|
|
|
// count pets, mounts collected totals, etc
|
|
|
|
|
$rootScope.countExists = function(items) {return _.reduce(items,function(m,v){return m+(v?1:0)},0)}
|
|
|
|
|
|
2014-01-12 23:05:15 +00:00
|
|
|
$rootScope.petCount = Shared.countPets(null, User.user.items.pets);
|
2013-11-23 16:42:47 +00:00
|
|
|
|
|
|
|
|
$rootScope.$watch('user.items.pets', function(pets){
|
2014-01-12 23:05:15 +00:00
|
|
|
$rootScope.petCount = Shared.countPets($rootScope.countExists(pets), User.user.items.pets);
|
2013-11-23 16:42:47 +00:00
|
|
|
}, true);
|
|
|
|
|
|
2013-09-06 02:33:31 +00:00
|
|
|
$scope.safeApply = function(fn) {
|
|
|
|
|
var phase = this.$root.$$phase;
|
|
|
|
|
if(phase == '$apply' || phase == '$digest') {
|
|
|
|
|
if(fn && (typeof(fn) === 'function')) {
|
|
|
|
|
fn();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.$apply(fn);
|
2013-09-02 01:31:38 +00:00
|
|
|
}
|
2013-09-06 02:33:31 +00:00
|
|
|
};
|
2013-09-02 01:31:38 +00:00
|
|
|
|
2013-09-06 02:33:31 +00:00
|
|
|
$rootScope.set = User.set;
|
|
|
|
|
$rootScope.authenticated = User.authenticated;
|
2013-08-27 23:59:53 +00:00
|
|
|
|
2013-09-06 02:33:31 +00:00
|
|
|
$rootScope.dismissAlert = function() {
|
|
|
|
|
$rootScope.modals.newStuff = false;
|
2013-12-16 22:43:16 +00:00
|
|
|
$rootScope.set({'flags.newStuff':false});
|
2013-09-06 02:33:31 +00:00
|
|
|
}
|
2013-08-28 02:37:56 +00:00
|
|
|
|
2013-09-06 02:33:31 +00:00
|
|
|
$rootScope.notPorted = function(){
|
|
|
|
|
alert("This feature is not yet ported from the original site.");
|
|
|
|
|
}
|
2013-08-29 02:51:45 +00:00
|
|
|
|
2013-10-28 19:16:40 +00:00
|
|
|
$rootScope.dismissErrorOrWarning = function(type, $index){
|
|
|
|
|
$rootScope.flash[type].splice($index, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-06 02:33:31 +00:00
|
|
|
$rootScope.showStripe = function() {
|
2013-10-12 23:47:49 +00:00
|
|
|
StripeCheckout.open({
|
|
|
|
|
key: window.env.STRIPE_PUB_KEY,
|
|
|
|
|
address: false,
|
|
|
|
|
amount: 500,
|
|
|
|
|
name: "Checkout",
|
2013-10-13 00:05:21 +00:00
|
|
|
description: "Buy 20 Gems, Disable Ads, Support the Developers",
|
2013-10-12 23:47:49 +00:00
|
|
|
panelLabel: "Checkout",
|
|
|
|
|
token: function(data) {
|
|
|
|
|
$scope.$apply(function(){
|
2013-12-16 00:22:35 +00:00
|
|
|
$http.post("/api/v2/user/buy-gems", data)
|
2013-10-12 23:47:49 +00:00
|
|
|
.success(function() {
|
|
|
|
|
window.location.href = "/";
|
|
|
|
|
}).error(function(err) {
|
|
|
|
|
alert(err);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-09-06 02:33:31 +00:00
|
|
|
}
|
2013-10-19 23:15:48 +00:00
|
|
|
|
2013-11-07 19:24:38 +00:00
|
|
|
$scope.contribText = function(contrib, backer){
|
|
|
|
|
if (!contrib && !backer) return;
|
|
|
|
|
if (backer && backer.npc) return backer.npc;
|
|
|
|
|
var l = contrib && contrib.level;
|
|
|
|
|
if (l && l > 0) {
|
|
|
|
|
var level = (l < 3) ? 'Friend' : (l < 5) ? 'Elite' : (l < 7) ? 'Champion' : (l < 8) ? 'Legendary' : 'Heroic';
|
|
|
|
|
return level + ' ' + contrib.text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-19 23:15:48 +00:00
|
|
|
$rootScope.charts = {};
|
|
|
|
|
$rootScope.toggleChart = function(id, task) {
|
|
|
|
|
var history = [], matrix, data, chart, options;
|
|
|
|
|
switch (id) {
|
|
|
|
|
case 'exp':
|
|
|
|
|
$rootScope.charts.exp = !$rootScope.charts.exp;
|
|
|
|
|
history = User.user.history.exp;
|
|
|
|
|
break;
|
|
|
|
|
case 'todos':
|
|
|
|
|
$rootScope.charts.todos = !$rootScope.charts.todos;
|
|
|
|
|
history = User.user.history.todos;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$rootScope.charts[id] = !$rootScope.charts[id];
|
|
|
|
|
history = task.history;
|
2013-10-21 18:41:19 +00:00
|
|
|
if (task && task._editing) task._editing = false;
|
2013-10-19 23:15:48 +00:00
|
|
|
}
|
|
|
|
|
matrix = [['Date', 'Score']];
|
|
|
|
|
_.each(history, function(obj) {
|
|
|
|
|
matrix.push([moment(obj.date).format('MM/DD/YY'), obj.value]);
|
|
|
|
|
});
|
|
|
|
|
data = google.visualization.arrayToDataTable(matrix);
|
|
|
|
|
options = {
|
|
|
|
|
title: 'History',
|
|
|
|
|
backgroundColor: {
|
|
|
|
|
fill: 'transparent'
|
2013-10-19 23:42:29 +00:00
|
|
|
},
|
|
|
|
|
width:300
|
2013-10-19 23:15:48 +00:00
|
|
|
};
|
|
|
|
|
chart = new google.visualization.LineChart($("." + id + "-chart")[0]);
|
|
|
|
|
chart.draw(data, options);
|
|
|
|
|
};
|
2013-11-16 10:22:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
------------------------
|
|
|
|
|
Spells
|
|
|
|
|
------------------------
|
|
|
|
|
*/
|
|
|
|
|
$scope.castStart = function(spell) {
|
|
|
|
|
if (User.user.stats.mp < spell.mana) return Notification.text("Not enough mana.");
|
|
|
|
|
$rootScope.applyingAction = true;
|
|
|
|
|
$scope.spell = spell;
|
|
|
|
|
if (spell.target == 'self') {
|
|
|
|
|
$scope.castEnd(null, 'self');
|
|
|
|
|
} else if (spell.target == 'party') {
|
2013-12-05 18:05:47 +00:00
|
|
|
var party = Groups.party();
|
|
|
|
|
party = (_.isArray(party) ? party : []).concat(User.user);
|
|
|
|
|
$scope.castEnd(party, 'party');
|
2013-11-16 10:22:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.castEnd = function(target, type, $event){
|
2014-01-22 07:04:01 +00:00
|
|
|
if (!$rootScope.applyingAction) return;
|
2013-12-21 02:57:03 +00:00
|
|
|
$event && ($event.stopPropagation(),$event.preventDefault());
|
2013-11-16 10:22:12 +00:00
|
|
|
if ($scope.spell.target != type) return Notification.text("Invalid target");
|
|
|
|
|
$scope.spell.cast(User.user, target);
|
2013-12-21 01:52:14 +00:00
|
|
|
User.save();
|
2014-01-22 08:16:52 +00:00
|
|
|
var spell = $scope.spell;
|
|
|
|
|
var targetId = type == 'party' ? '' : type == 'task' ? target.id : target._id;
|
|
|
|
|
$http.post('/api/v2/user/class/cast/' + spell.key, {params:{targetType:type, targetId:targetId}})
|
|
|
|
|
.success(function(){
|
2014-01-22 07:04:01 +00:00
|
|
|
var msg = "You cast " + spell.text;
|
2013-11-16 10:22:12 +00:00
|
|
|
switch (type) {
|
|
|
|
|
case 'task': msg += ' on ' + target.text;break;
|
|
|
|
|
case 'user': msg += ' on ' + target.profile.name;break;
|
|
|
|
|
case 'party': msg += ' on the Party';break;
|
|
|
|
|
}
|
|
|
|
|
Notification.text(msg);
|
2013-12-18 06:08:18 +00:00
|
|
|
});
|
2014-01-22 07:04:01 +00:00
|
|
|
$scope.spell = null;
|
2013-12-18 06:08:18 +00:00
|
|
|
$rootScope.applyingAction = false;
|
2013-11-16 10:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
2014-01-18 21:15:45 +00:00
|
|
|
$rootScope.castCancel = function(){
|
|
|
|
|
$rootScope.applyingAction = false;
|
|
|
|
|
$scope.spell = null;
|
|
|
|
|
}
|
2013-08-29 05:45:19 +00:00
|
|
|
}
|
2013-09-06 02:33:31 +00:00
|
|
|
]);
|