2015-08-19 13:20:13 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-09-09 18:13:21 +00:00
|
|
|
habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User','Challenges','$state','$compile','Analytics','Quests','Social', 'Achievement',
|
|
|
|
|
function($rootScope, $scope, Groups, Chat, User, Challenges, $state, $compile, Analytics, Quests, Social, Achievement) {
|
2016-03-09 01:55:18 +00:00
|
|
|
|
2016-08-12 13:53:03 +00:00
|
|
|
var PARTY_LOADING_MESSAGES = 4;
|
|
|
|
|
|
2016-03-09 01:55:18 +00:00
|
|
|
var user = User.user;
|
|
|
|
|
|
2015-08-19 13:20:13 +00:00
|
|
|
$scope.type = 'party';
|
|
|
|
|
$scope.text = window.env.t('party');
|
2016-08-12 13:53:03 +00:00
|
|
|
$scope.group = {loadingParty: true};
|
2016-12-21 19:45:45 +00:00
|
|
|
$scope.groupPanel = 'chat';
|
2016-04-25 21:11:23 +00:00
|
|
|
|
2015-08-19 13:20:13 +00:00
|
|
|
$scope.inviteOrStartParty = Groups.inviteOrStartParty;
|
2015-10-29 20:33:27 +00:00
|
|
|
$scope.loadWidgets = Social.loadWidgets;
|
2015-08-19 13:20:13 +00:00
|
|
|
|
2016-08-12 13:53:03 +00:00
|
|
|
// Random message between 1 and PARTY_LOADING_MESSAGES
|
|
|
|
|
var partyMessageNumber = Math.floor(Math.random() * PARTY_LOADING_MESSAGES) + 1;
|
|
|
|
|
$scope.partyLoadingMessage = window.env.t('partyLoading' + partyMessageNumber);
|
|
|
|
|
|
2016-05-23 08:40:04 +00:00
|
|
|
function handlePartyResponse (group) {
|
2016-09-08 16:48:07 +00:00
|
|
|
// Assign and not replace so that all the references get the modifications
|
|
|
|
|
_.assign($rootScope.party, group);
|
|
|
|
|
$scope.group = $rootScope.party;
|
|
|
|
|
$scope.group.loadingParty = false;
|
2016-05-23 08:40:04 +00:00
|
|
|
checkForNotifications();
|
2016-09-09 18:12:17 +00:00
|
|
|
if ($state.is('options.social.party')) {
|
|
|
|
|
if ('Notification' in window && window.Notification.permission === 'default') {
|
2016-09-09 19:26:40 +00:00
|
|
|
setTimeout(function () {
|
2016-09-13 21:08:54 +00:00
|
|
|
var notifsModal = $rootScope.openModal('enableDesktopNotifications', {
|
|
|
|
|
backdrop: true,
|
|
|
|
|
windowClass: 'vertically-centered-modals',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Safari doesn't support promises
|
|
|
|
|
var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
|
|
|
|
|
|
|
|
|
function closeModal () { notifsModal.close(); }
|
|
|
|
|
if (isSafari) {
|
|
|
|
|
window.Notification.requestPermission(closeModal);
|
|
|
|
|
} else {
|
|
|
|
|
window.Notification.requestPermission().then(closeModal);
|
|
|
|
|
}
|
2016-09-09 19:26:40 +00:00
|
|
|
}, 100);
|
2016-09-09 18:12:17 +00:00
|
|
|
}
|
|
|
|
|
Chat.markChatSeen($scope.group._id);
|
|
|
|
|
}
|
2016-05-23 08:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handlePartyError (response) {
|
|
|
|
|
$rootScope.party = $scope.group = $scope.newGroup = { type: 'party' };
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 13:52:28 +00:00
|
|
|
if ($state.is('options.social.party') && $rootScope.party && $rootScope.party.id) {
|
2016-09-06 09:18:52 +00:00
|
|
|
Groups.party().then(handlePartyResponse, handlePartyError);
|
2016-05-23 08:40:04 +00:00
|
|
|
} else {
|
|
|
|
|
Groups.Group.syncParty().then(handlePartyResponse, handlePartyError);
|
|
|
|
|
}
|
2016-05-05 18:02:56 +00:00
|
|
|
|
2016-04-25 21:11:23 +00:00
|
|
|
function checkForNotifications () {
|
2016-03-09 01:55:18 +00:00
|
|
|
// Checks if user's party has reached 2 players for the first time.
|
|
|
|
|
if(!user.achievements.partyUp
|
|
|
|
|
&& $scope.group.memberCount >= 2) {
|
2016-03-11 01:38:53 +00:00
|
|
|
User.set({'achievements.partyUp':true});
|
2016-09-09 12:58:44 +00:00
|
|
|
Achievement.displayAchievement('partyUp');
|
2016-03-09 01:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks if user's party has reached 4 players for the first time.
|
|
|
|
|
if(!user.achievements.partyOn
|
|
|
|
|
&& $scope.group.memberCount >= 4) {
|
2016-03-11 01:38:53 +00:00
|
|
|
User.set({'achievements.partyOn':true});
|
2016-09-09 12:58:44 +00:00
|
|
|
Achievement.displayAchievement('partyOn');
|
2016-03-09 01:55:18 +00:00
|
|
|
}
|
2015-08-19 13:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-27 13:59:10 +00:00
|
|
|
$scope.create = function(group) {
|
2016-05-23 12:15:46 +00:00
|
|
|
group.loadingParty = true;
|
|
|
|
|
|
2015-08-19 13:20:13 +00:00
|
|
|
if (!group.name) group.name = env.t('possessiveParty', {name: User.user.profile.name});
|
2016-05-11 17:34:29 +00:00
|
|
|
Groups.Group.create(group)
|
|
|
|
|
.then(function(response) {
|
2016-05-12 14:45:53 +00:00
|
|
|
Analytics.updateUser({'party.id': $scope.group ._id, 'partySize': 1});
|
2016-06-19 14:06:21 +00:00
|
|
|
$rootScope.hardRedirect('/#/options/groups/party');
|
2016-05-11 17:34:29 +00:00
|
|
|
});
|
2015-08-19 13:20:13 +00:00
|
|
|
};
|
|
|
|
|
|
2016-04-25 21:11:23 +00:00
|
|
|
$scope.join = function (party) {
|
|
|
|
|
Groups.Group.join(party.id)
|
|
|
|
|
.then(function (response) {
|
2016-05-18 10:16:31 +00:00
|
|
|
$rootScope.party = $scope.group = response.data.data;
|
2016-05-18 09:22:32 +00:00
|
|
|
User.sync();
|
2016-04-25 21:11:23 +00:00
|
|
|
Analytics.updateUser({'partyID': party.id});
|
|
|
|
|
$rootScope.hardRedirect('/#/options/groups/party');
|
|
|
|
|
});
|
2015-08-19 13:20:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO: refactor guild and party leave into one function
|
2016-04-25 21:11:23 +00:00
|
|
|
$scope.leave = function (keep) {
|
2015-08-19 13:20:13 +00:00
|
|
|
if (keep == 'cancel') {
|
|
|
|
|
$scope.selectedGroup = undefined;
|
|
|
|
|
$scope.popoverEl.popover('destroy');
|
|
|
|
|
} else {
|
2016-04-25 21:11:23 +00:00
|
|
|
Groups.Group.leave($scope.selectedGroup._id, keep)
|
|
|
|
|
.then(function (response) {
|
|
|
|
|
Analytics.updateUser({'partySize':null,'partyID':null});
|
2016-05-15 09:06:20 +00:00
|
|
|
User.sync().then(function () {
|
|
|
|
|
$rootScope.hardRedirect('/#/options/groups/party');
|
|
|
|
|
});
|
2016-04-25 21:11:23 +00:00
|
|
|
});
|
2015-08-19 13:20:13 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TODO: refactor guild and party clickLeave into one function
|
|
|
|
|
$scope.clickLeave = function(group, $event){
|
2015-09-07 16:02:01 +00:00
|
|
|
Analytics.track({'hitType':'event','eventCategory':'button','eventAction':'click','eventLabel':'Leave Party'});
|
2015-08-19 13:20:13 +00:00
|
|
|
$scope.selectedGroup = group;
|
2016-03-04 16:16:37 +00:00
|
|
|
$scope.popoverEl = $($event.target).closest('.btn');
|
2015-08-19 13:20:13 +00:00
|
|
|
var html, title;
|
2016-04-25 21:11:23 +00:00
|
|
|
html = $compile('<a ng-controller="GroupsCtrl" ng-click="leave(\'remove-all\')">' + window.env.t('removeTasks') + '</a><br/>\n<a ng-click="leave(\'keep-all\')">' + window.env.t('keepTasks') + '</a><br/>\n<a ng-click="leave(\'cancel\')">' + window.env.t('cancel') + '</a><br/>')($scope);
|
|
|
|
|
title = window.env.t('leavePartyCha');
|
|
|
|
|
|
|
|
|
|
//TODO: Move this to challenge service
|
2016-05-11 19:34:36 +00:00
|
|
|
Challenges.getGroupChallenges(group._id)
|
|
|
|
|
.then(function(response) {
|
|
|
|
|
var challenges = _.pluck(_.filter(response.data.data, function(c) {
|
|
|
|
|
return c.group._id == group._id;
|
|
|
|
|
}), '_id');
|
|
|
|
|
|
|
|
|
|
if (_.intersection(challenges, User.user.challenges).length > 0) {
|
|
|
|
|
html = $compile(
|
|
|
|
|
'<a ng-controller="GroupsCtrl" ng-click="leave(\'remove-all\')">' + window.env.t('removeTasks') + '</a><br/>\n<a ng-click="leave(\'keep-all\')">' + window.env.t('keepTasks') + '</a><br/>\n<a ng-click="leave(\'cancel\')">' + window.env.t('cancel') + '</a><br/>'
|
|
|
|
|
)($scope);
|
|
|
|
|
title = window.env.t('leavePartyCha');
|
|
|
|
|
} else {
|
|
|
|
|
html = $compile(
|
|
|
|
|
'<a ng-controller="GroupsCtrl" ng-click="leave(\'keep-all\')">' + window.env.t('confirm') + '</a><br/>\n<a ng-click="leave(\'cancel\')">' + window.env.t('cancel') + '</a><br/>'
|
|
|
|
|
)($scope);
|
|
|
|
|
title = window.env.t('leaveParty');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.popoverEl.popover('destroy').popover({
|
|
|
|
|
html: true,
|
|
|
|
|
placement: 'top',
|
|
|
|
|
trigger: 'manual',
|
|
|
|
|
title: title,
|
|
|
|
|
content: html
|
|
|
|
|
}).popover('show');
|
|
|
|
|
});
|
2015-08-19 13:20:13 +00:00
|
|
|
};
|
|
|
|
|
|
2016-04-25 21:11:23 +00:00
|
|
|
$scope.clickStartQuest = function () {
|
2015-09-07 16:02:01 +00:00
|
|
|
Analytics.track({'hitType':'event','eventCategory':'button','eventAction':'click','eventLabel':'Start a Quest'});
|
2015-08-19 13:20:13 +00:00
|
|
|
var hasQuests = _.find(User.user.items.quests, function(quest) {
|
|
|
|
|
return quest > 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (hasQuests){
|
|
|
|
|
$rootScope.openModal("ownedQuests", { controller:"InventoryCtrl" });
|
|
|
|
|
} else {
|
|
|
|
|
$rootScope.$state.go('options.inventory.quests');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-22 17:19:37 +00:00
|
|
|
$scope.leaveOldPartyAndJoinNewParty = function(newPartyId, newPartyName) {
|
|
|
|
|
if (confirm('Are you sure you want to delete your party and join ' + newPartyName + '?')) {
|
2016-05-18 09:22:32 +00:00
|
|
|
Groups.Group.leave(Groups.data.party._id, false)
|
|
|
|
|
.then(function() {
|
2016-05-18 10:16:31 +00:00
|
|
|
$rootScope.party = $scope.group = {
|
2016-05-23 12:15:46 +00:00
|
|
|
loadingParty: true
|
2016-05-18 09:22:32 +00:00
|
|
|
};
|
|
|
|
|
$scope.join({ id: newPartyId, name: newPartyName });
|
|
|
|
|
});
|
2015-08-22 17:19:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 21:11:23 +00:00
|
|
|
$scope.reject = function(party) {
|
2016-06-19 01:21:00 +00:00
|
|
|
Groups.Group.rejectInvite(party.id).then(function () {
|
|
|
|
|
User.sync();
|
|
|
|
|
});
|
2015-08-19 13:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-21 10:21:39 +00:00
|
|
|
$scope.questInit = function() {
|
|
|
|
|
var key = $rootScope.selectedQuest.key;
|
|
|
|
|
|
|
|
|
|
Quests.initQuest(key).then(function() {
|
|
|
|
|
$rootScope.selectedQuest = undefined;
|
|
|
|
|
$scope.$close();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-15 15:53:56 +00:00
|
|
|
$scope.questCancel = function(){
|
2015-08-19 13:20:13 +00:00
|
|
|
if (!confirm(window.env.t('sureCancel'))) return;
|
2015-09-15 15:53:56 +00:00
|
|
|
|
2016-05-03 15:16:30 +00:00
|
|
|
Quests.sendAction('quests/cancel')
|
2015-09-15 15:53:56 +00:00
|
|
|
.then(function(quest) {
|
|
|
|
|
$scope.group.quest = quest;
|
|
|
|
|
});
|
2015-08-19 13:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-15 15:53:56 +00:00
|
|
|
$scope.questAbort = function(){
|
2015-08-19 13:20:13 +00:00
|
|
|
if (!confirm(window.env.t('sureAbort'))) return;
|
|
|
|
|
if (!confirm(window.env.t('doubleSureAbort'))) return;
|
2015-09-15 15:53:56 +00:00
|
|
|
|
2016-05-03 15:16:30 +00:00
|
|
|
Quests.sendAction('quests/abort')
|
2015-09-15 15:53:56 +00:00
|
|
|
.then(function(quest) {
|
|
|
|
|
$scope.group.quest = quest;
|
|
|
|
|
});
|
2015-08-19 13:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-15 15:53:56 +00:00
|
|
|
$scope.questLeave = function(){
|
2015-08-24 03:38:55 +00:00
|
|
|
if (!confirm(window.env.t('sureLeave'))) return;
|
|
|
|
|
|
2016-05-03 15:16:30 +00:00
|
|
|
Quests.sendAction('quests/leave')
|
2015-09-15 15:53:56 +00:00
|
|
|
.then(function(quest) {
|
|
|
|
|
$scope.group.quest = quest;
|
|
|
|
|
});
|
2015-08-24 03:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-15 15:53:56 +00:00
|
|
|
$scope.questAccept = function(){
|
2016-05-03 15:16:30 +00:00
|
|
|
Quests.sendAction('quests/accept')
|
|
|
|
|
.then(function(quest) {
|
|
|
|
|
$scope.group.quest = quest;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.questForceStart = function(){
|
|
|
|
|
Quests.sendAction('quests/force-start')
|
2015-09-15 15:53:56 +00:00
|
|
|
.then(function(quest) {
|
|
|
|
|
$scope.group.quest = quest;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.questReject = function(){
|
2016-05-03 15:16:30 +00:00
|
|
|
Quests.sendAction('quests/reject')
|
2015-09-15 15:53:56 +00:00
|
|
|
.then(function(quest) {
|
|
|
|
|
$scope.group.quest = quest;
|
|
|
|
|
});
|
2015-08-19 13:20:13 +00:00
|
|
|
};
|
|
|
|
|
|
2016-05-03 15:16:30 +00:00
|
|
|
$scope.canEditQuest = function() {
|
|
|
|
|
var isQuestLeader = $scope.group.quest && $scope.group.quest.leader === User.user._id;
|
2015-09-15 15:53:56 +00:00
|
|
|
|
|
|
|
|
return isQuestLeader;
|
|
|
|
|
};
|
2015-08-19 13:20:13 +00:00
|
|
|
}
|
|
|
|
|
]);
|