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