ServerController.php 6.2 KB

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