-
Notifications
You must be signed in to change notification settings - Fork 4k
/
pinnedGearUtils.js
156 lines (123 loc) · 3.83 KB
/
pinnedGearUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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 getOfficialPinnedItems from '../libs/getOfficialPinnedItems';
import get from 'lodash/get';
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;
}, {});
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 = user.pinnedItems.findIndex(pinnedItem => {
return pinnedItem.path === 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 = user.pinnedItems.findIndex(pinnedItem => {
return pinnedItem.path === 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) {
let currentPinnedItems = selectGearToPin(user);
let removeAndAddAllItems = false;
for (let item of currentPinnedItems) {
let itemInfo = getItemInfo(user, 'marketGear', item);
if (itemInfo.path === itemPath) {
removeAndAddAllItems = true;
break;
}
}
removeItemByPath(user, itemPath);
if (removeAndAddAllItems) {
// 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);
} else {
// just change the new gear to owned
user.items.gear.owned[newItemKey] = true;
}
}
/**
* @returns {boolean} TRUE added the item / FALSE removed it
*/
function togglePinnedItem (user, {item, type, path}, req = {}) {
let arrayToChange;
if (!path) { // If path isn't passed it means an item was passed
path = getItemInfo(user, type, item, req.language).path;
}
if (!item) item = get(content, path);
if (path === 'armoire' || path === 'potion') {
throw new BadRequest(i18n.t('cannotUnpinArmoirPotion', req.language));
}
let officialPinnedItems = getOfficialPinnedItems(user);
let isOfficialPinned = officialPinnedItems.find(officialPinnedItem => {
return officialPinnedItem.path === path;
}) !== undefined;
if (isOfficialPinned) {
arrayToChange = user.unpinnedItems;
} else {
arrayToChange = user.pinnedItems;
}
const foundIndex = arrayToChange.findIndex(pinnedItem => {
return pinnedItem.path === path;
});
if (foundIndex >= 0) {
arrayToChange.splice(foundIndex, 1);
return isOfficialPinned;
} else {
arrayToChange.push({path, type});
return !isOfficialPinned;
}
}
module.exports = {
addPinnedGearByClass,
addPinnedGear,
removePinnedGearByClass,
removePinnedGearAddPossibleNewOnes,
togglePinnedItem,
removeItemByPath,
isPinned,
};