forked from mavrixwk/quagbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventories.js
441 lines (419 loc) · 20.9 KB
/
inventories.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
var gw2api = require('./api.js');
var sf = require('./sharedFunctions.js');
var debug = false;
module.exports = function() {
var ret = {
addResponses: function(controller) {
////wallet
controller.hears(['^wallet(.*)', '^dungeonwallet(.*)', '^dw(.*)'], 'direct_message,direct_mention,mention,ambient', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (err) {
bot.reply(message, "I got an error loading your data (or you have no access token set up). Try again later");
bot.botkit.log("Error: wallet no user data " + err);
return;
}
//precheck - input scrub a bit
var matches = sf.removePunctuationAndToLower(message.text).match(/(dw|dungeonwallet|wallet)([\s\w]*)$/i);
if (!matches) {
bot.reply(message, "I didn't quite get that. Maybe ask \'help wallet\'?");
return;
}
//precheck: access token.
if (!user || !user.access_token || !sf.userHasPermission(user, 'wallet')) {
bot.botkit.log('ERROR: bank no access token: ' + JSON.stringify(user) + "err: " + JSON.stringify(err));
bot.reply(message, "Sorry, I don't have your access token " + (user && user.access_token && !sf.userHasPermission(user, 'wallet') ? "with correct 'wallet' 'permissions' " : "") + "on file. Direct message me the phrase \'access token help\' for help.");
return;
}
var searchTerm = (matches[2] ? matches[2].replace(/\s+/g, '') : null);
var isDungeonOnly = (matches[1] == "dungeonwallet" || matches[1] == 'dw');
if (searchTerm) bot.reply(message, "Okay, " + user.dfid + sf.randomHonoriffic(user.dfid, user.id) + ", rifling through your wallet for " + searchTerm + ".");
else bot.reply(message, "Okay, " + user.dfid + sf.randomHonoriffic(user.dfid, user.id) + ", what's in YOUR wallet?");
gw2api.accountWallet(function(walletList, headers) {
var defaultIcon = 'https://render.guildwars2.com/file/0E011CD3AA34BB0C327D290830C7F10F0946307B/866837.png';
if (isDungeonOnly) {
//['Ascalonian Tear', 'Seal of Beetletun', 'Deadly Bloom', 'Manifesto of the Moletariate', 'Flame Legion Charr Carving', 'Symbol of Koda', 'Knowledge Crystal', 'Shard of Zhaitan', 'Fractal Relic', 'Pristine Fractal Relic'];
//Hard coded ID instead of title, since both are subject to change, but id is probably more stable
var dungeonCurrencyList = [5, 9, 11, 10, 13, 12, 14, 6, 7, 24];
walletList = walletList.filter(function(value) {
return (dungeonCurrencyList.indexOf(value.id) >= 0);
});
defaultIcon = 'https://render.guildwars2.com/file/943538394A94A491C8632FBEF6203C2013443555/102478.png';
}
walletList.sort(function(a, b) {
return (gw2api.findInData('id', a.id, 'currencies').order - gw2api.findInData('id', b.id, 'currencies').order);
});
var text = [];
var lastIcon;
for (var i in walletList) {
var currency = gw2api.findInData('id', walletList[i].id, 'currencies');
if (currency &&
(!searchTerm || (searchTerm && sf.removePunctuationAndToLower(currency.name).replace(/\s+/g, '').includes(searchTerm)))
) {
if (currency.name == 'Coin') {
text.push("Coin: " + sf.coinToString(walletList[i].value));
} else
text.push(currency.name + ": " + walletList[i].value);
lastIcon = currency.icon;
}
}
if (text.length > 0)
bot.reply(message, {
attachments: {
attachment: {
fallback: 'Too many items found in search.',
pretext: (searchTerm ? 'Looking for: ' + searchTerm : ''),
text: text.join("\n"),
thumb_url: ((lastIcon && text.length > 1) ? defaultIcon : lastIcon)
}
}
});
else bot.reply(message, "You don't have any.");
}, {
access_token: user.access_token,
});
});
});
////BANK
controller.hears(['^bank (.*)'], 'direct_message,direct_mention,mention,ambient', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (err) {
bot.reply(message, "I got an error loading your data (or you have no access token set up). Try again later");
bot.botkit.log("Error:bank no user data " + err);
return;
}
//precheck - input scrub a bit
var matches = sf.removePunctuationAndToLower(message.text).match(/(bank)\s?([\s\w]*)$/i);
if (!matches || !matches[2]) {
bot.reply(message, "I didn't quite get that. Maybe ask \'help bank\'?");
return;
}
//precheck: access token.
if (!user || !user.access_token || !sf.userHasPermission(user, 'inventories')) {
bot.botkit.log('ERROR: bank no access token: ' + JSON.stringify(user) + "err: " + JSON.stringify(err));
bot.reply(message, "Sorry, I don't have your access token " + (user && user.access_token && !sf.userHasPermission(user, 'inventories') ? "with correct 'inventories' permissions " : "") + "on file. Direct message me the phrase \'access token help\' for help.");
return;
}
var searchTerm = matches[2].replace(/\s+/g, '');
var bankAll = false;
if (searchTerm == 'all')
bankAll = true;
bot.reply(message, "Okay, " + user.dfid + sf.randomHonoriffic(user.dfid, user.id) + ", rifling through your pockets" + (!bankAll ? " for spare " + matches[2] : '') + ".");
//setup: fetch character list and callback
ret.fetchAllCharacterData(user.access_token)
.then(fetchAllItemAndSkinIds)
.then(function(itemList) { //find items with our original search string
if (debug) sf.log(itemList.length + " unique items in character inventories.");
if (bankAll)
tallyAndDisplay(itemList);
else {
var itemSearchResults = [];
for (var i in itemList) {
if (sf.removePunctuationAndToLower(itemList[i].inventoryName).replace(/\s+/g, '').includes(searchTerm))
itemSearchResults.push(itemList[i]);
}
//And Display!
if (itemSearchResults.length === 0) { //no match
bot.reply(message, "No item names on your account contain that exact text.");
} else if (itemSearchResults.length == 1) { //exactly one. Ship it.
tallyAndDisplay([itemSearchResults[0]]);
} else if (itemSearchResults.length > 10) { //too many matches in our 'contains' search, notify and give examples.
var itemNameList = [];
for (var n in itemSearchResults) {
itemNameList.push(itemSearchResults[n].inventoryName + sf.levelAndRarityForItem(itemSearchResults[n]));
}
bot.reply(message, {
attachments: {
attachment: {
fallback: 'Too many items found in search.',
text: "Bro. I found " + itemSearchResults.length + ' items. Get more specific.\n' + itemNameList.join("\n")
}
}
});
} else { //10 items or less, allow user to choose
bot.startConversation(message, function(err, convo) {
var listofItems = '';
for (var i in itemSearchResults) {
listofItems += '\n' + [i] + ": " + itemSearchResults[i].inventoryName + sf.levelAndRarityForItem(itemSearchResults[i]) + (itemSearchResults[i].forged ? " (Mystic Forge)" : "");
}
convo.ask('I found multiple items with that name. Which number you mean? (say no to quit)' + listofItems, [{
//number, no, or repeat
pattern: new RegExp(/^(\d{1,2})/i),
callback: function(response, convo) {
//if it's a number, and that number is within our search results, print it
var matches = response.text.match(/^(\d{1,2})/i);
var selection = matches[0];
if (selection < itemSearchResults.length) {
tallyAndDisplay([itemSearchResults[selection]]);
} else convo.repeat(); //invalid number. repeat choices.
convo.next();
}
}, {
//negative response. Stop repeating the list.
pattern: bot.utterances.no,
callback: function(response, convo) {
convo.say('¯\\_(ツ)_/¯');
convo.next();
}
}, {
default: true,
callback: function(response, convo) {
// loop back, user needs to pick or say no.
convo.say("Nope. Next time choose a number of the item you'd like to see.");
convo.next();
}
}]);
});
}
}
})
.catch(function(error) {
bot.reply(message, "I got an error on my way to promise land from the bank. Send help!\nTell them " + error);
if (convo) convo.next();
});
var tallyAndDisplay = function(itemList) {
var total = 0;
var totalStrings = [];
if (itemList.length == 1) { //find and count this item
var itemToDisplay = itemList[0];
for (var inv in inventories) {
var sourceCount = 0;
var ind = inventories[inv].ids.indexOf(itemToDisplay.id, 0);
while (ind >= 0) {
sourceCount += inventories[inv].counts[ind];
total += inventories[inv].counts[ind];
ind = inventories[inv].ids.indexOf(itemToDisplay.id, (ind + 1));
}
if (sourceCount > 0)
totalStrings.push(inventories[inv].source + " has " + (sourceCount > 500 ? sourceCount + ' of the goddamn things' : sourceCount));
}
if (total > 0 && totalStrings.length > 0) {
bot.reply(message, "*" + itemToDisplay.inventoryName + ": " + total + " owned*\n" + totalStrings.join('\n'));
} else
bot.reply(message, "You have none of that. None.");
} else { //bank all command. List ALL items
var tallyAllItemsArray = []; //index is id, object is like {total:total,sources:[{source: 'source', count:count}]}
for (var i in inventories) {
var uniqueIds = sf.arrayUnique(inventories[i].ids);
for (var uid in uniqueIds) {
var foundIndex = inventories[i].ids.indexOf(uniqueIds[uid], 0);
var subCount = 0;
while (foundIndex >= 0) {
subCount += inventories[i].counts[foundIndex];
total += inventories[i].counts[foundIndex];
foundIndex = inventories[i].ids.indexOf(uniqueIds[uid], (foundIndex + 1));
}
if (subCount > 0) {
if (!tallyAllItemsArray[uniqueIds[uid]]) { //new item
tallyAllItemsArray[uniqueIds[uid]] = {
total: 0,
sources: []
};
}
tallyAllItemsArray[uniqueIds[uid]].total += subCount;
tallyAllItemsArray[uniqueIds[uid]].sources.push({
source: inventories[i].source,
count: subCount
});
}
}
}
itemList.sort(function(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
for (var il in itemList) {
if (tallyAllItemsArray[itemList[il].id]) {
var pushString = itemList[il].inventoryName + ": " + tallyAllItemsArray[itemList[il].id].total;
for (var n in tallyAllItemsArray[itemList[il].id].sources) {
pushString += ", " + tallyAllItemsArray[itemList[il].id].sources[n].source + " has " + tallyAllItemsArray[itemList[il].id].sources[n].count;
}
totalStrings.push(pushString);
}
}
var attachments = [];
var loopString = '';
for (var ls in totalStrings) {
if (loopString.length + totalStrings[ls].length > 7900) { //if we're about to go above the limit, peel off an attachment
attachments.push({
fallback: 'ALL THE ITEMS',
text: loopString
});
loopString = '';
}
loopString += totalStrings[ls] + '\n';
}
if (loopString.length > 0) //catch the last attachment.
attachments.push({
fallback: 'ALL THE ITEMS',
text: loopString
});
attachments[0].title = "Everything Report: " + total + " total items";
bot.reply(message, {
attachments: attachments
});
}
};
});
});
},
addHelp: function(helpFile) {
helpFile.wallet = "List the contents of your wallet. Optionally add a search string to filter the list. Useage:wallet <name>";
helpFile.dungeonWallet = "Lists only your dungeon currencies.";
helpFile.dw = 'Alias for dungeon wallet: ' + JSON.stringify(helpFile.dungeonwallet);
helpFile.bank = "Search your possessions for an item. Looks in character inventories, shared inventory, bank and material storage. Usage: bank <item name>";
},
fetchAllCharacterData: function(access_token) {
//setup: promise fetch shared inventory, bank, and material storage.
inventories = [];
return gw2api.promise.characters(['all'], access_token)
.then(ret.collateCharacterItems)
.then(function() {
return Promise.all([
gw2api.promise.accountBank(['all'], access_token),
gw2api.promise.accountInventory(['all'], access_token),
gw2api.promise.accountMaterials(['all'], access_token)
])
.then(ret.collateStorageItems);
});
},
collateCharacterItems: function(characterList) {
if (characterList.text || characterList.error) {
bot.reply(message, "Oops. I got this error when asking about your character inventories: " + (characterList.text ? characterList.text : characterList.error) + '\n' + JSON.stringify(err));
return;
}
//Build a list of all inventories. Character and account
//setup:promise fetch items in each character inventory
for (var ch in characterList) {
var idList = [];
var countList = [];
var skinList = [];
for (var bg in characterList[ch].bags) {
if (characterList[ch].bags[bg] !== null) { //there can be no bag in the slot
for (var it in characterList[ch].bags[bg].inventory) {
if (characterList[ch].bags[bg].inventory[it] !== null) { //there can be no item in the bag
idList.push(characterList[ch].bags[bg].inventory[it].id);
countList.push(characterList[ch].bags[bg].inventory[it].count);
skinList.push(characterList[ch].bags[bg].inventory[it].skin || 0);
}
}
}
}
inventories.push({
source: characterList[ch].name,
ids: idList,
counts: countList,
skins: skinList
});
//Reset, add worn items
idList = [];
countList = [];
skinList = [];
for (var slot in characterList[ch].equipment) {
if (characterList[ch].equipment[slot] !== null) {
idList.push(characterList[ch].equipment[slot].id);
countList.push(1);
skinList.push(characterList[ch].equipment[slot].skin || 0);
}
}
inventories.push({
source: characterList[ch].name + " (worn)",
ids: idList,
counts: countList,
skins: skinList
});
}
},
collateStorageItems: function(results) {
return new Promise(function(resolve, reject) {
var sourceNames = ['Your bank', 'Your shared inventory', 'Your materials storage'];
if (debug) sf.log((results ? results.length : "0 (undefined)") + " owned item results returned, " + ((results[0] ? results[0].length : 0) + (results[1] ? results[1].length : 0) + (results[2] ? results[2].length : 0)) + " total items");
for (var sourceList in results) {
var idList = [];
var countList = [];
var skinsList = [];
for (var item in results[sourceList]) {
if (results[sourceList][item] !== null && (results[sourceList][item].count && results[sourceList][item].count !== 0)) {
if (results[sourceList][item].id === null) console.log("null item " + JSON.stringify(results[sourceList][item]));
idList.push(results[sourceList][item].id);
countList.push(results[sourceList][item].count);
skinsList.push(results[sourceList][item].skin || 0);
}
}
inventories.push({
source: sourceNames[sourceList],
ids: idList,
counts: countList,
skins: skinsList
});
}
if (debug)
for (var ch in inventories)
sf.log(inventories[ch].source + " has " + (inventories[ch].counts.length == inventories[ch].ids.length ? inventories[ch].counts.length + " items" : " an error"));
resolve(inventories);
});
}
};
return ret;
}();
function fetchAllItemAndSkinIds(inventories) { //collate the IDs of all items in all inventories, and fetch
return new Promise(function(resolve, reject) {
var itemPagePromises = [];
var ownedItemIds = [];
for (var inv in inventories)
ownedItemIds = ownedItemIds.concat(inventories[inv].ids);
ownedItemIds = sf.arrayUnique(ownedItemIds);
if (debug) sf.log("Fetching " + ownedItemIds.length + " unique items");
for (var i = 0; i < ownedItemIds.length; i += 200) {
itemPagePromises.push(gw2api.promise.items(ownedItemIds.slice(i, i + 200)));
}
return Promise.all(itemPagePromises)
.then(function(results) {
var itemList = [];
for (var list in results) {
itemList = itemList.concat(results[list]);
}
if (debug) {
sf.log("results has " + itemList.length + " items");
}
var skinPagePromises = [];
var ownedSkinIds = [];
for (var inv in inventories)
ownedSkinIds = ownedSkinIds.concat(inventories[inv].skins);
ownedSkinIds = sf.arrayUnique(ownedSkinIds);
ownedSkinIds.splice(ownedSkinIds.indexOf(0), 1); //Remove 0 (inserted as a placeholder for blank skins)
if (debug) sf.log("Fetching " + ownedSkinIds.length + " unique skins");
for (var i = 0; i < ownedSkinIds.length; i += 200) {
skinPagePromises.push(gw2api.promise.skins(ownedSkinIds.slice(i, i + 200)));
}
return Promise.all(skinPagePromises)
.then(function(results) {
var skinList = [];
for (var list in results) {
skinList = skinList.concat(results[list]);
}
if (debug) {
sf.log("results has " + skinList.length + " skins");
}
//add skin data to items
function getInventoryName(itemToDisplay) {
var findSkinInInventories = function(skin) {
return skin.id == inventories[source].skins[inSource];
};
for (var source in inventories) {
var inSource = inventories[source].ids.indexOf(itemToDisplay.id);
if (inSource >= 0 && inventories[source].skins[inSource] !== 0) {
var skinFound = skinList.find(findSkinInInventories);
if (skinFound)
return skinFound.name;
}
}
return itemToDisplay.name;
}
for (var i in itemList)
itemList[i].inventoryName = getInventoryName(itemList[i]);
resolve(itemList);
});
});
});
}