ServerController.php 5.1 KB

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