ProductController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. *
  17. * @param Request $request
  18. * @param Egg $egg
  19. * @return Collection|JsonResponse
  20. */
  21. public function getNodesBasedOnEgg(Request $request, Egg $egg)
  22. {
  23. if (is_null($egg->id)) {
  24. return response()->json('Egg ID is required', '400');
  25. }
  26. //get products that include this egg
  27. $products = Product::query()
  28. ->with('nodes')
  29. ->where('disabled', '=', false)
  30. ->whereHas('eggs', function (Builder $builder) use ($egg) {
  31. $builder->where('id', '=', $egg->id);
  32. })->get();
  33. $nodes = collect();
  34. //filter unique nodes
  35. $products->each(function (Product $product) use ($nodes) {
  36. $product->nodes->each(function (Node $node) use ($nodes) {
  37. if (! $nodes->contains('id', $node->id) && ! $node->disabled) {
  38. $nodes->add($node);
  39. }
  40. });
  41. });
  42. return $nodes;
  43. }
  44. /**
  45. * @description get product locations based on selected egg
  46. *
  47. * @param Request $request
  48. * @param Egg $egg
  49. * @return Collection|JsonResponse
  50. */
  51. public function getLocationsBasedOnEgg(Request $request, Egg $egg)
  52. {
  53. $nodes = $this->getNodesBasedOnEgg($request, $egg);
  54. foreach ($nodes as $key => $node) {
  55. $pteroNode = Pterodactyl::getNode($node->id);
  56. if ($pteroNode['allocated_resources']['memory'] >= ($pteroNode['memory'] * ($pteroNode['memory_overallocate'] + 100) / 100) || $pteroNode['allocated_resources']['disk'] >= ($pteroNode['disk'] * ($pteroNode['disk_overallocate'] + 100) / 100)) {
  57. $nodes->forget($key);
  58. }
  59. }
  60. $locations = collect();
  61. //locations
  62. $nodes->each(function (Node $node) use ($nodes, $locations) {
  63. /** @var Location $location */
  64. $location = $node->location;
  65. if (! $locations->contains('id', $location->id)) {
  66. $nodeIds = $nodes->map(function ($node) {
  67. return $node->id;
  68. });
  69. $location->nodes = $location->nodes()
  70. ->whereIn('id', $nodeIds)
  71. ->get();
  72. $locations->add($location);
  73. }
  74. });
  75. return $locations;
  76. }
  77. /**
  78. * @param Node $node
  79. * @param Egg $egg
  80. * @return Collection|JsonResponse
  81. */
  82. public function getProductsBasedOnNode(Egg $egg, Node $node)
  83. {
  84. if (is_null($egg->id) || is_null($node->id)) {
  85. return response()->json('node and egg id is required', '400');
  86. }
  87. $products = Product::query()
  88. ->where('disabled', '=', false)
  89. ->whereHas('nodes', function (Builder $builder) use ($node) {
  90. $builder->where('id', '=', $node->id);
  91. })
  92. ->whereHas('eggs', function (Builder $builder) use ($egg) {
  93. $builder->where('id', '=', $egg->id);
  94. })
  95. ->get();
  96. $pteroNode = Pterodactyl::getNode($node->id);
  97. foreach ($products as $key => $product) {
  98. 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']) {
  99. $product->doesNotFit = true;
  100. }
  101. }
  102. return $products;
  103. }
  104. }