2014-02-03 18:24:37 +00:00
habitrpg . controller ( "InventoryCtrl" , [ '$rootScope' , '$scope' , '$window' , 'User' , 'Content' ,
function ( $rootScope , $scope , $window , User , Content ) {
2013-11-10 04:15:55 +00:00
var user = User . user ;
2013-09-04 15:13:42 +00:00
2013-10-20 02:21:55 +00:00
// convenience vars since these are accessed frequently
2013-09-04 15:13:42 +00:00
2013-10-20 02:58:11 +00:00
$scope . selectedEgg = null ; // {index: 1, name: "Tiger", value: 5}
$scope . selectedPotion = null ; // {index: 5, name: "Red", value: 3}
2014-02-09 00:57:28 +00:00
$scope . totalPets = _ . size ( Content . dropEggs ) * _ . size ( Content . hatchingPotions ) ;
2014-09-11 07:59:37 +00:00
$scope . totalMounts = _ . size ( _ . reject ( Content . eggs , function ( egg ) { return egg . noMount } ) ) * _ . size ( Content . hatchingPotions ) ;
2013-11-10 04:15:55 +00:00
2013-11-10 19:47:33 +00:00
// count egg, food, hatchingPotion stack totals
var countStacks = function ( items ) { return _ . reduce ( items , function ( m , v ) { return m + v ; } , 0 ) ; }
2013-11-16 01:50:41 +00:00
$scope . $watch ( 'user.items.mounts' , function ( mounts ) { $scope . mountCount = $rootScope . countExists ( mounts ) ; } , true ) ;
$scope . $watch ( 'user.items.eggs' , function ( eggs ) { $scope . eggCount = countStacks ( eggs ) ; } , true ) ;
$scope . $watch ( 'user.items.hatchingPotions' , function ( pots ) { $scope . potCount = countStacks ( pots ) ; } , true ) ;
$scope . $watch ( 'user.items.food' , function ( food ) { $scope . foodCount = countStacks ( food ) ; } , true ) ;
2013-12-20 19:42:36 +00:00
$scope . $watch ( 'user.items.quests' , function ( quest ) { $scope . questCount = countStacks ( quest ) ; } , true ) ;
2013-10-20 02:58:11 +00:00
2013-12-06 16:26:02 +00:00
$scope . $watch ( 'user.items.gear' , function ( gear ) {
2014-03-21 21:03:46 +00:00
$scope . gear = { } ;
2013-12-14 18:34:41 +00:00
_ . each ( gear . owned , function ( v , key ) {
if ( v === false ) return ;
2013-12-12 22:58:01 +00:00
var item = Content . gear . flat [ key ] ;
2013-12-06 16:26:02 +00:00
if ( ! $scope . gear [ item . klass ] ) $scope . gear [ item . klass ] = [ ] ;
$scope . gear [ item . klass ] . push ( item ) ;
} )
2013-12-14 18:34:41 +00:00
} , true ) ;
2013-12-06 16:26:02 +00:00
2013-11-10 04:15:55 +00:00
$scope . chooseEgg = function ( egg ) {
2013-12-20 22:43:10 +00:00
if ( $scope . selectedEgg && $scope . selectedEgg . key == egg ) {
2013-10-20 06:44:03 +00:00
return $scope . selectedEgg = null ; // clicked same egg, unselect
}
2013-12-20 22:43:10 +00:00
var eggData = _ . findWhere ( Content . eggs , { key : egg } ) ;
2013-10-20 02:21:55 +00:00
if ( ! $scope . selectedPotion ) {
2013-10-20 02:58:11 +00:00
$scope . selectedEgg = eggData ;
2013-10-20 02:21:55 +00:00
} else {
2013-10-20 02:58:11 +00:00
$scope . hatch ( eggData , $scope . selectedPotion ) ;
2013-09-04 17:06:31 +00:00
}
2014-01-26 03:18:54 +00:00
$scope . selectedFood = null ;
2013-10-20 02:21:55 +00:00
}
2013-11-10 04:15:55 +00:00
$scope . choosePotion = function ( potion ) {
2013-12-20 22:43:10 +00:00
if ( $scope . selectedPotion && $scope . selectedPotion . key == potion ) {
2013-10-20 06:44:03 +00:00
return $scope . selectedPotion = null ; // clicked same egg, unselect
}
2013-10-20 02:58:11 +00:00
// we really didn't think through the way these things are stored and getting passed around...
2013-12-20 22:43:10 +00:00
var potionData = _ . findWhere ( Content . hatchingPotions , { key : potion } ) ;
2013-10-20 02:21:55 +00:00
if ( ! $scope . selectedEgg ) {
2013-10-20 02:58:11 +00:00
$scope . selectedPotion = potionData ;
2013-10-20 02:21:55 +00:00
} else {
2013-10-20 02:58:11 +00:00
$scope . hatch ( $scope . selectedEgg , potionData ) ;
2013-10-20 02:21:55 +00:00
}
2014-01-26 03:18:54 +00:00
$scope . selectedFood = null ;
2013-10-20 02:21:55 +00:00
}
2013-09-04 17:06:31 +00:00
2013-11-10 04:15:55 +00:00
$scope . chooseFood = function ( food ) {
2013-12-20 22:43:10 +00:00
if ( $scope . selectedFood && $scope . selectedFood . key == food ) return $scope . selectedFood = null ;
2013-12-11 16:30:39 +00:00
$scope . selectedFood = Content . food [ food ] ;
2014-01-26 03:18:54 +00:00
$scope . selectedEgg = $scope . selectedPotion = null ;
2013-11-10 04:15:55 +00:00
}
2013-10-20 03:04:26 +00:00
$scope . sellInventory = function ( ) {
2013-12-11 16:30:39 +00:00
var selected = $scope . selectedEgg ? 'selectedEgg' : $scope . selectedPotion ? 'selectedPotion' : $scope . selectedFood ? 'selectedFood' : undefined ;
if ( selected ) {
var type = $scope . selectedEgg ? 'eggs' : $scope . selectedPotion ? 'hatchingPotions' : $scope . selectedFood ? 'food' : undefined ;
2013-12-20 22:43:10 +00:00
user . ops . sell ( { params : { type : type , key : $scope [ selected ] . key } } ) ;
if ( user . items [ type ] [ $scope [ selected ] . key ] < 1 ) {
2013-12-11 16:30:39 +00:00
$scope [ selected ] = null ;
2013-12-01 16:16:21 +00:00
}
2013-10-20 03:04:26 +00:00
}
2013-09-04 17:06:31 +00:00
}
2013-11-10 04:15:55 +00:00
$scope . ownedItems = function ( inventory ) {
return _ . pick ( inventory , function ( v , k ) { return v > 0 ; } ) ;
2013-10-20 02:21:55 +00:00
}
2013-09-09 02:42:59 +00:00
2013-10-20 02:21:55 +00:00
$scope . hatch = function ( egg , potion ) {
2014-05-28 16:07:25 +00:00
var eggName = Content . eggs [ egg . key ] . text ( ) ;
var potName = Content . hatchingPotions [ potion . key ] . text ( ) ;
if ( ! $window . confirm ( window . env . t ( 'hatchAPot' , { potion : potName , egg : eggName } ) ) ) return ;
2013-12-20 22:43:10 +00:00
user . ops . hatch ( { params : { egg : egg . key , hatchingPotion : potion . key } } ) ;
2013-12-13 00:38:01 +00:00
$scope . selectedEgg = null ;
$scope . selectedPotion = null ;
2013-09-04 17:06:31 +00:00
}
2013-12-15 21:49:22 +00:00
$scope . purchase = function ( type , item ) {
2013-11-10 04:15:55 +00:00
var gems = User . user . balance * 4 ;
2014-02-06 16:36:33 +00:00
2014-02-02 15:43:58 +00:00
if ( gems < item . value ) return $rootScope . openModal ( 'buyGems' ) ;
2014-05-28 16:07:25 +00:00
var string = ( type == 'hatchingPotions' ) ? window . env . t ( 'hatchingPotion' ) : ( type == 'eggs' ) ? window . env . t ( 'eggSingular' ) : ( type == 'quests' ) ? window . env . t ( 'quest' ) : ( item . key == 'Saddle' ) ? window . env . t ( 'foodSaddleText' ) . toLowerCase ( ) : ( type == 'special' ) ? item . key : type ; // this is ugly but temporary, once the purchase modal is done this will be removed
2014-02-05 22:43:49 +00:00
var message = window . env . t ( 'buyThis' , { text : string , price : item . value , gems : gems } )
2014-02-06 16:36:33 +00:00
2014-01-04 03:01:24 +00:00
if ( $window . confirm ( message ) )
2013-12-20 22:43:10 +00:00
User . user . ops . purchase ( { params : { type : type , key : item . key } } ) ;
2013-11-10 04:15:55 +00:00
}
$scope . choosePet = function ( egg , potion ) {
2014-06-08 17:11:34 +00:00
var petDisplayName = env . t ( 'petName' , {
potion : Content . hatchingPotions [ potion ] ? Content . hatchingPotions [ potion ] . text ( ) : potion ,
egg : Content . eggs [ egg ] ? Content . eggs [ egg ] . text ( ) : egg
} ) ,
pet = egg + '-' + potion ;
2013-11-10 04:15:55 +00:00
// Feeding Pet
if ( $scope . selectedFood ) {
2013-12-12 22:58:01 +00:00
var food = $scope . selectedFood
2013-12-20 22:43:10 +00:00
if ( food . key == 'Saddle' ) {
2014-05-28 16:07:25 +00:00
if ( ! $window . confirm ( window . env . t ( 'useSaddle' , { pet : petDisplayName } ) ) ) return ;
2014-03-10 14:26:05 +00:00
} else if ( ! $window . confirm ( window . env . t ( 'feedPet' , { name : petDisplayName , article : food . article , text : food . text ( ) } ) ) ) {
2013-12-15 19:35:20 +00:00
return ;
}
2013-12-20 22:43:10 +00:00
User . user . ops . feed ( { params : { pet : pet , food : food . key } } ) ;
2013-11-10 04:15:55 +00:00
$scope . selectedFood = null ;
// Selecting Pet
} else {
2013-12-15 21:49:22 +00:00
User . user . ops . equip ( { params : { type : 'pet' , key : pet } } ) ;
2013-11-10 04:15:55 +00:00
}
}
$scope . chooseMount = function ( egg , potion ) {
2013-12-15 21:49:22 +00:00
User . user . ops . equip ( { params : { type : 'mount' , key : egg + '-' + potion } } ) ;
2013-11-10 04:15:55 +00:00
}
2013-12-20 19:42:36 +00:00
$scope . showQuest = function ( quest ) {
2014-01-22 01:46:17 +00:00
var item = Content . quests [ quest ] ;
var completedPrevious = ! item . previous || ( User . user . achievements . quests && User . user . achievements . quests [ item . previous ] ) ;
if ( ! completedPrevious )
2014-03-10 14:26:05 +00:00
return alert ( window . env . t ( 'mustComplete' , { quest : $rootScope . Content . quests [ item . previous ] . text ( ) } ) ) ;
2014-01-22 01:46:17 +00:00
if ( item . lvl && item . lvl > user . stats . lvl )
2014-02-05 22:43:49 +00:00
return alert ( window . env . t ( 'mustLevel' , { level : item . lvl } ) ) ;
2014-01-22 01:46:17 +00:00
$rootScope . selectedQuest = item ;
2014-02-10 17:39:01 +00:00
$rootScope . openModal ( 'showQuest' , { controller : 'InventoryCtrl' } ) ;
2013-12-20 19:42:36 +00:00
}
$scope . closeQuest = function ( ) {
$rootScope . selectedQuest = undefined ;
}
$scope . questInit = function ( ) {
2013-12-20 22:43:10 +00:00
$rootScope . party . $questAccept ( { key : $scope . selectedQuest . key } , function ( ) {
$rootScope . party . $get ( ) ;
} ) ;
2013-12-20 19:42:36 +00:00
$scope . closeQuest ( ) ;
}
2014-01-23 17:53:42 +00:00
$scope . buyQuest = function ( quest ) {
var item = Content . quests [ quest ] ;
2014-02-02 07:51:51 +00:00
if ( item . lvl && item . lvl > user . stats . lvl )
2014-02-05 22:43:49 +00:00
return alert ( window . env . t ( 'mustLvlQuest' , { level : item . lvl } ) ) ;
2014-01-23 20:26:55 +00:00
var completedPrevious = ! item . previous || ( User . user . achievements . quests && User . user . achievements . quests [ item . previous ] ) ;
if ( ! completedPrevious )
2014-01-24 01:11:30 +00:00
return $scope . purchase ( "quests" , item ) ;
2014-01-23 17:53:42 +00:00
$rootScope . selectedQuest = item ;
2014-02-10 17:39:01 +00:00
$rootScope . openModal ( 'buyQuest' , { controller : 'InventoryCtrl' } ) ;
2014-01-23 17:53:42 +00:00
}
2013-11-10 04:15:55 +00:00
}
2014-01-12 23:05:15 +00:00
] ) ;