|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gameap\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
10
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Server model |
|
14
|
|
|
* @package Gameap\Models |
|
15
|
|
|
* |
|
16
|
|
|
* @property integer $id |
|
17
|
|
|
* @property boolean $enabled |
|
18
|
|
|
* @property integer $installed |
|
19
|
|
|
* @property boolean $blocked |
|
20
|
|
|
* @property string $name |
|
21
|
|
|
* @property string $uuid |
|
22
|
|
|
* @property string $uuid_short |
|
23
|
|
|
* @property string $game_id |
|
24
|
|
|
* @property integer $ds_id |
|
25
|
|
|
* @property integer $game_mod_id |
|
26
|
|
|
* @property string $expires |
|
27
|
|
|
* @property string $server_ip |
|
28
|
|
|
* @property integer $server_port |
|
29
|
|
|
* @property integer $query_port |
|
30
|
|
|
* @property integer $rcon_port |
|
31
|
|
|
* @property string $rcon RCON password |
|
32
|
|
|
* @property string $dir |
|
33
|
|
|
* @property string $su_user |
|
34
|
|
|
* @property integer $cpu_limit |
|
35
|
|
|
* @property integer $ram_limit |
|
36
|
|
|
* @property integer $net_limit |
|
37
|
|
|
* @property string $start_command |
|
38
|
|
|
* @property string $stop_command |
|
39
|
|
|
* @property string $force_stop_command |
|
40
|
|
|
* @property string $restart_command |
|
41
|
|
|
* @property bool $process_active |
|
42
|
|
|
* @property string $last_process_check |
|
43
|
|
|
* @property array $vars |
|
44
|
|
|
* @property string $created_at |
|
45
|
|
|
* @property string $updated_at |
|
46
|
|
|
* @property string $deleted_at |
|
47
|
|
|
* |
|
48
|
|
|
* @property string $full_path |
|
49
|
|
|
* |
|
50
|
|
|
* @property DedicatedServer $dedicatedServer |
|
51
|
|
|
* @property Game $game |
|
52
|
|
|
* @property GameMod $gameMod |
|
53
|
|
|
* @property ServerSetting[] $settings |
|
54
|
|
|
* @property User[] $users |
|
55
|
|
|
* @property ServerTask[] $tasks |
|
56
|
|
|
*/ |
|
57
|
|
|
class Server extends Model |
|
58
|
|
|
{ |
|
59
|
|
|
use SoftDeletes; |
|
60
|
|
|
|
|
61
|
|
|
public const TIME_EXPIRE_PROCESS_CHECK = 120; |
|
62
|
|
|
|
|
63
|
|
|
// Installed statuses |
|
64
|
|
|
public const NOT_INSTALLED = 0; |
|
65
|
|
|
public const INSTALLED = 1; |
|
66
|
|
|
public const INSTALLATION_PROCESS = 2; |
|
67
|
|
|
|
|
68
|
|
|
public const AUTOSTART_SETTING_KEY = 'autostart'; |
|
69
|
|
|
public const AUTOSTART_CURRENT_SETTING_KEY = 'autostart_current'; |
|
70
|
|
|
public const UPDATE_BEFORE_START_SETTING_KEY = 'update_before_start'; |
|
71
|
|
|
|
|
72
|
|
|
protected $fillable = [ |
|
73
|
|
|
'uuid', 'uuid_short', |
|
74
|
|
|
'enabled', 'name', 'code_name', 'game_id', |
|
75
|
|
|
'ds_id', 'game_mod_id', 'expires', |
|
76
|
|
|
'installed', 'blocked', 'server_ip', 'server_port', |
|
77
|
|
|
'query_port', 'rcon_port', |
|
78
|
|
|
'rcon', 'dir', 'su_user', |
|
79
|
|
|
'cpu_limit', 'ram_limit', 'net_limit', |
|
80
|
|
|
'start_command', 'stop_command', |
|
81
|
|
|
'force_stop_command', 'restart_command', |
|
82
|
|
|
'vars', |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
protected $casts = [ |
|
86
|
|
|
'vars' => 'array', |
|
87
|
|
|
'enabled' => 'boolean', |
|
88
|
|
|
'installed' => 'integer', |
|
89
|
|
|
'blocked' => 'boolean', |
|
90
|
|
|
'ds_id' => 'integer', |
|
91
|
|
|
'game_mod_id' => 'integer', |
|
92
|
|
|
'server_port' => 'integer', |
|
93
|
|
|
'query_port' => 'integer', |
|
94
|
|
|
'rcon_port' => 'integer', |
|
95
|
|
|
'process_active' => 'boolean', |
|
96
|
|
|
'last_process_check' => 'datetime:Y-m-d H:i:s', |
|
97
|
|
|
]; |
|
98
|
3 |
|
|
|
99
|
|
|
public static function boot() |
|
100
|
3 |
|
{ |
|
101
|
3 |
|
parent::boot(); |
|
102
|
|
|
|
|
103
|
|
|
self::saving(function(Server $server) { |
|
104
|
3 |
|
$server->dir = Server::removeAbsoluteDedicatedServerPath( |
|
105
|
3 |
|
$server->dir, |
|
106
|
3 |
|
$server->dedicatedServer->work_path, |
|
107
|
3 |
|
); |
|
108
|
3 |
|
}); |
|
109
|
3 |
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
public function processActive(): bool |
|
112
|
3 |
|
{ |
|
113
|
|
|
if (empty($this->last_process_check)) { |
|
114
|
|
|
return false; |
|
115
|
3 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
$lastProcessCheck = Carbon::createFromFormat( |
|
118
|
|
|
Carbon::DEFAULT_TO_STRING_FORMAT, |
|
119
|
|
|
$this->last_process_check, |
|
120
|
|
|
'UTC' |
|
121
|
9 |
|
)->timestamp; |
|
122
|
|
|
|
|
123
|
9 |
|
return $this->process_active |
|
124
|
|
|
&& $lastProcessCheck >= Carbon::now()->timestamp - self::TIME_EXPIRE_PROCESS_CHECK; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function dedicatedServer(): BelongsTo |
|
128
|
|
|
{ |
|
129
|
9 |
|
return $this->belongsTo(DedicatedServer::class, 'ds_id'); |
|
130
|
|
|
} |
|
131
|
9 |
|
|
|
132
|
|
|
public function game(): BelongsTo |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->belongsTo(Game::class, 'game_id', 'code'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
6 |
|
public function gameMod(): BelongsTo |
|
138
|
|
|
{ |
|
139
|
6 |
|
return $this->belongsTo(GameMod::class, 'game_mod_id'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function settings(): HasMany |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->hasMany(ServerSetting::class); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
9 |
|
public function tasks(): HasMany |
|
148
|
|
|
{ |
|
149
|
9 |
|
return $this->hasMany(ServerTask::class); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function getFullPathAttribute(): string |
|
153
|
|
|
{ |
|
154
|
|
|
return rtrim($this->dedicatedServer->work_path, '/') . '/' . ltrim($this->dir, '/'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function getFileManagerDisksAttribute(): array |
|
158
|
|
|
{ |
|
159
|
|
|
$fileManagerDisks = [ |
|
160
|
|
|
'server' => array_merge( |
|
161
|
|
|
$this->dedicatedServer->gdaemonSettings('local'), |
|
162
|
|
|
['driver' => 'gameap', 'workDir' => $this->full_path, 'root' => $this->full_path] |
|
163
|
6 |
|
), |
|
164
|
|
|
]; |
|
165
|
6 |
|
|
|
166
|
|
|
$setting = $this->settings()->where('name', 'file-manager')->first(); |
|
167
|
|
|
|
|
168
|
|
|
if (!empty($setting)) { |
|
169
|
|
|
$disks = json_decode($setting->value, true); |
|
170
|
|
|
|
|
171
|
3 |
|
if (!empty($disks)) { |
|
172
|
|
|
$fileManagerDisks = array_merge($fileManagerDisks, $disks); |
|
173
|
|
|
} |
|
174
|
3 |
|
} |
|
175
|
3 |
|
|
|
176
|
3 |
|
|
|
177
|
|
|
return $fileManagerDisks; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
3 |
|
public function users(): BelongsToMany |
|
181
|
|
|
{ |
|
182
|
3 |
|
return $this->belongsToMany(User::class); |
|
183
|
3 |
|
} |
|
184
|
|
|
|
|
185
|
3 |
|
public function getAliasesAttribute(): array |
|
186
|
3 |
|
{ |
|
187
|
|
|
$aliases = [ |
|
188
|
|
|
'ip' => $this->server_ip, |
|
189
|
|
|
'port' => $this->server_port, |
|
190
|
|
|
'query_port' => $this->query_port, |
|
191
|
3 |
|
'rcon_port' => $this->rcon_port, |
|
192
|
|
|
'rcon_password' => $this->rcon, |
|
193
|
|
|
'uuid' => $this->uuid, |
|
194
|
|
|
'uuid_short' => $this->uuid_short, |
|
195
|
|
|
]; |
|
196
|
|
|
|
|
197
|
3 |
|
if ($this->gameMod !== null && is_array($this->gameMod->vars)) { |
|
|
|
|
|
|
198
|
|
|
foreach ($this->gameMod->vars as $var) { |
|
199
|
3 |
|
$varname = $var['var']; |
|
200
|
|
|
$aliases[$varname] = $this->vars[$varname] ?? $var['default']; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $aliases; |
|
205
|
3 |
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function isActive(): bool |
|
208
|
3 |
|
{ |
|
209
|
3 |
|
return $this->installed === self::INSTALLED && $this->enabled && !$this->blocked; |
|
210
|
3 |
|
} |
|
211
|
3 |
|
|
|
212
|
3 |
|
public function getSetting(string $key): ServerSetting |
|
213
|
3 |
|
{ |
|
214
|
3 |
|
return $this->settings->where('name', $key)->first() |
|
215
|
|
|
?? new ServerSetting([ |
|
216
|
|
|
'server_id' => $this->id, |
|
217
|
3 |
|
'name' => $key, |
|
218
|
3 |
|
'value' => false, |
|
219
|
3 |
|
]); |
|
220
|
3 |
|
} |
|
221
|
3 |
|
|
|
222
|
3 |
|
private static function removeAbsoluteDedicatedServerPath(string $path, string $dsWorkPath): string |
|
223
|
|
|
{ |
|
224
|
|
|
if (substr($path, 0, strlen($dsWorkPath)) == $dsWorkPath) { |
|
225
|
|
|
$path = substr($path, strlen($dsWorkPath)); |
|
226
|
3 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
$path = ltrim($path, '/\\'); |
|
229
|
|
|
|
|
230
|
|
|
return $path; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|