ServerController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Classes\Pterodactyl;
  4. use App\Models\Configuration;
  5. use App\Models\Egg;
  6. use App\Models\Location;
  7. use App\Models\Nest;
  8. use App\Models\Node;
  9. use App\Models\Product;
  10. use App\Models\Server;
  11. use App\Notifications\ServerCreationError;
  12. use Exception;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Http\Client\Response;
  15. use Illuminate\Http\RedirectResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Request as FacadesRequest;
  19. class ServerController extends Controller
  20. {
  21. /** Display a listing of the resource. */
  22. public function index()
  23. {
  24. return view('servers.index')->with([
  25. 'servers' => Auth::user()->Servers
  26. ]);
  27. }
  28. /** Show the form for creating a new resource. */
  29. public function create()
  30. {
  31. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  32. $productCount = Product::query()->where('disabled', '=', false)->count();
  33. $nodeCount = Node::query()->has('products')->count();
  34. $eggs = Egg::query()->has('products')->get();
  35. $locations = Location::all();
  36. $nests = Nest::query()->whereHas('eggs', function (Builder $builder) {
  37. $builder->has('products');
  38. })->get();
  39. return view('servers.create')->with([
  40. 'productCount' => $productCount,
  41. 'nodeCount' => $nodeCount,
  42. 'nests' => $nests,
  43. 'locations' => $locations,
  44. 'eggs' => $eggs,
  45. 'user' => Auth::user(),
  46. ]);
  47. }
  48. /**
  49. * @return null|RedirectResponse
  50. */
  51. private function validateConfigurationRules()
  52. {
  53. //limit validation
  54. if (Auth::user()->servers()->count() >= Auth::user()->server_limit) {
  55. return redirect()->route('servers.index')->with('error', 'Server limit reached!');
  56. }
  57. // minimum credits
  58. if (FacadesRequest::has("product")) {
  59. $product = Product::findOrFail(FacadesRequest::input("product"));
  60. if (
  61. Auth::user()->credits <
  62. ($product->minimum_credits == -1
  63. ? Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)
  64. : $product->minimum_credits)
  65. ) {
  66. return redirect()->route('servers.index')->with('error', "You do not have the required amount of " . CREDITS_DISPLAY_NAME . " to use this product!");
  67. }
  68. }
  69. //Required Verification for creating an server
  70. if (Configuration::getValueByKey('FORCE_EMAIL_VERIFICATION', 'false') === 'true' && !Auth::user()->hasVerifiedEmail()) {
  71. return redirect()->route('profile.index')->with('error', "You are required to verify your email address before you can create a server.");
  72. }
  73. //Required Verification for creating an server
  74. if (Configuration::getValueByKey('FORCE_DISCORD_VERIFICATION', 'false') === 'true' && !Auth::user()->discordUser) {
  75. return redirect()->route('profile.index')->with('error', "You are required to link your discord account before you can create a server.");
  76. }
  77. return null;
  78. }
  79. /** Store a newly created resource in storage. */
  80. public function store(Request $request)
  81. {
  82. /** @var Node $node */
  83. /** @var Egg $egg */
  84. /** @var Product $product */
  85. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  86. $request->validate([
  87. "name" => "required|max:191",
  88. "node" => "required|exists:nodes,id",
  89. "egg" => "required|exists:eggs,id",
  90. "product" => "required|exists:products,id"
  91. ]);
  92. //get required resources
  93. $product = Product::query()->findOrFail($request->input('product'));
  94. $egg = $product->eggs()->findOrFail($request->input('egg'));
  95. $node = $product->nodes()->findOrFail($request->input('node'));
  96. $server = $request->user()->servers()->create([
  97. 'name' => $request->input('name'),
  98. 'product_id' => $request->input('product'),
  99. ]);
  100. //get free allocation ID
  101. $allocationId = Pterodactyl::getFreeAllocationId($node);
  102. if (!$allocationId) return $this->noAllocationsError($server);
  103. //create server on pterodactyl
  104. $response = Pterodactyl::createServer($server, $egg, $allocationId);
  105. if ($response->failed()) return $this->serverCreationFailed($response, $server);
  106. //update server with pterodactyl_id
  107. $server->update([
  108. 'pterodactyl_id' => $response->json()['attributes']['id'],
  109. 'identifier' => $response->json()['attributes']['identifier']
  110. ]);
  111. if (Configuration::getValueByKey('SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') {
  112. if ($request->user()->credits >= $server->product->getHourlyPrice()) {
  113. $request->user()->decrement('credits', $server->product->getHourlyPrice());
  114. }
  115. }
  116. return redirect()->route('servers.index')->with('success', 'Server created');
  117. }
  118. /**
  119. * return redirect with error
  120. * @param Server $server
  121. * @return RedirectResponse
  122. */
  123. private function noAllocationsError(Server $server)
  124. {
  125. $server->delete();
  126. Auth::user()->notify(new ServerCreationError($server));
  127. return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment on this node were found.');
  128. }
  129. /**
  130. * return redirect with error
  131. * @param Response $response
  132. * @param Server $server
  133. * @return RedirectResponse
  134. */
  135. private function serverCreationFailed(Response $response, Server $server)
  136. {
  137. $server->delete();
  138. return redirect()->route('servers.index')->with('error', json_encode($response->json()));
  139. }
  140. /** Remove the specified resource from storage. */
  141. public function destroy(Server $server)
  142. {
  143. try {
  144. $server->delete();
  145. return redirect()->route('servers.index')->with('success', 'Server removed');
  146. } catch (Exception $e) {
  147. return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource "' . $e->getMessage() . '"');
  148. }
  149. }
  150. }