ServerController.php 4.5 KB

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