Path: blob/1.0-develop/app/Repositories/Eloquent/AllocationRepository.php
7460 views
<?php12namespace Pterodactyl\Repositories\Eloquent;34use Pterodactyl\Models\Allocation;5use Illuminate\Database\Eloquent\Builder;6use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;78class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface9{10/**11* Return the model backing this repository.12*/13public function model(): string14{15return Allocation::class;16}1718/**19* Return all the allocations that exist for a node that are not currently20* allocated.21*/22public function getUnassignedAllocationIds(int $node): array23{24return Allocation::query()->select('id')25->whereNull('server_id')26->where('node_id', $node)27->get()28->pluck('id')29->toArray();30}3132/**33* Return a concatenated result set of node ips that already have at least one34* server assigned to that IP. This allows for filtering out sets for35* dedicated allocation IPs.36*37* If an array of nodes is passed the results will be limited to allocations38* in those nodes.39*/40protected function getDiscardableDedicatedAllocations(array $nodes = []): array41{42$query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result');4344if (!empty($nodes)) {45$query->whereIn('node_id', $nodes);46}4748return $query->whereNotNull('server_id')49->groupByRaw('CONCAT(node_id, ip)')50->get()51->pluck('result')52->toArray();53}5455/**56* Return a single allocation from those meeting the requirements.57*/58public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false): ?Allocation59{60$query = Allocation::query()->whereNull('server_id');6162if (!empty($nodes)) {63$query->whereIn('node_id', $nodes);64}6566if (!empty($ports)) {67$query->where(function (Builder $inner) use ($ports) {68$whereIn = [];69foreach ($ports as $port) {70if (is_array($port)) {71$inner->orWhereBetween('port', $port);72continue;73}7475$whereIn[] = $port;76}7778if (!empty($whereIn)) {79$inner->orWhereIn('port', $whereIn);80}81});82}8384// If this allocation should not be shared with any other servers get85// the data and modify the query as necessary,86if ($dedicated) {87$discard = $this->getDiscardableDedicatedAllocations($nodes);8889if (!empty($discard)) {90$query->whereNotIn(91$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'),92$discard93);94}95}9697return $query->inRandomOrder()->first();98}99}100101102