|
@@ -10,6 +10,7 @@ use App\Models\Node;
|
|
|
use App\Models\Product;
|
|
|
use App\Models\Server;
|
|
|
use App\Notifications\ServerCreationError;
|
|
|
+use Carbon\Carbon;
|
|
|
use Exception;
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
use Illuminate\Http\Client\Response;
|
|
@@ -30,7 +31,7 @@ class ServerController extends Controller
|
|
|
|
|
|
//Get server infos from ptero
|
|
|
$serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id, true);
|
|
|
- if (! $serverAttributes) {
|
|
|
+ if (!$serverAttributes) {
|
|
|
continue;
|
|
|
}
|
|
|
$serverRelationships = $serverAttributes['relationships'];
|
|
@@ -67,7 +68,7 @@ class ServerController extends Controller
|
|
|
/** Show the form for creating a new resource. */
|
|
|
public function create()
|
|
|
{
|
|
|
- if (! is_null($this->validateConfigurationRules())) {
|
|
|
+ if (!is_null($this->validateConfigurationRules())) {
|
|
|
return $this->validateConfigurationRules();
|
|
|
}
|
|
|
|
|
@@ -122,33 +123,31 @@ class ServerController extends Controller
|
|
|
// Check if node has enough memory and disk space
|
|
|
$checkResponse = Pterodactyl::checkNodeResources($node, $product->memory, $product->disk);
|
|
|
if ($checkResponse == false) {
|
|
|
- return redirect()->route('servers.index')->with('error', __("The node '".$nodeName."' doesn't have the required memory or disk left to allocate this product."));
|
|
|
+ return redirect()->route('servers.index')->with('error', __("The node '" . $nodeName . "' doesn't have the required memory or disk left to allocate this product."));
|
|
|
}
|
|
|
|
|
|
// Min. Credits
|
|
|
if (
|
|
|
- Auth::user()->credits <
|
|
|
- ($product->minimum_credits == -1
|
|
|
- ? config('SETTINGS::USER:MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)
|
|
|
- : $product->minimum_credits)
|
|
|
+ Auth::user()->credits < $product->minimum_credits ||
|
|
|
+ Auth::user()->credits < $product->price
|
|
|
) {
|
|
|
- return redirect()->route('servers.index')->with('error', 'You do not have the required amount of '.CREDITS_DISPLAY_NAME.' to use this product!');
|
|
|
+ return redirect()->route('servers.index')->with('error', 'You do not have the required amount of ' . CREDITS_DISPLAY_NAME . ' to use this product!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//Required Verification for creating an server
|
|
|
- if (config('SETTINGS::USER:FORCE_EMAIL_VERIFICATION', 'false') === 'true' && ! Auth::user()->hasVerifiedEmail()) {
|
|
|
+ if (config('SETTINGS::USER:FORCE_EMAIL_VERIFICATION', 'false') === 'true' && !Auth::user()->hasVerifiedEmail()) {
|
|
|
return redirect()->route('profile.index')->with('error', __('You are required to verify your email address before you can create a server.'));
|
|
|
}
|
|
|
|
|
|
//Required Verification for creating an server
|
|
|
|
|
|
- if (! config('SETTINGS::SYSTEM:CREATION_OF_NEW_SERVERS', 'true') && Auth::user()->role != 'admin') {
|
|
|
+ if (!config('SETTINGS::SYSTEM:CREATION_OF_NEW_SERVERS', 'true') && Auth::user()->role != 'admin') {
|
|
|
return redirect()->route('servers.index')->with('error', __('The system administrator has blocked the creation of new servers.'));
|
|
|
}
|
|
|
|
|
|
//Required Verification for creating an server
|
|
|
- if (config('SETTINGS::USER:FORCE_DISCORD_VERIFICATION', 'false') === 'true' && ! Auth::user()->discordUser) {
|
|
|
+ if (config('SETTINGS::USER:FORCE_DISCORD_VERIFICATION', 'false') === 'true' && !Auth::user()->discordUser) {
|
|
|
return redirect()->route('profile.index')->with('error', __('You are required to link your discord account before you can create a server.'));
|
|
|
}
|
|
|
|
|
@@ -161,7 +160,7 @@ class ServerController extends Controller
|
|
|
/** @var Node $node */
|
|
|
/** @var Egg $egg */
|
|
|
/** @var Product $product */
|
|
|
- if (! is_null($this->validateConfigurationRules())) {
|
|
|
+ if (!is_null($this->validateConfigurationRules())) {
|
|
|
return $this->validateConfigurationRules();
|
|
|
}
|
|
|
|
|
@@ -180,11 +179,12 @@ class ServerController extends Controller
|
|
|
$server = $request->user()->servers()->create([
|
|
|
'name' => $request->input('name'),
|
|
|
'product_id' => $request->input('product'),
|
|
|
+ 'last_billed' => Carbon::now()->toDateTimeString(),
|
|
|
]);
|
|
|
|
|
|
//get free allocation ID
|
|
|
$allocationId = Pterodactyl::getFreeAllocationId($node);
|
|
|
- if (! $allocationId) {
|
|
|
+ if (!$allocationId) {
|
|
|
return $this->noAllocationsError($server);
|
|
|
}
|
|
|
|
|
@@ -198,14 +198,11 @@ class ServerController extends Controller
|
|
|
//update server with pterodactyl_id
|
|
|
$server->update([
|
|
|
'pterodactyl_id' => $serverAttributes['id'],
|
|
|
- 'identifier' => $serverAttributes['identifier'],
|
|
|
+ 'identifier' => $serverAttributes['identifier'],
|
|
|
]);
|
|
|
|
|
|
- if (config('SETTINGS::SYSTEM:SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') {
|
|
|
- if ($request->user()->credits >= $server->product->getHourlyPrice()) {
|
|
|
- $request->user()->decrement('credits', $server->product->getHourlyPrice());
|
|
|
- }
|
|
|
- }
|
|
|
+ // Charge first billing cycle
|
|
|
+ $request->user()->decrement('credits', $server->product->price);
|
|
|
|
|
|
return redirect()->route('servers.index')->with('success', __('Server created'));
|
|
|
}
|
|
@@ -234,8 +231,6 @@ class ServerController extends Controller
|
|
|
*/
|
|
|
private function serverCreationFailed(Response $response, Server $server)
|
|
|
{
|
|
|
- $server->delete();
|
|
|
-
|
|
|
return redirect()->route('servers.index')->with('error', json_encode($response->json()));
|
|
|
}
|
|
|
|
|
@@ -247,7 +242,21 @@ class ServerController extends Controller
|
|
|
|
|
|
return redirect()->route('servers.index')->with('success', __('Server removed'));
|
|
|
} catch (Exception $e) {
|
|
|
- return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to remove a resource "').$e->getMessage().'"');
|
|
|
+ return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to remove a resource"') . $e->getMessage() . '"');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** Cancel Server */
|
|
|
+ public function cancel(Server $server)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ error_log($server->update([
|
|
|
+ 'cancelled' => now(),
|
|
|
+ ]));
|
|
|
+
|
|
|
+ return redirect()->route('servers.index')->with('success', __('Server cancelled'));
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to cancel the server"') . $e->getMessage() . '"');
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -255,7 +264,7 @@ class ServerController extends Controller
|
|
|
public function show(Server $server)
|
|
|
{
|
|
|
if ($server->user_id != Auth::user()->id) {
|
|
|
- return back()->with('error', __('´This is not your Server!'));
|
|
|
+ return back()->with('error', __('This is not your Server!'));
|
|
|
}
|
|
|
$serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
|
|
|
$serverRelationships = $serverAttributes['relationships'];
|
|
@@ -276,10 +285,10 @@ class ServerController extends Controller
|
|
|
$pteroNode = Pterodactyl::getNode($serverRelationships['node']['attributes']['id']);
|
|
|
|
|
|
$products = Product::orderBy('created_at')
|
|
|
- ->whereHas('nodes', function (Builder $builder) use ($serverRelationships) { //Only show products for that node
|
|
|
- $builder->where('id', '=', $serverRelationships['node']['attributes']['id']);
|
|
|
- })
|
|
|
- ->get();
|
|
|
+ ->whereHas('nodes', function (Builder $builder) use ($serverRelationships) { //Only show products for that node
|
|
|
+ $builder->where('id', '=', $serverRelationships['node']['attributes']['id']);
|
|
|
+ })
|
|
|
+ ->get();
|
|
|
|
|
|
// Set the each product eggs array to just contain the eggs name
|
|
|
foreach ($products as $product) {
|
|
@@ -297,10 +306,8 @@ class ServerController extends Controller
|
|
|
|
|
|
public function upgrade(Server $server, Request $request)
|
|
|
{
|
|
|
- if ($server->user_id != Auth::user()->id) {
|
|
|
- return redirect()->route('servers.index');
|
|
|
- }
|
|
|
- if (! isset($request->product_upgrade)) {
|
|
|
+ if ($server->user_id != Auth::user()->id || $server->suspended) return redirect()->route('servers.index');
|
|
|
+ if (!isset($request->product_upgrade)) {
|
|
|
return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('this product is the only one'));
|
|
|
}
|
|
|
$user = Auth::user();
|
|
@@ -319,30 +326,56 @@ class ServerController extends Controller
|
|
|
$requiredisk = $newProduct->disk - $oldProduct->disk;
|
|
|
$checkResponse = Pterodactyl::checkNodeResources($node, $requireMemory, $requiredisk);
|
|
|
if ($checkResponse == false) {
|
|
|
- return redirect()->route('servers.index')->with('error', __("The node '".$nodeName."' doesn't have the required memory or disk left to upgrade the server."));
|
|
|
+ return redirect()->route('servers.index')->with('error', __("The node '" . $nodeName . "' doesn't have the required memory or disk left to upgrade the server."));
|
|
|
}
|
|
|
|
|
|
- $priceupgrade = $newProduct->getHourlyPrice();
|
|
|
-
|
|
|
- if ($priceupgrade < $oldProduct->getHourlyPrice()) {
|
|
|
- $priceupgrade = 0;
|
|
|
- }
|
|
|
- if ($user->credits >= $priceupgrade && $user->credits >= $newProduct->minimum_credits) {
|
|
|
- $server->product_id = $request->product_upgrade;
|
|
|
- $server->update();
|
|
|
+ // calculate the amount of credits that the user overpayed for the old product when canceling the server right now
|
|
|
+ // billing periods are hourly, daily, weekly, monthly, quarterly, half-annually, annually
|
|
|
+ $billingPeriod = $oldProduct->billing_period;
|
|
|
+ // seconds
|
|
|
+ $billingPeriods = [
|
|
|
+ 'hourly' => 3600,
|
|
|
+ 'daily' => 86400,
|
|
|
+ 'weekly' => 604800,
|
|
|
+ 'monthly' => 2592000,
|
|
|
+ 'quarterly' => 7776000,
|
|
|
+ 'half-annually' => 15552000,
|
|
|
+ 'annually' => 31104000
|
|
|
+ ];
|
|
|
+ // Get the amount of hours the user has been using the server
|
|
|
+ $billingPeriodMultiplier = $billingPeriods[$billingPeriod];
|
|
|
+ $timeDifference = now()->diffInSeconds($server->last_billed);
|
|
|
+
|
|
|
+ error_log("Time DIFFERENCE!!!! ", $timeDifference);
|
|
|
+ // Calculate the price for the time the user has been using the server
|
|
|
+ $overpayedCredits = $oldProduct->price - $oldProduct->price * ($timeDifference / $billingPeriodMultiplier);
|
|
|
+
|
|
|
+
|
|
|
+ if ($user->credits >= $newProduct->price && $user->credits >= $newProduct->minimum_credits) {
|
|
|
$server->allocation = $serverAttributes['allocation'];
|
|
|
+ // Update the server on the panel
|
|
|
$response = Pterodactyl::updateServer($server, $newProduct);
|
|
|
- if ($response->failed()) {
|
|
|
- return $this->serverCreationFailed($response, $server);
|
|
|
- }
|
|
|
- //update user balance
|
|
|
- $user->decrement('credits', $priceupgrade);
|
|
|
- //restart the server
|
|
|
- $response = Pterodactyl::powerAction($server, 'restart');
|
|
|
- if ($response->failed()) {
|
|
|
- return redirect()->route('servers.index')->with('error', $response->json()['errors'][0]['detail']);
|
|
|
- }
|
|
|
+ if ($response->failed()) return $this->serverCreationFailed($response, $server);
|
|
|
+
|
|
|
+ // Remove the allocation property from the server object as it is not a column in the database
|
|
|
+ unset($server->allocation);
|
|
|
+ // Update the server on controlpanel
|
|
|
+ $server->update([
|
|
|
+ 'product_id' => $newProduct->id,
|
|
|
+ 'updated_at' => now(),
|
|
|
+ 'last_billed' => now(),
|
|
|
+ 'cancelled' => null,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // Refund the user the overpayed credits
|
|
|
+ if ($overpayedCredits > 0) $user->increment('credits', $overpayedCredits);
|
|
|
|
|
|
+ // Withdraw the credits for the new product
|
|
|
+ $user->decrement('credits', $newProduct->price);
|
|
|
+
|
|
|
+ //restart the server
|
|
|
+ $response = Pterodactyl::powerAction($server, "restart");
|
|
|
+ if ($response->failed()) return redirect()->route('servers.index')->with('error', 'Server upgraded successfully! Could not restart the server: ' . $response->json()['errors'][0]['detail']);
|
|
|
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'));
|