v3: port hatch, equip and feed ops

This commit is contained in:
Matteo Pagliazzi
2016-03-24 16:08:58 +01:00
parent 23f92e2d28
commit 0a65e6a6f1
15 changed files with 783 additions and 336 deletions
+85
View File
@@ -0,0 +1,85 @@
/* eslint-disable camelcase */
import equip from '../../../common/script/ops/equip';
import i18n from '../../../common/script/i18n';
import {
generateUser,
} from '../../helpers/common.helper';
import content from '../../../common/script/content/index';
describe('shared.ops.equip', () => {
let user;
beforeEach(() => {
user = generateUser({
items: {
gear: {
owned: {
weapon_warrior_0: true,
weapon_warrior_1: true,
weapon_warrior_2: true,
weapon_wizard_1: true,
weapon_wizard_2: true,
shield_base_0: true,
shield_warrior_1: true,
},
equipped: {
weapon: 'weapon_warrior_0',
shield: 'shield_base_0',
},
},
},
stats: {gp: 200},
});
});
context('Gear', () => {
it('should not send a message if a weapon is equipped while only having zero or one weapons equipped', () => {
equip(user, {params: {key: 'weapon_warrior_1'}});
// one-handed to one-handed
let res = equip(user, {params: {key: 'weapon_warrior_2'}});
expect(res.message).to.not.exists;
// one-handed to two-handed
res = equip(user, {params: {key: 'weapon_wizard_1'}});
expect(res.message).to.not.exists;
// two-handed to two-handed
res = equip(user, {params: {key: 'weapon_wizard_2'}});
expect(res.message).to.not.exists;
// two-handed to one-handed
res = equip(user, {params: {key: 'weapon_warrior_2'}});
expect(res.message).to.not.exists;
});
it('should send messages if equipping a two-hander causes the off-hander to be unequipped', () => {
equip(user, {params: {key: 'weapon_warrior_1'}});
equip(user, {params: {key: 'shield_warrior_1'}});
// equipping two-hander
let res = equip(user, {params: {key: 'weapon_wizard_1'}});
let weapon = content.gear.flat.weapon_wizard_1;
let item = content.gear.flat.shield_warrior_1;
expect(res).to.eql({
message: i18n.t('messageTwoHandedEquip', {twoHandedText: weapon.text(), offHandedText: item.text()}),
data: user.items,
});
});
it('should send messages if equipping an off-hand item causes a two-handed weapon to be unequipped', () => {
// equipping two-hander
equip(user, {params: {key: 'weapon_wizard_1'}});
let weapon = content.gear.flat.weapon_wizard_1;
let shield = content.gear.flat.shield_warrior_1;
let res = equip(user, {params: {key: 'shield_warrior_1'}});
expect(res).to.eql({
message: i18n.t('messageTwoHandedUnequip', {twoHandedText: weapon.text(), offHandedText: shield.text()}),
data: user.items,
});
});
});
});
+219
View File
@@ -0,0 +1,219 @@
import feed from '../../../common/script/ops/feed';
import content from '../../../common/script/content';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../../common/script/libs/errors';
import i18n from '../../../common/script/i18n';
import {
generateUser,
} from '../../helpers/common.helper';
describe('shared.ops.feed', () => {
let user;
beforeEach(() => {
user = generateUser();
});
context('failure conditions', () => {
it('does not allow feeding without specifying pet and food', () => {
try {
feed(user);
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.equal(i18n.t('missingPetFoodFeed'));
}
});
it('does not allow feeding if pet name format is invalid', () => {
try {
feed(user, {params: {pet: 'invalid', food: 'food'}});
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.equal(i18n.t('invalidPetName'));
}
});
it('does not allow feeding if food does not exists', () => {
try {
feed(user, {params: {pet: 'valid-pet', food: 'invalid food name'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotFound);
expect(err.message).to.equal(i18n.t('messageFoodNotFound'));
}
});
it('does not allow feeding if pet is not owned', () => {
try {
feed(user, {params: {pet: 'not-owned', food: 'Meat'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotFound);
expect(err.message).to.equal(i18n.t('messagePetNotFound'));
}
});
it('does not allow feeding if food is not owned', () => {
user.items.pets['Wolf-Base'] = 5;
try {
feed(user, {params: {pet: 'Wolf-Base', food: 'Meat'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotFound);
expect(err.message).to.equal(i18n.t('messageFoodNotFound'));
}
});
it('does not allow feeding of special pets', () => {
user.items.pets['Wolf-Veteran'] = 5;
user.items.food.Meat = 1;
try {
feed(user, {params: {pet: 'Wolf-Veteran', food: 'Meat'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('messageCannotFeedPet'));
}
});
it('does not allow feeding of mounts', () => {
user.items.pets['Wolf-Base'] = -1;
user.items.mounts['Wolf-Base'] = true;
user.items.food.Meat = 1;
try {
feed(user, {params: {pet: 'Wolf-Base', food: 'Meat'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('messageAlreadyMount'));
}
});
});
context('successful feeding', () => {
it('evolves the pet if the food is a Saddle', () => {
user.items.pets['Wolf-Base'] = 5;
user.items.food.Saddle = 2;
user.items.currentPet = 'Wolf-Base';
let [egg, potion] = 'Wolf-Base'.split('-');
let potionText = content.hatchingPotions[potion] ? content.hatchingPotions[potion].text() : potion;
let eggText = content.eggs[egg] ? content.eggs[egg].text() : egg;
let res = feed(user, {params: {pet: 'Wolf-Base', food: 'Saddle'}});
expect(res).to.eql({
data: user.items.pets['Wolf-Base'],
message: i18n.t('messageEvolve', {
egg: i18n.t('petName', {
potion: potionText,
egg: eggText,
}),
}),
});
expect(user.items.food.Saddle).to.equal(1);
expect(user.items.pets['Wolf-Base']).to.equal(-1);
expect(user.items.mounts['Wolf-Base']).to.equal(true);
expect(user.items.currentPet).to.equal('');
});
it('enjoys the food', () => {
user.items.pets['Wolf-Base'] = 5;
user.items.food.Meat = 2;
let food = content.food.Meat;
let [egg, potion] = 'Wolf-Base'.split('-');
let potionText = content.hatchingPotions[potion] ? content.hatchingPotions[potion].text() : potion;
let eggText = content.eggs[egg] ? content.eggs[egg].text() : egg;
let res = feed(user, {params: {pet: 'Wolf-Base', food: 'Meat'}});
expect(res).to.eql({
data: user.items.pets['Wolf-Base'],
message: i18n.t('messageLikesFood', {
egg: i18n.t('petName', {
potion: potionText,
egg: eggText,
}),
foodText: food.text(),
}),
});
expect(user.items.food.Meat).to.equal(1);
expect(user.items.pets['Wolf-Base']).to.equal(10);
});
it('enjoys the food (premium potion)', () => {
user.items.pets['Wolf-Spooky'] = 5;
user.items.food.Milk = 2;
let food = content.food.Milk;
let [egg, potion] = 'Wolf-Spooky'.split('-');
let potionText = content.hatchingPotions[potion] ? content.hatchingPotions[potion].text() : potion;
let eggText = content.eggs[egg] ? content.eggs[egg].text() : egg;
let res = feed(user, {params: {pet: 'Wolf-Spooky', food: 'Milk'}});
expect(res).to.eql({
data: user.items.pets['Wolf-Spooky'],
message: i18n.t('messageLikesFood', {
egg: i18n.t('petName', {
potion: potionText,
egg: eggText,
}),
foodText: food.text(),
}),
});
expect(user.items.food.Milk).to.equal(1);
expect(user.items.pets['Wolf-Spooky']).to.equal(10);
});
it('does not like the food', () => {
user.items.pets['Wolf-Base'] = 5;
user.items.food.Milk = 2;
let food = content.food.Milk;
let [egg, potion] = 'Wolf-Base'.split('-');
let potionText = content.hatchingPotions[potion] ? content.hatchingPotions[potion].text() : potion;
let eggText = content.eggs[egg] ? content.eggs[egg].text() : egg;
let res = feed(user, {params: {pet: 'Wolf-Base', food: 'Milk'}});
expect(res).to.eql({
data: user.items.pets['Wolf-Base'],
message: i18n.t('messageDontEnjoyFood', {
egg: i18n.t('petName', {
potion: potionText,
egg: eggText,
}),
foodText: food.text(),
}),
});
expect(user.items.food.Milk).to.equal(1);
expect(user.items.pets['Wolf-Base']).to.equal(7);
});
it('evolves the pet into a mount when feeding user.items.pets[pet] >= 50', () => {
user.items.pets['Wolf-Base'] = 49;
user.items.food.Milk = 2;
user.items.currentPet = 'Wolf-Base';
let [egg, potion] = 'Wolf-Base'.split('-');
let potionText = content.hatchingPotions[potion] ? content.hatchingPotions[potion].text() : potion;
let eggText = content.eggs[egg] ? content.eggs[egg].text() : egg;
let res = feed(user, {params: {pet: 'Wolf-Base', food: 'Milk'}});
expect(res).to.eql({
data: user.items.pets['Wolf-Base'],
message: i18n.t('messageEvolve', {
egg: i18n.t('petName', {
potion: potionText,
egg: eggText,
}),
}),
});
expect(user.items.food.Milk).to.equal(1);
expect(user.items.pets['Wolf-Base']).to.equal(-1);
expect(user.items.mounts['Wolf-Base']).to.equal(true);
expect(user.items.currentPet).to.equal('');
});
});
});
+143
View File
@@ -0,0 +1,143 @@
import hatch from '../../../common/script/ops/hatch';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../../common/script/libs/errors';
import i18n from '../../../common/script/i18n';
import {
generateUser,
} from '../../helpers/common.helper';
describe('shared.ops.hatch', () => {
let user;
beforeEach(() => {
user = generateUser();
});
context('Pet Hatching', () => {
context('failure conditions', () => {
it('does not allow hatching without specifying egg and potion', () => {
user.items.pets = {};
try {
hatch(user);
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.equal(i18n.t('missingEggHatchingPotionHatch'));
expect(user.items.pets).to.be.empty;
}
});
it('does not allow hatching if user lacks specified egg', () => {
user.items.eggs.Wolf = 1;
user.items.hatchingPotions.Base = 1;
user.items.pets = {};
try {
hatch(user, {params: {egg: 'Dragon', hatchingPotion: 'Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotFound);
expect(err.message).to.equal(i18n.t('messageMissingEggPotion'));
expect(user.items.pets).to.be.empty;
expect(user.items.eggs.Wolf).to.equal(1);
expect(user.items.hatchingPotions.Base).to.equal(1);
}
});
it('does not allow hatching if user lacks specified hatching potion', () => {
user.items.eggs.Wolf = 1;
user.items.hatchingPotions.Base = 1;
user.items.pets = {};
try {
hatch(user, {params: {egg: 'Wolf', hatchingPotion: 'Golden'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotFound);
expect(err.message).to.equal(i18n.t('messageMissingEggPotion'));
expect(user.items.pets).to.be.empty;
expect(user.items.eggs.Wolf).to.equal(1);
expect(user.items.hatchingPotions.Base).to.equal(1);
}
});
it('does not allow hatching if user already owns target pet', () => {
user.items.eggs = {Wolf: 1};
user.items.hatchingPotions = {Base: 1};
user.items.pets = {'Wolf-Base': 10};
try {
hatch(user, {params: {egg: 'Wolf', hatchingPotion: 'Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('messageAlreadyPet'));
expect(user.items.pets).to.eql({'Wolf-Base': 10});
expect(user.items.eggs).to.eql({Wolf: 1});
expect(user.items.hatchingPotions).to.eql({Base: 1});
}
});
it('does not allow hatching quest pet egg using premium potion', () => {
user.items.eggs = {Cheetah: 1};
user.items.hatchingPotions = {Spooky: 1};
user.items.pets = {};
try {
hatch(user, {params: {egg: 'Cheetah', hatchingPotion: 'Spooky'}});
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.equal(i18n.t('messageInvalidEggPotionCombo'));
expect(user.items.pets).to.be.empty;
expect(user.items.eggs).to.eql({Cheetah: 1});
expect(user.items.hatchingPotions).to.eql({Spooky: 1});
}
});
});
context('successful hatching', () => {
it('hatches a basic pet', () => {
user.items.eggs = {Wolf: 1};
user.items.hatchingPotions = {Base: 1};
user.items.pets = {};
let res = hatch(user, {params: {egg: 'Wolf', hatchingPotion: 'Base'}});
expect(res.message).to.equal(i18n.t('messageHatched'));
expect(res.data).to.eql(user.items);
expect(user.items.pets).to.eql({'Wolf-Base': 5});
expect(user.items.eggs).to.eql({Wolf: 0});
expect(user.items.hatchingPotions).to.eql({Base: 0});
});
it('hatches a quest pet', () => {
user.items.eggs = {Cheetah: 1};
user.items.hatchingPotions = {Base: 1};
user.items.pets = {};
let res = hatch(user, {params: {egg: 'Cheetah', hatchingPotion: 'Base'}});
expect(res.message).to.equal(i18n.t('messageHatched'));
expect(res.data).to.eql(user.items);
expect(user.items.pets).to.eql({'Cheetah-Base': 5});
expect(user.items.eggs).to.eql({Cheetah: 0});
expect(user.items.hatchingPotions).to.eql({Base: 0});
});
it('hatches a premium pet', () => {
user.items.eggs = {Wolf: 1};
user.items.hatchingPotions = {Spooky: 1};
user.items.pets = {};
let res = hatch(user, {params: {egg: 'Wolf', hatchingPotion: 'Spooky'}});
expect(res.message).to.equal(i18n.t('messageHatched'));
expect(res.data).to.eql(user.items);
expect(user.items.pets).to.eql({'Wolf-Spooky': 5});
expect(user.items.eggs).to.eql({Wolf: 0});
expect(user.items.hatchingPotions).to.eql({Spooky: 0});
});
it('hatches a pet previously raised to a mount', () => {
user.items.eggs = {Wolf: 1};
user.items.hatchingPotions = {Base: 1};
user.items.pets = {'Wolf-Base': -1};
let res = hatch(user, {params: {egg: 'Wolf', hatchingPotion: 'Base'}});
expect(res.message).to.eql(i18n.t('messageHatched'));
expect(res.data).to.eql(user.items);
expect(user.items.pets).to.eql({'Wolf-Base': 5});
expect(user.items.eggs).to.eql({Wolf: 0});
expect(user.items.hatchingPotions).to.eql({Base: 0});
});
});
});
});