Passed
Push — develop ( 58c14e...47df8a )
by Nikita
01:36 queued 13s
created

Server::getOnlineAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
 * @property bool $online
50
 *
51
 * @property DedicatedServer $dedicatedServer
52
 * @property Game $game
53
 * @property GameMod $gameMod
54
 * @property ServerSetting[] $settings
55
 * @property User[] $users
56
 * @property ServerTask[] $tasks
57
 */
58
class Server extends Model
59
{
60
    use SoftDeletes;
61
62
    public const TIME_EXPIRE_PROCESS_CHECK = 120;
63
64
    // Installed statuses
65
    public const NOT_INSTALLED        = 0;
66
    public const INSTALLED            = 1;
67
    public const INSTALLATION_PROCESS = 2;
68
69
    public const AUTOSTART_SETTING_KEY           = 'autostart';
70
    public const AUTOSTART_CURRENT_SETTING_KEY   = 'autostart_current';
71
    public const UPDATE_BEFORE_START_SETTING_KEY = 'update_before_start';
72
73
    protected $fillable = [
74
        'uuid',
75
        'uuid_short',
76
        'enabled',
77
        'name',
78
        'code_name',
79
        'game_id',
80
        'ds_id',
81
        'game_mod_id',
82
        'expires',
83
        'installed',
84
        'blocked',
85
        'server_ip',
86
        'server_port',
87
        'query_port',
88
        'rcon_port',
89
        'rcon',
90
        'dir',
91
        'su_user',
92
        'cpu_limit',
93
        'ram_limit',
94
        'net_limit',
95
        'start_command',
96
        'stop_command',
97
        'force_stop_command',
98 3
        'restart_command',
99
        'vars',
100 3
    ];
101 3
102
    protected $casts = [
103
        'vars'                  => 'array',
104 3
        'enabled'               => 'boolean',
105 3
        'installed'             => 'integer',
106 3
        'blocked'               => 'boolean',
107 3
        'ds_id'                 => 'integer',
108 3
        'game_mod_id'           => 'integer',
109 3
        'server_port'           => 'integer',
110
        'query_port'            => 'integer',
111 3
        'rcon_port'             => 'integer',
112 3
        'process_active'        => 'boolean',
113
        'last_process_check'    => 'datetime:Y-m-d H:i:s',
114
    ];
115 3
116
    public static function boot()
117
    {
118
        parent::boot();
119
120
        self::saving(function(Server $server) {
121 9
            $server->dir = Server::removeAbsoluteDedicatedServerPath(
122
                $server->dir,
123 9
                $server->dedicatedServer->work_path,
124
            );
125
        });
126
    }
127
128
    public function getOnlineAttribute(): bool {
129 9
        return $this->processActive();
130
    }
131 9
132
    public function processActive(): bool
133
    {
134
        if (empty($this->last_process_check)) {
135
            return false;
136
        }
137 6
138
        $lastProcessCheck = Carbon::createFromFormat(
139 6
            Carbon::DEFAULT_TO_STRING_FORMAT,
140
            $this->last_process_check,
141
            'UTC'
142
        )->timestamp;
143
144
        return $this->process_active
145
            && $lastProcessCheck >= Carbon::now('UTC')->timestamp - self::TIME_EXPIRE_PROCESS_CHECK;
146
    }
147 9
148
    public function dedicatedServer(): BelongsTo
149 9
    {
150
        return $this->belongsTo(DedicatedServer::class, 'ds_id');
151
    }
152
153
    public function game(): BelongsTo
154
    {
155
        return $this->belongsTo(Game::class, 'game_id', 'code');
156
    }
157
158
    public function gameMod(): BelongsTo
159
    {
160
        return $this->belongsTo(GameMod::class, 'game_mod_id');
161
    }
162
163 6
    public function settings(): HasMany
164
    {
165 6
        return $this->hasMany(ServerSetting::class);
166
    }
167
168
    public function tasks(): HasMany
169
    {
170
        return $this->hasMany(ServerTask::class);
171 3
    }
172
173
    public function getFullPathAttribute(): string
174 3
    {
175 3
        return rtrim($this->dedicatedServer->work_path, '/') . '/' . ltrim($this->dir, '/');
176 3
    }
177
178
    public function getFileManagerDisksAttribute(): array
179
    {
180 3
        $fileManagerDisks = [
181
            'server' => array_merge(
182 3
                $this->dedicatedServer->gdaemonSettings('local'),
183 3
                ['driver' => 'gameap', 'workDir' => $this->full_path, 'root' => $this->full_path]
184
            ),
185 3
        ];
186 3
187
        $setting = $this->settings()->where('name', 'file-manager')->first();
188
189
        if (!empty($setting)) {
190
            $disks = json_decode($setting->value, true);
191 3
192
            if (!empty($disks)) {
193
                $fileManagerDisks = array_merge($fileManagerDisks, $disks);
194
            }
195
        }
196
197 3
198
        return $fileManagerDisks;
199 3
    }
200
201
    public function users(): BelongsToMany
202
    {
203
        return $this->belongsToMany(User::class);
204
    }
205 3
206
    public function getAliasesAttribute(): array
207
    {
208 3
        $aliases = [
209 3
            'ip'            => $this->server_ip,
210 3
            'port'          => $this->server_port,
211 3
            'query_port'    => $this->query_port,
212 3
            'rcon_port'     => $this->rcon_port,
213 3
            'rcon_password' => $this->rcon,
214 3
            'uuid'          => $this->uuid,
215
            'uuid_short'    => $this->uuid_short,
216
        ];
217 3
218 3
        if ($this->gameMod !== null && is_array($this->gameMod->vars)) {
0 ignored issues
show
introduced by
The condition is_array($this->gameMod->vars) is always false.
Loading history...
219 3
            foreach ($this->gameMod->vars as $var) {
220 3
                if(!isset($var['var'])) {
221 3
                    continue;
222 3
                }
223
                $varname           = $var['var'];
224
                $aliases[$varname] = $this->vars[$varname] ?? $var['default'];
225
            }
226 3
        }
227
228
        return $aliases;
229
    }
230
231
    public function isActive(): bool
232
    {
233
        return $this->installed === self::INSTALLED && $this->enabled && !$this->blocked;
234
    }
235
236
    public function getSetting(string $key): ServerSetting
237
    {
238
        return $this->settings->where('name', $key)->first()
239
        ?? new ServerSetting([
240
            'server_id' => $this->id,
241
            'name'      => $key,
242
            'value'     => false,
243
        ]);
244
    }
245
246
    private static function removeAbsoluteDedicatedServerPath(string $path, string $dsWorkPath): string
247
    {
248
        if (substr($path, 0, strlen($dsWorkPath)) == $dsWorkPath) {
249
            $path = substr($path, strlen($dsWorkPath));
250
        }
251
252
        $path = ltrim($path, '/\\');
253
254
        return $path;
255
    }
256
}
257