ServerController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\Contracts\View\Factory;
  14. use Illuminate\Contracts\View\View;
  15. use Illuminate\Http\RedirectResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Http\Response;
  18. use Illuminate\Support\Facades\Auth;
  19. class ServerController extends Controller
  20. {
  21. /**
  22. * Display a listing of the resource.
  23. *
  24. * @return Factory|View
  25. */
  26. public function index()
  27. {
  28. return view('servers.index')->with([
  29. 'servers' => Auth::user()->Servers
  30. ]);
  31. }
  32. /**
  33. * Show the form for creating a new resource.
  34. *
  35. * @return Factory|View|RedirectResponse
  36. */
  37. public function create()
  38. {
  39. //limit
  40. if (Auth::user()->Servers->count() >= Auth::user()->server_limit) {
  41. return redirect()->route('servers.index')->with('error', "You've already reached your server limit!");
  42. }
  43. //minimum credits
  44. if (Auth::user()->credits <= Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER' , 50)) {
  45. return redirect()->route('servers.index')->with('error', "You do not have the required amount of credits to create a new server!");
  46. }
  47. return view('servers.create')->with([
  48. 'products' => Product::where('disabled' , '=' , false)->orderBy('price', 'asc')->get(),
  49. 'locations' => Location::whereHas('nodes' , function ($query) {
  50. $query->where('disabled' , '=' , false);
  51. })->get(),
  52. 'nests' => Nest::where('disabled' , '=' , false)->get(),
  53. ]);
  54. }
  55. /**
  56. * Store a newly created resource in storage.
  57. *
  58. * @param Request $request
  59. * @return RedirectResponse
  60. */
  61. public function store(Request $request)
  62. {
  63. $request->validate([
  64. "name" => "required|max:191",
  65. "description" => "nullable|max:191",
  66. "node_id" => "required|exists:nodes,id",
  67. "egg_id" => "required|exists:eggs,id",
  68. "product_id" => "required|exists:products,id",
  69. ]);
  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 to create a new server!");
  77. }
  78. //create server
  79. $egg = Egg::findOrFail($request->input('egg_id'));
  80. $server = Auth::user()->servers()->create($request->all());
  81. $node = Node::findOrFail($request->input('node_id'));
  82. //create server on pterodactyl
  83. $response = Pterodactyl::createServer($server , $egg , $node);
  84. if (is_null($response)) return $this->serverCreationFailed($server);
  85. if ($response->failed()) return $this->serverCreationFailed($server);
  86. //update server with pterodactyl_id
  87. $server->update([
  88. 'pterodactyl_id' => $response->json()['attributes']['id'],
  89. 'identifier' => $response->json()['attributes']['identifier']
  90. ]);
  91. return redirect()->route('servers.index')->with('success', 'server created');
  92. }
  93. /**
  94. * Quick Fix
  95. * @param Server $server
  96. * @return RedirectResponse
  97. */
  98. private function serverCreationFailed(Server $server): RedirectResponse
  99. {
  100. $server->delete();
  101. Auth::user()->notify(new ServerCreationError($server));
  102. return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment were found.');
  103. }
  104. /**
  105. * Display the specified resource.
  106. *
  107. * @param Server $server
  108. * @return Response
  109. */
  110. public function show(Server $server)
  111. {
  112. //
  113. }
  114. /**
  115. * Show the form for editing the specified resource.
  116. *
  117. * @param Server $server
  118. * @return Response
  119. */
  120. public function edit(Server $server)
  121. {
  122. //
  123. }
  124. /**
  125. * Update the specified resource in storage.
  126. *
  127. * @param Request $request
  128. * @param Server $server
  129. * @return Response
  130. */
  131. public function update(Request $request, Server $server)
  132. {
  133. //
  134. }
  135. /**
  136. * Remove the specified resource from storage.
  137. *
  138. * @param Server $server
  139. * @return RedirectResponse
  140. * @throws Exception
  141. */
  142. public function destroy(Server $server)
  143. {
  144. $server->delete();
  145. return redirect()->route('servers.index')->with('success', 'server removed');
  146. }
  147. }