22
33namespace Pterodactyl \Repositories \Eloquent ;
44
5- use Illuminate \Support \Collection ;
65use Pterodactyl \Models \Allocation ;
76use Illuminate \Database \Eloquent \Builder ;
87use Pterodactyl \Contracts \Repository \AllocationRepositoryInterface ;
@@ -19,20 +18,6 @@ public function model()
1918 return Allocation::class;
2019 }
2120
22- /**
23- * Return all of the unique IPs that exist for a given node.
24- *
25- * @param int $node
26- * @return \Illuminate\Support\Collection
27- */
28- public function getUniqueAllocationIpsForNode (int $ node ): Collection
29- {
30- return $ this ->getBuilder ()->where ('node_id ' , $ node )
31- ->groupBy ('ip ' )
32- ->orderByRaw ('INET_ATON(ip) ASC ' )
33- ->get ($ this ->getColumns ());
34- }
35-
3621 /**
3722 * Return all of the allocations that exist for a node that are not currently
3823 * allocated.
@@ -42,22 +27,12 @@ public function getUniqueAllocationIpsForNode(int $node): Collection
4227 */
4328 public function getUnassignedAllocationIds (int $ node ): array
4429 {
45- $ results = $ this ->getBuilder ()->select ('id ' )->whereNull ('server_id ' )->where ('node_id ' , $ node )->get ();
46-
47- return $ results ->pluck ('id ' )->toArray ();
48- }
49-
50- /**
51- * Get an array of all allocations that are currently assigned to a given server.
52- *
53- * @param int $server
54- * @return array
55- */
56- public function getAssignedAllocationIds (int $ server ): array
57- {
58- $ results = $ this ->getBuilder ()->select ('id ' )->where ('server_id ' , $ server )->get ();
59-
60- return $ results ->pluck ('id ' )->toArray ();
30+ return Allocation::query ()->select ('id ' )
31+ ->whereNull ('server_id ' )
32+ ->where ('node_id ' , $ node )
33+ ->get ()
34+ ->pluck ('id ' )
35+ ->toArray ();
6136 }
6237
6338 /**
@@ -71,21 +46,19 @@ public function getAssignedAllocationIds(int $server): array
7146 * @param array $nodes
7247 * @return array
7348 */
74- public function getDiscardableDedicatedAllocations (array $ nodes = []): array
49+ protected function getDiscardableDedicatedAllocations (array $ nodes = []): array
7550 {
76- $ instance = $ this ->getBuilder ()->select (
77- $ this ->getBuilder ()->raw ('CONCAT_WS("-", node_id, ip) as result ' )
78- );
51+ $ query = Allocation::query ()->selectRaw ('CONCAT_WS("-", node_id, ip) as result ' );
7952
8053 if (! empty ($ nodes )) {
81- $ instance ->whereIn ('node_id ' , $ nodes );
54+ $ query ->whereIn ('node_id ' , $ nodes );
8255 }
8356
84- $ results = $ instance ->whereNotNull ('server_id ' )
85- ->groupBy ( $ this -> getBuilder ()-> raw ( 'CONCAT(node_id, ip) ' ) )
86- ->get ();
87-
88- return $ results -> pluck ( ' result ' ) ->toArray ();
57+ return $ query ->whereNotNull ('server_id ' )
58+ ->groupByRaw ( 'CONCAT(node_id, ip) ' )
59+ ->get ()
60+ -> pluck ( ' result ' )
61+ ->toArray ();
8962 }
9063
9164 /**
@@ -98,26 +71,26 @@ public function getDiscardableDedicatedAllocations(array $nodes = []): array
9871 */
9972 public function getRandomAllocation (array $ nodes , array $ ports , bool $ dedicated = false )
10073 {
101- $ instance = $ this -> getBuilder ()->whereNull ('server_id ' );
74+ $ query = Allocation:: query ()->whereNull ('server_id ' );
10275
10376 if (! empty ($ nodes )) {
104- $ instance ->whereIn ('node_id ' , $ nodes );
77+ $ query ->whereIn ('node_id ' , $ nodes );
10578 }
10679
10780 if (! empty ($ ports )) {
108- $ instance ->where (function (Builder $ query ) use ($ ports ) {
81+ $ query ->where (function (Builder $ inner ) use ($ ports ) {
10982 $ whereIn = [];
11083 foreach ($ ports as $ port ) {
11184 if (is_array ($ port )) {
112- $ query ->orWhereBetween ('port ' , $ port );
85+ $ inner ->orWhereBetween ('port ' , $ port );
11386 continue ;
11487 }
11588
11689 $ whereIn [] = $ port ;
11790 }
11891
11992 if (! empty ($ whereIn )) {
120- $ query ->orWhereIn ('port ' , $ whereIn );
93+ $ inner ->orWhereIn ('port ' , $ whereIn );
12194 }
12295 });
12396 }
@@ -128,12 +101,12 @@ public function getRandomAllocation(array $nodes, array $ports, bool $dedicated
128101 $ discard = $ this ->getDiscardableDedicatedAllocations ($ nodes );
129102
130103 if (! empty ($ discard )) {
131- $ instance ->whereNotIn (
104+ $ query ->whereNotIn (
132105 $ this ->getBuilder ()->raw ('CONCAT_WS("-", node_id, ip) ' ), $ discard
133106 );
134107 }
135108 }
136109
137- return $ instance ->inRandomOrder ()->first ();
110+ return $ query ->inRandomOrder ()->first ();
138111 }
139112}
0 commit comments