2015-03-05 03:43:17 +00:00
habitrpg . controller ( "InventoryCtrl" ,
2015-01-03 00:51:51 +00:00
[ '$rootScope' , '$scope' , 'Shared' , '$window' , 'User' , 'Content' ,
function ( $rootScope , $scope , Shared , $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-10-30 19:35:40 +00:00
$scope . totalMounts = _ . size ( Content . dropEggs ) * _ . 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.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 ;
2015-01-03 00:52:25 +00:00
2015-01-26 03:00:34 +00:00
$rootScope . petCount = Shared . countPets ( $rootScope . countExists ( User . user . items . pets ) , User . user . items . pets ) ;
2015-01-03 00:52:25 +00:00
// Checks if beastmaster has been reached for the first time
2015-03-05 03:43:17 +00:00
if ( ! User . user . achievements . beastMaster
2015-01-26 03:00:34 +00:00
&& $rootScope . petCount >= 90 ) {
2015-01-03 00:52:25 +00:00
User . user . achievements . beastMaster = true ;
$rootScope . openModal ( 'achievements/beastMaster' ) ;
}
2015-01-16 17:47:07 +00:00
// Checks if Triad Bingo has been reached for the first time
if ( ! User . user . achievements . triadBingo
2015-01-26 03:00:34 +00:00
&& $rootScope . mountCount >= 90
2015-01-28 16:56:16 +00:00
&& Shared . countTriad ( User . user . items . pets ) >= 90 ) {
2015-01-16 22:43:48 +00:00
User . user . achievements . triadBingo = true ;
$rootScope . openModal ( 'achievements/triadBingo' ) ;
2015-01-16 17:47:07 +00:00
}
2013-09-04 17:06:31 +00:00
}
2013-12-15 21:49:22 +00:00
$scope . purchase = function ( type , item ) {
2014-12-27 17:49:51 +00:00
if ( type == 'special' ) return User . user . ops . buySpecialSpell ( { params : { key : item . key } } ) ;
2014-10-03 01:17:26 +00:00
2013-11-10 04:15:55 +00:00
var gems = User . user . balance * 4 ;
2014-02-06 16:36:33 +00:00
2014-12-25 20:33:11 +00:00
var string = ( type == 'weapon' ) ? window . env . t ( 'weapon' ) : ( type == 'armor' ) ? window . env . t ( 'armor' ) : ( type == 'head' ) ? window . env . t ( 'headgear' ) : ( type == 'shield' ) ? window . env . t ( 'offhand' ) : ( 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 ; // this is ugly but temporary, once the purchase modal is done this will be removed
if ( type == 'weapon' || type == 'armor' || type == 'head' || type == 'shield' ) {
if ( gems < ( ( item . specialClass == "wizard" ) && ( item . type == "weapon" ) ) + 1 ) return $rootScope . openModal ( 'buyGems' ) ;
var message = window . env . t ( 'buyThis' , { text : string , price : ( ( item . specialClass == "wizard" ) && ( item . type == "weapon" ) ) + 1 , gems : gems } )
if ( $window . confirm ( message ) )
User . user . ops . purchase ( { params : { type : "gear" , key : item . key } } ) ;
} else {
if ( gems < item . value ) return $rootScope . openModal ( 'buyGems' ) ;
var message = window . env . t ( 'buyThis' , { text : string , price : item . value , gems : gems } )
if ( $window . confirm ( message ) )
User . user . ops . purchase ( { params : { type : type , key : item . key } } ) ;
}
2014-02-06 16:36:33 +00:00
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 ;
2015-01-26 02:26:51 +00:00
$rootScope . mountCount = Shared . countMounts ( $rootScope . countExists ( User . user . items . mounts ) , User . user . items . mounts ) ;
2013-11-10 04:15:55 +00:00
2015-01-03 01:09:50 +00:00
// Checks if mountmaster has been reached for the first time
2015-03-05 03:43:17 +00:00
if ( ! User . user . achievements . mountMaster
2015-01-26 02:26:51 +00:00
&& $rootScope . mountCount >= 90 ) {
2015-01-03 01:09:50 +00:00
User . user . achievements . mountMaster = true ;
$rootScope . openModal ( 'achievements/mountMaster' ) ;
}
2013-11-10 04:15:55 +00:00
// 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
2015-01-28 01:32:06 +00:00
$scope . questPopover = function ( quest ) {
2015-01-28 21:38:41 +00:00
// The popover gets parsed as markdown (hence the double \n for line breaks
2015-01-28 01:32:06 +00:00
var text = '' ;
if ( quest . boss ) {
2015-01-28 21:36:13 +00:00
text += '**' + window . env . t ( 'bossHP' ) + ':** ' + quest . boss . hp + '\n\n' ;
2015-01-28 01:32:06 +00:00
text += '**' + window . env . t ( 'bossStrength' ) + ':** ' + quest . boss . str + '\n\n' ;
} else if ( quest . collect ) {
var count = 0 ;
for ( var key in quest . collect ) {
text += '**' + window . env . t ( 'collect' ) + ':** ' + quest . collect [ key ] . count + ' ' + quest . collect [ key ] . text ( ) + '\n\n' ;
}
}
text += '---\n\n' ;
text += '**' + window . env . t ( 'rewards' ) + ':**\n\n' ;
if ( quest . drop . items ) {
for ( var item in quest . drop . items ) {
text += quest . drop . items [ item ] . text ( ) + '\n\n' ;
}
}
if ( quest . drop . exp )
text += quest . drop . exp + ' ' + window . env . t ( 'experience' ) + '\n\n' ;
if ( quest . drop . gp )
text += quest . drop . gp + ' ' + window . env . t ( 'gold' ) + '\n\n' ;
return text ;
}
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
}
2015-03-05 03:43:17 +00:00
2014-12-27 22:26:30 +00:00
$scope . getSeasonalShopArray = function ( set ) {
var flatGearArray = _ . toArray ( Content . gear . flat ) ;
2015-03-05 03:43:17 +00:00
2014-12-27 22:26:30 +00:00
var filteredArray = _ . where ( flatGearArray , { index : set } ) ;
return filteredArray ;
} ;
2015-03-05 03:43:17 +00:00
2014-12-27 22:26:30 +00:00
$scope . getSeasonalShopQuests = function ( set ) {
var questArray = _ . toArray ( Content . quests ) ;
2015-03-05 03:43:17 +00:00
2014-12-27 22:26:30 +00:00
var filteredArray = _ . filter ( questArray , function ( q ) {
return q . key == "evilsanta" || q . key == "evilsanta2" ;
} ) ;
return filteredArray ;
} ;
2015-03-05 03:43:17 +00:00
2015-03-05 15:40:37 +00:00
$scope . dequip = function ( gearSet ) {
if ( gearSet == "battleGear" ) {
for ( item in user . items . gear . equipped ) {
var itemKey = user . items . gear . equipped [ item ] ;
if ( user . items . gear . owned [ itemKey ] ) {
user . ops . equip ( { params : { key : itemKey } } ) ;
}
2015-03-05 03:43:17 +00:00
}
}
2015-03-05 15:40:37 +00:00
if ( gearSet == "costume" ) {
for ( item in user . items . gear . costume ) {
var itemKey = user . items . gear . costume [ item ] ;
if ( user . items . gear . owned [ itemKey ] ) {
user . ops . equip ( { params : { type : "costume" , key : itemKey } } ) ;
}
}
}
2015-03-05 03:43:17 +00:00
}
2013-11-10 04:15:55 +00:00
}
2014-01-12 23:05:15 +00:00
] ) ;