ServerController.php 5.9 KB

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