2013-08-25 01:06:37 +00:00
'use strict' ;
// Make user and settings available for everyone through root scope.
habitrpg . controller ( 'SettingsCtrl' ,
2015-01-18 01:43:37 +00:00
[ '$scope' , 'User' , '$rootScope' , '$http' , 'ApiUrl' , 'Guide' , '$location' , '$timeout' , 'Notification' , 'Shared' ,
function ( $scope , User , $rootScope , $http , ApiUrl , Guide , $location , $timeout , Notification , Shared ) {
2013-08-28 23:20:47 +00:00
// FIXME we have this re-declared everywhere, figure which is the canonical version and delete the rest
// $scope.auth = function (id, token) {
// User.authenticate(id, token, function (err) {
// if (!err) {
// alert('Login successful!');
// $location.path("/habit");
// }
// });
// }
2015-02-23 20:25:01 +00:00
// A simple object to map the key stored in the db (user.preferences.emailNotification[key])
// to its string but ONLY when the preferences' key and the string key don't match
var mapPrefToEmailString = {
'importantAnnouncements' : 'inactivityEmails'
} ;
2015-05-09 02:24:57 +00:00
2015-02-23 20:25:01 +00:00
// If ?unsubFrom param is passed with valid email type,
// automatically unsubscribe users from that email and
// show an alert
$timeout ( function ( ) {
var unsubFrom = $location . search ( ) . unsubFrom ;
if ( unsubFrom ) {
2015-02-23 20:51:15 +00:00
var emailPrefKey = 'preferences.emailNotifications.' + unsubFrom ;
var emailTypeString = env . t ( mapPrefToEmailString [ unsubFrom ] || unsubFrom ) ;
2015-02-23 20:25:01 +00:00
User . set ( { emailPrefKey : false } ) ;
2015-02-23 20:51:15 +00:00
User . user . preferences . emailNotifications [ unsubFrom ] = false ;
2015-02-23 20:25:01 +00:00
Notification . text ( env . t ( 'correctlyUnsubscribedEmailType' , { emailType : emailTypeString } ) ) ;
$location . search ( { } ) ;
}
2015-03-08 15:25:19 +00:00
} , 1000 ) ;
2015-02-23 20:25:01 +00:00
2014-06-18 03:03:45 +00:00
$scope . hideHeader = function ( ) {
2014-06-18 03:03:21 +00:00
User . set ( { "preferences.hideHeader" : ! User . user . preferences . hideHeader } )
if ( User . user . preferences . hideHeader && User . user . preferences . stickyHeader ) {
User . set ( { "preferences.stickyHeader" : false } ) ;
$rootScope . $on ( 'userSynced' , function ( ) {
window . location . reload ( ) ;
2015-05-09 02:24:57 +00:00
} ) ;
2014-06-18 03:03:21 +00:00
}
2014-08-26 04:27:42 +00:00
}
2014-06-18 03:03:21 +00:00
2013-12-17 21:04:33 +00:00
$scope . toggleStickyHeader = function ( ) {
2014-03-06 13:47:35 +00:00
$rootScope . $on ( 'userSynced' , function ( ) {
window . location . reload ( ) ;
} ) ;
2013-12-17 21:04:33 +00:00
User . set ( { "preferences.stickyHeader" : ! User . user . preferences . stickyHeader } ) ;
}
2013-09-10 22:36:10 +00:00
$scope . showTour = function ( ) {
2013-12-11 16:30:39 +00:00
User . set ( { 'flags.showTour' : true } ) ;
2015-02-06 00:57:34 +00:00
Guide . goto ( 'intro' , 0 , true ) ;
2013-12-18 02:22:21 +00:00
}
2013-09-10 22:36:10 +00:00
$scope . showBailey = function ( ) {
2013-12-11 16:30:39 +00:00
User . set ( { 'flags.newStuff' : true } ) ;
2013-09-10 22:36:10 +00:00
}
2015-08-06 02:47:45 +00:00
$scope . dayStart = User . user . preferences . dayStart ;
2015-07-18 01:17:07 +00:00
2015-08-06 02:47:45 +00:00
function updateLastCron ( oldDayStart , newDayStart ) {
var getOldStart = Shared . startOfDayAllowsFuture ( { dayStart : oldDayStart } ) ;
var getNewStart = Shared . startOfDayAllowsFuture ( { dayStart : newDayStart } ) ;
2015-07-18 01:17:07 +00:00
var lastCron = User . user . lastCron ;
2015-08-06 02:47:45 +00:00
var momentLastCron = Shared . momentTimestamp ( lastCron ) ;
2015-07-18 01:17:07 +00:00
var isoNewStart = Shared . isoTimestamp ( getNewStart ) ;
2015-08-06 02:47:45 +00:00
alert ( 'Times are oldstart' + Shared . friendlyTimestamp ( getOldStart ) + ' lastcron ' + Shared . friendlyTimestamp ( lastCron ) + ' and newstart ' + Shared . friendlyTimestamp ( getNewStart ) ) ;
if ( getOldStart < momentLastCron && momentLastCron < getNewStart ) {
alert ( 'Setting lastcron to ' + Shared . friendlyTimestamp ( getNewStart ) ) ;
User . set ( { 'lastCron' : isoNewStart } ) ;
}
} ;
2015-07-18 01:17:07 +00:00
2015-09-05 14:45:08 +00:00
$scope . saveDayStart = function ( varDayStart ) {
2015-08-06 02:47:45 +00:00
var oldDayStart = User . user . preferences . dayStart ;
var newDayStart = varDayStart ;
if ( newDayStart != Math . floor ( newDayStart ) || newDayStart < 0 || newDayStart > 24 ) {
newDayStart = 0 ;
2014-02-05 22:43:49 +00:00
return alert ( window . env . t ( 'enterNumber' ) ) ;
2013-08-28 23:20:47 +00:00
}
2015-08-06 02:47:45 +00:00
updateLastCron ( oldDayStart , newDayStart ) ;
User . set ( { 'preferences.dayStart' : Math . floor ( newDayStart ) } ) ;
2013-08-25 01:06:37 +00:00
}
2013-11-12 15:13:35 +00:00
$scope . language = window . env . language ;
$scope . avalaibleLanguages = window . env . avalaibleLanguages ;
$scope . changeLanguage = function ( ) {
2013-11-12 15:23:21 +00:00
$rootScope . $on ( 'userSynced' , function ( ) {
2014-03-06 13:47:35 +00:00
window . location . reload ( ) ;
2013-11-12 15:23:21 +00:00
} ) ;
2013-12-11 16:30:39 +00:00
User . set ( { 'preferences.language' : $scope . language . code } ) ;
2013-11-12 15:13:35 +00:00
}
2014-12-29 21:26:12 +00:00
$scope . availableFormats = [ 'MM/dd/yyyy' , 'dd/MM/yyyy' , 'yyyy/MM/dd' ] ;
2014-12-29 20:21:54 +00:00
2013-08-29 02:51:45 +00:00
$scope . reroll = function ( ) {
2013-12-15 21:49:22 +00:00
User . user . ops . reroll ( { } ) ;
$rootScope . $state . go ( 'tasks' ) ;
2013-08-29 02:51:45 +00:00
}
2013-12-25 00:09:32 +00:00
$scope . rebirth = function ( ) {
User . user . ops . rebirth ( { } ) ;
$rootScope . $state . go ( 'tasks' ) ;
}
2015-02-03 02:27:22 +00:00
$scope . changeUser = function ( attr , updates ) {
$http . post ( ApiUrl . get ( ) + '/api/v2/user/change-' + attr , updates )
2014-06-12 14:34:10 +00:00
. success ( function ( ) {
2015-02-03 02:27:22 +00:00
alert ( window . env . t ( attr + 'Success' ) ) ;
_ . each ( updates , function ( v , k ) { updates [ k ] = null ; } ) ;
2015-01-18 21:50:43 +00:00
User . sync ( ) ;
2013-09-06 13:55:24 +00:00
} ) ;
}
2013-10-13 17:58:13 +00:00
$scope . restoreValues = { } ;
2014-02-02 15:43:58 +00:00
$rootScope . openRestoreModal = function ( ) {
$scope . restoreValues . stats = angular . copy ( User . user . stats ) ;
$scope . restoreValues . achievements = { streak : User . user . achievements . streak || 0 } ;
2014-02-10 17:39:01 +00:00
$rootScope . openModal ( 'restore' , { scope : $scope } ) ;
2014-02-02 15:43:58 +00:00
} ;
2013-10-13 17:58:13 +00:00
2013-09-08 17:23:37 +00:00
$scope . restore = function ( ) {
2013-10-13 17:58:13 +00:00
var stats = $scope . restoreValues . stats ,
2013-10-24 18:04:35 +00:00
achievements = $scope . restoreValues . achievements ;
2013-12-11 16:30:39 +00:00
User . set ( {
2013-09-08 17:23:37 +00:00
"stats.hp" : stats . hp ,
"stats.exp" : stats . exp ,
"stats.gp" : stats . gp ,
"stats.lvl" : stats . lvl ,
2013-12-17 07:16:48 +00:00
"stats.mp" : stats . mp ,
2013-10-24 18:04:35 +00:00
"achievements.streak" : achievements . streak
2013-09-08 17:23:37 +00:00
} ) ;
}
2013-12-15 21:49:22 +00:00
2013-09-08 19:11:04 +00:00
$scope . reset = function ( ) {
2013-12-15 21:49:22 +00:00
User . user . ops . reset ( { } ) ;
$rootScope . $state . go ( 'tasks' ) ;
2013-09-08 19:11:04 +00:00
}
2013-12-15 21:49:22 +00:00
2013-09-08 19:17:49 +00:00
$scope [ 'delete' ] = function ( ) {
2015-01-18 01:43:37 +00:00
$http [ 'delete' ] ( ApiUrl . get ( ) + '/api/v2/user' )
2014-04-15 05:07:30 +00:00
. success ( function ( res , code ) {
if ( res . err ) return alert ( res . err ) ;
2013-09-08 19:17:49 +00:00
localStorage . clear ( ) ;
window . location . href = '/logout' ;
} ) ;
}
2014-04-16 17:33:03 +00:00
$scope . enterCoupon = function ( code ) {
2015-01-18 01:43:37 +00:00
$http . post ( ApiUrl . get ( ) + '/api/v2/user/coupon/' + code ) . success ( function ( res , code ) {
2014-04-16 17:33:03 +00:00
if ( code !== 200 ) return ;
User . sync ( ) ;
2015-05-09 02:24:57 +00:00
Notification . text ( env . t ( 'promoCodeApplied' ) ) ;
2014-04-16 17:33:03 +00:00
} ) ;
}
$scope . generateCodes = function ( codes ) {
2015-01-18 01:43:37 +00:00
$http . post ( ApiUrl . get ( ) + '/api/v2/coupons/generate/' + codes . event + '?count=' + ( codes . count || 1 ) )
2014-04-16 17:33:03 +00:00
. success ( function ( res , code ) {
$scope . _codes = { } ;
if ( code !== 200 ) return ;
window . location . href = '/api/v2/coupons?limit=' + codes . count + '&_id=' + User . user . _id + '&apiToken=' + User . user . apiToken ;
} )
}
2015-01-02 18:43:40 +00:00
$scope . releasePets = function ( ) {
User . user . ops . releasePets ( { } ) ;
2014-09-29 20:35:38 +00:00
$rootScope . $state . go ( 'tasks' ) ;
}
2015-01-02 18:43:40 +00:00
$scope . releaseMounts = function ( ) {
User . user . ops . releaseMounts ( { } ) ;
2014-09-29 20:35:38 +00:00
$rootScope . $state . go ( 'tasks' ) ;
}
2014-11-27 10:32:57 +00:00
2015-01-02 19:33:41 +00:00
$scope . releaseBoth = function ( ) {
User . user . ops . releaseBoth ( { } ) ;
$rootScope . $state . go ( 'tasks' ) ;
}
2014-11-27 10:32:57 +00:00
// ---- Webhooks ------
$scope . _newWebhook = { url : '' } ;
$scope . $watch ( 'user.preferences.webhooks' , function ( webhooks ) {
$scope . hasWebhooks = _ . size ( webhooks ) ;
} )
$scope . addWebhook = function ( url ) {
User . user . ops . addWebhook ( { body : { url : url , id : Shared . uuid ( ) } } ) ;
$scope . _newWebhook . url = '' ;
}
$scope . saveWebhook = function ( id , webhook ) {
delete webhook . _editing ;
User . user . ops . updateWebhook ( { params : { id : id } , body : webhook } ) ;
}
$scope . deleteWebhook = function ( id ) {
User . user . ops . deleteWebhook ( { params : { id : id } } ) ;
}
2014-12-21 03:47:16 +00:00
$scope . applyCoupon = function ( coupon ) {
2015-01-18 01:43:37 +00:00
$http . get ( ApiUrl . get ( ) + '/api/v2/coupons/valid-discount/' + coupon )
2014-12-21 03:47:16 +00:00
. success ( function ( ) {
Notification . text ( "Coupon applied!" ) ;
var subs = $scope . Content . subscriptionBlocks ;
subs [ "basic_6mo" ] . discount = true ;
subs [ "google_6mo" ] . discount = false ;
} ) ;
}
2013-08-25 01:06:37 +00:00
}
] ) ;