Skip to content

Commit

Permalink
Manage recipe items in root node
Browse files Browse the repository at this point in the history
Add all recipes to the root node.
  • Loading branch information
RedGlow committed Sep 5, 2015
1 parent 50ad831 commit fe21984
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/program/recursive_recipe_computer.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ angular.module('legendarySearch.recursiveRecipeComputer', [
getRecipeTree: function(rootItemId, bankContent, buyImmediately) {
// local bankContent copy
bankContent = jQuery.extend({}, bankContent);
function getRecipe(itemId, unitaryRecipeAmount, remainingNeededAmount) {
function getRecipe(itemId, unitaryRecipeAmount, remainingNeededAmount, isRootNode) {
// check what we can get from the bank
var ownedAmount = Math.min(remainingNeededAmount, get(bankContent, itemId));
bankContent[itemId] -= ownedAmount;
Expand Down Expand Up @@ -139,7 +139,7 @@ angular.module('legendarySearch.recursiveRecipeComputer', [
iamount = ingredient.amount || 1,
iRemainingNeededAmount = iamount * remainingNeededAmount;
if(ingredient.type === 'item') {
return getRecipe(parseInt(iid), iamount, iRemainingNeededAmount);
return getRecipe(parseInt(iid), iamount, iRemainingNeededAmount, false);
} else {
return $q.when(perfectNode({
currencyId: ingredient.id,
Expand All @@ -165,19 +165,45 @@ angular.module('legendarySearch.recursiveRecipeComputer', [
}
// return the analysis result
return $q.all([ingredientsPromises, $q.when(tradingPostCost), $q.when(crafter), $q.when(recipeItemId)]);
}).then(function(results) {
// sum all recipe item ids
var recipeItemIds = [];
if(!!results[3]) {
recipeItemIds.push(results[3]);
}
var ingredientsResults = results[0];
if(!!ingredientsResults) {
jQuery.each(ingredientsResults, function(i, ingredient) {
Array.prototype.push.apply(recipeItemIds, ingredient.recipeItemIds);
});
}
recipeItemIds = recipeItemIds.filter(function(value, index, arr) {
return arr.indexOf(value) == index;
});
results[3] = recipeItemIds;
// add recipe item ids to the recipe, if root node
if(isRootNode) {
return $q.all(jQuery.map(recipeItemIds, function(recipeItemId) {
return getRecipe(recipeItemId, 1, 1);
})).then(function(recipeNodes) {
console.debug("Extending", results[0], "with", recipeNodes);
Array.prototype.push.apply(results[0], recipeNodes);
return results;
});
} else {
// not the root node: return the result directly
return results;
}
}).then(function(results) {
var ingredientsResults = results[0],
tradingPostCostResult = results[1],
crafters = [],
recipeItemIds = [];
recipeItemIds = results[3];
if(!!results[2]) {
var crafter = results[2];
crafter.itemId = itemId;
crafters.push(crafter);
}
if(!!results[3]) {
recipeItemIds.push(results[3]);
}
// compute the summed up total costs and completion percentage
var totalCosts = [];
var percentage = ownedAmount / (ownedAmount + remainingNeededAmount);
Expand All @@ -187,7 +213,6 @@ angular.module('legendarySearch.recursiveRecipeComputer', [
totalCostsItemMap = {};
jQuery.each(ingredientsResults, function(i, ingredient) {
Array.prototype.push.apply(crafters, ingredient.crafters);
Array.prototype.push.apply(recipeItemIds, ingredient.recipeItemIds);
jQuery.each(ingredient.totalCosts, function(j, cost) {
if(!!cost.currencyId) {
add(totalCostsCurrencyMap, cost.currencyId, cost.amount);
Expand Down Expand Up @@ -258,7 +283,17 @@ angular.module('legendarySearch.recursiveRecipeComputer', [
});
}
// get the base node, declaring we want one and still need one.
return getRecipe(rootItemId, 1, 1);
return getRecipe(rootItemId, 1, 1, true);
// add the recipe to the ingredients of the root
/*.then(function(rootNode) {
return $q.all(jQuery.map(rootNode.recipeItemIds, function(recipeItemId) {
return getRecipe(recipeItemId, 1, 1);
})).then(function(recipeIngredients) {
console.debug("Extending root node ingredients", rootNode.ingredients, "with recipe ingredients", recipeIngredients);
Array.prototype.push.apply(rootNode.ingredients, recipeIngredients);
return rootNode;
});
});*/
}
};
}
Expand Down

0 comments on commit fe21984

Please sign in to comment.