mounts: update all html & javascript to support UserSchema overhaul (60421a8). Also add very basic feeding & saddling mounts functionality. use progress bars to indicate pet feeding progress

This commit is contained in:
Tyler Renelle
2013-11-09 20:15:55 -08:00
parent 60421a85cc
commit 085919c000
17 changed files with 248 additions and 199 deletions

View File

@@ -1,18 +1,27 @@
habitrpg.controller("InventoryCtrl", ['$scope', 'User',
function($scope, User) {
habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', 'User', 'API_URL', '$http', 'Notification',
function($rootScope, $scope, User, API_URL, $http, Notification) {
var user = User.user;
// convenience vars since these are accessed frequently
$scope.userEggs = User.user.items.eggs;
$scope.userHatchingPotions = User.user.items.hatchingPotions;
$scope.selectedEgg = null; // {index: 1, name: "Tiger", value: 5}
$scope.selectedPotion = null; // {index: 5, name: "Red", value: 3}
$scope.petCount = _.size(User.user.items.pets);
$scope.totalPets = _.size($scope.Items.eggs) * _.size($scope.Items.hatchingPotions);
$scope.chooseEgg = function(egg, $index){
if ($scope.selectedEgg && $scope.selectedEgg.index == $index) {
var countItems = function(items) {
return _.reduce(items,function(m,v){return m+v;},0);
}
$scope.$watch('user.items.pets', function(pets){ $scope.petCount = countItems(pets); });
$scope.$watch('user.items.eggs', function(eggs){ $scope.eggCount = countItems(eggs); });
$scope.$watch('user.items.hatchingPotions', function(pots){ $scope.potCount = countItems(pots); });
$scope.chooseEgg = function(egg){
if ($scope.selectedEgg && $scope.selectedEgg.name == egg) {
return $scope.selectedEgg = null; // clicked same egg, unselect
}
var eggData = _.defaults({index:$index}, egg);
var eggData = _.findWhere(window.habitrpgShared.items.items.eggs, {name:egg});
if (!$scope.selectedPotion) {
$scope.selectedEgg = eggData;
} else {
@@ -20,13 +29,12 @@ habitrpg.controller("InventoryCtrl", ['$scope', 'User',
}
}
$scope.choosePotion = function(potion, $index){
if ($scope.selectedPotion && $scope.selectedPotion.index == $index) {
$scope.choosePotion = function(potion){
if ($scope.selectedPotion && $scope.selectedPotion.name == potion) {
return $scope.selectedPotion = null; // clicked same egg, unselect
}
// we really didn't think through the way these things are stored and getting passed around...
var potionData = _.findWhere(window.habitrpgShared.items.items.hatchingPotions, {name:potion});
potionData = _.defaults({index:$index}, potionData);
if (!$scope.selectedEgg) {
$scope.selectedPotion = potionData;
} else {
@@ -34,54 +42,52 @@ habitrpg.controller("InventoryCtrl", ['$scope', 'User',
}
}
$scope.chooseFood = function(food){
$scope.selectedFood = $scope.Items.food[food];
}
$scope.sellInventory = function() {
// TODO DRY this
if ($scope.selectedEgg) {
$scope.userEggs.splice($scope.selectedEgg.index, 1);
user.items.eggs[$scope.selectedEgg.name]--;
User.setMultiple({
'items.eggs': $scope.userEggs,
'items.eggs': user.items.eggs,
'stats.gp': User.user.stats.gp + $scope.selectedEgg.value
});
$scope.selectedEgg = null;
} else if ($scope.selectedPotion) {
$scope.userHatchingPotions.splice($scope.selectedPotion.index, 1);
user.items.hatchingPotions[$scope.selectedPotion.name]--;
User.setMultiple({
'items.hatchingPotions': $scope.userHatchingPotions,
'items.hatchingPotions': user.items.hatchingPotions,
'stats.gp': User.user.stats.gp + $scope.selectedPotion.value
});
$scope.selectedPotion = null;
} else if ($scope.selectedFood) {
user.items.food[$scope.selectedFood.name]--;
User.setMultiple({
'items.food': user.items.food,
'stats.gp': User.user.stats.gp + $scope.selectedFood.value
});
$scope.selectedFood = null;
}
}
$scope.ownsPet = function(egg, potion){
if (!egg || !potion) return;
var pet = egg.name + '-' + potion;
return User.user.items.pets && ~User.user.items.pets.indexOf(pet)
}
$scope.selectableInventory = function(egg, potion, $index) {
if (!egg || !potion) return;
// FIXME this isn't updating the view for some reason
//if ($scope.selectedEgg && $scope.selectedEgg.index == $index) return 'selectableInventory';
//if ($scope.selectedPotion && $scope.selectedPotion.index == $index) return 'selectableInventory';
if (!$scope.ownsPet(egg, potion)) return 'selectableInventory';
$scope.ownedItems = function(inventory){
return _.pick(inventory, function(v,k){return v>0;});
}
$scope.hatch = function(egg, potion){
if ($scope.ownsPet(egg, potion.name)){
return alert("You already have that pet, hatch a different combo.")
}
var pet = egg.name + '-' + potion.name;
$scope.userEggs.splice(egg.index, 1);
$scope.userHatchingPotions.splice(potion.index, 1);
var pet = egg.name+"-"+potion.name;
if (user.items.pets[pet])
return alert("You already have that pet, hatch a different combo.");
if(!User.user.items.pets) User.user.items.pets = [];
User.user.items.pets.push(pet);
var setObj = {};
setObj['items.pets.' + pet] = 5;
setObj['items.eggs.' + egg.name] = user.items.eggs[egg.name] - 1;
setObj['items.hatchingPotions.' + potion.name] = user.items.hatchingPotions[potion.name] - 1;
User.log([
{ op: 'set', data: {'items.pets': User.user.items.pets} },
{ op: 'set', data: {'items.eggs': $scope.userEggs} },
{ op: 'set', data: {'items.hatchingPotions': $scope.userHatchingPotions} }
]);
User.setMultiple(setObj);
alert("Your egg hatched! Visit your stable to equip your pet.");
@@ -89,4 +95,68 @@ habitrpg.controller("InventoryCtrl", ['$scope', 'User',
$scope.selectedPotion = null;
}
}]);
$scope.buy = function(type, item){
var gems = User.user.balance * 4;
if(gems < item.value) return $rootScope.modals.buyGems = true;
var string = (type == 'hatchingPotion') ? 'hatching potion' : type; // give hatchingPotion a space
var message = "Buy this " + string + " with " + item.value + " of your " + gems + " Gems?"
if(confirm(message)){
$http.post(API_URL + '/api/v1/market/buy?type=' + type, item)
.success(function(data){
User.user.items = data.items;
});
}
}
$scope.choosePet = function(egg, potion){
var pet = egg + '-' + potion;
// Feeding Pet
if ($scope.selectedFood) {
var setObj = {};
var userPets = user.items.pets;
// Saddling a pet
if ($scope.selectedFood.name == 'Saddle') {
if (userPets[pet] < 150)
return Notification.text(egg+" is not strong enough yet to saddle.");
if (!confirm('Saddle ' + pet + '?')) return;
userPets[pet] = 0;
setObj['items.mounts.' + pet] = true;
Notification.text('You have tamed '+egg+", let's go for a ride!");
} else {
if (userPets[pet] >= 150)
return Notification.text(egg+" has become very strong and wild, try using a saddle and something might happen.");
if (!confirm('Feed ' + pet + ' a ' + $scope.selectedFood.name + '?')) return;
if ($scope.selectedFood.target == potion) {
userPets[pet] += 50; // FIXME change this to 5 before we're live
Notification.text(egg+' really likes the '+$scope.selectedFood.name+'!');
} else {
userPets[pet] += 2;
Notification.text(egg+' eats the '+$scope.selectedFood.name+" but doesn't seem to enjoy it.");
}
}
setObj['items.pets.' + pet] = userPets[pet];
setObj['items.food.' + $scope.selectedFood.name] = user.items.food[$scope.selectedFood.name] - 1;
User.setMultiple(setObj);
$scope.selectedFood = null;
// Selecting Pet
} else {
var userCurrentPet = User.user.items.currentPet;
if(userCurrentPet && userCurrentPet == pet){
User.user.items.currentPet = null;
}else{
User.user.items.currentPet = pet;
}
User.set('items.currentPet', User.user.items.currentPet);
}
}
$scope.chooseMount = function(egg, potion) {
var mount = egg + '-' + potion;
User.set('items.currentMount', (user.items.currentMount == mount) ? null : mount);
}
}
]);

View File

@@ -1,37 +0,0 @@
habitrpg.controller("MarketCtrl", ['$rootScope', '$scope', 'User', 'API_URL', '$http',
function($rootScope, $scope, User, API_URL, $http) {
$scope.eggs = window.habitrpgShared.items.items.pets;
$scope.hatchingPotions = window.habitrpgShared.items.items.hatchingPotions;
$scope.userEggs = User.user.items.eggs;
$scope.userHatchingPotions = User.user.items.hatchingPotions;
$scope.buy = function(type, item){
var gems = User.user.balance * 4,
store = type === 'egg' ? $scope.userEggs : $scope.userHatchingPotions,
storePath = type === 'egg' ? 'items.eggs' : 'items.hatchingPotions'
if(gems < item.value){
return $rootScope.modals.buyGems = true;
}
var message = "Buy this " + (type == 'egg' ? 'egg' : 'hatching potion') + " with " + item.value + " of your " + gems + " Gems?"
if(confirm(message)){
$http.post(API_URL + '/api/v1/market/buy?type=' + type, item)
.success(function(data){
// don't know what's going on, but trying to work with the returned data (a) isn't updating the ui, (b) isnt'
// stickign between refreshes until a force-refresh is called (user._v--).
User.user._v--;
User.log({});
//User.user.balance = data.balance;
store.push(type === 'egg' ? item : item.name);
//$scope.items = data.items.eggs; // FIXME this isn't updating the UI
}).error(function(data){
alert(data);
console.error(data);
});
}
}
}]);

View File

@@ -43,8 +43,8 @@ habitrpg.controller('NotificationCtrl',
$rootScope.modals.achievements.ultimateGear = true;
});
$rootScope.$watch('user.items.pets.length', function(after, before){
if(after === before || after < 90) return;
$rootScope.$watch('user.items.pets', function(after, before){
if(_.size(after) === _.size(before) || _.size(after) < 90) return;
User.user.achievements.beastMaster = true;
$rootScope.modals.achievements.beastMaster = true;
})

View File

@@ -1,32 +0,0 @@
habitrpg.controller("PetsCtrl", ['$scope', 'User',
function($scope, User) {
$scope.userPets = User.user.items.pets;
$scope.userCurrentPet = User.user.items.currentPet;
$scope.pets = window.habitrpgShared.items.items.pets;
$scope.hatchingPotions = window.habitrpgShared.items.items.hatchingPotions;
$scope.totalPets = $scope.pets.length * $scope.hatchingPotions.length;
$scope.hasPet = function(name, potion){
if (!$scope.userPets) return false;
return _.contains($scope.userPets, name + '-' + potion) ? true : false;
}
$scope.isCurrentPet = function(name, potion){
if (!$scope.userCurrentPet || !$scope.userPets) return false;
return $scope.userCurrentPet.str === (name + '-' + potion);
}
$scope.choosePet = function(name, potion){
if($scope.userCurrentPet && $scope.userCurrentPet.str === (name + '-' + potion)){
$scope.userCurrentPet = null;
}else{
var pet = _.find($scope.pets, {name: name});
pet.modifier = potion;
pet.str = name + '-' + potion;
$scope.userCurrentPet = pet;
}
User.set('items.currentPet', $scope.userCurrentPet);
}
}]);

View File

@@ -10,6 +10,7 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
$rootScope.User = User;
$rootScope.user = User.user;
$rootScope.settings = User.settings;
$rootScope.Items = window.habitrpgShared.items.items;
// Angular UI Router
$rootScope.$state = $state;