2025-04-24 18:24:18 -04:00
< ? php
2025-07-01 21:33:11 -04:00
namespace App\Extensions\Features\Schemas ;
2025-04-24 18:24:18 -04:00
2025-12-11 14:34:27 +01:00
use App\Enums\SubuserPermission ;
2025-07-01 21:33:11 -04:00
use App\Extensions\Features\FeatureSchemaInterface ;
2025-04-24 18:24:18 -04:00
use App\Facades\Activity ;
use App\Models\Server ;
use App\Models\ServerVariable ;
2025-09-08 08:56:59 +02:00
use App\Repositories\Daemon\DaemonServerRepository ;
2025-04-24 18:24:18 -04:00
use Closure ;
2025-05-06 13:01:34 +02:00
use Exception ;
2025-04-24 18:24:18 -04:00
use Filament\Actions\Action ;
use Filament\Facades\Filament ;
use Filament\Forms\Components\TextInput ;
2025-09-08 13:12:33 -04:00
use Filament\Infolists\Components\TextEntry ;
2025-04-24 18:24:18 -04:00
use Filament\Notifications\Notification ;
2025-05-09 23:57:18 +02:00
use Illuminate\Database\Eloquent\Builder ;
use Illuminate\Support\Facades\Blade ;
2025-04-24 18:24:18 -04:00
use Illuminate\Support\Facades\Validator ;
2025-05-09 23:57:18 +02:00
use Illuminate\Support\HtmlString ;
2025-04-24 18:24:18 -04:00
2025-07-01 21:33:11 -04:00
class GSLTokenSchema implements FeatureSchemaInterface
2025-04-24 18:24:18 -04:00
{
/** @return array<string> */
public function getListeners () : array
{
return [
2025-05-06 13:01:34 +02:00
'(gsl token expired)' ,
'(account not found)' ,
2025-04-24 18:24:18 -04:00
];
}
public function getId () : string
{
2025-05-09 23:57:18 +02:00
return 'gsl_token' ;
2025-04-24 18:24:18 -04:00
}
2025-09-08 13:12:33 -04:00
/**
* @ throws Exception
*/
2025-04-24 18:24:18 -04:00
public function getAction () : Action
{
/** @var Server $server */
$server = Filament :: getTenant ();
/** @var ServerVariable $serverVariable */
2025-05-09 23:57:18 +02:00
$serverVariable = $server -> serverVariables () -> whereHas ( 'variable' , function ( Builder $query ) {
$query -> where ( 'env_variable' , 'STEAM_ACC' );
}) -> first ();
2025-04-24 18:24:18 -04:00
return Action :: make ( $this -> getId ())
-> requiresConfirmation ()
-> modalHeading ( 'Invalid GSL token' )
-> modalDescription ( 'It seems like your Gameserver Login Token (GSL token) is invalid or has expired.' )
-> modalSubmitActionLabel ( 'Update GSL Token' )
2025-12-11 14:34:27 +01:00
-> disabledSchema ( fn () => ! user () ? -> can ( SubuserPermission :: StartupUpdate , $server ))
2025-09-08 13:12:33 -04:00
-> schema ([
TextEntry :: make ( 'info' )
2025-05-09 23:57:18 +02:00
-> label ( new HtmlString ( Blade :: render ( 'You can either <x-filament::link href="https://steamcommunity.com/dev/managegameservers" target="_blank">generate a new one</x-filament::link> and enter it below or leave the field blank to remove it completely.' ))),
2025-04-24 18:24:18 -04:00
TextInput :: make ( 'gsltoken' )
-> label ( 'GSL Token' )
-> rules ([
fn () : Closure => function ( string $attribute , $value , Closure $fail ) use ( $serverVariable ) {
$validator = Validator :: make ([ 'validatorkey' => $value ], [
'validatorkey' => $serverVariable -> variable -> rules ,
]);
if ( $validator -> fails ()) {
$message = str ( $validator -> errors () -> first ()) -> replace ( 'validatorkey' , $serverVariable -> variable -> name );
$fail ( $message );
}
},
])
2025-09-15 23:28:57 +02:00
-> hintIcon ( 'tabler-code' , fn () => implode ( '|' , $serverVariable -> variable -> rules ))
2025-04-24 18:24:18 -04:00
-> label ( fn () => $serverVariable -> variable -> name )
-> prefix ( fn () => '{{' . $serverVariable -> variable -> env_variable . '}}' )
-> helperText ( fn () => empty ( $serverVariable -> variable -> description ) ? '—' : $serverVariable -> variable -> description ),
])
2025-09-08 08:56:59 +02:00
-> action ( function ( array $data , DaemonServerRepository $serverRepository ) use ( $server , $serverVariable ) {
2025-04-24 18:24:18 -04:00
/** @var Server $server */
$server = Filament :: getTenant ();
try {
$new = $data [ 'gsltoken' ] ? ? '' ;
$original = $serverVariable -> variable_value ;
$serverVariable -> update ([
'variable_value' => $new ,
]);
if ( $original !== $new ) {
Activity :: event ( 'server:startup.edit' )
-> property ([
'variable' => $serverVariable -> variable -> env_variable ,
'old' => $original ,
'new' => $new ,
])
-> log ();
}
2025-09-08 08:56:59 +02:00
$serverRepository -> setServer ( $server ) -> power ( 'restart' );
2025-04-24 18:24:18 -04:00
Notification :: make ()
-> title ( 'GSL Token updated' )
2025-05-06 13:01:34 +02:00
-> body ( 'Server will restart now.' )
2025-04-24 18:24:18 -04:00
-> success ()
-> send ();
2025-05-06 13:01:34 +02:00
} catch ( Exception $exception ) {
2025-04-24 18:24:18 -04:00
Notification :: make ()
2025-05-06 13:01:34 +02:00
-> title ( 'Could not update GSL Token' )
-> body ( $exception -> getMessage ())
2025-04-24 18:24:18 -04:00
-> danger ()
-> send ();
}
});
}
}