ServerController.php 5.3 KB

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