function buildRecipeTree($item, $app, $recipe = null, $multiplier = 1) { $tree = array('dataId' => $item->getDataId(), 'name' => $item->getName(), 'href' => $app['url_generator']->generate('item', array('dataId' => $item->getDataId())), 'rarity' => $item->getRarityName(), 'img' => $item->getImg(), 'price' => $item->getBestPrice(), 'vendor' => !!$item->getVendorPrice(), 'multiplier' => $multiplier, 'karma' => $item->getKarmaPrice()); if ($recipe) { $recipeTree = array(); foreach ($recipe->getIngredients() as $ingredient) { $ingredientItem = $ingredient->getItem(); $ingredientRecipe = null; $ingredientRecipes = $ingredientItem->getResultOfRecipes(); $ingredientMultiplier = $multiplier; if (count($ingredientRecipes)) { $ingredientRecipe = $ingredientRecipes[0]; $ingredientMultiplier /= multiplier($ingredientRecipe->getCount(), $ingredient->getCount()); } $recipeTree[] = array(buildRecipeTree($ingredientItem, $app, $ingredientRecipe, $ingredientMultiplier), $ingredient->getCount() * $multiplier); } $tree['recipe'] = array('count' => $recipe->getCount() * $multiplier, 'ingredients' => $recipeTree); } return $tree; }
function multiplier($n) { static $z = 2; if ($n >= $z) { if ($n % $z == 0) { echo $z; echo ' '; $n = $n / $z; } if ($n % $z != 0) { $z++; } multiplier($n); } }