Skip to content

Commit

Permalink
Tweaks to recipe calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
BigFunger committed Oct 27, 2015
1 parent dba9abf commit 7bcda11
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 59 deletions.
8 changes: 7 additions & 1 deletion public/recipe_calculator/lookup/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h2>Materials</h2>
<thead>
<tr>
<th>Material</th>
<th>Id</th>
<th>Count</th>
</tr>
</thead>
Expand All @@ -36,6 +37,7 @@ <h2>Materials</h2>
<a ng-href="#/recipe_calculator/editor/{{material._id}}">add</a>
</span>
</td>
<td>{{material._id}}</td>
<td>{{material.count}}</td>
</tr>
</tbody>
Expand All @@ -46,19 +48,23 @@ <h2>Total Materials</h2>
<table class="table table-condensed">
<thead>
<tr>
<th>Step</th>
<th>Material</th>
<th>Id</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="material in totalMaterials">
<tr ng-repeat="material in totalMaterials.used">
<td>{{material.step}}</td>
<td>
{{material.name}}
<span ng-hide="material.found">
&nbsp;
<a ng-href="#/recipe_calculator/editor/{{material._id}}">add</a>
</span>
</td>
<td>{{material._id}}</td>
<td>{{material.count}}</td>
</tr>
</tbody>
Expand Down
5 changes: 3 additions & 2 deletions public/recipe_calculator/lookup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ define(function (require) {
});

fungerPluginMinecraft.getTotalMaterials($scope.recipe._id)
.then((totalMaterials) => {
$scope.totalMaterials = totalMaterials;
.then((result) => {
$scope.totalMaterials = result;
console.log(result);
});
}

Expand Down
147 changes: 147 additions & 0 deletions public/services/lib/total_materials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
define(function (require) {
return function CourierFetchCallResponseHandlers(Private, Promise, $http) {
var _ = require('lodash');

function getRecipe(recipeId) {
return $http.get(`/funger-plugin/api/getMaterials/${recipeId}`)
.catch(function(result) {
return {
'_id': recipeId,
'name': recipeId,
'found': false
};
})
.then(function (result) {
var materials = [];
var recipeDoc = result.data.recipeDoc;
var materialDocs = result.data.materialDocs;

if (materialDocs) {
materials = materialDocs.map(function(materialDoc) {
if (materialDoc.found) {
return {
'_id' : materialDoc._id,
'name' : materialDoc._source.name,
'basic' : !!materialDoc._source.basic,
'found' : true
};
} else {
return {
'_id' : materialDoc._id,
'name' : materialDoc._id,
'found' : false
};
}
});
}

return {
'_id' : recipeDoc._id,
'name' : recipeDoc._source.name,
'basic' : !!recipeDoc._source.basic,
'found' : true,
'amount' : recipeDoc._source.amount || 1,
'material' : {
'_id' : recipeDoc._id,
'name' : recipeDoc._source.name,
'basic' : !!recipeDoc._source.basic,
'found' : true
},
'materials' : materials
};
});
}

function getTotalMaterials(recipeId) {
var produced = [];

return iteration(recipeId, 1)
.then((totalMaterials) => {
return {
used: groupMaterial(totalMaterials),
extra: groupMaterial(produced)
}
});

function iteration(recipeId, step) {
return getRecipe(recipeId)
.then((recipe) => {
if (contains(produced, recipe.material)) {
produced = removeOne(produced, recipe.material);
return [];
}

var result = [];
var childMaterials = recipe.materials || [];
return iterateChildMaterials()
.then(() => {
recipe.material.step = recipe.material.basic ? 0 : step;

for (var i=0;i<recipe.amount;i++) {
produced.push(recipe.material);
result.push(recipe.material);
}
produced = removeOne(produced, recipe.material);

//result.push(recipe.material);
return result;
});

function iterateChildMaterials() {
var childMaterial = childMaterials.pop();
if (!childMaterial)
return Promise.resolve([]);

if (!childMaterial.found) {
result.push(childMaterial);
return iterateChildMaterials();
}

return iteration(childMaterial._id, step + 1)
.then((iterationResult) => {
result = result.concat(iterationResult);
return iterateChildMaterials();
});
}
});
}
}

function contains(materials, materialToFind) {
return _.findIndex(materials, function(material) {
return material._id === materialToFind._id;
}) !== -1;
}

function removeOne(materials, materialToRemove) {
var removed = _.remove(materials, function(material) {
return material._id === materialToRemove._id;
});
removed.pop();
return materials.concat(removed);
}

function groupMaterial(arr) {
var result = [];

arr.forEach((ele) => {
var index = _.findIndex(result, '_id', ele._id);
if (index === -1) {
result.push({
_id: ele._id,
name: ele.name,
found: ele.found,
step: ele.step,
count: 1
});
} else {
result[index].count += 1;
}
});

return result;
}

return getTotalMaterials;
}
});
54 changes: 9 additions & 45 deletions public/services/minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ define(function (require) {
var _ = require('lodash');
var module = require('ui/modules').get('app/recipe_calculator', []);

module.service('fungerPluginMinecraft', function ($http, Promise) {
module.service('fungerPluginMinecraft', function ($http, Private) {
var getTotalMaterials = Private(require('plugins/funger-plugin/services/lib/total_materials'));

return {
recipeSearch: recipeSearch,
getMaterials: getMaterials,
Expand Down Expand Up @@ -44,7 +46,8 @@ define(function (require) {
'_id' : recipeDoc._id,
'name' : recipeDoc._source.name,
'basic' : recipeDoc._source.basic,
'found' : true
'found' : true,
'amount' : recipeDoc._source.amount
});
} else {
var childMaterials = materialDocs.map(function(materialDoc) {
Expand All @@ -53,13 +56,15 @@ define(function (require) {
'_id' : materialDoc._id,
'name' : materialDoc._source.name,
'basic' : materialDoc._source.basic,
'found' : true
'found' : true,
'amount' : materialDoc._source.amount
};
} else {
return {
'_id' : materialDoc._id,
'name' : materialDoc._id,
'found' : false
'found' : false,
'amount' : 1
};
}
});
Expand All @@ -76,47 +81,6 @@ define(function (require) {
});
}

function getTotalMaterials(recipeId) {
return iteration(recipeId)
.then((totalMaterials) => {
// console.log('****************************************************');
// console.log(totalMaterials);
// console.log('****************************************************');
//return totalMaterials;

var groupedTotal = groupMaterial(totalMaterials);
//console.log(groupedTotal);
return groupedTotal;
});

function iteration(recipeId) {
var total = [];

return getMaterials(recipeId)
.then((materials) => {
var promises = [];

materials.forEach((material) => {
if (!material.found || material.basic) {
total.push(material);
} else {
promises.push(iteration(material._id));
}
});

return Promise.all(promises)
.then((resultsArray) => {
resultsArray.forEach((result) => {
var parentId = recipeId;
total = total.concat(result);
});

return total;
});
});
}
}

function groupMaterial(arr) {
var result = [];

Expand Down
Loading

0 comments on commit 7bcda11

Please sign in to comment.