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