1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gameap\Repositories; |
4
|
|
|
|
5
|
|
|
use Gameap\Models\DedicatedServer; |
6
|
|
|
use Gameap\Models\Game; |
7
|
|
|
use Gameap\Models\Server; |
8
|
|
|
use Gameap\Models\GameMod; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Gameap\Http\Requests\ServerVarsRequest; |
11
|
|
|
use Illuminate\Support\Facades\Auth; |
12
|
|
|
use Illuminate\Support\Facades\DB; |
13
|
|
|
|
14
|
|
|
class ServerRepository |
15
|
|
|
{ |
16
|
|
|
protected $model; |
17
|
|
|
|
18
|
|
|
protected $gdaemonTaskRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ServerRepository constructor. |
22
|
|
|
* @param Server $server |
23
|
|
|
* @param GdaemonTaskRepository $gdaemonTaskRepository |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Server $server, GdaemonTaskRepository $gdaemonTaskRepository) |
26
|
|
|
{ |
27
|
|
|
$this->model = $server; |
28
|
|
|
$this->gdaemonTaskRepository = $gdaemonTaskRepository; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param int $perPage |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function getAll($perPage = 20) |
36
|
|
|
{ |
37
|
|
|
$servers = Server::orderBy('id')->with('game')->paginate($perPage); |
38
|
|
|
|
39
|
|
|
return $servers; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Store server |
44
|
|
|
* |
45
|
|
|
* @param array $attributes |
46
|
|
|
* @throws \Gameap\Exceptions\Repositories\RecordExistExceptions |
47
|
|
|
*/ |
48
|
|
|
public function store(array $attributes) |
49
|
|
|
{ |
50
|
|
|
$attributes['uuid'] = Str::orderedUuid()->toString(); |
51
|
|
|
$attributes['uuid_short'] = Str::substr($attributes['uuid'], 0, 8); |
52
|
|
|
|
53
|
|
|
$attributes['enabled'] = true; |
54
|
|
|
$attributes['blocked'] = false; |
55
|
|
|
|
56
|
|
|
$addInstallTask = false; |
57
|
|
|
if (isset($attributes['install'])) { |
58
|
|
|
$attributes['installed'] = ! $attributes['install']; |
59
|
|
|
$addInstallTask = true; |
60
|
|
|
|
61
|
|
|
unset($attributes['install']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$dedicatedServer = DedicatedServer::findOrFail($attributes['ds_id']); |
65
|
|
|
|
66
|
|
|
if (empty($attributes['start_command'])) { |
67
|
|
|
$gameMod = GameMod::select('default_start_cmd_linux', 'default_start_cmd_windows')->where('id', '=', $attributes['game_mod_id'])->firstOrFail(); |
68
|
|
|
|
69
|
|
|
$attributes['start_command'] = |
70
|
|
|
$dedicatedServer->isLinux() |
71
|
|
|
? $gameMod->default_start_cmd_linux |
72
|
|
|
: $gameMod->default_start_cmd_windows; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (empty($attributes['dir'])) { |
76
|
|
|
$attributes['dir'] = 'servers/' . $attributes['uuid']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Fix path. Remove absolute dedicated server path |
80
|
|
|
$attributes['dir'] = $this->fixPath($attributes['dir'], $dedicatedServer->work_path); |
81
|
|
|
|
82
|
|
|
$server = Server::create($attributes); |
83
|
|
|
|
84
|
|
|
if ($addInstallTask) { |
85
|
|
|
$this->gdaemonTaskRepository->addServerUpdate($server); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get Servers list for Dedicated server |
91
|
|
|
* |
92
|
|
|
* @param int $dedicatedServerId |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function getServersListForDedicatedServer(int $dedicatedServerId) |
96
|
|
|
{ |
97
|
|
|
return $this->model->select('*') |
98
|
|
|
->where('ds_id', '=', $dedicatedServerId) |
99
|
|
|
->get(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get Servers id list for Dedicated server |
105
|
|
|
* |
106
|
|
|
* @param int $dedicatedServerId |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function getServerIdsForDedicatedServer(int $dedicatedServerId) |
110
|
|
|
{ |
111
|
|
|
return $this->model->select('id') |
112
|
|
|
->where('ds_id', '=', $dedicatedServerId) |
113
|
|
|
->get(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function getServersForAuth() |
120
|
|
|
{ |
121
|
|
|
if (Auth::user()->can('admin roles & permissions')) { |
122
|
|
|
return $this->getAll(); |
123
|
|
|
} else { |
124
|
|
|
return Auth::user()->servers; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $engines |
130
|
|
|
* @param array $dedicatedServers |
131
|
|
|
* @return \Illuminate\Support\Collection |
132
|
|
|
*/ |
133
|
|
|
public function getServersForEngine(array $engines, array $dedicatedServers = []) |
134
|
|
|
{ |
135
|
|
|
$query = DB::table($this->model->getTable()) |
136
|
|
|
->whereIn('game_id', function($query) use ($engines) |
137
|
|
|
{ |
138
|
|
|
$query->select('code') |
139
|
|
|
->from((new Game)->getTable()) |
140
|
|
|
->whereIn('engine', $engines); |
141
|
|
|
}); |
142
|
|
|
|
143
|
|
|
if (!empty($dedicatedServers)) { |
144
|
|
|
$query->whereIn('ds_id', $dedicatedServers); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $query->get(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $query |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
public function search($query) |
155
|
|
|
{ |
156
|
|
|
return $this->model->select(['id', 'name', 'server_ip', 'server_port', 'game_id', 'game_mod_id']) |
157
|
|
|
->with(['game' => function($query) { |
158
|
|
|
$query->select('code','name'); |
159
|
|
|
}]) |
160
|
|
|
->where('name', 'LIKE', '%' . $query . '%') |
161
|
|
|
->get(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param Server $server |
166
|
|
|
* @param array $attributes |
167
|
|
|
*/ |
168
|
|
|
public function update(Server $server, array $attributes) |
169
|
|
|
{ |
170
|
|
|
$attributes['enabled'] = (bool)array_key_exists('enabled', $attributes); |
171
|
|
|
$attributes['blocked'] = (bool)array_key_exists('blocked', $attributes); |
172
|
|
|
$attributes['installed'] = (bool)array_key_exists('installed', $attributes); |
173
|
|
|
|
174
|
|
|
// Fix path. Remove absolute dedicated server path |
175
|
|
|
$attributes['dir'] = $this->fixPath($attributes['dir'], $server->dedicatedServer->work_path); |
176
|
|
|
|
177
|
|
|
$server->update($attributes); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param Server $server |
182
|
|
|
* @param ServerVarsRequest $request |
183
|
|
|
*/ |
184
|
|
|
public function updateVars(Server $server, ServerVarsRequest $request) |
185
|
|
|
{ |
186
|
|
|
$only = []; |
187
|
|
|
foreach ($server->gameMod->vars as $var) { |
|
|
|
|
188
|
|
|
if (!empty($var['admin_var']) && Auth::user()->cannot('admin roles & permissions')) { |
189
|
|
|
continue; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$only[] = 'vars.' . $var['var']; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$server->update($request->only($only)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param $path |
200
|
|
|
* @param $dsWorkPath |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
private function fixPath($path, $dsWorkPath) |
204
|
|
|
{ |
205
|
|
|
if (substr($path, 0, strlen($dsWorkPath)) == $dsWorkPath) { |
206
|
|
|
$path = substr($path, strlen($dsWorkPath)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$path = ltrim($path, '/\\'); |
210
|
|
|
|
211
|
|
|
return $path; |
212
|
|
|
} |
213
|
|
|
} |