Add some new options for ports

This commit is contained in:
Lance Pioch 2024-06-02 16:58:32 -04:00
parent 2e7c534a3b
commit bbe09ced1d

View File

@ -129,157 +129,97 @@ class CreateServer extends CreateRecord
->required() ->required()
->columnSpanFull(), ->columnSpanFull(),
Forms\Components\Select::make('allocation_id') Forms\Components\TagsInput::make('ports')
->preload() ->columnSpan(3)
->live() ->placeholder('Example: 25565, 8080, 1337-1340')
->prefixIcon('tabler-network') ->reorderable()
->label('Primary Allocation') ->splitKeys(['Tab', ' ', ','])
->columnSpan(2) ->helperText(new HtmlString('
->disabled(fn (Forms\Get $get) => $get('node_id') === null)
->searchable(['ip', 'port', 'ip_alias'])
->afterStateUpdated(function (Forms\Set $set) {
$set('allocation_additional', null);
$set('allocation_additional.needstobeastringhere.extra_allocations', null);
})
->getOptionLabelFromRecordUsing(
fn (Allocation $allocation) => "$allocation->ip:$allocation->port" .
($allocation->ip_alias ? " ($allocation->ip_alias)" : '')
)
->placeholder(function (Forms\Get $get) {
$node = Node::find($get('node_id'));
if ($node?->allocations) {
return 'Select an Allocation';
}
return 'Create a New Allocation';
})
->relationship(
'allocation',
'ip',
fn (Builder $query, Forms\Get $get) => $query
->where('node_id', $get('node_id'))
->whereNull('server_id'),
)
->createOptionForm(fn (Forms\Get $get) => [
Forms\Components\TextInput::make('allocation_ip')
->datalist(Node::find($get('node_id'))?->ipAddresses() ?? [])
->label('IP Address')
->inlineLabel()
->ipv4()
->helperText("Usually your machine's public IP unless you are port forwarding.")
// ->selectablePlaceholder(false)
->required(),
Forms\Components\TextInput::make('allocation_alias')
->label('Alias')
->inlineLabel()
->default(null)
->datalist([
$get('name'),
$this->egg?->name,
])
->helperText('Optional display name to help you remember what these are.')
->required(false),
Forms\Components\TagsInput::make('allocation_ports')
->placeholder('Examples: 27015, 27017-27019')
->helperText(new HtmlString('
These are the ports that users can connect to this Server through. These are the ports that users can connect to this Server through.
<br /> <br />
You would have to port forward these on your home network. You would typically port forward these on your home network.
')) '))
->label('Ports') ->label('Ports')
->inlineLabel() ->afterStateUpdated(function ($state, Forms\Set $set) {
->live() $ports = collect();
->afterStateUpdated(function ($state, Forms\Set $set) { $update = false;
$ports = collect(); foreach ($state as $portEntry) {
$update = false; if (!str_contains($portEntry, '-')) {
foreach ($state as $portEntry) { if (is_numeric($portEntry)) {
if (!str_contains($portEntry, '-')) { $ports->push((int) $portEntry);
if (is_numeric($portEntry)) {
$ports->push((int) $portEntry);
continue; continue;
}
// Do not add non-numerical ports
$update = true;
continue;
}
$update = true;
[$start, $end] = explode('-', $portEntry);
if (!is_numeric($start) || !is_numeric($end)) {
continue;
}
$start = max((int) $start, 0);
$end = min((int) $end, 2 ** 16 - 1);
for ($i = $start; $i <= $end; $i++) {
$ports->push($i);
}
} }
$uniquePorts = $ports->unique()->values(); // Do not add non-numerical ports
if ($ports->count() > $uniquePorts->count()) { $update = true;
$update = true;
$ports = $uniquePorts;
}
$sortedPorts = $ports->sort()->values(); continue;
if ($sortedPorts->all() !== $ports->all()) { }
$update = true;
$ports = $sortedPorts;
}
$ports = $ports->filter(fn ($port) => $port > 1024 && $port < 65535)->values(); $update = true;
[$start, $end] = explode('-', $portEntry);
if (!is_numeric($start) || !is_numeric($end)) {
continue;
}
if ($update) { $start = max((int) $start, 0);
$set('allocation_ports', $ports->all()); $end = min((int) $end, 2 ** 16 - 1);
} for ($i = $start; $i <= $end; $i++) {
}) $ports->push($i);
->splitKeys(['Tab', ' ', ',']) }
->required(), }
])
->createOptionUsing(function (array $data, Forms\Get $get): int { $uniquePorts = $ports->unique()->values();
return collect( if ($ports->count() > $uniquePorts->count()) {
resolve(AssignmentService::class)->handle(Node::find($get('node_id')), $data) $update = true;
)->first(); $ports = $uniquePorts;
}
$sortedPorts = $ports->sort()->values();
if ($sortedPorts->all() !== $ports->all()) {
$update = true;
$ports = $sortedPorts;
}
$ports = $ports->filter(fn ($port) => $port > 1024 && $port < 65535)->values();
if ($update) {
$set('allocation_ports', $ports->all());
}
}) })
->required(), ->live(),
Forms\Components\Repeater::make('allocation_additional') Forms\Components\Select::make('primary_port')
->label('Additional Allocations') ->live()
->columnSpan(2) ->helperText(fn (Forms\Get $get) => match (true) {
->addActionLabel('Add Allocation') !str_contains($get('startup'), '{{SERVER_PORT}}') => 'This is disabled because there is no primary server port in the startup command.',
->disabled(fn (Forms\Get $get) => $get('allocation_id') === null) empty($get('ports')) => 'This is disabled because you haven\'t entered any ports yet.',
// ->addable() TODO disable when all allocations are taken true => 'This port will take the place of {{SERVER_PORT}} in your startup command.',
// ->addable() TODO disable until first additional allocation is selected })
->simple( ->columnSpan(3)
Forms\Components\Select::make('extra_allocations') ->selectablePlaceholder(false)
->live() ->disabled(fn (Forms\Get $get) => empty($get('ports')) || !str_contains($get('startup'), '{{SERVER_PORT}}'))
->preload() ->options(fn (Forms\Get $get) => $get('ports'))
->disableOptionsWhenSelectedInSiblingRepeaterItems() ->label('Primary Port'),
->prefixIcon('tabler-network')
->label('Additional Allocations') Forms\Components\KeyValue::make('pts')
->columnSpan(2) // ->deletable(fn ($state) => empty($state) || dd('SERVER_PORT', array_keys($state), $state) || !in_array('SERVER_PORT', array_keys($state)))
->disabled(fn (Forms\Get $get) => $get('../../node_id') === null) // ->addable(false)
->searchable(['ip', 'port', 'ip_alias']) // ->editableKeys(false)
->getOptionLabelFromRecordUsing( ->columnSpan(3)
fn (Allocation $allocation) => "$allocation->ip:$allocation->port" . ->keyLabel('Name')
($allocation->ip_alias ? " ($allocation->ip_alias)" : '') ->valueLabel('Port')
) ->label('Ports')
->placeholder('Select additional Allocations') ->live(),
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->relationship( Forms\Components\Placeholder::make('instructions')
'allocations', ->label(new HtmlString('Port Instructions:<br><br>
'ip', These are the ports that users can connect to this Server through.
fn (Builder $query, Forms\Get $get, Forms\Components\Select $component, $state) => $query <br>
->where('node_id', $get('../../node_id')) You would typically port forward these on your home network.
->whereNot('id', $get('../../allocation_id')) '))
->whereNull('server_id'), ->columnSpan(3),
),
),
Forms\Components\Textarea::make('startup') Forms\Components\Textarea::make('startup')
->hintIcon('tabler-code') ->hintIcon('tabler-code')