allocation ?? throw new UnexpectedValueException('The server does not have a primary allocation.'); $allocation = $server->node->allocations()->lockForUpdate()->where('ip', $primaryAllocation->ip)->whereNull('server_id')->inRandomOrder()->first(); if ($allocation === null) { $start = config('pterodactyl.client_features.allocations.range_start'); $end = config('pterodactyl.client_features.allocations.range_end'); $range = AllocationRange::fromConfig($start, $end); throw_unless($range instanceof AllocationRange, NoAutoAllocationSpaceAvailableException::class); $ports = $server->node->allocations()->where('ip', $primaryAllocation->ip)->whereBetween('port', [$range->start, $range->end])->get(['port'])->map(fn (Allocation $allocation): int => $allocation->port)->all(); $available = array_diff(range($range->start, $range->end), $ports); throw_if($available === [], NoAutoAllocationSpaceAvailableException::class); $port = Collection::make($available)->random(); $this->allocations->create($server->node, ['allocation_ip' => $primaryAllocation->ip, 'allocation_ports' => [$port]]); $allocation = $server->node->allocations()->lockForUpdate()->where('ip', $primaryAllocation->ip)->where('port', $port)->firstOrFail(); } $allocation->update(['server_id' => $server->id]); Activity::event('server:allocation.create')->subject($server, $allocation)->property('allocation', $allocation->toString())->log(); return $allocation; } }