ProductController.php 4.0 KB

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