et-nik /
gameap
| 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 | { |
|
| 130 | return $this->processActive(); |
||
| 131 | 9 | } |
|
| 132 | |||
| 133 | public function processActive(): bool |
||
| 134 | { |
||
| 135 | if (empty($this->last_process_check)) { |
||
| 136 | return false; |
||
| 137 | 6 | } |
|
| 138 | |||
| 139 | 6 | $lastProcessCheck = Carbon::createFromFormat( |
|
| 140 | Carbon::DEFAULT_TO_STRING_FORMAT, |
||
| 141 | $this->last_process_check, |
||
| 142 | 'UTC' |
||
| 143 | )->timestamp; |
||
| 144 | |||
| 145 | return $this->process_active |
||
| 146 | && $lastProcessCheck >= Carbon::now('UTC')->timestamp - self::TIME_EXPIRE_PROCESS_CHECK; |
||
| 147 | 9 | } |
|
| 148 | |||
| 149 | 9 | public function dedicatedServer(): BelongsTo |
|
| 150 | { |
||
| 151 | return $this->belongsTo(DedicatedServer::class, 'ds_id'); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function game(): BelongsTo |
||
| 155 | { |
||
| 156 | return $this->belongsTo(Game::class, 'game_id', 'code'); |
||
| 157 | } |
||
| 158 | |||
| 159 | public function gameMod(): BelongsTo |
||
| 160 | { |
||
| 161 | return $this->belongsTo(GameMod::class, 'game_mod_id'); |
||
| 162 | } |
||
| 163 | 6 | ||
| 164 | public function settings(): HasMany |
||
| 165 | 6 | { |
|
| 166 | return $this->hasMany(ServerSetting::class); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function tasks(): HasMany |
||
| 170 | { |
||
| 171 | 3 | return $this->hasMany(ServerTask::class); |
|
| 172 | } |
||
| 173 | |||
| 174 | 3 | public function getFullPathAttribute(): string |
|
| 175 | 3 | { |
|
| 176 | 3 | return rtrim($this->dedicatedServer->work_path, '/') . '/' . ltrim($this->dir, '/'); |
|
| 177 | } |
||
| 178 | |||
| 179 | public function getFileManagerDisksAttribute(): array |
||
| 180 | 3 | { |
|
| 181 | $fileManagerDisks = [ |
||
| 182 | 3 | 'server' => array_merge( |
|
| 183 | 3 | $this->dedicatedServer->gdaemonSettings('local'), |
|
| 184 | ['driver' => 'gameap', 'workDir' => $this->full_path, 'root' => $this->full_path] |
||
| 185 | 3 | ), |
|
| 186 | 3 | ]; |
|
| 187 | |||
| 188 | $setting = $this->settings()->where('name', 'file-manager')->first(); |
||
| 189 | |||
| 190 | if (!empty($setting)) { |
||
| 191 | 3 | $disks = json_decode($setting->value, true); |
|
| 192 | |||
| 193 | if (!empty($disks)) { |
||
| 194 | $fileManagerDisks = array_merge($fileManagerDisks, $disks); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | 3 | ||
| 198 | |||
| 199 | 3 | return $fileManagerDisks; |
|
| 200 | } |
||
| 201 | |||
| 202 | public function users(): BelongsToMany |
||
| 203 | { |
||
| 204 | return $this->belongsToMany(User::class); |
||
| 205 | 3 | } |
|
| 206 | |||
| 207 | public function getAliasesAttribute(): array |
||
| 208 | 3 | { |
|
| 209 | 3 | $aliases = [ |
|
| 210 | 3 | 'ip' => $this->server_ip, |
|
| 211 | 3 | 'port' => $this->server_port, |
|
| 212 | 3 | 'query_port' => $this->query_port, |
|
| 213 | 3 | 'rcon_port' => $this->rcon_port, |
|
| 214 | 3 | 'rcon_password' => $this->rcon, |
|
| 215 | 'uuid' => $this->uuid, |
||
| 216 | 'uuid_short' => $this->uuid_short, |
||
| 217 | 3 | ]; |
|
| 218 | 3 | ||
| 219 | 3 | if ($this->gameMod !== null && is_array($this->gameMod->vars)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 220 | 3 | foreach ($this->gameMod->vars as $var) { |
|
| 221 | 3 | if(!isset($var['var'])) { |
|
| 222 | 3 | continue; |
|
| 223 | } |
||
| 224 | $varname = $var['var']; |
||
| 225 | $aliases[$varname] = $this->vars[$varname] ?? $var['default']; |
||
| 226 | 3 | } |
|
| 227 | } |
||
| 228 | |||
| 229 | return $aliases; |
||
| 230 | } |
||
| 231 | |||
| 232 | public function isActive(): bool |
||
| 233 | { |
||
| 234 | return $this->installed === self::INSTALLED && $this->enabled && !$this->blocked; |
||
| 235 | } |
||
| 236 | |||
| 237 | public function getSetting(string $key): ServerSetting |
||
| 238 | { |
||
| 239 | return $this->settings->where('name', $key)->first() |
||
| 240 | ?? new ServerSetting([ |
||
| 241 | 'server_id' => $this->id, |
||
| 242 | 'name' => $key, |
||
| 243 | 'value' => false, |
||
| 244 | ]); |
||
| 245 | } |
||
| 246 | |||
| 247 | private static function removeAbsoluteDedicatedServerPath(string $path, string $dsWorkPath): string |
||
| 248 | { |
||
| 249 | if (substr($path, 0, strlen($dsWorkPath)) == $dsWorkPath) { |
||
| 250 | $path = substr($path, strlen($dsWorkPath)); |
||
| 251 | } |
||
| 252 | |||
| 253 | $path = ltrim($path, '/\\'); |
||
| 254 | |||
| 255 | return $path; |
||
| 256 | } |
||
| 257 | } |
||
| 258 |