ServerController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Http\Client\Response;
  14. use Illuminate\Http\RedirectResponse;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Log;
  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. return view('servers.create')->with([
  33. 'products' => Product::where('disabled', '=', false)->orderBy('price', 'asc')->get(),
  34. 'locations' => Location::whereHas('nodes', function ($query) {
  35. $query->where('disabled', '=', false);
  36. })->get(),
  37. 'nests' => Nest::where('disabled', '=', false)->get(),
  38. 'minimum_credits' => Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)
  39. ]);
  40. }
  41. /** Store a newly created resource in storage. */
  42. public function store(Request $request)
  43. {
  44. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  45. $request->validate([
  46. "name" => "required|max:191",
  47. "description" => "nullable|max:191",
  48. "node_id" => "required|exists:nodes,id",
  49. "egg_id" => "required|exists:eggs,id",
  50. "product_id" => "required|exists:products,id"
  51. ]);
  52. //get required resources
  53. $egg = Egg::findOrFail($request->input('egg_id'));
  54. $node = Node::findOrFail($request->input('node_id'));
  55. $server = Auth::user()->servers()->create($request->all());
  56. //get free allocation ID
  57. $allocationId = Pterodactyl::getFreeAllocationId($node);
  58. if (!$allocationId) return $this->noAllocationsError($server);
  59. //create server on pterodactyl
  60. $response = Pterodactyl::createServer($server, $egg, $allocationId);
  61. if ($response->failed()) return $this->serverCreationFailed($response, $server);
  62. //update server with pterodactyl_id
  63. $server->update([
  64. 'pterodactyl_id' => $response->json()['attributes']['id'],
  65. 'identifier' => $response->json()['attributes']['identifier']
  66. ]);
  67. if (Configuration::getValueByKey('SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') {
  68. if (Auth::user()->credits >= $server->product->getHourlyPrice()) {
  69. Auth::user()->decrement('credits', $server->product->getHourlyPrice());
  70. }
  71. }
  72. return redirect()->route('servers.index')->with('success', 'server created');
  73. }
  74. /**
  75. * @return null|RedirectResponse
  76. */
  77. private function validateConfigurationRules()
  78. {
  79. //limit validation
  80. if (Auth::user()->servers()->count() >= Auth::user()->server_limit) {
  81. return redirect()->route('servers.index')->with('error', 'Server limit reached!');
  82. }
  83. // minimum credits
  84. if (FacadesRequest::has("product_id")) {
  85. $product = Product::findOrFail(FacadesRequest::input("product_id"));
  86. if (
  87. Auth::user()->credits <
  88. ($product->minimum_credits == -1
  89. ? Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)
  90. : $product->minimum_credits)
  91. ) {
  92. return redirect()->route('servers.index')->with('error', "You do not have the required amount of " . CREDITS_DISPLAY_NAME . " to use this product!");
  93. }
  94. }
  95. //Required Verification for creating an server
  96. if (Configuration::getValueByKey('FORCE_EMAIL_VERIFICATION', 'false') === 'true' && !Auth::user()->hasVerifiedEmail()) {
  97. return redirect()->route('profile.index')->with('error', "You are required to verify your email address before you can create a server.");
  98. }
  99. //Required Verification for creating an server
  100. if (Configuration::getValueByKey('FORCE_DISCORD_VERIFICATION', 'false') === 'true' && !Auth::user()->discordUser) {
  101. return redirect()->route('profile.index')->with('error', "You are required to link your discord account before you can create a server.");
  102. }
  103. return null;
  104. }
  105. /** Remove the specified resource from storage. */
  106. public function destroy(Server $server)
  107. {
  108. try {
  109. $server->delete();
  110. return redirect()->route('servers.index')->with('success', 'server removed');
  111. } catch (Exception $e) {
  112. return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource "' . $e->getMessage() . '"');
  113. }
  114. }
  115. /**
  116. * return redirect with error
  117. * @param Server $server
  118. * @return RedirectResponse
  119. */
  120. private function noAllocationsError(Server $server)
  121. {
  122. $server->delete();
  123. Auth::user()->notify(new ServerCreationError($server));
  124. return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment were found.');
  125. }
  126. /**
  127. * return redirect with error
  128. * @param Response $response
  129. * @param Server $server
  130. * @return RedirectResponse
  131. */
  132. private function serverCreationFailed(Response $response, Server $server)
  133. {
  134. $server->delete();
  135. return redirect()->route('servers.index')->with('error', json_encode($response->json()));
  136. }
  137. }