1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gameap\Repositories; |
4
|
|
|
|
5
|
|
|
use Gameap\Helpers\PermissionHelper; |
6
|
|
|
use Gameap\Http\Requests\ServerVarsRequest; |
7
|
|
|
use Gameap\Models\Game; |
8
|
|
|
use Gameap\Models\Server; |
9
|
|
|
use Illuminate\Contracts\Auth\Factory as AuthFactory; |
10
|
|
|
use Illuminate\Support\Facades\Auth; |
11
|
|
|
use Illuminate\Support\Facades\DB; |
12
|
|
|
use Mavinoo\Batch\Batch; |
13
|
|
|
use Spatie\QueryBuilder\QueryBuilder; |
14
|
|
|
|
15
|
|
|
class ServerRepository |
16
|
|
|
{ |
17
|
|
|
public const DEFAULT_RCON_PASSWORD_LENGTH = 10; |
18
|
|
|
|
19
|
|
|
public const DEFAULT_PER_PAGE = 20; |
20
|
|
|
|
21
|
|
|
/** @var Server */ |
22
|
|
|
protected $model; |
23
|
|
|
|
24
|
|
|
/** @var GdaemonTaskRepository */ |
25
|
|
|
protected $gdaemonTaskRepository; |
26
|
|
|
|
27
|
|
|
/** @var Batch */ |
28
|
|
|
protected $mavinooBatch; |
29
|
|
|
|
30
|
|
|
/** @var AuthFactory */ |
31
|
|
|
protected $authFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ServerRepository constructor. |
35
|
|
|
* @param Server $server |
36
|
9 |
|
* @param GdaemonTaskRepository $gdaemonTaskRepository |
37
|
|
|
*/ |
38
|
9 |
|
public function __construct( |
39
|
9 |
|
Server $server, |
40
|
9 |
|
GdaemonTaskRepository $gdaemonTaskRepository, |
41
|
|
|
Batch $mavinooBatch, |
42
|
|
|
AuthFactory $auth |
43
|
|
|
) { |
44
|
|
|
$this->model = $server; |
45
|
|
|
$this->gdaemonTaskRepository = $gdaemonTaskRepository; |
46
|
3 |
|
$this->mavinooBatch = $mavinooBatch; |
47
|
|
|
$this->authFactory = $auth; |
48
|
3 |
|
} |
49
|
|
|
|
50
|
3 |
|
public function find(int $id): Server |
51
|
|
|
{ |
52
|
|
|
return $this->model->findOrFail($id); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $perPage |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function getAll($perPage = self::DEFAULT_PER_PAGE) |
60
|
|
|
{ |
61
|
|
|
$servers = Server::orderBy('id')->with('game')->paginate($perPage); |
62
|
|
|
|
63
|
|
|
return $servers; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get Servers list for Dedicated server |
68
|
|
|
* |
69
|
|
|
* @param int $dedicatedServerId |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function getServersListForDedicatedServer(int $dedicatedServerId) |
73
|
|
|
{ |
74
|
|
|
return $this->model->select('*') |
75
|
|
|
->where('ds_id', '=', $dedicatedServerId) |
76
|
|
|
->get(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get Servers id list for Dedicated server |
82
|
|
|
* |
83
|
|
|
* @param int $dedicatedServerId |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function getServerIdsForDedicatedServer(int $dedicatedServerId) |
87
|
|
|
{ |
88
|
|
|
return $this->model->select('id') |
89
|
|
|
->where('ds_id', '=', $dedicatedServerId) |
90
|
|
|
->get(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function getServersForAuth() |
97
|
|
|
{ |
98
|
|
|
$currentUser = $this->authFactory->guard()->user(); |
99
|
|
|
|
100
|
|
|
if ($currentUser->can(PermissionHelper::ADMIN_PERMISSIONS)) { |
101
|
|
|
return $this->getAll(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $currentUser->servers->paginate(self::DEFAULT_PER_PAGE); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getServersForUser(int $userId) |
108
|
|
|
{ |
109
|
|
|
$qb = QueryBuilder::for(Server::class) |
110
|
|
|
->allowedFilters('ds_id') |
111
|
|
|
->allowedAppends('full_path') |
112
|
|
|
->with('game') |
113
|
|
|
->whereRaw('id IN(SELECT server_id FROM server_user su WHERE su.user_id = ?)', [$userId]); |
114
|
|
|
|
115
|
|
|
return $qb->get([ |
116
|
|
|
'id', |
117
|
|
|
'uuid', |
118
|
|
|
'uuid_short', |
119
|
|
|
'enabled', |
120
|
|
|
'installed', |
121
|
|
|
'blocked', |
122
|
|
|
'name', |
123
|
|
|
'ds_id', |
124
|
|
|
'game_id', |
125
|
|
|
'game_mod_id', |
126
|
|
|
'server_ip', |
127
|
|
|
'server_port', |
128
|
|
|
'query_port', |
129
|
|
|
'rcon_port', |
130
|
|
|
'dir', |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getAllServers() |
135
|
|
|
{ |
136
|
|
|
$qb = QueryBuilder::for(Server::class) |
137
|
|
|
->allowedFilters('ds_id') |
138
|
|
|
->allowedAppends('full_path') |
139
|
|
|
->with('game'); |
140
|
|
|
|
141
|
|
|
return $qb->get([ |
142
|
|
|
'id', |
143
|
|
|
'uuid', |
144
|
|
|
'uuid_short', |
145
|
|
|
'enabled', |
146
|
|
|
'installed', |
147
|
|
|
'blocked', |
148
|
|
|
'name', |
149
|
|
|
'ds_id', |
150
|
|
|
'game_id', |
151
|
|
|
'game_mod_id', |
152
|
|
|
'server_ip', |
153
|
|
|
'server_port', |
154
|
|
|
'query_port', |
155
|
|
|
'rcon_port', |
156
|
|
|
'dir', |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $engines |
162
|
|
|
* @param int|array $dedicatedServers |
163
|
|
|
* @return \Illuminate\Support\Collection |
164
|
|
|
*/ |
165
|
|
|
public function getServersForEngine(array $engines, $dedicatedServers = [], $excludeIds = []) |
166
|
|
|
{ |
167
|
|
|
if (is_int($dedicatedServers)) { |
168
|
|
|
$dedicatedServers = [$dedicatedServers]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$serversTable = $this->model->getTable(); |
172
|
|
|
$gamesTable = (new Game())->getTable(); |
173
|
|
|
|
174
|
|
|
$query = DB::table($serversTable) |
175
|
|
|
->selectRaw("{$serversTable}.*, {$gamesTable}.name as game_name") |
176
|
|
|
->whereIn('game_id', function ($query) use ($engines, $serversTable, $gamesTable): void { |
|
|
|
|
177
|
|
|
$query->select('code') |
178
|
|
|
->from($gamesTable) |
179
|
|
|
->whereIn('engine', $engines); |
180
|
|
|
}) |
181
|
|
|
->where('deleted_at', null) |
182
|
|
|
->join($gamesTable, "{$serversTable}.game_id", '=', "{$gamesTable}.code"); |
183
|
|
|
|
184
|
|
|
if (!empty($dedicatedServers)) { |
185
|
|
|
$query->whereIn('ds_id', $dedicatedServers); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (!empty($excludeIds)) { |
189
|
|
|
$query->whereNotIn('id', $excludeIds); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $query->get(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param $query |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
public function search($query) |
200
|
|
|
{ |
201
|
|
|
return $this->model->select(['id', 'name', 'server_ip', 'server_port', 'game_id', 'game_mod_id']) |
202
|
|
|
->with(['game' => function ($query): void { |
203
|
|
|
$query->select('code', 'name'); |
204
|
|
|
}]) |
205
|
|
|
->where('name', 'LIKE', '%' . $query . '%') |
206
|
|
|
->get(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param Server $server |
211
|
|
|
* @param array $attributes |
212
|
|
|
*/ |
213
|
|
|
public function update(Server $server, array $attributes): void |
214
|
|
|
{ |
215
|
|
|
$attributes['enabled'] = (bool)array_key_exists('enabled', $attributes); |
216
|
|
|
$attributes['blocked'] = (bool)array_key_exists('blocked', $attributes); |
217
|
|
|
$attributes['installed'] = (bool)array_key_exists('installed', $attributes); |
218
|
|
|
|
219
|
|
|
if (isset($attributes['ds_id'])) { |
220
|
|
|
$server->ds_id = $attributes['ds_id']; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$server->update($attributes); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param Server $server |
228
|
|
|
* @param ServerVarsRequest $request |
229
|
|
|
*/ |
230
|
|
|
public function updateVars(Server $server, ServerVarsRequest $request): void |
231
|
|
|
{ |
232
|
|
|
$only = []; |
233
|
|
|
foreach ($server->gameMod->vars as $var) { |
|
|
|
|
234
|
|
|
if (!empty($var['admin_var']) && Auth::user()->cannot(PermissionHelper::ADMIN_PERMISSIONS)) { |
235
|
|
|
continue; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$only[] = 'vars.' . $var['var']; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$server->update($request->only($only)); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function updateSettings(Server $server, ServerVarsRequest $request): void |
245
|
|
|
{ |
246
|
|
|
$autostartSetting = $server->getSetting($server::AUTOSTART_SETTING_KEY); |
247
|
|
|
$autostartSetting->value = $request->autostart(); |
248
|
|
|
$autostartSetting->save(); |
249
|
|
|
|
250
|
|
|
$autostartCurrentSetting = $server->getSetting($server::AUTOSTART_CURRENT_SETTING_KEY); |
251
|
|
|
$autostartCurrentSetting->value = $request->autostart(); |
252
|
|
|
$autostartCurrentSetting->save(); |
253
|
|
|
|
254
|
|
|
$updateBeforeStartSetting = $server->getSetting($server::UPDATE_BEFORE_START_SETTING_KEY); |
255
|
|
|
$updateBeforeStartSetting->value = $request->updateBeforeStart(); |
256
|
|
|
$updateBeforeStartSetting->save(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function save(Server $server): void |
260
|
|
|
{ |
261
|
|
|
$server->save(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function saveBatch(array $serverValues): void |
265
|
|
|
{ |
266
|
|
|
$this->mavinooBatch->update(new Server(), $serverValues, 'id'); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|