From 203a26b6dfee506438bce7cc5d0b09ff0e2a96dd Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Wed, 12 Aug 2015 08:36:57 -0500 Subject: [PATCH] Refactor how cards are recieved --- common/locales/en/limited.json | 13 +++-- common/script/index.coffee | 11 ++-- test/common/user.ops.test.js | 41 +++++++++++++++ test/spec/controllers/inventoryCtrlSpec.js | 44 +++++++++++++++- .../public/js/controllers/inventoryCtrl.js | 18 ++++++- website/views/options/inventory/drops.jade | 4 +- website/views/shared/modals/limited.jade | 51 +++---------------- 7 files changed, 121 insertions(+), 61 deletions(-) create mode 100644 test/common/user.ops.test.js diff --git a/common/locales/en/limited.json b/common/locales/en/limited.json index 26d22f14fa..0f84ac8f10 100644 --- a/common/locales/en/limited.json +++ b/common/locales/en/limited.json @@ -11,6 +11,7 @@ "aquaticFriends": "Aquatic Friends", "aquaticFriendsText": "Got splashed <%= seafoam %> times by party members.", "valentineCard": "Valentine's Day Card", + "valentineCardExplanation": "For enduring such a saccharine poem, you both receive the \"Adoring Friends\" badge!", "valentineCardNotes": "Send a Valentine's Day card to a party member.", "valentine0": "\"Roses are red\n\nMy Dailies are blue\n\nI'm happy that I'm\n\nIn a Party with you!\"", "valentine1": "\"Roses are red\n\nViolets are nice\n\nLet's get together\n\nAnd fight against Vice!\"", @@ -33,16 +34,18 @@ "skiSet": "Ski-sassin (Rogue)", "snowflakeSet": "Snowflake (Healer)", "yetiSet": "Yeti Tamer (Warrior)", + "toAndFromCard": "To: <%= toName %>, From: <%= fromName %>", "nyeCard": "New Year's Card", + "nyeCardExplanation": "For celebrating the new year together, you both receive the \"Auld Acquaintance\" badge!", "nyeCardNotes": "Send a New Year's card to a party member.", "seasonalItems": "Seasonal Items", "auldAcquaintance": "Auld Acquaintance", "auldAcquaintanceText": "Happy New Year! Sent or received <%= cards %> New Year's cards.", - "newYear0": "Happy New Year! May you slay many a bad Habit.", - "newYear1": "Happy New Year! May you reap many Rewards.", - "newYear2": "Happy New Year! May you earn many a Perfect Day.", - "newYear3": "Happy New Year! May your To-Do list stay short and sweet.", - "newYear4": "Happy New Year! May you not get attacked by a raging Hippogriff.", + "nye0": "Happy New Year! May you slay many a bad Habit.", + "nye1": "Happy New Year! May you reap many Rewards.", + "nye2": "Happy New Year! May you earn many a Perfect Day.", + "nye3": "Happy New Year! May your To-Do list stay short and sweet.", + "nye4": "Happy New Year! May you not get attacked by a raging Hippogriff.", "holidayCard": "Received a holiday card!", "mightyBunnySet": "Mighty Bunny (Warrior)", "magicMouseSet": "Magic Mouse (Mage)", diff --git a/common/script/index.coffee b/common/script/index.coffee index 27b44392f1..cb199f148c 100644 --- a/common/script/index.coffee +++ b/common/script/index.coffee @@ -1168,9 +1168,9 @@ api.wrap = (user, main=true) -> user.stats.mp++ if stat is 'int' #increase their MP along with their max MP cb? null, _.pick(user,$w 'stats') - readValentine: (req,cb) -> - user.items.special.valentineReceived.shift() - user.markModified? 'items.special.valentineReceived' + readCard: (cardType, cb) -> + user.items.special["#{cardType}Received"].shift() + user.markModified? "items.special.#{cardType}Received" cb? null, 'items.special' openMysteryItem: (req,cb,analytics) -> @@ -1191,11 +1191,6 @@ api.wrap = (user, main=true) -> (user._tmp?={}).drop = item if typeof window != 'undefined' cb? null, user.items.gear.owned - readNYE: (req,cb) -> - user.items.special.nyeReceived.shift() - user.markModified? 'items.special.nyeReceived' - cb? null, 'items.special' - # ------ # Score # ------ diff --git a/test/common/user.ops.test.js b/test/common/user.ops.test.js new file mode 100644 index 0000000000..c5ea8d9132 --- /dev/null +++ b/test/common/user.ops.test.js @@ -0,0 +1,41 @@ +var sinon = require('sinon'); +var chai = require("chai") +chai.use(require("sinon-chai")) +var expect = chai.expect +var _ = require('lodash'); + +require('coffee-script'); +var shared = require('../../common/script/index.coffee'); + +describe('user.ops', function() { + var user; + + beforeEach(function() { + user = { + items: { + gear: { }, + special: { } + }, + achievements: { }, + flags: { } + }; + + shared.wrap(user); + }); + + describe('readCard', function() { + it('removes card from invitation array', function() { + user.items.special.valentineReceived = ['Leslie']; + user.ops.readCard('valentine'); + + expect(user.items.special.valentineReceived).to.be.empty; + }); + + it('removes the first card from invitation array', function() { + user.items.special.valentineReceived = ['Leslie', 'Vicky']; + user.ops.readCard('valentine'); + + expect(user.items.special.valentineReceived).to.eql(['Vicky']); + }); + }); +}); diff --git a/test/spec/controllers/inventoryCtrlSpec.js b/test/spec/controllers/inventoryCtrlSpec.js index 1d8d0c95c3..339d9a8cd7 100644 --- a/test/spec/controllers/inventoryCtrlSpec.js +++ b/test/spec/controllers/inventoryCtrlSpec.js @@ -1,7 +1,7 @@ 'use strict'; describe('Inventory Controller', function() { - var scope, ctrl, user, $rootScope; + var scope, ctrl, user, rootScope; beforeEach(function() { module(function($provide) { @@ -23,6 +23,7 @@ describe('Inventory Controller', function() { } }; scope = $rootScope.$new(); + rootScope = $rootScope; // Load RootCtrl to ensure shared behaviors are loaded $controller('RootCtrl', {$scope: scope, User: {user: user}, $window: mockWindow}); @@ -109,4 +110,45 @@ describe('Inventory Controller', function() { expect(scope.selectedEgg).to.eql(null); }); }); + + describe('openCardsModal', function(type, numberOfVariations) { + var cardsModalScope; + + beforeEach(function() { + cardsModalScope = {}; + sandbox.stub(rootScope, 'openModal'); + sandbox.stub(rootScope, '$new').returns(cardsModalScope); + }); + + it('opens cards modal', function() { + scope.openCardsModal('valentine', 4); + + expect(rootScope.openModal).to.be.calledOnce; + expect(rootScope.openModal).to.be.calledWith( + 'cards' + ); + }); + + it('instantiates a new scope for the modal', function() { + scope.openCardsModal('valentine', 4); + + expect(rootScope.$new).to.be.calledOnce; + expect(cardsModalScope.cardType).to.eql('valentine'); + expect(cardsModalScope.cardMessage).to.exist; + }); + + it('provides a card message', function() { + scope.openCardsModal('valentine', 1); + + expect(cardsModalScope.cardMessage).to.eql(env.t('valentine0')); + }); + + it('randomly generates message from x number of messages', function() { + var possibleValues = [env.t('valentine0'), env.t('valentine1')]; + + scope.openCardsModal('valentine', 2); + + expect(possibleValues).to.contain(cardsModalScope.cardMessage); + }); + }); }); diff --git a/website/public/js/controllers/inventoryCtrl.js b/website/public/js/controllers/inventoryCtrl.js index b46b0cef96..4edad98680 100644 --- a/website/public/js/controllers/inventoryCtrl.js +++ b/website/public/js/controllers/inventoryCtrl.js @@ -216,7 +216,17 @@ habitrpg.controller("InventoryCtrl", $scope.selectedFood = null; $scope.selectedPotion = null; $scope.selectedEgg = null; - } + }; + + $scope.openCardsModal = function(type, numberOfVariations) { + var cardsModalScope = $rootScope.$new(); + cardsModalScope.cardType = type; + cardsModalScope.cardMessage = _generateCard(type, numberOfVariations); + + $rootScope.openModal('cards', { + scope: cardsModalScope + }); + }; function _updateDropAnimalCount(items) { $scope.petCount = Shared.count.beastMasterProgress(items.pets); @@ -224,5 +234,11 @@ habitrpg.controller("InventoryCtrl", $scope.beastMasterProgress = Stats.beastMasterProgress(items.pets); $scope.mountMasterProgress = Stats.mountMasterProgress(items.mounts); } + + function _generateCard(kind, numberOfVariations) { + var random = Math.random() * numberOfVariations; + var selection = Math.floor(random); + return env.t(kind + selection); + } } ]); diff --git a/website/views/options/inventory/drops.jade b/website/views/options/inventory/drops.jade index e8443328a7..2b63ce6a03 100644 --- a/website/views/options/inventory/drops.jade +++ b/website/views/options/inventory/drops.jade @@ -67,7 +67,7 @@ popover="Valentine's Day Card from {{User.user.items.special.valentineReceived[0]}}", popover-trigger='mouseenter', popover-placement='right', popover-append-to-body='true', - ng-click='openModal("valentine")') + ng-click='openCardsModal("valentine", 4)') .badge.badge-info.stack-count {{user.items.special.valentineReceived.length}} div(ng-if='user.purchased.plan.customerId || user.purchased.plan.mysteryItems.length') @@ -89,7 +89,7 @@ popover="New Year's Card from {{User.user.items.special.nyeReceived[0]}}", popover-trigger='mouseenter', popover-placement='right', popover-append-to-body='true', - ng-click='openModal("nye")') + ng-click='openCardsModal("nye", 5)') .badge.badge-info.stack-count {{user.items.special.nyeReceived.length}} .col-md-6.border-left diff --git a/website/views/shared/modals/limited.jade b/website/views/shared/modals/limited.jade index b2001e81d3..024bfb6a81 100644 --- a/website/views/shared/modals/limited.jade +++ b/website/views/shared/modals/limited.jade @@ -1,49 +1,12 @@ -//Valentine -script(id='modals/valentine.html', type='text/ng-template') +script(id='modals/cards.html', type='text/ng-template') .modal-header - h4 - .inventory_special_valentine.pull-right - =env.t('valentineCard') + .pull-right(class='inventory_special_{{::cardType}}') + h4 {{::env.t(cardType + 'Card')}} .modal-body .bg-info(style='padding:10px') - p To: {{user.profile.name}}, From: {{user.items.special.valentineReceived[0]}} + p {{::env.t('toAndFromCard', { toName: user.profile.name, fromName: user.items.special[cardType + 'Received'][0]})}} hr - ul.list-unstyled(ng-switch='::Math.floor(Math.random()*4)') - li(ng-switch-when='0') - markdown(text=env.t('valentine0')) - li(ng-switch-when='1') - markdown(text=env.t('valentine1')) - li(ng-switch-when='2') - markdown(text=env.t('valentine2')) - li(ng-switch-default) - markdown(text=env.t('valentine3')) - p - small For enduring such a saccharine poem, you both receive the "Adoring Friends" badge! + markdown(text='{{::cardMessage}}') .modal-footer - button.btn.btn-default(ng-click='user.ops.readValentine({});$close()')=env.t('ok') - -// New Year's -script(id='modals/nye.html', type='text/ng-template') - .modal-header - h4 - .inventory_special_nye.pull-right - =env.t('nyeCard') - .modal-body - .bg-info(style='padding:10px') - p To: {{user.profile.name}}, From: {{user.items.special.nyeReceived[0]}} - hr - ul.list-unstyled(ng-switch='::Math.floor(Math.random()*5)') - li(ng-switch-when='0') - =env.t('newYear0') - li(ng-switch-when='1') - =env.t('newYear1') - li(ng-switch-when='2') - =env.t('newYear2') - li(ng-switch-when='3') - =env.t('newYear3') - li(ng-switch-default) - =env.t('newYear4') - p - small For celebrating the new year together, you both receive the "Auld Acquaintance" badge! - .modal-footer - button.btn.btn-default(ng-click='user.ops.readNYE({});$close()')=env.t('ok') + small.pull-left {{::env.t(cardType + 'CardExplanation')}} + button.btn.btn-default(ng-click='user.ops.readCard(cardType, {}); $close()')=env.t('ok')