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

View File

@@ -1,52 +1,66 @@
import content from '../content/index';
import i18n from '../i18n';
import handleTwoHanded from '../fns/handleTwoHanded';
import {
NotFound,
BadRequest,
} from '../libs/errors';
import _ from 'lodash';
module.exports = function equip (user, req = {}) {
// Being type a parameter followed by another parameter
// when using the API it must be passes specifically in the URL, it's won't default to equipped
let type = _.get(req, 'params.type', 'equipped');
let key = _.get(req, 'params.key');
if (!key || !type) throw new BadRequest(i18n.t('missingTypeKeyEquip', req.language));
if (['mount', 'pet', 'costume', 'equipped'].indexOf(type) === -1) {
throw new BadRequest(i18n.t('invalidTypeEquip', req.language));
}
let message;
module.exports = function(user, req, cb) {
var item, key, message, ref, type;
ref = [req.params.type || 'equipped', req.params.key], type = ref[0], key = ref[1];
switch (type) {
case 'mount':
if (!user.items.mounts[key]) {
return typeof cb === "function" ? cb({
code: 404,
message: ":You do not own this mount."
}) : void 0;
throw new NotFound(i18n.t('mountNotOwned', req.language));
}
user.items.currentMount = user.items.currentMount === key ? '' : key;
break;
case 'pet':
if (!user.items.pets[key]) {
return typeof cb === "function" ? cb({
code: 404,
message: ":You do not own this pet."
}) : void 0;
throw new NotFound(i18n.t('petNotOwned', req.language));
}
user.items.currentPet = user.items.currentPet === key ? '' : key;
break;
case 'costume':
case 'equipped':
item = content.gear.flat[key];
if (!user.items.gear.owned[key]) {
return typeof cb === "function" ? cb({
code: 404,
message: ":You do not own this gear."
}) : void 0;
throw new NotFound(i18n.t('gearNotOwned', req.language));
}
let item = content.gear.flat[key];
if (user.items.gear[type][item.type] === key) {
user.items.gear[type][item.type] = item.type + "_base_0";
user.items.gear[type][item.type] = `${item.type}_base_0`;
message = i18n.t('messageUnEquipped', {
itemText: item.text(req.language)
itemText: item.text(req.language),
}, req.language);
} else {
user.items.gear[type][item.type] = item.key;
message = user.fns.handleTwoHanded(item, type, req);
}
if (typeof user.markModified === "function") {
user.markModified("items.gear." + type);
message = handleTwoHanded(user, item, type, req);
}
break;
}
return typeof cb === "function" ? cb((message ? {
code: 200,
message: message
} : null), user.items) : void 0;
let res = {
data: user.items,
};
if (message) res.message = message;
return res;
};

View File

@@ -1,78 +1,96 @@
import content from '../content/index';
import i18n from '../i18n';
import _ from 'lodash';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../libs/errors';
function evolve (user, pet, petDisplayName, req) {
user.items.pets[pet] = -1;
user.items.mounts[pet] = true;
if (pet === user.items.currentPet) {
user.items.currentPet = '';
}
return i18n.t('messageEvolve', {
egg: petDisplayName,
}, req.language);
}
module.exports = function feed (user, req = {}) {
let pet = _.get(req, 'params.pet');
let foodK = _.get(req, 'params.food');
if (!pet || !foodK) throw new BadRequest(i18n.t('missingPetFoodFeed'));
if (pet.indexOf('-') === -1) {
throw new BadRequest(i18n.t('invalidPetName', req.language));
}
let food = content.food[foodK];
if (!food) {
throw new NotFound(i18n.t('messageFoodNotFound', req.language));
}
let userPets = user.items.pets;
module.exports = function(user, req, cb) {
var egg, eggText, evolve, food, message, pet, petDisplayName, potion, potionText, ref, ref1, ref2, userPets;
ref = req.params, pet = ref.pet, food = ref.food;
food = content.food[food];
ref1 = pet.split('-'), egg = ref1[0], potion = ref1[1];
userPets = user.items.pets;
potionText = content.hatchingPotions[potion] ? content.hatchingPotions[potion].text() : potion;
eggText = content.eggs[egg] ? content.eggs[egg].text() : egg;
petDisplayName = i18n.t('petName', {
potion: potionText,
egg: eggText
});
if (!userPets[pet]) {
return typeof cb === "function" ? cb({
code: 404,
message: i18n.t('messagePetNotFound', req.language)
}) : void 0;
throw new NotFound(i18n.t('messagePetNotFound', req.language));
}
if (!((ref2 = user.items.food) != null ? ref2[food.key] : void 0)) {
return typeof cb === "function" ? cb({
code: 404,
message: i18n.t('messageFoodNotFound', req.language)
}) : void 0;
let [egg, potion] = pet.split('-');
let potionText = content.hatchingPotions[potion] ? content.hatchingPotions[potion].text() : potion;
let eggText = content.eggs[egg] ? content.eggs[egg].text() : egg;
let petDisplayName = i18n.t('petName', {
potion: potionText,
egg: eggText,
}, req.language);
if (!user.items.food[food.key]) {
throw new NotFound(i18n.t('messageFoodNotFound', req.language));
}
if (content.specialPets[pet]) {
return typeof cb === "function" ? cb({
code: 401,
message: i18n.t('messageCannotFeedPet', req.language)
}) : void 0;
throw new NotAuthorized(i18n.t('messageCannotFeedPet', req.language));
}
if (user.items.mounts[pet]) {
return typeof cb === "function" ? cb({
code: 401,
message: i18n.t('messageAlreadyMount', req.language)
}) : void 0;
throw new NotAuthorized(i18n.t('messageAlreadyMount', req.language));
}
message = '';
evolve = function() {
userPets[pet] = -1;
user.items.mounts[pet] = true;
if (pet === user.items.currentPet) {
user.items.currentPet = "";
}
return message = i18n.t('messageEvolve', {
egg: petDisplayName
}, req.language);
};
let message;
if (food.key === 'Saddle') {
evolve();
message = evolve(user, pet, petDisplayName, req);
} else {
if (food.target === potion || content.hatchingPotions[potion].premium) {
userPets[pet] += 5;
message = i18n.t('messageLikesFood', {
egg: petDisplayName,
foodText: food.text(req.language)
foodText: food.text(req.language),
}, req.language);
} else {
userPets[pet] += 2;
message = i18n.t('messageDontEnjoyFood', {
egg: petDisplayName,
foodText: food.text(req.language)
foodText: food.text(req.language),
}, req.language);
}
if (userPets[pet] >= 50 && !user.items.mounts[pet]) {
evolve();
message = evolve(user, pet, petDisplayName, req);
}
}
user.items.food[food.key]--;
return typeof cb === "function" ? cb({
code: 200,
message: message
}, {
value: userPets[pet]
}) : void 0;
return {
data: userPets[pet],
message,
};
};

View File

@@ -1,39 +1,40 @@
import content from '../content/index';
import i18n from '../i18n';
import _ from 'lodash';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../libs/errors';
module.exports = function hatch (user, req = {}) {
let egg = _.get(req, 'params.egg');
let hatchingPotion = _.get(req, 'params.hatchingPotion');
module.exports = function(user, req, cb) {
var egg, hatchingPotion, pet, ref;
ref = req.params, egg = ref.egg, hatchingPotion = ref.hatchingPotion;
if (!(egg && hatchingPotion)) {
return typeof cb === "function" ? cb({
code: 400,
message: "Please specify query.egg & query.hatchingPotion"
}) : void 0;
throw new BadRequest(i18n.t('missingEggHatchingPotionHatch', req.language));
}
if (!(user.items.eggs[egg] > 0 && user.items.hatchingPotions[hatchingPotion] > 0)) {
return typeof cb === "function" ? cb({
code: 403,
message: i18n.t('messageMissingEggPotion', req.language)
}) : void 0;
throw new NotFound(i18n.t('messageMissingEggPotion', req.language));
}
if (content.hatchingPotions[hatchingPotion].premium && !content.dropEggs[egg]) {
return typeof cb === "function" ? cb({
code: 403,
message: i18n.t('messageInvalidEggPotionCombo', req.language)
}) : void 0;
throw new BadRequest(i18n.t('messageInvalidEggPotionCombo', req.language));
}
pet = egg + "-" + hatchingPotion;
let pet = `${egg}-${hatchingPotion}`;
if (user.items.pets[pet] && user.items.pets[pet] > 0) {
return typeof cb === "function" ? cb({
code: 403,
message: i18n.t('messageAlreadyPet', req.language)
}) : void 0;
throw new NotAuthorized(i18n.t('messageAlreadyPet', req.language));
}
user.items.pets[pet] = 5;
user.items.eggs[egg]--;
user.items.hatchingPotions[hatchingPotion]--;
return typeof cb === "function" ? cb({
code: 200,
message: i18n.t('messageHatched', req.language)
}, user.items) : void 0;
return {
message: i18n.t('messageHatched', req.language),
data: user.items,
};
};