diff --git a/assets/js/controllers/marketCtrl.js b/assets/js/controllers/marketCtrl.js index 9f5b3c6cb4..6a51d62ca6 100644 --- a/assets/js/controllers/marketCtrl.js +++ b/assets/js/controllers/marketCtrl.js @@ -1,5 +1,5 @@ -habitrpg.controller("MarketCtrl", ['$rootScope', '$scope', 'User', - function($rootScope, $scope, User) { +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; @@ -8,7 +8,7 @@ habitrpg.controller("MarketCtrl", ['$rootScope', '$scope', 'User', $scope.buy = function(type, item){ var gems = User.user.balance * 4, - store = type === 'egg' ? $scope.userEggs : $scope.userHatchingPotions; + store = type === 'egg' ? $scope.userEggs : $scope.userHatchingPotions, storePath = type === 'egg' ? 'items.eggs' : 'items.hatchingPotions' if(gems < item.value){ @@ -19,20 +19,15 @@ habitrpg.controller("MarketCtrl", ['$rootScope', '$scope', 'User', if(confirm(message)){ var toPush = type === 'egg' ? item : item.name; - store.push(toPush); - - var dataStore = { - op: 'set', - data: {} - }; - - dataStore.data[storePath] = store; - User.user.balance = (gems - item.value) / 4; - - User.log([ - dataStore, - { op: 'set', data: {'balance': User.user.balance} } - ]); + $http.post(API_URL + '/api/v1/market/buy?type=' + type, toPush) + .success(function(data){ + User.user.balance = data.balance; + store.push(toPush); + //$sc”ope.items = data.items.eggs; // FIXME this isn't updating the UI + }).error(function(data){ + alert(data); + console.error(data); + }); } } diff --git a/src/controllers/user.js b/src/controllers/user.js index e03a9caa28..a6eeae4364 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -16,6 +16,30 @@ var User = require('./../models/user').model; var Group = require('./../models/group').model; var api = module.exports; +// FIXME put this in a proper location +api.marketBuy = function(req, res, next){ + var user = res.locals.user, + type = req.query.type, + item = req.body; + + if (!_.contains(['hatchingPotion', 'egg'], req.query.type)) + return res.json(400, {err: "Type must be in 'hatchingPotion' or 'egg'"}); + var item; + if (type == 'egg'){ + if (!user.items && !user.items.eggs) user.items.eggs = []; + user.items.eggs.push(item); + } else { + if (!user.items && !user.items.hatchingPotions) user.items.hatchingPotions = []; + user.items.hatchingPotions.push(item.name); + } + user.markModified('items'); // I still don't get when this is necessary and when not.. + user.balance -= (item.value/4); + user.save(function(err, saved){ + if (err) return res.json(500, {err:err}); + res.json(saved); + }) +} + /* ------------------------------------------------------------------------ diff --git a/src/routes/api.js b/src/routes/api.js index 0d036c4803..c9bb7231b4 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -65,6 +65,7 @@ router.post('/groups/:gid/chat', auth.auth, groups.attachGroup, groups.postChat) //PUT /groups/:gid/chat/:messageId //DELETE /groups/:gid/chat/:messageId - +// Market +router.post('/market/buy', auth.auth, user.marketBuy); module.exports = router; \ No newline at end of file diff --git a/views/options/market.jade b/views/options/market.jade index 7edf743025..a0190f9d34 100644 --- a/views/options/market.jade +++ b/views/options/market.jade @@ -6,11 +6,10 @@ td .popover.static-popover.fade.right.in .arrow - h3.popover-title Alexander Augustin + h3.popover-title + a(target='_blank', href='http://www.kickstarter.com/profile/523661924') Alexander Augustin .popover-content - | Welcome to the Market! I'm the merchant, - a(target='_blank', href='http://www.kickstarter.com/profile/523661924') Alexander - | . Dying to get that particular pet you're after, but don't want to wait for it to drop? Buy it here! + | Welcome to the Market! I'm the merchant, Alexander. Dying to get that particular pet you're after, but don't want to wait for it to drop? Buy it here! menu.inventory-list(type='list') li.customize-menu diff --git a/views/options/pets.jade b/views/options/pets.jade index c3eaa6640e..8afc98672c 100644 --- a/views/options/pets.jade +++ b/views/options/pets.jade @@ -2,12 +2,11 @@ .NPC-Matt .popover.static-popover.fade.right.in(style='max-width: 550px; margin-left: 10px;') .arrow - h3.popover-title Matt Boch + h3.popover-title + a(target='_blank', href='http://www.kickstarter.com/profile/mattboch') Matt Boch .popover-content p - | Welcome to the Stable! I'm - a(target='_blank', href='http://www.kickstarter.com/profile/mattboch') Matt - | , the beast master. Choose a pet here to adventure at your side - they aren't much help yet, but I forsee a time when they're able to + | Welcome to the Stable! I'm Matt, the beast master. Choose a pet here to adventure at your side - they aren't much help yet, but I forsee a time when they're able to a(href='https://trello.com/card/mounts/50e5d3684fe3a7266b0036d6/221') grow into powerful steeds | ! Until that day, a(target='_blank', href='https://f.cloud.github.com/assets/2374703/164631/3ed5fa6c-78cd-11e2-8743-f65ac477b55e.png') have a look-see