habitica/website/common/script/ops/pinnedGearUtils.js

181 lines
4.6 KiB
JavaScript
Raw Normal View History

import content from '../content/index';
import getItemInfo from '../libs/getItemInfo';
import { BadRequest } from '../libs/errors';
import i18n from '../i18n';
import isPinned from '../libs/isPinned';
import getItemByPathAndType from '../libs/getItemByPathAndType';
[WIP] new client - seasonal-shop (#9018) * extract seasonal-shop config - use summer season items (to work on) * add suggested border to shopItems * refactor getOfficialPinnedItems (now includes the seasonal gear) * refactor shops.getSeasonalShop - add featured items to result - add the set to special equipment items * feat(content): Fall 2017 seasonal gear Also adds set keys for all prior seasonal gear. * show item limited time (buyModal & shopItem) * select seasonal fall sets * WIP(seasonal-shop): placeholder Fall 2017 items * fix lint * sprites * styling + fix purchase of seasonal spells * compile sprites * fixes: check isPinned with officialItems * enable purchase of seasonal items for testing * fix shop apis * add featuredItems to market * quest shop: add featuredItems to api * tiem travelers shop: add featuredItems to api * fix gear types filter * feat(content): Fall 2017 compleat * chore(sprites): compile * show opened shop state (npc+background) * add opened seasonal npc * current seasonal users class set = purchase by gold - lock other sets of the current season * hide event badge in seasonal shop - dot only for suggested items - cursor: pointer on shopItems * refresh rewards column list (seasonal gear won't refresh it on purchase) * fix duplicate seasonal gear -> remove special items from the old reward gear (which is used to reset the pinned gears) * every current season gear is purchased by gold - prevent buyModal on locked items * use the current event date range * list seasonal sets by event date * use custom method instead of updateStore to list the pinnable gear * change daterange to 10-31 * fix start quest modal from items - disable invite quest button if a quest is already active * toggle pin in buy-dialogs * check if the item is not undefined/null - renamed the watch function
2017-09-21 00:28:11 +00:00
import getOfficialPinnedItems from '../libs/getOfficialPinnedItems';
[WIP] new client - seasonal-shop (#9018) * extract seasonal-shop config - use summer season items (to work on) * add suggested border to shopItems * refactor getOfficialPinnedItems (now includes the seasonal gear) * refactor shops.getSeasonalShop - add featured items to result - add the set to special equipment items * feat(content): Fall 2017 seasonal gear Also adds set keys for all prior seasonal gear. * show item limited time (buyModal & shopItem) * select seasonal fall sets * WIP(seasonal-shop): placeholder Fall 2017 items * fix lint * sprites * styling + fix purchase of seasonal spells * compile sprites * fixes: check isPinned with officialItems * enable purchase of seasonal items for testing * fix shop apis * add featuredItems to market * quest shop: add featuredItems to api * tiem travelers shop: add featuredItems to api * fix gear types filter * feat(content): Fall 2017 compleat * chore(sprites): compile * show opened shop state (npc+background) * add opened seasonal npc * current seasonal users class set = purchase by gold - lock other sets of the current season * hide event badge in seasonal shop - dot only for suggested items - cursor: pointer on shopItems * refresh rewards column list (seasonal gear won't refresh it on purchase) * fix duplicate seasonal gear -> remove special items from the old reward gear (which is used to reset the pinned gears) * every current season gear is purchased by gold - prevent buyModal on locked items * use the current event date range * list seasonal sets by event date * use custom method instead of updateStore to list the pinnable gear * change daterange to 10-31 * fix start quest modal from items - disable invite quest button if a quest is already active * toggle pin in buy-dialogs * check if the item is not undefined/null - renamed the watch function
2017-09-21 00:28:11 +00:00
import each from 'lodash/each';
import sortBy from 'lodash/sortBy';
import lodashFind from 'lodash/find';
import reduce from 'lodash/reduce';
let sortOrder = reduce(content.gearTypes, (accumulator, val, key) => {
accumulator[val] = key;
return accumulator;
}, {});
/**
* Returns the index if found
* @param Array array
* @param String path
*/
function pathExistsInArray (array, path) {
return array.findIndex(item => {
return item.path === path;
});
}
[WIP] new client - seasonal-shop (#9018) * extract seasonal-shop config - use summer season items (to work on) * add suggested border to shopItems * refactor getOfficialPinnedItems (now includes the seasonal gear) * refactor shops.getSeasonalShop - add featured items to result - add the set to special equipment items * feat(content): Fall 2017 seasonal gear Also adds set keys for all prior seasonal gear. * show item limited time (buyModal & shopItem) * select seasonal fall sets * WIP(seasonal-shop): placeholder Fall 2017 items * fix lint * sprites * styling + fix purchase of seasonal spells * compile sprites * fixes: check isPinned with officialItems * enable purchase of seasonal items for testing * fix shop apis * add featuredItems to market * quest shop: add featuredItems to api * tiem travelers shop: add featuredItems to api * fix gear types filter * feat(content): Fall 2017 compleat * chore(sprites): compile * show opened shop state (npc+background) * add opened seasonal npc * current seasonal users class set = purchase by gold - lock other sets of the current season * hide event badge in seasonal shop - dot only for suggested items - cursor: pointer on shopItems * refresh rewards column list (seasonal gear won't refresh it on purchase) * fix duplicate seasonal gear -> remove special items from the old reward gear (which is used to reset the pinned gears) * every current season gear is purchased by gold - prevent buyModal on locked items * use the current event date range * list seasonal sets by event date * use custom method instead of updateStore to list the pinnable gear * change daterange to 10-31 * fix start quest modal from items - disable invite quest button if a quest is already active * toggle pin in buy-dialogs * check if the item is not undefined/null - renamed the watch function
2017-09-21 00:28:11 +00:00
function selectGearToPin (user) {
let changes = [];
each(content.gearTypes, (type) => {
let found = lodashFind(content.gear.tree[type][user.stats.class], (item) => {
return !user.items.gear.owned[item.key];
});
if (found) changes.push(found);
});
return sortBy(changes, (change) => sortOrder[change.type]);
}
function addPinnedGear (user, type, path) {
const foundIndex = pathExistsInArray(user.pinnedItems, path);
if (foundIndex === -1) {
user.pinnedItems.push({
type,
path,
});
}
}
function addPinnedGearByClass (user) {
let newPinnedItems = selectGearToPin(user);
for (let item of newPinnedItems) {
let itemInfo = getItemInfo(user, 'marketGear', item);
addPinnedGear(user, itemInfo.pinType, itemInfo.path);
}
}
function removeItemByPath (user, path) {
const foundIndex = pathExistsInArray(user.pinnedItems, path);
if (foundIndex >= 0) {
user.pinnedItems.splice(foundIndex, 1);
return true;
}
return false;
}
function removePinnedGearByClass (user) {
let currentPinnedItems = selectGearToPin(user);
for (let item of currentPinnedItems) {
let itemInfo = getItemInfo(user, 'marketGear', item);
removeItemByPath(user, itemInfo.path);
}
}
function removePinnedGearAddPossibleNewOnes (user, itemPath, newItemKey) {
removeItemByPath(user, itemPath);
// an item of the users current "new" gear was bought
// remove the old pinned gear items and add the new gear back
removePinnedGearByClass(user);
user.items.gear.owned[newItemKey] = true;
addPinnedGearByClass(user);
// update the version, so that vue can refresh the seasonal shop
user._v++;
}
/**
* removes all pinned gear that the user already owns (like class starter gear which has been pinned before)
* @param user
*/
function removePinnedItemsByOwnedGear (user) {
each(user.items.gear.owned, (bool, key) => {
if (bool) {
removeItemByPath(user, `gear.flat.${key}`);
}
});
}
const PATHS_WITHOUT_ITEM = ['special.gems', 'special.rebirth_orb', 'special.fortify'];
/**
* @returns {boolean} TRUE added the item / FALSE removed it
*/
function togglePinnedItem (user, {item, type, path}, req = {}) {
let arrayToChange;
let officialPinnedItems = getOfficialPinnedItems(user);
if (!path) {
// If path isn't passed it means an item was passed
path = getItemInfo(user, type, item, officialPinnedItems, req.language).path;
} else {
item = getItemByPathAndType(type, path);
if (!item && PATHS_WITHOUT_ITEM.indexOf(path) === -1) {
// path not exists in our content structure
throw new BadRequest(i18n.t('wrongItemPath', {path}, req.language));
}
// check if item exists & valid to be pinned
getItemInfo(user, type, item, officialPinnedItems, req.language);
}
if (path === 'armoire' || path === 'potion') {
throw new BadRequest(i18n.t('cannotUnpinArmoirPotion', req.language));
}
const isOfficialPinned = pathExistsInArray(officialPinnedItems, path) !== -1;
if (isOfficialPinned) {
arrayToChange = user.unpinnedItems;
} else {
arrayToChange = user.pinnedItems;
}
if (isOfficialPinned) {
// if an offical item is also present in the user.pinnedItems array
const itemInUserItems = pathExistsInArray(user.pinnedItems, path);
if (itemInUserItems !== -1) {
removeItemByPath(user, path);
}
}
const foundIndex = pathExistsInArray(arrayToChange, path);
if (foundIndex >= 0) {
arrayToChange.splice(foundIndex, 1);
return isOfficialPinned;
} else {
arrayToChange.push({path, type});
return !isOfficialPinned;
}
}
module.exports = {
addPinnedGearByClass,
addPinnedGear,
removePinnedGearByClass,
removePinnedGearAddPossibleNewOnes,
removePinnedItemsByOwnedGear,
togglePinnedItem,
removeItemByPath,
Release with develop (#9162) * Client: fix Apidoc and move email files (#9139) * fix apidoc * move emails files * quest leader can start/end quest; admins can edit challenges/guilds; reverse chat works; remove static/videos link; etc (#9140) * enable link to markdown info on group and challenge edit screen * allow admin (moderators and staff) to edit challenges * allow admin (moderators and staff) to edit guilds Also add some unrelated TODO comments. * allow any party member (not just leader) to start quest from party page * allow quest owner to cancel, begin, abort quest Previously only the party leader could see those buttons. The leader still can. This also hides those buttons from all other party members. * enable reverse chat in guilds and party * remove outdated videos from press kit * adjust various wordings * Be consistent with capitalization of Check-In. (#9118) * limit for inlined svg images and make home leaner by not bundling it with the rest of static pages * sep 27 fixes (#9088) * fix item paddings / drawer width * expand the width of item-rows by the margin of an item * fix hatchedPet-dialog * fix hatching-modal * remove min-height * Oct 3 fixes (#9148) * Only show level after yesterdailies modal * Fixed zindex * Added spcial spells to rewards column * Added single click buy for health and armoire * Prevented task scoring when casting a spell * Renamed generic purchase method * Updated nav for small screen * Hide checklist while casting * fix some text describing menu items (#9145) * 4.1.3 * use `selectGearToPin` instead of updateStore in migration-script (#9102) * Tags redesign in edit-task modal (#9122) * Truncate tags list to maximum number of tasks This commit truncates the list of tags in the edit task modal and displays the remaining selected tasks as a number. * Align tags-select dropdown with "Tags" label * Add tags popup component * Use solid purple for tags-select dropdown * Remove shadow when tags-select is active * Add border-radius to tags-select * Re-add previously disabled transitions * Remove unused template element * Add Clear Tags button to footer of tags popup * Decrease column size for tags to better match design * Truncate tag name to avoid overflows * Add tag name as title to show full name on hover * Grow inline tags select from left to right * Style none button * Add spacing to streak reset button to line up with tags select * Add top offset to tags dropdown toggle to line up with label * Ability to collapse checklists (#9158) * ability to collapse checklists * typo * show warning to use the mobile apps (#9152) * Oct 4 fixes (#9159) * Added gem purchase note * Added notification count * Added party reload * Added description when user can no longer purchase gems this month * Prevent modal from showing when pruchasing recently purchased items * Added progress bar * Prevented non leader from loading approvals * Added group billing * Release fix (#9161) * Merge Develop onto Release (#9123) * Some random quick (#9111) * Switch group button directions * Allowed admins to export challenges * Added scoping to some stable styles * Fixed challenge cloning * Tasks tags (#9112) * Added auto apply and exit * Add challenge tag editing * Fixed lint * Skill fixes (#9113) * Added local storage setting for spell drawer * Added new spell styles * Fixed typo * Reset local creds if access is denied (#9114) * various fixes: group leader's name at top of edit drop-down; Members List; etc (#9117) * fix text describing location of subscription/gem gift box * disable Copy As To-Do in Tavern, guilds, party because it's not working * change members label on group pages to Member List * remove outdated info about seeing number of Gems available to buy * allow Danger Zone to be seen by players without local authentication Also add an hr because the Danger Zone heading was crammed up against the button above it. * put current group leader's name at top of Leader change drop-down * Client Fixes (#9120) * unduplicate logout code * re-enable debug menu * fix pets badge and equipping mounts * close gift modal after sending gems * armoire notifications * Oct 1 fixes (#9121) * Added default tags to task * Added seasonal gear check and show spooky * Disabled spooky sparkles * Fixed challenge remove tasks modal * Hid checklist * Added group gems modal * Purchase with amazon * Added check for user health * Added missing notification file * 4.1.1 * 4.1.2 * Merge develop into release (#9154) * Client: fix Apidoc and move email files (#9139) * fix apidoc * move emails files * quest leader can start/end quest; admins can edit challenges/guilds; reverse chat works; remove static/videos link; etc (#9140) * enable link to markdown info on group and challenge edit screen * allow admin (moderators and staff) to edit challenges * allow admin (moderators and staff) to edit guilds Also add some unrelated TODO comments. * allow any party member (not just leader) to start quest from party page * allow quest owner to cancel, begin, abort quest Previously only the party leader could see those buttons. The leader still can. This also hides those buttons from all other party members. * enable reverse chat in guilds and party * remove outdated videos from press kit * adjust various wordings * Be consistent with capitalization of Check-In. (#9118) * limit for inlined svg images and make home leaner by not bundling it with the rest of static pages * sep 27 fixes (#9088) * fix item paddings / drawer width * expand the width of item-rows by the margin of an item * fix hatchedPet-dialog * fix hatching-modal * remove min-height * Oct 3 fixes (#9148) * Only show level after yesterdailies modal * Fixed zindex * Added spcial spells to rewards column * Added single click buy for health and armoire * Prevented task scoring when casting a spell * Renamed generic purchase method * Updated nav for small screen * Hide checklist while casting * fix some text describing menu items (#9145)
2017-10-04 23:26:05 +00:00
selectGearToPin,
isPinned,
};