1313use Pterodactyl \Http \Requests \Api \Client \Servers \Network \GetNetworkRequest ;
1414use Pterodactyl \Http \Requests \Api \Client \Servers \Network \DeleteAllocationRequest ;
1515use Pterodactyl \Http \Requests \Api \Client \Servers \Network \UpdateAllocationRequest ;
16+ use Pterodactyl \Http \Requests \Api \Client \Servers \Network \NewAllocationRequest ;
1617use Pterodactyl \Http \Requests \Api \Client \Servers \Network \SetPrimaryAllocationRequest ;
18+ use Pterodactyl \Services \Allocations \AssignmentService ;
19+ use Illuminate \Support \Facades \Log ;
1720
1821class NetworkAllocationController extends ClientApiController
1922{
@@ -27,20 +30,28 @@ class NetworkAllocationController extends ClientApiController
2730 */
2831 private $ serverRepository ;
2932
33+ /**
34+ * @var \Pterodactyl\Services\Allocations\AssignmentService
35+ */
36+ protected $ assignmentService ;
37+
3038 /**
3139 * NetworkController constructor.
3240 *
3341 * @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository
3442 * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
43+ * @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
3544 */
3645 public function __construct (
3746 AllocationRepository $ repository ,
38- ServerRepository $ serverRepository
47+ ServerRepository $ serverRepository ,
48+ AssignmentService $ assignmentService
3949 ) {
4050 parent ::__construct ();
4151
4252 $ this ->repository = $ repository ;
4353 $ this ->serverRepository = $ serverRepository ;
54+ $ this ->assignmentService = $ assignmentService ;
4455 }
4556
4657 /**
@@ -100,6 +111,66 @@ public function setPrimary(SetPrimaryAllocationRequest $request, Server $server,
100111 ->toArray ();
101112 }
102113
114+ /**
115+ * Set the notes for the allocation for a server.
116+ *s
117+ * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest $request
118+ * @param \Pterodactyl\Models\Server $server
119+ * @param \Pterodactyl\Models\Allocation $allocation
120+ * @return array
121+ *
122+ * @throws \Pterodactyl\Exceptions\DisplayException
123+ * @throws \Pterodactyl\Exceptions\Model\DataValidationException
124+ * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
125+ */
126+ public function addNew (NewAllocationRequest $ request , Server $ server ): array
127+ {
128+ Log::info ('addNew() ' );
129+ $ topRange = 25700 ;
130+ $ bottomRange = 25565 ;
131+
132+ if ($ server ->allocation_limit <= $ server ->allocations ->count ()) {
133+ Log::error ('You have created the maximum number of allocations! ' );
134+ throw new DisplayException (
135+ 'You have created the maximum number of allocations! '
136+ );
137+ }
138+
139+ $ allocation = $ server ->node ->allocations ()->where ('ip ' ,$ server ->allocation ->ip )->whereNull ('server_id ' )->first ();
140+
141+ if (!$ allocation ) {
142+ if ($ server ->node ->allocations ()->where ('ip ' ,$ server ->allocation ->ip )->count () >= $ topRange -$ bottomRange ) {
143+ Log::error ('No allocations available! ' );
144+ throw new DisplayException (
145+ 'No more allocations available! '
146+ );
147+ }
148+ Log::info ('Creating new allocation... ' );
149+ $ allPorts = $ server ->node ->allocations ()->select (['port ' ])->where ('ip ' ,$ server ->allocation ->ip )->get ()->pluck ('port ' )->toArray ();
150+
151+ do {
152+ $ port = rand ($ bottomRange , $ topRange );
153+ Log::info ('Picking port.... ' );
154+ // TODO ADD ITERATOR THAT TIMES OUT AFTER SEARCHING FOR SO MUCH TIME?
155+ } while (array_search ($ port , $ allPorts ));
156+
157+ $ this ->assignmentService ->handle ($ server ->node ,[
158+ 'allocation_ip ' =>$ server ->allocation ->ip ,
159+ 'allocation_ports ' =>[$ port ],
160+ 'server_id ' =>$ server ->id
161+ ]);
162+
163+ $ allocation = $ server ->node ->allocations ()->where ('ip ' ,$ server ->allocation ->ip )->where ('port ' , $ port )->first ();
164+
165+ }
166+
167+ $ allocation ->update (['server_id ' => $ server ->id ]);
168+
169+ return $ this ->fractal ->item ($ allocation )
170+ ->transformWith ($ this ->getTransformer (AllocationTransformer::class))
171+ ->toArray ();
172+ }
173+
103174 /**
104175 * Delete an allocation from a server.
105176 *
0 commit comments