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