ProductController.php 4.2 KB

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