ProductController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Classes\Pterodactyl;
  4. use App\Models\Egg;
  5. use App\Models\Location;
  6. use App\Models\Node;
  7. use App\Models\Product;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Http\JsonResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Collection;
  12. class ProductController extends Controller
  13. {
  14. /**
  15. * @description get product locations based on selected egg
  16. * @param Request $request
  17. * @param Egg $egg
  18. * @return Collection|JsonResponse
  19. */
  20. public function getNodesBasedOnEgg(Request $request, Egg $egg)
  21. {
  22. if (is_null($egg->id)) return response()->json('Egg ID is required', '400');
  23. #get products that include this egg
  24. $products = Product::query()
  25. ->with('nodes')
  26. ->where('disabled', '=', false)
  27. ->whereHas('eggs', function (Builder $builder) use ($egg) {
  28. $builder->where('id', '=', $egg->id);
  29. })->get();
  30. $nodes = collect();
  31. #filter unique nodes
  32. $products->each(function (Product $product) use ($nodes) {
  33. $product->nodes->each(function (Node $node) use ($nodes) {
  34. if (!$nodes->contains('id', $node->id) && !$node->disabled) {
  35. $nodes->add($node);
  36. }
  37. });
  38. });
  39. return $nodes;
  40. }
  41. /**
  42. * @description get product locations based on selected egg
  43. * @param Request $request
  44. * @param Egg $egg
  45. * @return Collection|JsonResponse
  46. */
  47. public function getLocationsBasedOnEgg(Request $request, Egg $egg)
  48. {
  49. $nodes = $this->getNodesBasedOnEgg($request, $egg);
  50. foreach($nodes as $key => $node){
  51. $pteroNode = Pterodactyl::getNode($node->id);
  52. if($pteroNode['allocated_resources']['memory']>=($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)||$pteroNode['allocated_resources']['disk']>=($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)) $nodes->forget($key);
  53. }
  54. $locations = collect();
  55. //locations
  56. $nodes->each(function (Node $node) use ($nodes, $locations) {
  57. /** @var Location $location */
  58. $location = $node->location;
  59. if (!$locations->contains('id', $location->id)) {
  60. $nodeIds = $nodes->map(function ($node) {
  61. return $node->id;
  62. });
  63. $location->nodes = $location->nodes()
  64. ->whereIn('id', $nodeIds)
  65. ->get();
  66. $locations->add($location);
  67. }
  68. });
  69. return $locations;
  70. }
  71. /**
  72. * @param Node $node
  73. * @param Egg $egg
  74. * @return Collection|JsonResponse
  75. */
  76. public function getProductsBasedOnNode(Egg $egg, Node $node)
  77. {
  78. if (is_null($egg->id) || is_null($node->id)) return response()->json('node and egg id is required', '400');
  79. $products = Product::query()
  80. ->where('disabled', '=', false)
  81. ->whereHas('nodes', function (Builder $builder) use ($node) {
  82. $builder->where('id', '=', $node->id);
  83. })
  84. ->whereHas('eggs', function (Builder $builder) use ($egg) {
  85. $builder->where('id', '=', $egg->id);
  86. })
  87. ->get();
  88. $pteroNode = Pterodactyl::getNode($node->id);
  89. foreach($products as $key => $product){
  90. if($product->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true;
  91. }
  92. return $products;
  93. }
  94. }