|
15 | 15 | }; |
16 | 16 |
|
17 | 17 | function updateItemModel(item, source, target, equip) { |
| 18 | + var matchingItem; |
| 19 | + |
18 | 20 | if (source.id !== target.id) { |
19 | 21 | var index = _.findIndex(source.items, function(i) { |
20 | 22 | return (item.index === i.index); |
21 | 23 | }); |
22 | 24 |
|
| 25 | + if (item.maxStackSize > 1 && item.amount < item.maxStackSize) { // Balance the stacks. |
| 26 | + if (_.has(item, 'moveAmount') && (item.moveAmount > 0)) { |
| 27 | + matchingItem = _.reduce(source.items, function(memo, i) { |
| 28 | + if (item.hash === i.hash) { |
| 29 | + if (!(_.has(i, 'moveAmount')) || ((_.has(i, 'moveAmount') && i.moveAmount === 0))) { |
| 30 | + if (memo === null) { |
| 31 | + memo = i; |
| 32 | + } else if (memo.amount > i.amount) { |
| 33 | + memo = i; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return memo; |
| 39 | + }, null); |
| 40 | + |
| 41 | + if (!_.isNull(matchingItem)) { |
| 42 | + if (item.moveAmount > item.amount) { |
| 43 | + matchingItem.amount = matchingItem.amount + (item.amount - item.moveAmount); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + item.amount = item.moveAmount; |
| 48 | + } |
| 49 | + |
| 50 | + item.moveAmount = 0; |
| 51 | + |
| 52 | + matchingItem = _.filter(target.items, function(i) { |
| 53 | + return ((i.amount < item.maxStackSize) && (i.hash === item.hash)); |
| 54 | + }); |
| 55 | + |
| 56 | + if (_.size(matchingItem) > 0) { |
| 57 | + var mItem = matchingItem[0]; |
| 58 | + var combinedTotal = mItem.amount + item.amount; |
| 59 | + |
| 60 | + item.moveAmount = item.amount; |
| 61 | + |
| 62 | + if (combinedTotal <= item.maxStackSize) { |
| 63 | + mItem.amount = combinedTotal; |
| 64 | + item.amount = 0; |
| 65 | + } else { |
| 66 | + mItem.amount = mItem.maxStackSize; |
| 67 | + item.amount = combinedTotal - item.maxStackSize; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + item.owner = target.id; |
| 73 | + |
23 | 74 | if (index >= 0) { |
24 | | - item.owner = target.id; |
25 | 75 | source.items.splice(index, 1); |
| 76 | + } |
| 77 | + |
| 78 | + if (item.amount > 0) { |
26 | 79 | target.items.push(item); |
27 | 80 | } |
28 | 81 | } |
|
147 | 200 | scope.similarItem = similarItem; |
148 | 201 |
|
149 | 202 | if (!equipExotic && (similarItem) && (similarItem.tier === 'Exotic')) { |
150 | | - return $q.reject('There are no items to equip in the \'' + item.type + '\' slot.') |
| 203 | + return $q.reject('There are no items to equip in the \'' + item.type + '\' slot.'); |
151 | 204 | } else if (!similarItem) { |
152 | | - return $q.reject('There are no items to equip in the \'' + item.type + '\' slot.') |
| 205 | + return $q.reject('There are no items to equip in the \'' + item.type + '\' slot.'); |
153 | 206 | } |
154 | 207 |
|
155 | 208 | return dimStoreService.getStore(item.owner); |
|
167 | 220 | } else { |
168 | 221 | return dimBungieService.transfer(scope.similarItem, scope.source) |
169 | 222 | .then(function() { |
170 | | - updateItemModel(scope.similarItem, scope.target, scope.source, false) |
| 223 | + updateItemModel(scope.similarItem, scope.target, scope.source, false); |
171 | 224 | }); |
172 | 225 | } |
173 | 226 | }) |
|
199 | 252 | .then(function(source) { |
200 | 253 | scope.source = source; |
201 | 254 |
|
| 255 | + // if (_.has(item, 'moveAmount') && (item.moveAmount > 0)) { |
| 256 | + // item.amount = item.moveAmount; |
| 257 | + // } |
| 258 | + |
202 | 259 | return dimBungieService.transfer(item, scope.target); |
203 | 260 | }) |
204 | 261 | .then(function() { |
|
328 | 385 | .catch(function(err) { |
329 | 386 | // createSpace(vault, item, store) |
330 | 387 | // .then(function() { |
331 | | - deferred.reject(err); |
332 | | - // }); |
| 388 | + deferred.reject(err); |
| 389 | + // }); |
333 | 390 | }); |
334 | 391 | } else { |
335 | 392 | deferred.resolve(true); |
|
341 | 398 | // deferred.reject(new Error('There are too many \'' + (store.id === 'vault' ? item.sort : item.type) + '\' items in the ' + (store.id === 'vault' ? 'vault' : 'guardian') + '.')); |
342 | 399 | // }); |
343 | 400 | // } else { |
344 | | - deferred.reject(new Error('There are too many \'' + (store.id === 'vault' ? item.sort : item.type) + '\' items in the ' + (store.id === 'vault' ? 'vault' : 'guardian') + '.')); |
| 401 | + deferred.reject(new Error('There are too many \'' + (store.id === 'vault' ? item.sort : item.type) + '\' items in the ' + (store.id === 'vault' ? 'vault' : 'guardian') + '.')); |
345 | 402 | // } |
346 | 403 | } |
347 | 404 |
|
|
354 | 411 |
|
355 | 412 | var promise = $q.when(dimStoreService.getStores()) |
356 | 413 | .then(function(stores) { |
357 | | - // var sortedStores = _.chain(stores) |
358 | | - // .filter(function(s) { |
359 | | - // return (s.id !== 'vault'); |
360 | | - // }) |
361 | | - // .sortBy(function(s) { |
362 | | - // if (s.id === store.id) { |
363 | | - // return 2; |
364 | | - // } else if (s.id === target.id) { |
365 | | - // return 0; |
366 | | - // } else { |
367 | | - // return 1; |
368 | | - // } |
369 | | - // }) |
370 | | - // .value(); |
371 | | - // |
372 | | - // var i = _.findWhere(store.items, { type: item.type, equipped: false }); |
373 | | - |
374 | 414 | return $q.reject('woopsie'); |
375 | 415 | }); |
376 | 416 |
|
377 | 417 | return promise; |
378 | | - |
379 | | - // var source = null; |
380 | | - // var checkVault = true; |
381 | | - // |
382 | | - // return dimStoreService.getStore(item.owner) |
383 | | - // .then(function(store) { |
384 | | - // source = store; |
385 | | - // |
386 | | - // if ((source.id === target.id) || (source.id === 'vault') || (target.id === 'vault')) { |
387 | | - // checkVault = false; |
388 | | - // } |
389 | | - // |
390 | | - // if (checkVault) { |
391 | | - // var itemToMove; |
392 | | - // var stores; |
393 | | - // var vault; |
394 | | - // |
395 | | - // return dimStoreService.getStores() |
396 | | - // .then(function(_stores) { |
397 | | - // stores = _stores; |
398 | | - // |
399 | | - // vault = _.findWhere(stores, { |
400 | | - // id: 'vault' |
401 | | - // }); |
402 | | - // |
403 | | - // itemToMove = _.findWhere(store.items, { |
404 | | - // equipped: false, |
405 | | - // type: item.type |
406 | | - // }); |
407 | | - // }) |
408 | | - // .then(function() { |
409 | | - // // var overflow = _.chain(stores) |
410 | | - // // .sortBy(function(s) { |
411 | | - // // if (s.id === 'vault') { |
412 | | - // // return 0; |
413 | | - // // } else if (s.id === source.id) { |
414 | | - // // return 2; |
415 | | - // // } else if (s.id === target.id) { |
416 | | - // // return 3; |
417 | | - // // } else { |
418 | | - // // return 1; |
419 | | - // // } |
420 | | - // // }) |
421 | | - // // .filter(function(s) { |
422 | | - // // var count = _.chain(s.items) |
423 | | - // // .where({ |
424 | | - // // equipped: false, |
425 | | - // // type: item.type |
426 | | - // // }) |
427 | | - // // .size() |
428 | | - // // .value(); |
429 | | - // // |
430 | | - // // if (size < 9) |
431 | | - // // |
432 | | - // // return ((s.id !== 'vault') && (s.id !== source.id) && (s.id !== target.id)); |
433 | | - // // }); |
434 | | - // // |
435 | | - // // if (_.isUndefined(overflow)) { |
436 | | - // // |
437 | | - // // } |
438 | | - // }); |
439 | | - // } |
440 | | - // }) |
441 | | - // .then(function() { |
442 | | - // |
443 | | - // }); |
444 | 418 | } |
445 | 419 |
|
446 | 420 | function isVaultToVault(item, store) { |
|
528 | 502 | promise = promise.then(moveToStore.bind(null, item, data.target, equip)); |
529 | 503 | } |
530 | 504 |
|
| 505 | + promise = promise |
| 506 | + .then(function() { |
| 507 | + item.moveAmount = 0; |
| 508 | + }); |
| 509 | + |
531 | 510 | return promise; |
532 | 511 | }) |
533 | 512 | .catch(function(e) { |
|
0 commit comments