2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2018-01-11 05:19:03 +00:00
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
|
|
|
|
|
|
|
class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface
|
|
|
|
{
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return the model backing this repository.
|
|
|
|
*
|
|
|
|
* @return string
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return Allocation::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Set an array of allocation IDs to be assigned to a specific server.
|
|
|
|
*
|
|
|
|
* @param int|null $server
|
|
|
|
* @param array $ids
|
|
|
|
* @return int
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function assignAllocationsToServer(int $server = null, array $ids): int
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
|
|
|
return $this->getBuilder()->whereIn('id', $ids)->update(['server_id' => $server]);
|
|
|
|
}
|
2017-07-22 02:17:42 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return all of the allocations for a specific node.
|
|
|
|
*
|
|
|
|
* @param int $node
|
|
|
|
* @return \Illuminate\Support\Collection
|
2017-07-22 02:17:42 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function getAllocationsForNode(int $node): Collection
|
2017-07-22 02:17:42 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
return $this->getBuilder()->where('node_id', $node)->get($this->getColumns());
|
2017-07-22 02:17:42 +00:00
|
|
|
}
|
2018-01-09 04:12:19 +00:00
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
/**
|
|
|
|
* Return all of the allocations for a node in a paginated format.
|
|
|
|
*
|
|
|
|
* @param int $node
|
|
|
|
* @param int $perPage
|
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
|
|
|
*/
|
|
|
|
public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->where('node_id', $node)->paginate($perPage, $this->getColumns());
|
|
|
|
}
|
|
|
|
|
2018-01-09 04:12:19 +00:00
|
|
|
/**
|
|
|
|
* Return all of the unique IPs that exist for a given node.
|
|
|
|
*
|
|
|
|
* @param int $node
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public function getUniqueAllocationIpsForNode(int $node): Collection
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->where('node_id', $node)
|
|
|
|
->groupBy('ip')
|
|
|
|
->orderByRaw('INET_ATON(ip) ASC')
|
|
|
|
->get($this->getColumns());
|
|
|
|
}
|
2018-01-21 22:02:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the allocations that exist for a node that are not currently
|
|
|
|
* allocated.
|
|
|
|
*
|
|
|
|
* @param int $node
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getUnassignedAllocationIds(int $node): array
|
|
|
|
{
|
|
|
|
$results = $this->getBuilder()->select('id')->whereNull('server_id')->where('node_id', $node)->get();
|
|
|
|
|
|
|
|
return $results->pluck('id')->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of all allocations that are currently assigned to a given server.
|
|
|
|
*
|
|
|
|
* @param int $server
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAssignedAllocationIds(int $server): array
|
|
|
|
{
|
|
|
|
$results = $this->getBuilder()->select('id')->where('server_id', $server)->get();
|
|
|
|
|
|
|
|
return $results->pluck('id')->toArray();
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|