mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 22:27:26 +01:00
wip: fix setting (some) items values in the hall of heroes (#11133)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
import {
|
||||
validateItemPath,
|
||||
getDefaultOwnedGear,
|
||||
castItemVal,
|
||||
} from '../../../../../website/server/libs/items/utils';
|
||||
|
||||
describe('Items Utils', () => {
|
||||
@@ -64,4 +65,49 @@ describe('Items Utils', () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,7 +7,10 @@ import {
|
||||
import _ from 'lodash';
|
||||
import apiError from '../../libs/apiError';
|
||||
import validator from 'validator';
|
||||
import { validateItemPath } from '../../libs/items/utils';
|
||||
import {
|
||||
validateItemPath,
|
||||
castItemVal,
|
||||
} from '../../libs/items/utils';
|
||||
|
||||
|
||||
let api = {};
|
||||
@@ -271,7 +274,7 @@ api.updateHero = {
|
||||
hero.markModified('items.pets');
|
||||
}
|
||||
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) {
|
||||
|
||||
@@ -55,3 +55,23 @@ export function validateItemPath (itemPath) {
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user