2021-06-05 09:26:32 +00:00
< ? php
namespace App\Console\Commands ;
use App\Classes\Pterodactyl ;
2021-06-25 21:42:53 +00:00
use App\Models\Product ;
2021-06-05 09:26:32 +00:00
use App\Models\Server ;
2021-06-25 21:42:53 +00:00
use App\Models\User ;
2021-06-05 09:26:32 +00:00
use Carbon\Carbon ;
use Illuminate\Console\Command ;
2021-06-25 21:42:53 +00:00
use Illuminate\Support\Collection ;
2021-06-05 09:26:32 +00:00
class ChargeCreditsCommand extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = 'credits:charge' ;
/**
* The console command description .
*
* @ var string
*/
protected $description = 'Charge all users with active servers' ;
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return string
*/
public function handle ()
{
2021-06-25 21:42:53 +00:00
return Server :: whereNull ( 'suspended' ) -> chunk ( 10 , function ( $servers ) {
2021-06-12 12:24:50 +00:00
/** @var Server $server */
foreach ( $servers as $server ) {
2021-06-25 21:42:53 +00:00
/** @var Product $product */
$product = $server -> product ;
/** @var User $user */
2021-06-05 09:26:32 +00:00
$user = $server -> user ;
2021-06-25 21:42:53 +00:00
#charge credits / suspend server
if ( $user -> credits >= $product -> getHourlyPrice ()){
$this -> line ( " <fg=blue> { $user -> name } </> Current credits: <fg=green> { $user -> credits } </> Credits to be removed: <fg=red> { $product -> getHourlyPrice () } </> " );
$user -> decrement ( 'credits' , $product -> getHourlyPrice ());
2021-06-05 09:26:32 +00:00
} else {
2021-06-25 21:42:53 +00:00
$this -> line ( " server <fg=blue> { $server -> name } </> <fg=red>has been suspended! </> " );
$server -> suspend ();
2021-06-05 09:26:32 +00:00
}
}
});
}
}