Passed
Push — master ( 2d2309...cbe71d )
by Nikita
26:59 queued 05:24
created

Server::getSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 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
 *
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
99
    public function processActive(): bool
100
    {
101
        if (empty($this->last_process_check)) {
102
            return false;
103
        }
104
105
        $lastProcessCheck = Carbon::createFromFormat(
106
            Carbon::DEFAULT_TO_STRING_FORMAT,
107
            $this->last_process_check,
108
            'UTC'
109
        )->timestamp;
110
111
        return $this->process_active
112
            && $lastProcessCheck >= Carbon::now()->timestamp - self::TIME_EXPIRE_PROCESS_CHECK;
113
    }
114
115
    public function dedicatedServer(): BelongsTo
116
    {
117
        return $this->belongsTo(DedicatedServer::class, 'ds_id');
118
    }
119
120
    public function game(): BelongsTo
121
    {
122
        return $this->belongsTo(Game::class, 'game_id', 'code');
123
    }
124
125
    public function gameMod(): BelongsTo
126
    {
127
        return $this->belongsTo(GameMod::class, 'game_mod_id');
128
    }
129
130
    public function settings(): HasMany
131
    {
132
        return $this->hasMany(ServerSetting::class);
133
    }
134
135
    public function tasks(): HasMany
136
    {
137
        return $this->hasMany(ServerTask::class);
138
    }
139
140
    public function getFullPathAttribute(): string
141
    {
142
        return rtrim($this->dedicatedServer->work_path, '/') . '/' . ltrim($this->dir, '/');
143
    }
144
145
    public function getFileManagerDisksAttribute(): array
146
    {
147
        $fileManagerDisks = [
148
            'server' => array_merge(
149
                $this->dedicatedServer->gdaemonSettings('local'),
150
                ['driver' => 'gameap', 'workDir' => $this->full_path, 'root' => $this->full_path]
151
            ),
152
        ];
153
154
        $setting = $this->settings()->where('name', 'file-manager')->first();
155
156
        if (!empty($setting)) {
157
            $disks = json_decode($setting->value, true);
158
159
            if (!empty($disks)) {
160
                $fileManagerDisks = array_merge($fileManagerDisks, $disks);
161
            }
162
        }
163
164
165
        return $fileManagerDisks;
166
    }
167
168
    public function users(): BelongsToMany
169
    {
170
        return $this->belongsToMany(User::class);
171
    }
172
173
    public function getAliasesAttribute(): array
174
    {
175
        $aliases = [
176
            'ip'            => $this->server_ip,
177
            'port'          => $this->server_port,
178
            'query_port'    => $this->query_port,
179
            'rcon_port'     => $this->rcon_port,
180
            'rcon_password' => $this->rcon,
181
            'uuid'          => $this->uuid,
182
            'uuid_short'    => $this->uuid_short,
183
        ];
184
185
        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...
186
            foreach ($this->gameMod->vars as $var) {
187
                $varname           = $var['var'];
188
                $aliases[$varname] = $this->vars[$varname] ?? $var['default'];
189
            }
190
        }
191
192
        return $aliases;
193
    }
194
195
    public function isActive(): bool
196
    {
197
        return $this->installed === self::INSTALLED && $this->enabled && !$this->blocked;
198
    }
199
200
    public function getSetting(string $key): ServerSetting
201
    {
202
        $value = $this->settings->where('name', $key)->first()
203
        ?? new ServerSetting([
204
            'server_id' => $this->id,
205
            'name'      => $key,
206
            'value'     => false,
207
        ]);
208
209
        return $value;
210
    }
211
}
212