ServerController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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")) {
  49. $product = Product::findOrFail(FacadesRequest::input("product"));
  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. "node" => "required|exists:nodes,id",
  76. "egg" => "required|exists:eggs,id",
  77. "product" => "required|exists:products,id"
  78. ]);
  79. //get required resources
  80. $egg = Egg::findOrFail($request->input('egg'));
  81. $node = Node::findOrFail($request->input('node'));
  82. $server = $request->user()->servers()->create([
  83. 'name' => $request->input('name'),
  84. 'product_id' => $request->input('product'),
  85. ]);
  86. //get free allocation ID
  87. $allocationId = Pterodactyl::getFreeAllocationId($node);
  88. if (!$allocationId) return $this->noAllocationsError($server);
  89. //create server on pterodactyl
  90. $response = Pterodactyl::createServer($server, $egg, $allocationId);
  91. if ($response->failed()) return $this->serverCreationFailed($response, $server);
  92. //update server with pterodactyl_id
  93. $server->update([
  94. 'pterodactyl_id' => $response->json()['attributes']['id'],
  95. 'identifier' => $response->json()['attributes']['identifier']
  96. ]);
  97. if (Configuration::getValueByKey('SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') {
  98. if ($request->user()->credits >= $server->product->getHourlyPrice()) {
  99. $request->user()->decrement('credits', $server->product->getHourlyPrice());
  100. }
  101. }
  102. return redirect()->route('servers.index')->with('success', 'server created');
  103. }
  104. /**
  105. * return redirect with error
  106. * @param Server $server
  107. * @return RedirectResponse
  108. */
  109. private function noAllocationsError(Server $server)
  110. {
  111. $server->delete();
  112. Auth::user()->notify(new ServerCreationError($server));
  113. return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment were found.');
  114. }
  115. /**
  116. * return redirect with error
  117. * @param Response $response
  118. * @param Server $server
  119. * @return RedirectResponse
  120. */
  121. private function serverCreationFailed(Response $response, Server $server)
  122. {
  123. $server->delete();
  124. return redirect()->route('servers.index')->with('error', json_encode($response->json()));
  125. }
  126. /** Remove the specified resource from storage. */
  127. public function destroy(Server $server)
  128. {
  129. try {
  130. $server->delete();
  131. return redirect()->route('servers.index')->with('success', 'server removed');
  132. } catch (Exception $e) {
  133. return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource "' . $e->getMessage() . '"');
  134. }
  135. }
  136. }