ServerController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /** Display a listing of the resource. */
  22. public function index(): View|Factory
  23. {
  24. return view('servers.index')->with([
  25. 'servers' => Auth::user()->Servers
  26. ]);
  27. }
  28. /** Show the form for creating a new resource. */
  29. public function create(): View|Factory|RedirectResponse
  30. {
  31. //limit
  32. if (Auth::user()->Servers->count() >= Auth::user()->server_limit) {
  33. return redirect()->route('servers.index')->with('error', "You've already reached your server limit!");
  34. }
  35. //minimum credits
  36. if (Auth::user()->credits <= Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER' , 50)) {
  37. return redirect()->route('servers.index')->with('error', "You do not have the required amount of credits to create a new server!");
  38. }
  39. return view('servers.create')->with([
  40. 'products' => Product::where('disabled' , '=' , false)->orderBy('price', 'asc')->get(),
  41. 'locations' => Location::whereHas('nodes' , function ($query) {
  42. $query->where('disabled' , '=' , false);
  43. })->get(),
  44. 'nests' => Nest::where('disabled' , '=' , false)->get(),
  45. ]);
  46. }
  47. /** Store a newly created resource in storage. */
  48. public function store(Request $request): RedirectResponse
  49. {
  50. $request->validate([
  51. "name" => "required|max:191",
  52. "description" => "nullable|max:191",
  53. "node_id" => "required|exists:nodes,id",
  54. "egg_id" => "required|exists:eggs,id",
  55. "product_id" => "required|exists:products,id",
  56. ]);
  57. //limit validation
  58. if (Auth::user()->servers()->count() >= Auth::user()->server_limit) {
  59. return redirect()->route('servers.index')->with('error', 'Server limit reached!');
  60. }
  61. //minimum credits
  62. if (Auth::user()->credits <= Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER' , 50)) {
  63. return redirect()->route('servers.index')->with('error', "You do not have the required amount of credits to create a new server!");
  64. }
  65. //create server
  66. $egg = Egg::findOrFail($request->input('egg_id'));
  67. $server = Auth::user()->servers()->create($request->all());
  68. $node = Node::findOrFail($request->input('node_id'));
  69. //create server on pterodactyl
  70. $response = Pterodactyl::createServer($server , $egg , $node);
  71. if (is_null($response)) return $this->serverCreationFailed($server);
  72. if ($response->failed()) return $this->serverCreationFailed($server);
  73. //update server with pterodactyl_id
  74. $server->update([
  75. 'pterodactyl_id' => $response->json()['attributes']['id'],
  76. 'identifier' => $response->json()['attributes']['identifier']
  77. ]);
  78. return redirect()->route('servers.index')->with('success', 'server created');
  79. }
  80. /** Quick Fix */
  81. private function serverCreationFailed(Server $server): RedirectResponse
  82. {
  83. $server->delete();
  84. Auth::user()->notify(new ServerCreationError($server));
  85. return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment were found.');
  86. }
  87. /** Remove the specified resource from storage. */
  88. public function destroy(Server $server): RedirectResponse
  89. {
  90. try {
  91. $server->delete();
  92. return redirect()->route('servers.index')->with('success', 'server removed');
  93. } catch (\Exception $e) {
  94. return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource');
  95. }
  96. }
  97. }