ServerController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\Database\Eloquent\Builder;
  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. $productCount = Product::query()->where('disabled', '=', false)->count();
  32. $nodeCount = Node::query()->has('products')->count();
  33. $eggs = Egg::query()->has('products')->get();
  34. $nests = Nest::query()->whereHas('eggs', function (Builder $builder) {
  35. $builder->has('products');
  36. })->get();
  37. return view('servers.create')->with([
  38. 'productCount' => $productCount,
  39. 'nodeCount' => $nodeCount,
  40. 'nests' => $nests,
  41. 'eggs' => $eggs,
  42. 'user' => Auth::user(),
  43. ]);
  44. }
  45. /**
  46. * @return null|RedirectResponse
  47. */
  48. private function validateConfigurationRules()
  49. {
  50. //limit validation
  51. if (Auth::user()->servers()->count() >= Auth::user()->server_limit) {
  52. return redirect()->route('servers.index')->with('error', 'Server limit reached!');
  53. }
  54. // minimum credits
  55. if (FacadesRequest::has("product")) {
  56. $product = Product::findOrFail(FacadesRequest::input("product"));
  57. if (
  58. Auth::user()->credits <
  59. ($product->minimum_credits == -1
  60. ? Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)
  61. : $product->minimum_credits)
  62. ) {
  63. return redirect()->route('servers.index')->with('error', "You do not have the required amount of " . CREDITS_DISPLAY_NAME . " to use this product!");
  64. }
  65. }
  66. //Required Verification for creating an server
  67. if (Configuration::getValueByKey('FORCE_EMAIL_VERIFICATION', 'false') === 'true' && !Auth::user()->hasVerifiedEmail()) {
  68. return redirect()->route('profile.index')->with('error', "You are required to verify your email address before you can create a server.");
  69. }
  70. //Required Verification for creating an server
  71. if (Configuration::getValueByKey('FORCE_DISCORD_VERIFICATION', 'false') === 'true' && !Auth::user()->discordUser) {
  72. return redirect()->route('profile.index')->with('error', "You are required to link your discord account before you can create a server.");
  73. }
  74. return null;
  75. }
  76. /** Store a newly created resource in storage. */
  77. public function store(Request $request)
  78. {
  79. /** @var Node $node */
  80. /** @var Egg $egg */
  81. /** @var Product $product */
  82. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  83. $request->validate([
  84. "name" => "required|max:191",
  85. "node" => "required|exists:nodes,id",
  86. "egg" => "required|exists:eggs,id",
  87. "product" => "required|exists:products,id"
  88. ]);
  89. //get required resources
  90. $product = Product::query()->findOrFail($request->input('product'));
  91. $egg = $product->eggs()->findOrFail($request->input('egg'));
  92. $node = $product->nodes()->findOrFail($request->input('node'));
  93. $server = $request->user()->servers()->create([
  94. 'name' => $request->input('name'),
  95. 'product_id' => $request->input('product'),
  96. ]);
  97. //get free allocation ID
  98. $allocationId = Pterodactyl::getFreeAllocationId($node);
  99. if (!$allocationId) return $this->noAllocationsError($server);
  100. //create server on pterodactyl
  101. $response = Pterodactyl::createServer($server, $egg, $allocationId);
  102. if ($response->failed()) return $this->serverCreationFailed($response, $server);
  103. //update server with pterodactyl_id
  104. $server->update([
  105. 'pterodactyl_id' => $response->json()['attributes']['id'],
  106. 'identifier' => $response->json()['attributes']['identifier']
  107. ]);
  108. if (Configuration::getValueByKey('SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') {
  109. if ($request->user()->credits >= $server->product->getHourlyPrice()) {
  110. $request->user()->decrement('credits', $server->product->getHourlyPrice());
  111. }
  112. }
  113. return redirect()->route('servers.index')->with('success', 'server created');
  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. /** Remove the specified resource from storage. */
  138. public function destroy(Server $server)
  139. {
  140. try {
  141. $server->delete();
  142. return redirect()->route('servers.index')->with('success', 'server removed');
  143. } catch (Exception $e) {
  144. return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource "' . $e->getMessage() . '"');
  145. }
  146. }
  147. }