diff --git a/app/Classes/Pterodactyl.php b/app/Classes/Pterodactyl.php index bee54b71..f75a3062 100644 --- a/app/Classes/Pterodactyl.php +++ b/app/Classes/Pterodactyl.php @@ -6,6 +6,7 @@ use App\Models\Egg; use App\Models\Nest; use App\Models\Node; use App\Models\Server; +use App\Models\Product; use Exception; use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\Response; @@ -271,4 +272,22 @@ class Pterodactyl if ($response->failed()) throw self::getException("Failed to get server attributes from pterodactyl - ", $response->status()); return $response->json()['attributes']; } + + public static function updateServer(Server $server, Product $product) + { + return self::client()->patch("/application/servers/{$server->pterodactyl_id}/build", [ + "allocation" => $server->allocation, + "memory" => $product->memory, + "swap" => $product->swap, + "disk" => $product->disk, + "io" => $product->io, + "cpu" => $product->cpu, + "threads" => null, + "feature_limits" => [ + "databases" => $product->databases, + "backups" => $product->backups, + "allocations" => $product->allocations, + ] + ]); + } } diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index c1c0a33a..e5ead954 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -213,4 +213,65 @@ class ServerController extends Controller return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to remove a resource "') . $e->getMessage() . '"'); } } + + /** Show Server Settings */ + public function show(Server $server) + { + $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id); + $serverRelationships = $serverAttributes['relationships']; + $serverLocationAttributes = $serverRelationships['location']['attributes']; + + //Set server infos + $server->location = $serverLocationAttributes['long'] ? + $serverLocationAttributes['long'] : + $serverLocationAttributes['short']; + + $server->node = $serverRelationships['node']['attributes']['name']; + $server->name = $serverAttributes['name']; + $server->egg = $serverRelationships['egg']['attributes']['name']; + $products = Product::orderBy("created_at")->get(); + + // Set the each product eggs array to just contain the eggs name + foreach ($products as $product) { + $product->eggs = $product->eggs->pluck('name')->toArray(); + } + + return view('servers.settings')->with([ + 'server' => $server, + 'products' => $products + ]); + } + + public function upgrade(Server $server, Request $request) + { + if(!isset($request->product_upgrade)) + { + return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('this product is the only one')); + } + $user = Auth::user(); + $oldProduct = Product::where('id', $server->product->id)->first(); + $newProduct = Product::where('id', $request->product_upgrade)->first(); + $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id); + $priceupgrade = $newProduct->getHourlyPrice(); + + if ($priceupgrade < $oldProduct->getHourlyPrice()) { + $priceupgrade = 0; + } + if ($user->credits >= $priceupgrade) + { + + $server->product_id = $request->product_upgrade; + $server->update(); + $server->allocation = $serverAttributes['allocation']; + $response = Pterodactyl::updateServer($server, $newProduct); + if ($response->failed()) return $this->serverCreationFailed($response, $server); + //update user balance + $user->decrement('credits', $priceupgrade); + return redirect()->route('servers.show', ['server' => $server->id])->with('success', __('Server Successfully Upgraded')); + } + else + { + return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('Not Enough Balance for Upgrade')); + } + } } diff --git a/resources/views/servers/index.blade.php b/resources/views/servers/index.blade.php index 72187653..3c84086d 100644 --- a/resources/views/servers/index.blade.php +++ b/resources/views/servers/index.blade.php @@ -155,11 +155,10 @@ {{ __('Manage') }} - + + + {{ __('Settings') }} + @endforeach @@ -168,42 +167,4 @@ - - @endsection diff --git a/resources/views/servers/settings.blade.php b/resources/views/servers/settings.blade.php new file mode 100644 index 00000000..f7db9fd1 --- /dev/null +++ b/resources/views/servers/settings.blade.php @@ -0,0 +1,308 @@ +@extends('layouts.main') + +@section('content') + +
+
+
+
+

{{__('Server Settings')}}

+
+ +
+
+ +
+ + + +
+
+
+
+
+
+
+
+
+

SERVER NAME

+
+ {{ $server->name }} +
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+

CPU

+
+ @if($server->product->cpu == 0)Unlimited @else {{$server->product->cpu}} % @endif +
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+

Memory

+
+ @if($server->product->memory == 0)Unlimited @else {{$server->product->memory}}MB @endif +
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+

STORAGE

+
+ @if($server->product->disk == 0)Unlimited @else {{$server->product->disk}}MB @endif +
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
{{__('Server Information')}}
+
+
+
+ +
+
+
+ +
+
+ + {{ $server->id }} + +
+
+
+
+
+
+ +
+
+ + {{ $server->identifier }} + +
+
+
+
+
+
+ +
+
+ + {{ number_format($server->product->getHourlyPrice(), 2, '.', '') }} + +
+
+
+
+
+
+ +
+
+ + {{ $server->product->getHourlyPrice() * 24 * 30 }} + +
+
+
+
+
+
+ +
+
+ + {{ $server->location }} + +
+
+
+
+
+
+ +
+
+ + {{ $server->node }} + +
+
+
+
+
+
+ +
+
+ + {{ $server->product->backups }} + +
+
+
+
+
+
+ +
+
+ + {{ $server->product->databases }} + +
+
+
+ +
+
+ +
+ + + +
+ + +
+ + + +@endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index cdd00a92..01fce126 100644 --- a/routes/web.php +++ b/routes/web.php @@ -63,6 +63,7 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () { Route::get('notifications/readAll',[NotificationController::class,'readAll'])->name('notifications.readAll'); Route::resource('notifications', NotificationController::class); Route::resource('servers', ServerController::class); + Route::post('servers/{server}/upgrade', [ServerController::class,'upgrade'])->name('servers.upgrade'); Route::resource('profile', ProfileController::class); Route::resource('store', StoreController::class);