ServerController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if (Configuration::getValueByKey('SERVER_CREATE_CHARGE_FIRST_HOUR' , 'true') == 'true'){
  65. if (Auth::user()->credits >= $server->product->getHourlyPrice()){
  66. Auth::user()->decrement('credits', $server->product->getHourlyPrice());
  67. }
  68. }
  69. return redirect()->route('servers.index')->with('success', 'server created');
  70. }
  71. /**
  72. * @return null|RedirectResponse
  73. */
  74. private function validateConfigurationRules(){
  75. //limit validation
  76. if (Auth::user()->servers()->count() >= Auth::user()->server_limit) {
  77. return redirect()->route('servers.index')->with('error', 'Server limit reached!');
  78. }
  79. //minimum credits
  80. if (Auth::user()->credits <= Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)) {
  81. return redirect()->route('servers.index')->with('error', "You do not have the required amount of ".CREDITS_DISPLAY_NAME." to create a new server!");
  82. }
  83. //Required Verification for creating an server
  84. if (Configuration::getValueByKey('FORCE_EMAIL_VERIFICATION', 'false') === 'true' && !Auth::user()->hasVerifiedEmail()) {
  85. return redirect()->route('profile.index')->with('error', "You are required to verify your email address before you can create a server.");
  86. }
  87. //Required Verification for creating an server
  88. if (Configuration::getValueByKey('FORCE_DISCORD_VERIFICATION', 'false') === 'true' && !Auth::user()->discordUser) {
  89. return redirect()->route('profile.index')->with('error', "You are required to link your discord account before you can create a server.");
  90. }
  91. return null;
  92. }
  93. /** Remove the specified resource from storage. */
  94. public function destroy(Server $server)
  95. {
  96. try {
  97. $server->delete();
  98. return redirect()->route('servers.index')->with('success', 'server removed');
  99. } catch (Exception $e) {
  100. return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource "' . $e->getMessage() . '"');
  101. }
  102. }
  103. /**
  104. * return redirect with error
  105. * @param Server $server
  106. * @return RedirectResponse
  107. */
  108. private function noAllocationsError(Server $server)
  109. {
  110. $server->delete();
  111. Auth::user()->notify(new ServerCreationError($server));
  112. return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment were found.');
  113. }
  114. /**
  115. * return redirect with error
  116. * @param Response $response
  117. * @param Server $server
  118. * @return RedirectResponse
  119. */
  120. private function serverCreationFailed(Response $response , Server $server)
  121. {
  122. $server->delete();
  123. return redirect()->route('servers.index')->with('error', json_encode($response->json()));
  124. }
  125. }