wip: fix setting (some) items values in the hall of heroes (#11133)

This commit is contained in:
Matteo Pagliazzi
2019-04-25 22:41:43 +02:00
committed by GitHub
parent 99dec2eb0c
commit 9d473cc92e
3 changed files with 71 additions and 2 deletions

View File

@@ -2,6 +2,7 @@
import { import {
validateItemPath, validateItemPath,
getDefaultOwnedGear, getDefaultOwnedGear,
castItemVal,
} from '../../../../../website/server/libs/items/utils'; } from '../../../../../website/server/libs/items/utils';
describe('Items Utils', () => { describe('Items Utils', () => {
@@ -64,4 +65,49 @@ describe('Items Utils', () => {
expect(validateItemPath('items.quests.invalid')).to.equal(false); expect(validateItemPath('items.quests.invalid')).to.equal(false);
}); });
}); });
describe('castItemVal', () => {
it('returns the item val untouched if not an item path', () => {
expect(castItemVal('notitems.gear.owned.item', 'a string')).to.equal('a string');
});
it('returns the item val untouched if an unsupported path', () => {
expect(validateItemPath('items.gear.equipped.weapon', 'a string')).to.equal('a string');
expect(validateItemPath('items.currentPet', 'a string')).to.equal('a string');
expect(validateItemPath('items.special.snowball', 'a string')).to.equal('a string');
});
it('converts values for pets paths to numbers', () => {
expect(validateItemPath('items.pets.Wolf-CottonCandyPink', '5')).to.equal(5);
expect(validateItemPath('items.pets.Wolf-Invalid', '5')).to.equal(5);
});
it('converts values for eggs paths to numbers', () => {
expect(validateItemPath('items.eggs.LionCub', '5')).to.equal(5);
expect(validateItemPath('items.eggs.Armadillo', '5')).to.equal(5);
expect(validateItemPath('items.eggs.NotAnArmadillo', '5')).to.equal(5);
});
it('converts values for hatching potions paths to numbers', () => {
expect(validateItemPath('items.hatchingPotions.Base', '5')).to.equal(5);
expect(validateItemPath('items.hatchingPotions.StarryNight', '5')).to.equal(5);
expect(validateItemPath('items.hatchingPotions.Invalid', '5')).to.equal(5);
});
it('converts values for food paths to numbers', () => {
expect(validateItemPath('items.food.Cake_Base', '5')).to.equal(5);
expect(validateItemPath('items.food.Cake_Invalid', '5')).to.equal(5);
});
it('converts values for mounts paths to numbers', () => {
expect(validateItemPath('items.mounts.Cactus-Base', '5')).to.equal(5);
expect(validateItemPath('items.mounts.Aether-Invisible', '5')).to.equal(5);
expect(validateItemPath('items.mounts.Aether-Invalid', '5')).to.equal(5);
});
it('converts values for quests paths to numbers', () => {
expect(validateItemPath('items.quests.atom3', '5')).to.equal(5);
expect(validateItemPath('items.quests.invalid', '5')).to.equal(5);
});
});
}); });

View File

@@ -7,7 +7,10 @@ import {
import _ from 'lodash'; import _ from 'lodash';
import apiError from '../../libs/apiError'; import apiError from '../../libs/apiError';
import validator from 'validator'; import validator from 'validator';
import { validateItemPath } from '../../libs/items/utils'; import {
validateItemPath,
castItemVal,
} from '../../libs/items/utils';
let api = {}; let api = {};
@@ -271,7 +274,7 @@ api.updateHero = {
hero.markModified('items.pets'); hero.markModified('items.pets');
} }
if (updateData.itemPath && updateData.itemVal && validateItemPath(updateData.itemPath)) { if (updateData.itemPath && updateData.itemVal && validateItemPath(updateData.itemPath)) {
_.set(hero, updateData.itemPath, updateData.itemVal); // Sanitization at 5c30944 (deemed unnecessary) _.set(hero, updateData.itemPath, castItemVal(updateData.itemPath, updateData.itemVal)); // Sanitization at 5c30944 (deemed unnecessary)
} }
if (updateData.auth && updateData.auth.blocked === true) { if (updateData.auth && updateData.auth.blocked === true) {

View File

@@ -54,4 +54,24 @@ export function validateItemPath (itemPath) {
if (itemPath.indexOf('items.quests') === 0) { if (itemPath.indexOf('items.quests') === 0) {
return Boolean(shared.content.quests[key]); return Boolean(shared.content.quests[key]);
} }
}
// When passed a value of an item in the user object it'll convert the
// value to the correct format.
// Example a numeric string like "5" applied to a food item (expecting an interger)
// will be converted to the number 5
// TODO cast the correct value for `items.gear.owned`
export function castItemVal (itemPath, itemVal) {
if (
itemPath.indexOf('items.pets') === 0 ||
itemPath.indexOf('items.eggs') === 0 ||
itemPath.indexOf('items.hatchingPotions') === 0 ||
itemPath.indexOf('items.food') === 0 ||
itemPath.indexOf('items.mounts') === 0 ||
itemPath.indexOf('items.quests') === 0
) {
return Number(itemVal);
}
return itemVal;
} }