From 78a8eea79ae4cd2750b4380e9d8206df4f298c29 Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Tue, 3 May 2016 10:16:30 -0500 Subject: [PATCH] Updated quest service to use new api-v3 (#7126) * Updated quest service to use new api-v3 * Updated inviteToQuest function name. Used quest return rather than syncing party --- test/spec/services/questServicesSpec.js | 45 ++++++++++++++---- website/public/js/controllers/partyCtrl.js | 21 ++++++--- website/public/js/services/groupServices.js | 4 +- website/public/js/services/questServices.js | 47 ++++++++----------- .../options/social/quests/questActive.jade | 2 +- .../options/social/quests/questNotActive.jade | 4 +- 6 files changed, 73 insertions(+), 50 deletions(-) diff --git a/test/spec/services/questServicesSpec.js b/test/spec/services/questServicesSpec.js index e7f8c638b2..6a16df2d5a 100644 --- a/test/spec/services/questServicesSpec.js +++ b/test/spec/services/questServicesSpec.js @@ -1,7 +1,7 @@ 'use strict'; describe('Quests Service', function() { - var groupsService, quest, questsService, user, content, resolveSpy, rejectSpy; + var groupsService, quest, questsService, user, content, resolveSpy, rejectSpy, state; beforeEach(function() { user = specHelper.newUser(); @@ -16,10 +16,11 @@ describe('Quests Service', function() { $provide.value('User', {sync: sinon.stub(), user: user}); }); - inject(function(Quests, Groups, Content) { + inject(function(Quests, Groups, Content, _$state_) { questsService = Quests; groupsService = Groups; content = Content; + state = _$state_; }); sandbox.stub(groupsService, 'inviteOrStartParty'); @@ -335,13 +336,36 @@ describe('Quests Service', function() { }); describe('#initQuest', function() { + var fakeBackend, scope, key = 'whale'; + + beforeEach(inject(function($httpBackend, $rootScope) { + scope = $rootScope.$new(); + fakeBackend = $httpBackend; + var partyResponse = {data:{_id: 'party-id'}}; + + fakeBackend.when('GET', 'partials/main.html').respond({}); + fakeBackend.when('GET', 'partials/main.html').respond({}); + fakeBackend.when('GET', '/api/v3/groups/party').respond(partyResponse); + fakeBackend.when('POST', '/api/v3/groups/party-id/quests/invite/' + key).respond({quest: { key: 'whale' } }); + fakeBackend.flush(); + })); it('returns a promise', function() { - var promise = questsService.initQuest('whale'); + var promise = questsService.initQuest(key); expect(promise).to.respondTo('then'); }); - it('accepts quest'); + it('starts a quest', function(done) { + fakeBackend.expectPOST( '/api/v3/groups/party-id/quests/invite/' + key); + + questsService.initQuest(key) + .then(function(res) { + done(); + }); + + fakeBackend.flush(); + scope.$apply(); + }); it('brings user to party page'); }); @@ -352,22 +376,23 @@ describe('Quests Service', function() { beforeEach(inject(function($httpBackend, $rootScope) { scope = $rootScope.$new(); fakeBackend = $httpBackend; + var partyResponse = {data:{_id: 'party-id'}}; fakeBackend.when('GET', 'partials/main.html').respond({}); - fakeBackend.when('GET', '/api/v2/groups/party').respond({_id: 'party-id'}); - fakeBackend.when('POST', '/api/v2/groups/party-id/questReject').respond({quest: { key: 'whale' } }); + fakeBackend.when('GET', '/api/v3/groups/party').respond(partyResponse); + fakeBackend.when('POST', '/api/v3/groups/party-id/quests/reject').respond({quest: { key: 'whale' } }); fakeBackend.flush(); })); it('returns a promise', function() { - var promise = questsService.sendAction('questReject'); + var promise = questsService.sendAction('quests/reject'); expect(promise).to.respondTo('then'); }); it('calls specified quest endpoint', function(done) { - fakeBackend.expectPOST('/api/v2/groups/party-id/questReject'); + fakeBackend.expectPOST('/api/v3/groups/party-id/quests/reject'); - questsService.sendAction('questReject') + questsService.sendAction('quests/reject') .then(function(res) { expect(res.key).to.eql('whale'); done(); @@ -378,7 +403,7 @@ describe('Quests Service', function() { }); it('syncs User', function() { - questsService.sendAction('questReject') + questsService.sendAction('quests/reject') .then(function(res) { expect(User.sync).to.be.calledOnce; done(); diff --git a/website/public/js/controllers/partyCtrl.js b/website/public/js/controllers/partyCtrl.js index d80414c37a..39cdb4dfce 100644 --- a/website/public/js/controllers/partyCtrl.js +++ b/website/public/js/controllers/partyCtrl.js @@ -149,7 +149,7 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' $scope.questCancel = function(){ if (!confirm(window.env.t('sureCancel'))) return; - Quests.sendAction('questCancel') + Quests.sendAction('quests/cancel') .then(function(quest) { $scope.group.quest = quest; }); @@ -159,7 +159,7 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' if (!confirm(window.env.t('sureAbort'))) return; if (!confirm(window.env.t('doubleSureAbort'))) return; - Quests.sendAction('questAbort') + Quests.sendAction('quests/abort') .then(function(quest) { $scope.group.quest = quest; }); @@ -168,28 +168,35 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' $scope.questLeave = function(){ if (!confirm(window.env.t('sureLeave'))) return; - Quests.sendAction('questLeave') + Quests.sendAction('quests/leave') .then(function(quest) { $scope.group.quest = quest; }); } $scope.questAccept = function(){ - Quests.sendAction('questAccept') + Quests.sendAction('quests/accept') + .then(function(quest) { + $scope.group.quest = quest; + }); + }; + + $scope.questForceStart = function(){ + Quests.sendAction('quests/force-start') .then(function(quest) { $scope.group.quest = quest; }); }; $scope.questReject = function(){ - Quests.sendAction('questReject') + Quests.sendAction('quests/reject') .then(function(quest) { $scope.group.quest = quest; }); }; - $scope.canEditQuest = function(party) { - var isQuestLeader = party.quest && party.quest.leader === User.user._id; + $scope.canEditQuest = function() { + var isQuestLeader = $scope.group.quest && $scope.group.quest.leader === User.user._id; return isQuestLeader; }; diff --git a/website/public/js/services/groupServices.js b/website/public/js/services/groupServices.js index bff834582a..e7ea8e71e9 100644 --- a/website/public/js/services/groupServices.js +++ b/website/public/js/services/groupServices.js @@ -94,10 +94,10 @@ angular.module('habitrpg') }); }; - Group.startQuest = function(gid) { + Group.inviteToQuest = function(gid, key) { return $http({ method: "POST", - url: groupApiURLPrefix + '/' + gid + '/questAccept', + url: groupApiURLPrefix + '/' + gid + '/quests/invite/' + key, }); }; diff --git a/website/public/js/services/questServices.js b/website/public/js/services/questServices.js index a69ae800d8..73fa78e42c 100644 --- a/website/public/js/services/questServices.js +++ b/website/public/js/services/questServices.js @@ -1,25 +1,16 @@ 'use strict'; -(function(){ - angular - .module('habitrpg') - .factory('Quests', questsFactory); - - questsFactory.$inject = [ - '$http', - '$state', - '$q', - 'ApiUrl', - 'Content', - 'Groups', - 'User', - 'Analytics' - ]; - +angular.module('habitrpg') +.factory('Quests', ['$http', '$state','$q', 'ApiUrl', 'Content', 'Groups', 'User', 'Analytics', function questsFactory($http, $state, $q, ApiUrl, Content, Groups, User, Analytics) { var user = User.user; - var party = Groups.party(); + var party; + + Groups.party() + .then(function (partyFound) { + party = partyFound; + }); function lockQuest(quest,ignoreLevel) { if (!ignoreLevel){ @@ -106,20 +97,21 @@ function initQuest(key) { return $q(function(resolve, reject) { - Analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'quest','owner':true,'response':'accept','questName': key}); - Analytics.updateUser({'partyID':party._id,'partySize':party.memberCount}); - party.$startQuest({key:key}, function(){ - party.$syncParty(); - $state.go('options.social.party'); - resolve(); - }); + Analytics.track({'hitType':'event', 'eventCategory':'behavior', 'eventAction':'quest', 'owner':true, 'response':'accept', 'questName': key}); + Analytics.updateUser({'partyID': party._id, 'partySize': party.memberCount}); + Groups.Group.inviteToQuest(party._id, key) + .then(function(response) { + party.quest = response.data.data; + Groups.data.party = party; + $state.go('options.social.party'); + resolve(); + }); }); } function sendAction(action) { return $q(function(resolve, reject) { - - $http.post(ApiUrl.get() + '/api/v2/groups/' + party._id + '/' + action) + $http.post(ApiUrl.get() + '/api/v3/groups/' + party._id + '/' + action) .then(function(response) { User.sync(); @@ -142,5 +134,4 @@ showQuest: showQuest, initQuest: initQuest } - } -}()); + }]); diff --git a/website/views/options/social/quests/questActive.jade b/website/views/options/social/quests/questActive.jade index 969861e8c1..b457f8d0e3 100644 --- a/website/views/options/social/quests/questActive.jade +++ b/website/views/options/social/quests/questActive.jade @@ -23,7 +23,7 @@ div(ng-if='group.quest.active===true') include ./ianQuestInfo unless tavern - button.btn.btn-sm.btn-warning(ng-if='::canEditQuest(party)', + button.btn.btn-sm.btn-warning(ng-if='::canEditQuest()', ng-click='questAbort()')=env.t('abort') button.btn.btn-sm.btn-warning(ng-if='!(group.quest.leader && group.quest.leader === user._id) && isMemberOfRunningQuest(user._id,group)', ng-click='questLeave()')=env.t('leaveQuest') diff --git a/website/views/options/social/quests/questNotActive.jade b/website/views/options/social/quests/questNotActive.jade index 3472011e29..c20ae5a226 100644 --- a/website/views/options/social/quests/questNotActive.jade +++ b/website/views/options/social/quests/questNotActive.jade @@ -26,6 +26,6 @@ div(ng-if='group.quest.active===false') button.btn.btn-sm.btn-success(ng-click='questAccept()')=env.t('accept') button.btn.btn-sm.btn-danger(ng-click='questReject()')=env.t('reject') - span(ng-if='::canEditQuest(party)') - button.btn.btn-sm.btn-warning(ng-click='party.$startQuest({"force":true})')=env.t('begin') + span(ng-if='::canEditQuest()') + button.btn.btn-sm.btn-warning(ng-click='questForceStart()')=env.t('begin') button.btn.btn-sm.btn-danger(ng-click='questCancel()')=env.t('cancel')