*/ use HasFactory; use HasHashid; /** * The resource name for this model when it is transformed into an * API representation using fractal. */ public const string RESOURCE_NAME = 'schedule_task'; /** * The default actions that can exist for a task in Pterodactyl. */ public const string ACTION_POWER = 'power'; public const string ACTION_COMMAND = 'command'; public const string ACTION_BACKUP = 'backup'; /** * The signals a power task may send. * * @var list */ public const array POWER_ACTIONS = ['start', 'stop', 'restart', 'kill']; /** * Default attributes when creating a new model. */ protected $attributes = [ 'time_offset' => 0, 'is_queued' => false, 'continue_on_failure' => false, ]; /** * @return array */ protected function casts(): array { return [ 'id' => 'integer', 'schedule_id' => 'integer', 'sequence_id' => 'integer', 'time_offset' => 'integer', 'is_queued' => 'boolean', 'continue_on_failure' => 'boolean', ]; } /** * The permission a user needs to run a task with the given action and payload, or * null when the action or power signal is not one a task can perform. */ public static function permissionForAction(string $action, ?string $payload = null): ?Permissions { return match ($action) { self::ACTION_COMMAND => Permissions::ControlConsole, self::ACTION_BACKUP => Permissions::BackupCreate, self::ACTION_POWER => match (mb_trim((string) $payload)) { 'start' => Permissions::ControlStart, 'stop', 'kill' => Permissions::ControlStop, 'restart' => Permissions::ControlRestart, default => null, }, default => null, }; } public function getRouteKeyName(): string { return $this->getKeyName(); } /** * Return the schedule that a task belongs to. * * @return BelongsTo */ public function schedule(): BelongsTo { return $this->belongsTo(Schedule::class); } /** * The server a task runs against is whatever its schedule belongs to, so this * resolves through the schedule with local keys. * * @return HasOneThrough */ public function server(): HasOneThrough { return $this->hasOneThrough(Server::class, Schedule::class, 'id', 'id', 'schedule_id', 'server_id'); } }